# SDL: How would I add tile layers with my area class as a singleton?

I´m trying to wrap my head around how to get this done, if at all possible. So basically I have a Area class, Map class and Tile class. My Area class is a singleton, and this is causing some confusion. I´m trying to draw like this: Background / Tiles / Entities / Overlay Tiles / UI.

void C_Application::OnRender()
{
// Fill the screen black
SDL_FillRect( Surf_Screen, &Surf_Screen->clip_rect, SDL_MapRGB( Surf_Screen->format, 0x00, 0x00, 0x00 ) );

// Draw background

// Draw tiles
C_Area::AreaControl.OnRender(Surf_Screen, -C_Camera::CameraControl.GetX(), -C_Camera::CameraControl.GetY());

// Draw entities
for(unsigned int i = 0;i < C_Entity::EntityList.size();i++)
{
if( !C_Entity::EntityList[i] )
{
continue;
}

C_Entity::EntityList[i]->OnRender( Surf_Screen );
}

// Draw overlay tiles

// Draw UI

// Update the Surf_Screen surface
SDL_Flip( Surf_Screen);
}


Would be nice if someone could give a little input. Thanks.

Are you using the tutorials from SDLTutorials.com?

Assuming you're following the tutorials and don't want to modify things too heavily yet, you could create two rendering functions for C_Area: C_Area::OnRender(...) which would function exactly the same as it already does and C_Area::OnRenderOverlay(...) which would be called after your entities are drawn:

// Draw tiles
C_Area::AreaControl.OnRender(Surf_Screen, -C_Camera::CameraControl.GetX(), -C_Camera::CameraControl.GetY());

// Draw entities
for(unsigned int i = 0;i < C_Entity::EntityList.size();i++)
{
if( !C_Entity::EntityList[i] )
{

continue;

}

C_Entity::EntityList[i]->OnRender( Surf_Screen );

}

// Draw overlay tiles
C_Area::AreaControl.OnRenderOverlay(Surf_Screen, -C_Camera::CameraControl.GetX(), -C_Camera::CameraControl.GetY());


In C_Area's variables, you would then have to maintain another std::vector<CMap> that would be read in like the first map, except the data file you're reading would contain information about your overlay tiles. You would have to modify the OnLoad function to read it. C_Area::OnRenderOverlay(...) would simply draw the data associated with this vector on top of everything else.

Alternatively... redefine C_Area so it's not a singleton?

• Thanks for your response, I did think about adding a map for the overlay and adding a OnRender method for that one. It´s just that I´m "pretty" new to this and just wanted to get a better grasp on if that would be a good idea, or just plain bad design in some way. Thanks again.
– Tony
Sep 16 '12 at 17:38