Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL event loop quitting?

I am making an RTS in C++ using SDL for graphics.

Every time I run the game, it crashes without errors or anything in the compiler debug window. It doesn't crash immediately or consistently, sometimes taking 10 seconds to crash and other times taking 2 minutes to crash.

When I played around with return values (at the end of the main function) it turned out it wasn't crashing, but rather quitting as the return values were consistent with what I changed it to.

The only theory that I have is that my poll event loop is glitching and telling the program to quit when it isn't supposed to.

Here is my event loop, contained within my game loop:

    if( SDL_PollEvent( &event ) )
    {
        if( event.type == SDL_MOUSEMOTION )
        {
            mx = event.motion.x;
            my = event.motion.y;
        }

        if( event.type == SDL_MOUSEBUTTONDOWN )
        {
            if( hut.getselected() && hut.getplacable() )
            {
                hut.place( map );
            }
        }

        if( event.type == SDL_QUIT )
        {
            quit = true;
        }

        switch( event.key.keysym.sym )
        {
        case SDLK_ESCAPE: quit = true; break;
        }
    }

Is it possible that when the mouse moves or clicks, that it is confusing it for exiting? I don't think the ram is overloading either because it only displays what it needs to tile-wise.

Is it also possible that my compiler, VisualC++, is screwing up?

like image 239
Blake Doeren Avatar asked Dec 27 '22 07:12

Blake Doeren


1 Answers

How about changing the switch at end of your snippet to:

if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
    quit = true;
}

Because in your code you check keysym for all events, so usually event is of wrong type when you test if it is escape, and you get "garbage" value for keysym, and sometimes it matches with ESC.


Actually might be good idea to test the event.type with switch:

switch(event.type) {
case SDL_MOUSEMOTION:
    //....
    break;
case SDK_KEYDOWN:
    switch(event.key.keysym.sym) {
    case SDLK_ESCAPE: 
        quit = true;
        break;
    // cases for other keypresses
    }
    break;
// cases for other events
}
like image 51
hyde Avatar answered Jan 08 '23 10:01

hyde