Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Game.h"
- Game::Game()
- {
- frame = 0;
- fps = 60;
- frame_start = 0;
- screen_width = 1024;
- screen_height = 768;
- total_tiles = 0;
- map_width = 0;
- map_height = 0;
- camera.x = 0;
- camera.y = 0;
- camera.w = screen_width;
- camera.h = screen_height;
- quit = false;
- SDL_Init( SDL_INIT_EVERYTHING );
- buffer = SDL_SetVideoMode( screen_width, screen_height, 32, SDL_SWSURFACE );
- SDL_WM_SetCaption( "for_loop[-]", NULL );
- }
- Game::~Game()
- {
- SDL_Quit();
- }
- SDL_Surface* Game::load_image( std::string filename )
- {
- //surfaces for the loaded and optimized images
- SDL_Surface* loaded_image = NULL;
- SDL_Surface* opt_image = NULL;
- //load the image
- loaded_image = IMG_Load( filename.c_str() );
- //optimze the image
- if ( loaded_image != NULL )
- {
- opt_image = SDL_DisplayFormat( loaded_image );
- SDL_FreeSurface( loaded_image );
- //colorkey
- if ( opt_image != NULL )
- {
- SDL_SetColorKey( opt_image, SDL_SRCCOLORKEY, SDL_MapRGB( opt_image->format, 0, 255, 255 ) );
- }
- }
- //return the optimized image
- return opt_image;
- }
- void Game::apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip )
- {
- //rectangle for holding the offset
- SDL_Rect offset;
- offset.x = x;
- offset.y = y;
- //blit the surface
- SDL_BlitSurface( source, clip, destination, &offset );
- }
- bool Game::check_collision( int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh )
- {
- //a is right of b
- if ( ax >= bx + bw )
- return false;
- //a is left of b
- if ( ax + aw <= bx )
- return false;
- //a is below b
- if ( ay >= by + bh )
- return false;
- //a is above b
- if ( ay + ah <= by )
- return false;
- //they collide
- return true;
- }
- void Game::regulate_frame()
- {
- frame++;
- if ( SDL_GetTicks() - frame_start < 1000 / fps )
- {
- SDL_Delay( (1000 / fps) - (SDL_GetTicks() - frame_start) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement