10.07.2015 Views

[ebook]Programacion de videojuegos con SDL

[ebook]Programacion de videojuegos con SDL

[ebook]Programacion de videojuegos con SDL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

P R O G R A M A C I Ó N D E V I D E O J U E G O S C O N S D L// finalizamos los spritesnave.finalize();malo.finalize();}// cerramos el joystickif (<strong>SDL</strong>_JoystickOpened(0)) {<strong>SDL</strong>_JoystickClose(joystick);}// Preparamos los espritesint InitSprites() {fnave.load("minave.bmp");nave.addframe(fnave);fmalo.load("nave.bmp");malo.addframe(fmalo);}return 0;int main(int argc, char *argv[]) {<strong>SDL</strong>_Event event;Uint8 *keys;if (<strong>SDL</strong>_Init(<strong>SDL</strong>_INIT_VIDEO|<strong>SDL</strong>_INIT_JOYSTICK) < 0) {printf("No se pudo iniciar <strong>SDL</strong>: %s\n",<strong>SDL</strong>_GetError());return 1;}screen = <strong>SDL</strong>_SetVi<strong>de</strong>oMo<strong>de</strong>(640,480,24,<strong>SDL</strong>_HWSURFACE);if (screen == NULL) {printf("No se pue<strong>de</strong> inicializar el modo gráfico: \n",<strong>SDL</strong>_GetError());return 1;}atexit(<strong>SDL</strong>_Quit);inicializa();InitSprites();while (done == 0) {// dibujamos el frameDrawScene(screen);// <strong>con</strong>sultamos el estado <strong>de</strong>l tecladokeys=<strong>SDL</strong>_GetKeyState(NULL);// <strong>con</strong>sultamos el estado <strong>de</strong>l joystick<strong>SDL</strong>_JoystickUpdate();joyx = <strong>SDL</strong>_JoystickGetAxis(joystick, 0);joyy = <strong>SDL</strong>_JoystickGetAxis(joystick, 1);if ((keys[<strong>SDL</strong>K_UP] || joyy < -10) && (jugador.y > 0)) {jugador.y=jugador.y-(5);}if ((keys[<strong>SDL</strong>K_DOWN] || joyy > 10) && (jugador.y < 460)) {jugador.y=jugador.y+(5);}if ((keys[<strong>SDL</strong>K_LEFT] || joyx < -10) && (jugador.x > 0)) {jugador.x=jugador.x-(5);}if ((keys[<strong>SDL</strong>K_RIGHT] || joyx > 10) && (jugador.x < 620)) {jugador.x=jugador.x+(5);}while (<strong>SDL</strong>_PollEvent(&event)) {if (event.type == <strong>SDL</strong>_QUIT) {done=1;}if (event.type == <strong>SDL</strong>_KEYDOWN || event.type == <strong>SDL</strong>_JOYBUTTONDOWN) {}if (event.key.keysym.sym == <strong>SDL</strong>K_ESCAPE) {done=1;}88

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!