Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "main.h"
- extern void getInput(SDL_Event event);
- int main(int argc, char* argv[])
- {
- SDL_Surface *screen, *temp, *sprite;
- int colorkey;
- SDL_Init(SDL_INIT_VIDEO); //Initialize SDL
- SDL_WM_SetCaption(WINDOW_TITLE, 0); //Window title
- screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0); //Create window frame
- SDL_EnableKeyRepeat(70, 70); //Keyboard Repeat
- temp = SDL_LoadBMP("sprite.bmp"); //Load the actual sprite
- sprite = SDL_DisplayFormat(temp);
- SDL_FreeSurface(temp);
- /* setup sprite colorkey and turn on RLE */
- colorkey = SDL_MapRGB(screen->format, 160, 136, 128); //Make this RGB color transparent, so sprite isn't surrounded by a colored box
- SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
- spriteRect.x = 150; //Initial X Position
- spriteRect.y = 150; //Initial Y Position
- animRect.x = 150;
- animRect.y = 150;
- /* set animation frame */
- srcRect.x = 0;
- srcRect.y = 0;
- srcRect.w = SPRITE_SIZE;
- srcRect.h = SPRITE_SIZE;
- /* initialize bullet */
- for(int i=0; i<Bullet_No; i++) {
- bullet[i].alive = 0;
- bullet[i].w = bullet[i].h = 5 ; // Bullet Size Configuration
- }
- gameover = 0;
- /* message pump */
- while (!gameover)
- {
- SDL_Event event;
- /* look for an event */
- getInput(event);
- if(bKeyA == 1 && bKeyATick%10 == 1) {
- for(int i=0;i<Bullet_No;i++) if(!bullet[i].alive) {
- bullet[i].alive = 1;
- bullet[i].b.x = animRect.x + 6 ;
- bullet[i].b.y = animRect.y + 3 ;
- break; // break this for( ) loop
- }
- } // if(bKeyA == 1 && bKeyATick%10 == 1) { END
- for(int i=0; i<Bullet_No; i++) if(bullet[i].alive) {
- bullet[i].b.x += 5;
- bullet[i].b.w = bullet[i].w;
- bullet[i].b.h = bullet[i].h;
- SDL_FillRect(screen , &bullet[i].b , 0xFFFF66);
- if(bullet[i].b.x > 380) bullet[i].alive = 0; // Bullet Destroy condition
- }
- if(bKeyA) bKeyATick++; // Press KeyA counter + 1
- if(!bKeyA) bKeyATick=0; // Release KeyA counter = 0
- /* collide with edges of screen */
- if (spriteRect.x <= 0)
- spriteRect.x = 0;
- if (spriteRect.x >= WINDOW_WIDTH - SPRITE_SIZE)
- spriteRect.x = WINDOW_WIDTH - SPRITE_SIZE;
- if (spriteRect.y <= 0)
- spriteRect.y = 0;
- if (spriteRect.y >= WINDOW_HEIGHT - SPRITE_SIZE)
- spriteRect.y = WINDOW_HEIGHT - SPRITE_SIZE;
- SDL_BlitSurface(sprite, &srcRect, screen, &spriteRect);
- SDL_UpdateRect(screen, 0, 0, 0, 0); //Update screen with new sprite
- SDL_FillRect(screen, NULL, 0x000000);
- }
- /* clean up */
- SDL_FreeSurface(sprite);
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement