0 Members and 1 Guest are viewing this topic.
#include "SDL/SDL.h"#include "SDL/SDL_image.h"#include "SDL/SDL_Mixer.h"#include <string>#include <fstream>#include <sstream>const int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;const int FRAMES_PER_SECOND = 20;const int DOT_WIDTH = 20;const int DOT_HEIGHT = 20;const int LEVEL_WIDTH = 1280;const int LEVEL_HEIGHT = 960;int AREAX = 190;int AREAY = 190;const int TILE_WIDTH = 80;const int TILE_HEIGHT = 80;const int TOTAL_TILES = 192;const int TILE_SPRITES = 12;const int TILE_00 = 0;const int TILE_01 = 1;const int TILE_02 = 2;const int TILE_03 = 3;const int TILE_04 = 4;const int TILE_05 = 5;const int TILE_06 = 6;const int TILE_07 = 7;const int TILE_08 = 8;const int TILE_09 = 9;const int TILE_10 = 10;const int TILE_11 = 11;SDL_Surface *dot = NULL;SDL_Surface *screen = NULL;SDL_Surface *tileSheet = NULL;SDL_Rect clips[ TILE_SPRITES ];SDL_Event event;SDL_Rect camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };Mix_Music *music = NULL;class Tile{ private: SDL_Rect box; int type; public: Tile( int x, int y, int tileType ); void show(); int get_type(); SDL_Rect get_box();};class Dot{ private: SDL_Rect box; int xVel, yVel; public: Dot(); void handle_input(); void move( Tile *tiles[] ); void show(); void set_camera();};class Timer{ private: int startTicks; int pausedTicks; bool paused; bool started; public: Timer(); void start(); void stop(); void pause(); void unpause(); int get_ticks(); bool is_started(); bool is_paused();};SDL_Surface *load_image( std::string filename ){ SDL_Surface* loadedImage = NULL; SDL_Surface* optimizedImage = NULL; loadedImage = IMG_Load( filename.c_str() ); if( loadedImage != NULL ) { optimizedImage = SDL_DisplayFormat( loadedImage ); SDL_FreeSurface( loadedImage ); if( optimizedImage != NULL ) { SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) ); } } return optimizedImage;}void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ){ SDL_Rect offset; offset.x = x; offset.y = y; SDL_BlitSurface( source, clip, destination, &offset );}bool check_collision( SDL_Rect A, SDL_Rect B ){ int leftA, leftB; int rightA, rightB; int topA, topB; int bottomA, bottomB; leftA = A.x; rightA = A.x + A.w; topA = A.y; bottomA = A.y + A.h; leftB = B.x; rightB = B.x + B.w; topB = B.y; bottomB = B.y + B.h; if( bottomA <= topB ) { return false; } if( topA >= bottomB ) { return false; } if( rightA <= leftB ) { return false; } if( leftA >= rightB ) { return false; } return true;}bool init(){ if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false; } screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE ); if( screen == NULL ) { return false; } if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 ) { return false; } return true;}bool load_files(){ dot = load_image( "Images/dot.png" ); if( dot == NULL ) { return false; } tileSheet = load_image( "Images/tiles.png" ); if( tileSheet == NULL ) { return false; } music = Mix_LoadMUS( "Music/Music1.mp3" ); if( music == NULL ) { return false; } return true;}void clean_up( Tile *tiles[] ){ SDL_FreeSurface( dot ); SDL_FreeSurface( tileSheet ); for( int t = 0; t < TOTAL_TILES; t++ ) { delete tiles[ t ]; } Mix_FreeMusic( music ); Mix_CloseAudio(); SDL_Quit();}void clip_tiles(){ clips[ TILE_00 ].x = 0; clips[ TILE_00 ].y = 0; clips[ TILE_00 ].w = TILE_WIDTH; clips[ TILE_00 ].h = TILE_HEIGHT; clips[ TILE_01 ].x = 0; clips[ TILE_01 ].y = 80; clips[ TILE_01 ].w = TILE_WIDTH; clips[ TILE_01 ].h = TILE_HEIGHT; clips[ TILE_02 ].x = 0; clips[ TILE_02 ].y = 160; clips[ TILE_02 ].w = TILE_WIDTH; clips[ TILE_02 ].h = TILE_HEIGHT; clips[ TILE_11 ].x = 80; clips[ TILE_11 ].y = 0; clips[ TILE_11 ].w = TILE_WIDTH; clips[ TILE_11 ].h = TILE_HEIGHT; clips[ TILE_10 ].x = 80; clips[ TILE_10 ].y = 80; clips[ TILE_10 ].w = TILE_WIDTH; clips[ TILE_10 ].h = TILE_HEIGHT; clips[ TILE_09 ].x = 80; clips[ TILE_09 ].y = 160; clips[ TILE_09 ].w = TILE_WIDTH; clips[ TILE_09 ].h = TILE_HEIGHT; clips[ TILE_04 ].x = 160; clips[ TILE_04 ].y = 0; clips[ TILE_04 ].w = TILE_WIDTH; clips[ TILE_04 ].h = TILE_HEIGHT; clips[ TILE_03 ].x = 160; clips[ TILE_03 ].y = 80; clips[ TILE_03 ].w = TILE_WIDTH; clips[ TILE_03 ].h = TILE_HEIGHT; clips[ TILE_08 ].x = 160; clips[ TILE_08 ].y = 160; clips[ TILE_08 ].w = TILE_WIDTH; clips[ TILE_08 ].h = TILE_HEIGHT; clips[ TILE_05 ].x = 240; clips[ TILE_05 ].y = 0; clips[ TILE_05 ].w = TILE_WIDTH; clips[ TILE_05 ].h = TILE_HEIGHT; clips[ TILE_06 ].x = 240; clips[ TILE_06 ].y = 80; clips[ TILE_06 ].w = TILE_WIDTH; clips[ TILE_06 ].h = TILE_HEIGHT; clips[ TILE_07 ].x = 240; clips[ TILE_07 ].y = 160; clips[ TILE_07 ].w = TILE_WIDTH; clips[ TILE_07 ].h = TILE_HEIGHT;}bool set_tiles( Tile *tiles[] ){ int x = 0, y = 0; std::ifstream map( "Maps/MAP1.map" ); if( map == NULL ) { return false; } for( int t = 0; t < TOTAL_TILES; t++ ) { int tileType = -1; map >> tileType; if( map.fail() == true ) { map.close(); return false; } if( ( tileType >= 0 ) && ( tileType < TILE_SPRITES ) ) { tiles[ t ] = new Tile( x, y, tileType ); } else { map.close(); return false; } x += TILE_WIDTH; if( x >= LEVEL_WIDTH ) { x = 0; y += TILE_HEIGHT; } } map.close(); return true;}bool touches_wall( SDL_Rect box, Tile *tiles[] ){ for( int t = 0; t < TOTAL_TILES; t++ ) { if( ( tiles[ t ]->get_type() >= TILE_03 ) && ( tiles[ t ]->get_type() <= TILE_11 ) ) { if( check_collision( box, tiles[ t ]->get_box() ) == true ) { return true; } } } return false;}Tile::Tile( int x, int y, int tileType ){ box.x = x; box.y = y; box.w = TILE_WIDTH; box.h = TILE_HEIGHT; type = tileType;}void Tile::show(){ if( check_collision( camera, box ) == true ) { apply_surface( box.x - camera.x, box.y - camera.y, tileSheet, screen, &clips[ type ] ); }}int Tile::get_type(){ return type;}SDL_Rect Tile::get_box(){ return box;}Dot::Dot(){ box.x = 190; box.y = 190; box.w = DOT_WIDTH; box.h = DOT_HEIGHT; xVel = 0; yVel = 0;}void Dot::handle_input(){ if( event.type == SDL_KEYDOWN ) { switch( event.key.keysym.sym ) { case SDLK_UP: yVel -= DOT_HEIGHT / 2; break; case SDLK_DOWN: yVel += DOT_HEIGHT / 2; break; case SDLK_LEFT: xVel -= DOT_WIDTH / 2; break; case SDLK_RIGHT: xVel += DOT_WIDTH / 2; break; } } else if( event.type == SDL_KEYUP ) { switch( event.key.keysym.sym ) { case SDLK_UP: yVel += DOT_HEIGHT / 2; break; case SDLK_DOWN: yVel -= DOT_HEIGHT / 2; break; case SDLK_LEFT: xVel += DOT_WIDTH / 2; break; case SDLK_RIGHT: xVel -= DOT_WIDTH / 2; break; } }}void Dot::move( Tile *tiles[] ){ box.x += xVel; AREAX += xVel; if( ( box.x < 0 ) || ( box.x + DOT_WIDTH > LEVEL_WIDTH ) || touches_wall( box, tiles ) ) { box.x -= xVel; AREAX -= xVel; } box.y += yVel; AREAY += yVel; if( ( box.y < 0 ) || ( box.y + DOT_HEIGHT > LEVEL_HEIGHT ) || touches_wall( box, tiles ) ) { box.y -= yVel; AREAY -= yVel; } std::stringstream caption; caption << "DEMO " << box.x << " " << box.y; SDL_WM_SetCaption( caption.str().c_str(), NULL ); }void Dot::show(){ apply_surface( box.x - camera.x, box.y - camera.y, dot, screen );}void Dot::set_camera(){ if( ( AREAX >= SCREEN_WIDTH ) && ( box.x < LEVEL_WIDTH ) && ( xVel == 10 ) ) { for( int SCROLL = 0; SCROLL < 650; SCROLL += 10 ) { camera.x = SCROLL; } AREAX -= SCREEN_WIDTH; } if( ( AREAX <= -15 ) && ( box.x > 0 ) && ( xVel == -10 ) ) { for( int SCROLL = 650; SCROLL > -10; SCROLL -= 10 ) { camera.x = SCROLL; } AREAX += SCREEN_WIDTH;}if( ( AREAY >= SCREEN_HEIGHT ) && ( box.y < LEVEL_HEIGHT ) && ( yVel == 10 ) ){ for( int SCROLL = 0; SCROLL < 490; SCROLL += 10 ) { camera.y = SCROLL; } AREAY -= SCREEN_HEIGHT;}if( ( AREAY <= -15 ) && ( box.y > 0 ) && ( yVel == -10 ) ){ for( int SCROLL = 490; SCROLL > -10; SCROLL -= 10 ) { camera.y = SCROLL; } AREAY += SCREEN_HEIGHT;}}Timer::Timer(){ startTicks = 0; pausedTicks = 0; paused = false; started = false;}void Timer::start(){ started = true; paused = false; startTicks = SDL_GetTicks();}void Timer::stop(){ started = false; paused = false;}void Timer::pause(){ if( ( started == true ) && ( paused == false ) ) { paused = true; pausedTicks = SDL_GetTicks() - startTicks; }}void Timer::unpause(){ if( paused == true ) { paused = false; startTicks = SDL_GetTicks() - pausedTicks; pausedTicks = 0; }}int Timer::get_ticks(){ if( started == true ) { if( paused == true ) { return pausedTicks; } else { return SDL_GetTicks() - startTicks; } } return 0;}bool Timer::is_started(){ return started;}bool Timer::is_paused(){ return paused;}int main( int argc, char* args[] ){ bool quit = false; Dot myDot; Tile *tiles[ TOTAL_TILES ]; Timer fps; if( init() == false ) { return 1; } if( load_files() == false ) { return 1; } clip_tiles(); if( set_tiles( tiles ) == false ) { return 1; } if( Mix_PlayMusic( music, -1 ) == -1 ) { return 1; } while( quit == false ) { fps.start(); while( SDL_PollEvent( &event ) ) { myDot.handle_input(); if( event.type == SDL_QUIT ) { quit = true; } } myDot.move( tiles ); myDot.set_camera(); for( int t = 0; t < TOTAL_TILES; t++ ) { tiles[ t ]->show(); } myDot.show(); if( SDL_Flip( screen ) == -1 ) { return 1; } if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) { SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() ); } } clean_up( tiles ); return 0;}