Bueno, lo primero de todo es conseguir las librerias, para esto nos vamos a
www.libsdl.org y descargamos la SDL, SDL_image, SDL_mixer, SDL_ttf(al menos son las que considero indispensables).
Nos bajamos las que son .tar.gz, y las instalamos con el tipico procedimiento de:
./configure
make
make install
Y Pues aqui dejo un pequeño ejemplo:
MAIN.CPP
//===============================================================================
/*Este codigo es propiedad de Fernando Escutia Rodriguez, escrito el 05/04/2010*/
//===============================================================================
/*Iniciamos las cabezeras necesarias para el programa*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_ttf.h>
#include "csprite.h"
/*Definimos las definiciones y variables globales*/
SDL_Surface *screen, *texto;
SDL_Color bgcolor, fgcolor;
SDL_Rect rectangulo;
TTF_Font *fuente, *fon;
char msg[100];
CFrame ftitle;
CSprite title;
CFrame fmagic;
CSprite magic;
Mix_Music *tit;
Uint32 ini_milisegundos, fin_milisegundos, frametime;
/*estructura del cartel magic puzzle*/
struct mag{
int x, y;
} magi;
/*Cargamos los sprites*/
int initsprites(){
ftitle.load("img/title.bmp");
title.addframe(ftitle);
fmagic.load("img/magic.bmp");
magic.addframe(fmagic);
}
/*Cargamos los archivos de musica*/
void inicializa(){
tit = Mix_LoadMUS("music/trust.mp3");
}
/*iniciamos las coordenadas moviles*/
void moviles(){
magi.x = -5;
magi.y = 100;
}
/*Iniciamos el frameo*/
void ResetTimeBase() {
ini_milisegundos=SDL_GetTicks();
}
int CurrentTime() {
fin_milisegundos=SDL_GetTicks();
return fin_milisegundos-ini_milisegundos;
}
/*iniciamos los textos que vamos a usar*/
const char text[100]="Presione espacio para continuar o esc para salir";
/*Iniciamos codigo del programa*/
int main(int argc, char *argv[]) {
int done = 0;
int estado = 1;
int pan = 0;
SDL_Event event;
Uint8 *keys;
/*Iniciamos SDL*/
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) {
printf("No se pudo iniciar SDL: %s\n", SDL_GetError());
exit(1);
}
/*Iniciamos modo de video*/
screen = SDL_SetVideoMode(598, 429, 24, SDL_HWSURFACE|SDL_DOUBLEBUF);
if (screen == NULL){
printf("No se puede iniciar el modo grafico: %s\n", SDL_GetError());
exit(1);
}
/*Iniciamos SDL_ttf*/
if (TTF_Init() < 0){
printf("No se puede iniciar SDL_ttf: %s\n", SDL_GetError());
exit(1);
}
atexit(TTF_Quit);
/*Iniciamos el audio*/
if (Mix_OpenAudio(44100,AUDIO_S16SYS,2,4096) < 0){
printf("No se puede iniciar el audio: %s\n", Mix_GetError());
exit(1);
}
atexit(Mix_CloseAudio);
/*Cargamos las fuentes*/
fuente = TTF_OpenFont("Fonts/ionic.ttf", 20);
fon = TTF_OpenFont("Fonts/monofont.ttf", 20);
/*Iniciamos los colores para las fuentes*/
bgcolor.r = 255;
bgcolor.g = 0;
bgcolor.b = 0;
initsprites();
inicializa();
moviles();
/*Iniciamos ejecucion del programa*/
while (done == 0){
switch (estado){
/*Iniciamos la pantalla de presentación*/
case 1:
Mix_PlayMusic(tit, -1);
while ((done == 0) && (estado == 1)){
ResetTimeBase();
title.setx(0);
title.sety(0);
title.draw(screen);
magic.setx(magi.x);
magic.sety(magi.y);
magic.draw(screen);
if (pan == 0){
fgcolor.r = 0;
fgcolor.g = 14;
fgcolor.b = 255;
sprintf(msg, "%s", text);
texto = TTF_RenderText_Blended(fuente, msg, fgcolor);
rectangulo.y = 370;
rectangulo.x = 87;
rectangulo.w = texto->w;
rectangulo.h = texto->h;
SDL_BlitSurface(texto, NULL, screen, &rectangulo);
}
SDL_Flip(screen);
keys=SDL_GetKeyState(NULL);
if (pan == 1){
if((magi.x < 270) && (magi.y < 260)){
magi.x=magi.x+(43);
magi.y=magi.y+(41);
}
}
/*Iniciamos el timing*/
do {
frametime=CurrentTime();
} while (frametime<300);
/*Iniciamos el chequeo de eventos*/
while (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT){done = 1;}
if (event.type == SDL_KEYDOWN){
if (event.key.keysym.sym == SDLK_ESCAPE){done = 1;}
if (event.key.keysym.sym == SDLK_SPACE){
if (pan == 0){pan = 1;}
}
}
}
}
break;
}
}
/*Paramos la musica*/
Mix_HaltMusic();
/*Liberamos el recurso de la musica*/
Mix_FreeMusic(tit);
/*Liberamos recursos de fuentes*/
TTF_CloseFont(fuente);
TTF_CloseFont(fon);
return 0;
}
CSPRITE.H
#ifndef CSPRITE_H_
#define CSPRITE_H_
#define TRUE 1
#define FALSE 0
// CFrame representa un frame independiente de un sprite.
class CFrame {
public:
SDL_Surface *img;
void load(char *path);
void unload();
};
// La clase CSprite esta formada por un array de frames;
class CSprite {
private:
int posx,posy;
int estado;
int nframes;
int cont;
public:
CFrame *sprite;
CSprite(int nf);
CSprite();
void finalize();
void addframe(CFrame frame);
void selframe(int nf);
int frames() {return cont;}
void setx(int x) {posx=x;}
void sety(int y) {posy=y;}
void addx(int c) {posx+=c;}
void addy(int c) {posy+=c;}
int getx() {return posx;}
int gety() {return posy;}
int getw() {return sprite[estado].img->w;}
int geth() {return sprite[estado].img->h;}
void draw(SDL_Surface *superficie);
int colision(CSprite sp);
int colision2(CSprite sp);
};
#endif /* CSPRITE_H_ */
CSPRITE.CPP
#include <SDL/SDL.h>
#include "csprite.h"
#include <SDL/SDL_image.h>
// Sprite Class implementation
void CFrame::load(char *path) {
img=IMG_Load(path);
// Asignamos el color transparente al color rojo.
SDL_SetColorKey(img,SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(img->format,255,0,0));
img=SDL_DisplayFormat(img);
}
void CFrame::unload(){
SDL_FreeSurface(img);
}
CSprite::CSprite(int nf) {
sprite=new CFrame[nf];
nframes=nf;
cont=0;
}
CSprite::CSprite() {
int nf=1;
sprite=new CFrame[nf];
nframes=nf;
cont=0;
}
void CSprite::finalize() {
int i;
for (i=0 ; i<=nframes-1 ; i++)
sprite
.unload();
}
void CSprite::addframe(CFrame frame) {
if (cont<nframes) {
sprite[cont]=frame;
cont++;
}
}
void CSprite::selframe(int nf) {
if (nf<=nframes) {
estado=nf;
}
}
void CSprite::draw(SDL_Surface *superficie) {
SDL_Rect dest;
dest.x=posx;
dest.y=posy;
SDL_BlitSurface(sprite[estado].img,NULL,superficie,&dest);
}
int CSprite::colision(CSprite sp) {
int w1,h1,w2,h2,x1,y1,x2,y2;
w1=getw(); // ancho del sprite1
h1=geth(); // altura del sprite1
w2=sp.getw(); // ancho del sprite2
h2=sp.geth(); // alto del sprite2
x1=getx(); // pos. X del sprite1
y1=gety(); // pos. Y del sprite1
x2=sp.getx(); // pos. X del sprite2
y2=sp.gety(); // pos. Y del sprite2
if (((x1+w1)>x2)&&((y1+h1)>y2)&&((x2+w2)>x1)&&((y2+h2)>y1)) {
return TRUE;
} else {
return FALSE;
}
}
int CSprite::colision2(CSprite sp) {
int w1,h1,w2,h2,x1,y1,x2,y2;
w1=getw()+2; // ancho del sprite1
h1=geth()+2; // altura del sprite1
w2=sp.getw()+2; // ancho del sprite2
h2=sp.geth()+2; // alto del sprite2
x1=getx()-1; // pos. X del sprite1
y1=gety()-1; // pos. Y del sprite1
x2=sp.getx()-1; // pos. X del sprite2
y2=sp.gety()-1; // pos. Y del sprite2
if (((x1+w1)>x2)&&((y1+h1)>y2)&&((x2+w2)>x1)&&((y2+h2)>y1)) {
return TRUE;
} else {
return FALSE;
}
}
Para que este funcione, debemos de tener los tres archivos en un mismo directorio, y meter el siguiente comando en una terminal:
g++ -L/usr/lib -lSDL -lSDL_image -lSDL_mixer -lSDL_ttf -lpthread -I/usr/include/SDL -D_REENTRANT /mnt/sda7/c01/game/main.cpp /mnt/sda7/c01/game/csprite.cpp -o /mnt/sda7/c01/game/magic_puzzle
Los directorios los substituyes por los tuyos.
Y he aqui el resultado:
NOTA: Si las librerias y las cabezeras no estan en los directorios especificados arriba, se encuentran en /mnt/home/usr/local/include/SDL y /mnt/home/usr/local/lib, nadamas habria que copiar las librerias y cabeceras sdl a los directorios de arriba o poner en la linia para compilar estos directorios en lugar de otros a la hora de compilar.
Espero y les sirva, de todos modos recomiendo que busquen en google por "[ebook]programacion de videojuegos con sdl.pdf", cualquier duda pueden hacermela llegar y tratare de darle respuesta lo mas pronto posible.