Tuesday, January 11, 2011

Code::Blocks(7) RPG 1

Hello again!

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.
So let's create a new project called "rpg" , then we have to add again 3 files: engine.h, engine.cpp and rpg.cpp, then we have to copy the contents of the engine class from the previous tutorial (copy+paste) and save the project.

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:
  1. Width of the desired video mode
  2. Height of the desired video mode
  3. BPP:Color depth 8bits, 16bits, 32bits...
  4. 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  */
}
To:
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)
    }
}
Before, the engine used the default parameters initialized when we create the Engine class, after we can set any video mode.

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
}
To:
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
}
We can now load any file.

Enough for now, in the next post I'll talk more about tilesets and multi-layered maps..

No comments: