Sign in to follow this  
Sceletor

[SDL] clearing surfaces

Recommended Posts

Sceletor    139
Hi there, Im currently trying to convince my SDL game to clear a surface by painting it black and then paint some new .png´s to the surface. Right now I have encountered an error that I can´t understand, and I hope someone here with a deeper understanding of SDL can. My Problem is that when I call my clear() method before calling checkWindowState(), pointer_Screen is undefined and I get a segmentation fault. When I call clear() after checkWindowState(), everything runs fine. As you can see, checkWindowState() doesnt have anything to do with the pointer, and thats why Im so confused..
void Game::init()
{
    pointer_Background = new Sprite;
    pointer_Background->load("Images/welcome.png");

    pointer_Mouse = new Sprite;
    pointer_Mouse->load("Images/cursor1.png");

    gameWindow = firstScreen;
    gameRun =  true;

}

void Game::run()
{
    SDL_ShowCursor(0);

    while (gameRun == true)
    {
        processEvents();

        pointer_Frame->clear(); //if I use clear() here, pointer_Screen trigger a seg fault

        checkWindowState();

        pointer_Frame->clear(); //if I call it here, everything is fine

        SDL_GetMouseState(&mouseX, &mouseY);

        pointer_Mouse->setPos(mouseX, mouseY);
        pointer_Mouse->render();

        pointer_Frame->flip();


    }
}

void Game::checkWindowState()
{
    switch(gameWindow)
    {
        case(firstScreen):
        {
            pointer_Background->load("Images/welcome.png");
            pointer_Background->render();
        } break;

        case(galaxy):
        {
            pointer_Background->load("Images/galaxy.png");
            pointer_Background->render();
        } break;
    }
}

void Frame::clear()
{
    pointer_Screen = getScreen();  //This is where the segfault is triggered
    SDL_FillRect(pointer_Screen, NULL, SDL_MapRGB(pointer_Screen->format, 0, 0, 0));
}
If you need more parts of the code to understand my problem, just tell me...

Share this post


Link to post
Share on other sites
Kylotan    10008
What does getScreen() do? Surely that is returning something different depending on the circumstance, and therefore the content of that function may be useful. I notice that you don't seem to be checking for errors at any stage, which is likely to lead to problems that are hard to understand.

Share this post


Link to post
Share on other sites
Sceletor    139
SDL_Surface *getScreen() {return SDL_GetVideoSurface();}


And regarding error checking:
Of course you´re right, but my program was running fine, and just checking for an invalid pointer is useless to me since I know that it becomes invalid and I used the debugger to determine the exact time it happens.

I just don´t know why the screen pointer is sometimes defined and sometimes not, I guess its either something SDL-specific or I missed something very important in C++.

So, could you please elaborate this:

Quote:
Original post by Kylotan Surely that is returning something different depending on the circumstance


a little further? What circumstances? Im currently jumping between an Instance of my class Game, my class Frame and my Class Sprite, and sometimes the pointer_screen in Frame (which is a singleton class) is valid and sometimes not. I hoped I solved the problem by using SDL_GetVideoSurface(), but appearently I was wrong.

If this has something to do with an error message like "pointer_screen not defined in current context", which I get repeatedly when I run in debug mode (and only then), please tell me whats going on or where I can find some information on this matter, I just have no clue what this could mean.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this