Tuesday, February 8, 2011

Code::Blocks(9) RPG 1 - Tileset and maps(2)

Thinking about the tileset blitting function i have changed my mind, i have realized that it's easier to make just one blitting function that can draw tiles using a grid system and adjust the tiles with an x and y offset.

Now let's change the function:

engine.h
 void BlitTileset(int x,int y, int OX, int OY, int tile); // Blit a tile from our tileset to the screen

Notice: OX and OY >> (Offset X and Y)

engine.cpp

void Engine::BlitTileset(int x, int y, int OX, int OY,int tile) //Blits a tile from our tileset to the screen
{
SDL_Rect start,end;
//set the cliping for the source surface (the tileset)
start.x= (tile%8)*TileSize; //Column = tile % 8 (0-7)
start.y= (tile/8)*TileSize; //Row = tile / 8 (0-7) >> we could use here a bit shift to divide by 8, but i think it's easier to understand that way...
start.w=TileSize;
start.h=TileSize;
//set the cliping for the destination surface (the screen)
end.x = (x-1)*TileSize + OX; //Offset X
end.y = (y-1)*TileSize + OY; //Offset Y
end.w = TileSize;
end.h = TileSize;
SDL_BlitSurface(tileset, &start, screen, &end); //Blits a tile from our tileset to the screen
}

That way we can use the same function to draw the map, the pcs , npcs and the mouse cursor, in fact we are going to change the mouse drawing function too so it will be like:

void Engine::UpdateMouse()
{
MouseButtons = SDL_GetMouseState(&MouseX, &MouseY); //Update mouse state
BlitTileset(0,0,MouseX,MouseY,24); //Blits the mouse pointer into the screen
}

Easier that way , right?

In the next post, i will show how to draw a multi - layer map...

see you soon

No comments: