After writing the first tutorial i have realized that the post got too big, so in that tutorial i will split it into smaller posts, making it easier to follow.
I'm going to start modifying the functions "SetVga" and "LoadTileset", the objectives are simples:
- We want the "SetVga" function to set any graphic mode and Full Screen/windowed mode.
- The function "LoadTileset" should get the path to the file we want to load instead of having it "Hard Coded" inside the Engine class.
Now we have to open engine.h and change:
void SetVga(); //Set the video mode
To:void SetVga(Uint16 Width, Uint16 Height, Uint8 BPP,bool FullScreen); //Set the video mode
SetVga now accepts 4 parameters:- Width of the desired video mode
- Height of the desired video mode
- BPP:Color depth 8bits, 16bits, 32bits...
- FullScreen: true to make it fullscreen, false to start a windowed mode
Then change:
void LoadTileset(); //loads a test tileset
To:void LoadTileset(char *File); //loads a test tileset
Notice that we only add the parameter "File".Save the file and open engine.cpp
Now we have to replace
void Engine::SetVga() // set video mode { screen = SDL_SetVideoMode(ScreenX, ScreenY, bpp,SDL_SWSURFACE|SDL_DOUBLEBUF); /* It will set up a windowed video mode (width=screenx, height = screeny, bpp = bpp) if we want to use a fullscreen mode we have to add "|SDL_FULLSCREEN" after SDL_DOUBLEBUF */ }
void Engine::SetVga(Uint16 Width, Uint16 Height, Uint8 BPP,bool FullScreen) // set video mode { ScreenX = Width; ScreenY = Height; bpp = BPP; if(FullScreen==true) { screen = SDL_SetVideoMode(ScreenX, ScreenY, bpp,SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN); }else{ screen = SDL_SetVideoMode(ScreenX, ScreenY, bpp,SDL_SWSURFACE|SDL_DOUBLEBUF); // It will set up a windowed video mode (width=screenx, height = screeny, bpp = bpp) } }
Then replace:
void Engine::LoadTileset() //loads an image into a memory (tileset), assigns the colorkey { SDL_Surface* TmpSurface = NULL; //Temporary surface for loading TmpSurface = IMG_Load( "tileset.png"); //Load the image into a temporary surface if( TmpSurface != NULL ) { tileset= SDL_DisplayFormat( TmpSurface );//optimize surface SDL_FreeSurface( TmpSurface); //free temporary surface } SDL_SetColorKey(tileset,SDL_SRCCOLORKEY | SDL_RLEACCEL,SDL_MapRGB(screen -> format, 255, 0, 255)); //set the color key }
void Engine::LoadTileset(char *File) //loads an image into a memory (tileset), assigns the colorkey { SDL_Surface* TmpSurface = NULL; //Temporary surface for loading TmpSurface = IMG_Load(File); //Load the image into a temporary surface if( TmpSurface != NULL ) { tileset= SDL_DisplayFormat( TmpSurface );//optimize surface SDL_FreeSurface( TmpSurface); //free temporary surface } SDL_SetColorKey(tileset,SDL_SRCCOLORKEY | SDL_RLEACCEL,SDL_MapRGB(screen -> format, 255, 0, 255)); //set the color key }
Enough for now, in the next post I'll talk more about tilesets and multi-layered maps..