#include "SDLhelpfunctions.h" #include "Map.h" #include "SDL/SDL.h" #include //X Y Z //0 is for Map 1 is for player; SDL_Event event; int main (int argc, char *argv[]) { load_map(); SDL_Init(); //The frame rate regulator Timer fps; int frame = 0; player = load_image("Player.bmp"); background = load_image("Ground.bmp"); stone = load_image("Stone.bmp"); bool quit = false; //Sets Players starting position map[x][y][1]=1; while(quit==false) { fps.start(); draw_map(); frame++; while( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) ; if( SDL_PollEvent( &event ) ) { //Checks if user wants to quit if( event.type == SDL_QUIT ) { quit = true; } //Checks if user has pressed a key } Uint8 *keystates = SDL_GetKeyState( NULL ); if( keystates[ SDLK_UP ] ) { switch (map[x][y-1][0]) { case 0: map[x][y][1]=0; map[x][--y][1]=1; break; default: break; } } if( keystates[ SDLK_DOWN ] ) { switch (map[x][y+1][0]) { case 0: map[x][y][1]=0; map[x][++y][1]=1; break; default: break; } } if( keystates[ SDLK_LEFT ] ) { switch (map[x-1][y][0]) { case 0: map[x][y][1]=0; map[--x][y][1]=1; break; default: break; } } if( keystates[ SDLK_RIGHT ] ) { switch (map[x+1][y][0]) { case 0: map[x][y][1]=0; map[++x][y][1]=1; break; default: break; } } } return 0; } //SDLhelpfunctions.h #ifndef SDLHELPFUNCTIONS_H_0000000000 #define SDLHELPFUNCTIONS_H_0000000000 //header files #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include //Screen attributes const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; //Set FPS const int FRAMES_PER_SECOND = 10; SDL_Surface *stone; SDL_Surface *player; SDL_Surface *background; SDL_Surface *screen; //The timer class Timer { private: //The clock time when the timer started int startTicks; //The ticks stored when the timer was paused int pausedTicks; //The timer status bool paused; bool started; public: //Initializes variables Timer(); //The various clock actions void start(); void stop(); void pause(); void unpause(); //Get the number of ticks since the timer started //or gets the number of ticks when the timer was paused int get_ticks(); //Checks the status of the timer bool is_started(); bool is_paused(); }; //Load image function SDL_Surface *load_image( char filename [50] ) { //Temporary storage for the image that's loaded SDL_Surface* loadedImage = NULL; //The optimized image that will be used SDL_Surface* optimizedImage = NULL; //Load the image loadedImage = SDL_LoadBMP( filename ); //If nothing went wrong in loading the image if( loadedImage != NULL ) { //Create an optimized image optimizedImage = SDL_DisplayFormat( loadedImage ); //Free the old image SDL_FreeSurface( loadedImage ); //Map the color key Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 255, 255, 255 ); //Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey ); } //Return the optimized image return optimizedImage; } void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination ) { //Temporary rectangle to hold the offsets SDL_Rect offset; //Get the offsets offset.x = x; offset.y = y; //Blit the surface SDL_BlitSurface( source, NULL, destination, &offset ); } bool SDL_Init() { //Initialize all SDL subsystems if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false; } //Set up the screen screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE ); //If there was in error in setting up the screen if( screen == NULL ) { return false; } //Set the window caption SDL_WM_SetCaption( "Mirnia (C) Mirchiss 2006", NULL ); //If everything initialized fine return true; } Timer::Timer() { //Initialize the variables startTicks = 0; pausedTicks = 0; paused = false; started = false; } void Timer::start() { //Start the timer started = true; //Unpause the timer paused = false; //Get the current clock time startTicks = SDL_GetTicks(); } void Timer::stop() { //Stop the timer started = false; //Unpause the timer paused = false; } void Timer::pause() { //If the timer is running and isn't already paused if( ( started == true ) && ( paused == false ) ) { //Pause the timer paused = true; //Calculate the paused ticks pausedTicks = SDL_GetTicks() - startTicks; } } void Timer::unpause() { //If the timer is paused if( paused == true ) { //Unpause the timer paused = false; //Reset the starting ticks startTicks = SDL_GetTicks() - pausedTicks; //Reset the paused ticks pausedTicks = 0; } } int Timer::get_ticks() { //If the timer is running if( started == true ) { //If the timer is paused if( paused == true ) { //Return the number of ticks when the the timer was paused return pausedTicks; } else { //Return the current time minus the start time return SDL_GetTicks() - startTicks; } } //If the timer isn't running return 0; } bool Timer::is_started() { return started; } bool Timer::is_paused() { return paused; } #endif //Map.h #ifndef MAP_H_1893463875 #define MAP_H_1893463875 //header files #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include int map[20][15][2]; char x=4,y=4; void load_map() { FILE *tiedosto; int k=0,i,j; char loader[600]; tiedosto = fopen("map.txt","r"); while (!feof(tiedosto)) { fgets(loader, 600, tiedosto); } fclose(tiedosto); for (i = 0; i<=19;i++ ) { for (j = 0; j<=14;j++ ) { map[i][j][0]=loader[k]; k++; } } return ; } void draw_map() { int i=0,j=0; for (i = 0; i<=19;i++ ) { for (j = 0; j<=14;j++ ) { switch (map[i][j][0]) { case 0: apply_surface(i*32,j*32,background,screen); break; case 1: apply_surface(i*32,j*32,stone,screen); break; default : break; } switch (map[i][j][1]) { case 1: apply_surface(i*32,j*32,player,screen); break; default : break; } } } SDL_Flip(screen); } #endif