Advertisement
Guest User

main.c

a guest
May 12th, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.66 KB | None | 0 0
  1. #include "main.h"
  2.  
  3. extern void getInput(SDL_Event event);
  4.  
  5. int main(int argc, char* argv[])
  6. {
  7.     SDL_Surface *screen, *temp, *sprite;
  8.     int colorkey;
  9.  
  10.     SDL_Init(SDL_INIT_VIDEO); //Initialize SDL
  11.  
  12.     SDL_WM_SetCaption(WINDOW_TITLE, 0); //Window title
  13.  
  14.     screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0); //Create window frame
  15.  
  16.     SDL_EnableKeyRepeat(70, 70); //Keyboard Repeat
  17.  
  18.     temp   = SDL_LoadBMP("sprite.bmp"); //Load the actual sprite
  19.     sprite = SDL_DisplayFormat(temp);
  20.     SDL_FreeSurface(temp);
  21.  
  22.     /* setup sprite colorkey and turn on RLE */
  23.     colorkey = SDL_MapRGB(screen->format, 160, 136, 128); //Make this RGB color transparent, so sprite isn't surrounded by a colored box
  24.     SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
  25.  
  26.     spriteRect.x = 150; //Initial X Position
  27.     spriteRect.y = 150; //Initial Y Position
  28.     animRect.x = 150;
  29.     animRect.y = 150;
  30.  
  31.     /* set animation frame */
  32.     srcRect.x = 0;
  33.     srcRect.y = 0;
  34.     srcRect.w = SPRITE_SIZE;
  35.     srcRect.h = SPRITE_SIZE;
  36.  
  37.     /* initialize bullet */
  38.     for(int i=0; i<Bullet_No; i++) {
  39.         bullet[i].alive = 0;
  40.         bullet[i].w = bullet[i].h = 5 ; // Bullet Size Configuration
  41.     }
  42.  
  43.     gameover = 0;
  44.  
  45.     /* message pump */
  46.     while (!gameover)
  47.     {
  48.         SDL_Event event;
  49.  
  50.         /* look for an event */
  51.         getInput(event);
  52.  
  53.         if(bKeyA == 1 && bKeyATick%10 == 1) {
  54.             for(int i=0;i<Bullet_No;i++) if(!bullet[i].alive) {
  55.                 bullet[i].alive = 1;
  56.                 bullet[i].b.x = animRect.x + 6 ;
  57.                 bullet[i].b.y = animRect.y + 3 ;
  58.                 break; // break this for( ) loop
  59.             }
  60.         } // if(bKeyA == 1 && bKeyATick%10 == 1) { END
  61.         for(int i=0; i<Bullet_No; i++) if(bullet[i].alive) {
  62.             bullet[i].b.x += 5;
  63.             bullet[i].b.w = bullet[i].w;
  64.             bullet[i].b.h = bullet[i].h;
  65.             SDL_FillRect(screen , &bullet[i].b , 0xFFFF66);
  66.             if(bullet[i].b.x > 380) bullet[i].alive = 0; // Bullet Destroy condition
  67.         }
  68.  
  69.         if(bKeyA) bKeyATick++; // Press KeyA counter + 1
  70.         if(!bKeyA) bKeyATick=0; // Release KeyA counter = 0
  71.  
  72.         /* collide with edges of screen */
  73.         if (spriteRect.x <= 0)
  74.             spriteRect.x = 0;
  75.         if (spriteRect.x >= WINDOW_WIDTH - SPRITE_SIZE)
  76.             spriteRect.x = WINDOW_WIDTH - SPRITE_SIZE;
  77.  
  78.         if (spriteRect.y <= 0)
  79.             spriteRect.y = 0;
  80.         if (spriteRect.y >= WINDOW_HEIGHT - SPRITE_SIZE)
  81.             spriteRect.y = WINDOW_HEIGHT - SPRITE_SIZE;
  82.  
  83.         SDL_BlitSurface(sprite, &srcRect, screen, &spriteRect);
  84.         SDL_UpdateRect(screen, 0, 0, 0, 0); //Update screen with new sprite
  85.         SDL_FillRect(screen, NULL, 0x000000);
  86.     }
  87.  
  88.     /* clean up */
  89.     SDL_FreeSurface(sprite);
  90.     SDL_Quit();
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement