C++/SDL Clear Surface?

Hey everyone I was wondering how I would clear a surface from the screen with SDL.

Here is my CGame class which runs the core function of the game. I wrote the Tic Tac Toe game really sloppy but I will be improving this later, wrote this in like 10 minutes. Wouldn't let me post my whole class to big sorry everyone

Here is where I want to reset the game board surface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if (event->button.button == SDL_BUTTON_LEFT)
        {
            if (won == true) // If game is over reset game on click
            {
                // RESET BOARD
                board[0] = 0;
                board[1] = 0;
                board[2] = 0;
                board[3] = 0;
                board[4] = 0;
                board[5] = 0;
                board[6] = 0;
                board[7] = 0;
                board[8] = 0;

                //clear X surface's
                //clear O surface's
                //and update game board

                won = false;
            }
}
Just overwrite the screen surface with black, what's the problem?
When I do:
SDL_FillRect(background, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
It fills it over my Tic Tac Toe background grid surface
Last edited on
So draw your "Tic Tac Toe background grid" again.
It worked thanks Peter87 :D Should I set background NULL then load the image back to it? It works either way just curious on what I should do.

1
2
3
4
5
6
 //clear X surface's
                //clear O surface's
                //and update game board
                background = NULL;
                background = CResourceManager::loadImage("background.png", false);
                CResourceManager::applySurface(background, screen);


1
2
3
4
5
 //clear X surface's
                //clear O surface's
                //and update game board
                background = CResourceManager::loadImage("background.png", false);
                CResourceManager::applySurface(background, screen);
Haven't you loaded the background image already? No point doing that again. Just draw the background on to the screen.
CResourceManager::applySurface(background, screen);
If background was already pointing to the loaded PNG, why would you load the PNG again?
It only works if I reload the image with my loadImage function. If I just draw it to the screen again it doesn't clear the background of the X's and O's.
In my render function I have it like this:

1
2
3
4
5
6
void CGame::render()
{
    CResourceManager::applySurface(background, screen);

    SDL_Flip(screen);
}


So if I load the background surface to the old background image it clears it and don't need to draw it again cause i'm looping through it in my main class.
It sounds like you have drawn the X's and O's to the background. Draw them to the screen instead.
When I draw them to the screen they don't show up. If I draw them to the background it does.
Did you draw them in the right order? First the background, then the X's and O's.
That's because you're drawing the background over them. Either draw the background just once, or draw the background and every X and O in every frame.
Topic archived. No new replies allowed.