Puntoinfinito

Naves SDL

Jan 20th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include "functions.h"
  2. #include <iostream>
  3. #include <SDL/SDL.h>
  4. #include <SDL/SDL_mixer.h>
  5. #include <SDL/SDL_ttf.h>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.  
  11.     //Iniciamos
  12.     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  13.     cout << "Error inesperado";
  14.     cin.sync();
  15.     cin.get();
  16.     return 0;
  17.     }
  18.  
  19.     if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) < 0 ) {
  20.     cout << "Error inesperado";
  21.     return 0;
  22. }
  23.  
  24.     //Variables
  25.     SDL_Surface *nave;
  26.     SDL_Surface *fondo;
  27.     SDL_Surface *screen;
  28.     SDL_Surface *texto;
  29.     SDL_Rect destino;
  30.     Uint8 * teclas = SDL_GetKeyState ( NULL );
  31.     SDL_Event suceso;
  32.     int yNave = 230/2, xNave = 240/2;
  33.     int yFondo = 0, xFondo = 0;
  34.     int terminado = 0;
  35.     int tecla;
  36.     TTF_Font* font = TTF_OpenFont("air.ttf",36);
  37.     SDL_Color color = {250,250,250};
  38.  
  39.     // The music that will be played
  40.     Mix_Music *musica = NULL;
  41.     //The sound effects that will be used  
  42.     Mix_Chunk *disparos = NULL;
  43.     Mix_Chunk *motor = NULL;
  44.     Mix_Chunk *explosion = NULL;
  45.     Mix_VolumeMusic(50);
  46.     musica = Mix_LoadMUS("pixeluniverse.wav");
  47.     if (!Mix_PlayMusic(musica, -1)) {
  48.     cout << "Error inesperado";
  49.     }
  50.  
  51.     //Definir variables multimedia
  52.     nave = SDL_LoadBMP("nave.bmp");
  53.     fondo = SDL_LoadBMP("fondo.bmp");
  54.     texto = TTF_RenderText_Solid(font,"Score : 0",color);
  55.     //Definir variables numericas
  56.     int yPos = fondo->h;
  57.  
  58.     //Poner transparencia a la nave
  59.     quit_color(nave, 0, 255, 0);
  60.  
  61.     //Creamos ventana
  62.     screen = SDL_SetVideoMode(320, 240, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
  63.     if (screen == NULL) {
  64.     cout << "Error inesperado";
  65.     cin.sync();
  66.     cin.get();
  67.     SDL_Quit();
  68.     return 0;
  69.     }
  70.    
  71.     // El getch cada 20 segundos
  72.     SDL_EnableKeyRepeat(5, 5);
  73.    
  74.     //Nombre de ventana
  75.     SDL_WM_SetCaption( "Naves", NULL );
  76.  
  77.     while (terminado == 0) {
  78.  
  79.     //Dibujamos fondo
  80.     SDL_Rect posFondo;
  81.     posFondo.y = yFondo;
  82.     posFondo.x = xFondo;
  83.    
  84.     SDL_BlitSurface(fondo, NULL, screen, &posFondo);
  85.     apply_surface( 0, yPos, fondo, screen );
  86.  
  87.     //Dibujamos texto
  88.     SDL_BlitSurface(texto,NULL,screen,NULL);
  89.  
  90.     //Dibujamos nave
  91.     destino.y = yNave;
  92.     destino.x = xNave;
  93.     SDL_BlitSurface(nave, NULL , screen, &destino);
  94.  
  95.     SDL_Flip(screen);
  96.  
  97.     //Miramos tecla
  98.     while (SDL_PollEvent(&suceso)) {
  99.     if (suceso.type == SDL_QUIT)    terminado = 1;
  100.     if (suceso.type == SDL_KEYDOWN)
  101.     if (suceso.key.keysym.sym == SDLK_ESCAPE)  terminado = 1;
  102.     }
  103.    
  104.     if (teclas[SDLK_ESCAPE]) { terminado = 1; }
  105.     if (teclas[SDLK_UP]) { yNave -= 2; }
  106.     if (teclas[SDLK_DOWN]) { yNave += 2; }
  107.     if (teclas[SDLK_LEFT]) { xNave -= 3; }
  108.     if (teclas[SDLK_RIGHT]) { xNave += 3; }
  109.  
  110.     yPos -= -1;
  111.     yFondo -= -1;  
  112.  
  113.     //Scrolling background
  114.     if( yFondo >= fondo->h )
  115.     {
  116.     yFondo = 0; }
  117.  
  118.     if( yPos >= fondo->h ) { yPos = fondo->h; }
  119.    
  120.     //Bucle pause
  121.     SDL_Delay( 5 );
  122.  
  123.     }
  124.  
  125.     Mix_FreeMusic(musica);
  126.     SDL_Quit();
  127.     cout << "EXIT";
  128.     return 0;;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment