Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include "Game.h"
  2.  
  3. Game::Game()
  4. {
  5.     frame = 0;
  6.     fps = 60;
  7.     frame_start = 0;
  8.  
  9.     screen_width = 1024;
  10.     screen_height = 768;
  11.  
  12.     total_tiles = 0;
  13.     map_width = 0;
  14.     map_height = 0;
  15.  
  16.     camera.x = 0;
  17.     camera.y = 0;
  18.     camera.w = screen_width;
  19.     camera.h = screen_height;
  20.  
  21.     quit = false;
  22.  
  23.     SDL_Init( SDL_INIT_EVERYTHING );
  24.     buffer = SDL_SetVideoMode( screen_width, screen_height, 32, SDL_SWSURFACE );
  25.     SDL_WM_SetCaption( "for_loop[-]", NULL );
  26. }
  27.  
  28. Game::~Game()
  29. {
  30.     SDL_Quit();
  31. }
  32.  
  33. SDL_Surface* Game::load_image( std::string filename )
  34. {
  35.   //surfaces for the loaded and optimized images
  36.     SDL_Surface* loaded_image = NULL;
  37.     SDL_Surface* opt_image = NULL;
  38.  
  39.   //load the image
  40.     loaded_image = IMG_Load( filename.c_str() );
  41.  
  42.   //optimze the image
  43.     if ( loaded_image != NULL )
  44.     {
  45.         opt_image = SDL_DisplayFormat( loaded_image );
  46.         SDL_FreeSurface( loaded_image );
  47.  
  48.       //colorkey
  49.         if ( opt_image != NULL )
  50.         {
  51.             SDL_SetColorKey( opt_image, SDL_SRCCOLORKEY, SDL_MapRGB( opt_image->format, 0, 255, 255 ) );
  52.         }
  53.     }
  54.  
  55.   //return the optimized image
  56.     return opt_image;
  57. }
  58.  
  59. void Game::apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip )
  60. {
  61.   //rectangle for holding the offset
  62.     SDL_Rect offset;
  63.     offset.x = x;
  64.     offset.y = y;
  65.  
  66.   //blit the surface
  67.     SDL_BlitSurface( source, clip, destination, &offset );
  68. }
  69.  
  70. bool Game::check_collision( int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh )
  71. {
  72.   //a is right of b
  73.     if ( ax >= bx + bw )
  74.         return false;
  75.  
  76.   //a is left of b
  77.     if ( ax + aw <= bx )
  78.         return false;
  79.  
  80.   //a is below b
  81.     if ( ay >= by + bh )
  82.         return false;
  83.  
  84.   //a is above b
  85.     if ( ay + ah <= by )
  86.         return false;
  87.  
  88.   //they collide
  89.     return true;
  90. }
  91.  
  92. void Game::regulate_frame()
  93. {
  94.     frame++;
  95.  
  96.     if ( SDL_GetTicks() - frame_start < 1000 / fps )
  97.     {
  98.         SDL_Delay( (1000 / fps) - (SDL_GetTicks() - frame_start) );
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement