ajtgarber

SDL_Rect draw bug (main.cpp)

Apr 8th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <SDL/SDL.h>
  2. #include <SDL_image/SDL_image.h>
  3.  
  4. #include "Player.h"
  5. #include "Bullet.h"
  6.  
  7. #define WIDTH   475
  8. #define HEIGHT  625
  9. #define FPS     60
  10.  
  11. SDL_Surface* loadImage(char* filename) {
  12.     SDL_Surface* loadedImage = IMG_Load(filename);
  13.     SDL_Surface* optimized = NULL;
  14.     if(loadedImage) {
  15.         optimized = SDL_DisplayFormat(loadedImage);
  16.         SDL_FreeSurface(loadedImage);
  17.     }
  18.     return optimized;
  19. }
  20.  
  21. int main(int argc, char* argv[]) {
  22.     SDL_Init(SDL_INIT_EVERYTHING);
  23.    
  24.     SDL_Surface* screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_SWSURFACE);
  25.     SDL_WM_SetCaption("Galaga Test", NULL);
  26.    
  27.     //init
  28.     Player player((WIDTH-25)/2, HEIGHT-35);
  29.     Bullet bill(player.getX(), player.getY(), 0, -8);
  30.    
  31.     bool running = true;
  32.     while(running) {
  33.         SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
  34.         SDL_Event event;
  35.         int start = SDL_GetTicks();
  36.         while(SDL_PollEvent(&event)) {
  37.             if(event.type == SDL_QUIT) {
  38.                 running = false;
  39.             } else if(event.type == SDL_KEYDOWN) {
  40.                 int key = event.key.keysym.sym;
  41.                
  42.                 if(key == SDLK_RIGHT || key == SDLK_d) {
  43.                     player.setXVel(6);
  44.                 } else if(key == SDLK_LEFT || key == SDLK_a) {
  45.                     player.setXVel(-6);
  46.                 }
  47.             } else if(event.type == SDL_KEYUP) {
  48.                 int key = event.key.keysym.sym;
  49.                
  50.                 if(key == SDLK_RIGHT || key == SDLK_d) {
  51.                     player.setXVel(0);
  52.                 } else if(key == SDLK_LEFT || key == SDLK_a) {
  53.                     player.setXVel(0);
  54.                 }
  55.             }
  56.         }
  57.        
  58.         //update
  59.         player.update();
  60.         bill.update();
  61.         if(player.getX() < 0 || player.getX()+player.getWidth() > WIDTH) {
  62.             player.setX(player.getX()-player.getXVel());
  63.             player.setXVel(0);
  64.         }
  65.        
  66.         //render
  67.         bill.render(screen);
  68.         player.render(screen);
  69.        
  70.         int end = SDL_GetTicks();
  71.         int duration = end-start;
  72.         if(duration > (1000/FPS)) {
  73.             SDL_Delay(5);
  74.         } else {
  75.             SDL_Delay((1000/FPS)-duration);
  76.         }
  77.         SDL_Flip(screen);
  78.     }
  79.    
  80.     SDL_Quit();
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment