Sign in to follow this  
skwee

SDL: cant handle all events before exit

Recommended Posts

Hello. I'm using SDL and i have normal handle while. I'm handling 5 buttons, up, right, left, down, esc. The arrow keys moving the player i also have an inside if for players walking delay:
if(pWalkTime > 10)
  {
    if(event->type == SDL_KEYDOWN)
    {
      switch(event->key.keysym.sym)
      {
        case SDLK_UP:
          CreatureSetDir(UP);
          CreatureSetAction(WALKING);
          if((pTemp.y-1) >= 0)
          {
            if(map->MapGetBlockStatus(pTemp.x, pTemp.y-1) == PASSABLE)
            {
              CreatureSetFrame();
              if(((y-1) == (HSCREEN / 2) / HBLOCK) && (*mapStartY > 0))
              {
                *mapStartY -= 1;
                CreatureSetCoord(x, y);
              }
              else
                CreatureSetCoord(x, y-1);
              pTemp.y -= 1;
            }
          }
          break;
      }
    }
   pWalkTime = 0;
  }
Sometimes when i press a lot of buttons in small period of time and then press the X to close the window my program stuck and the i can only shut it down by task manager. I think its because SDL don't have the needed amount of time to handle all the events before closing the window. So the question is how i can empty the event queue before closing the window? Maybe the problem is not because of event handling? I need to mark that it happen when i press 3-4 keys at one time many time in small period and then close the window. If i'll wait after the pressing the window will be closed normally. EDIT It not happened every time i tried now 3-4 times its not happened but sometimes it is and its annoying. Thanks a lot

Share this post


Link to post
Share on other sites
I'm not sure what the problem is from what you posted. What is the pWalkTime supposed to accomplish? Post your entire event loop (including the handling of the quit event), and maybe I can help.

Share this post


Link to post
Share on other sites

while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
SDL_Delay(1);
running = false;
}
player->PlayerHandleInput(&event, map, &mapX, &mapY);
}

void Player::PlayerHandleInput(SDL_Event *event, Map *map,
int *mapStartX, int *mapStartY) //Handle input etc...
{
int x,y;
int mx, my;
CreatureGetCoord(&x, &y);
map->MapGetSize(&mx, &my);
if(event->type == SDL_KEYDOWN)
{
switch(event->key.keysym.sym)
{
case SDLK_UP:
if(pWalkTime > 10)
{
CreatureSetDir(UP);
CreatureSetAction(WALKING);
if((pTemp.y-1) >= 0)
{
if(map->MapGetBlockStatus(pTemp.x, pTemp.y-1) == PASSABLE)
{
CreatureSetFrame();
if(((y-1) == (HSCREEN / 2) / HBLOCK) && (*mapStartY > 0))
{
*mapStartY -= 1;
CreatureSetCoord(x, y);
}
else
CreatureSetCoord(x, y-1);
pTemp.y -= 1;
}
}
}
break;
case SDLK_DOWN:
if(pWalkTime > 10)
{
CreatureSetDir(DOWN);
CreatureSetAction(WALKING);
if((pTemp.y+1) < my)
{
if(map->MapGetBlockStatus(pTemp.x, pTemp.y+1) == PASSABLE)
{
CreatureSetFrame();
if(((y+1) == (HSCREEN / 2) / HBLOCK) && (*mapStartY < (my - (HSCREEN / HBLOCK))))
{
*mapStartY += 1;
CreatureSetCoord(x, y);
}
else
CreatureSetCoord(x, y+1);
pTemp.y += 1;
}
}
}
break;
case SDLK_LEFT:
if(pWalkTime > 10)
{
CreatureSetDir(LEFT);
CreatureSetAction(WALKING);
if((pTemp.x-1) >= 0)
{
if(map->MapGetBlockStatus(pTemp.x-1, pTemp.y) == PASSABLE)
{
CreatureSetFrame();
if(((x-1) == (WSCREEN / 2) / WBLOCK) && (*mapStartX > 0))
{
*mapStartX -= 1;
CreatureSetCoord(x, y);
}
else
CreatureSetCoord(x-1, y);
pTemp.x -= 1;
}
}
}
break;
case SDLK_RIGHT:
if(pWalkTime > 10)
{
CreatureSetDir(RIGHT);
CreatureSetAction(WALKING);
if((pTemp.x+1) < mx)
{
if(map->MapGetBlockStatus(pTemp.x+1, pTemp.y) == PASSABLE)
{
CreatureSetFrame();
if(((x+1) == (WSCREEN / 2) / WBLOCK) && (*mapStartX < (mx - (WSCREEN / WBLOCK))))
{
*mapStartX += 1;
CreatureSetCoord(x, y);
}
else
CreatureSetCoord(x+1, y);
pTemp.x += 1;
}
}
}
break;
}
}
pWalkTime = 0;
if(event->type == SDL_KEYUP)
{
switch(event->key.keysym.sym)
{
case SDLK_UP:
case SDLK_DOWN:
case SDLK_LEFT:
case SDLK_RIGHT:
CreatureSetAction(STANDING);
CreatureSetFrameNull();
break;
}
}
}


pWalkTime is a variable that limit the players movement speed. it increased in the main while game running

Share this post


Link to post
Share on other sites
You've got a lot of code duplication there. Try moving the code that changes the creature position out of the switch, so the only thing the switch does is an appropriate CreatureSetDir(). It may also help to deal with the key up and key down closer to each other.

It might not help with the actual issue. Event handling in SDL is pretty tricky.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Minios
Sometimes when i press a lot of buttons in small period of time and then press the X to close the window my program stuck and the i can only shut it down by task manager. I think its because SDL don't have the needed amount of time to handle all the events before closing the window.


Your best bet is to learn to debug, see where it's stuck and stop guessing.


Quote:

So the question is how i can empty the event queue before closing the window?


That's most likely impossible, you could try something like this:

SDL_Event event;
while(SDL_PollEvent(&event))
{ /* do nothing */ }



but even with the above, more events can be generated between emptying the queue and closing the window. So you see, "emptying" the event queue will be a total waste of time.

Share this post


Link to post
Share on other sites
Huh :) I love Anonymous Posters and MS Visual studio debugger :)
The problem found and fixed (i hope).
If some one will stuck with this problem I'm explaining now how to fix it:
I got class named Block.
Class block have private member called "struct bInfo_type{SDL_Surface *bPic;}bInfo;"
that hold pointer to SDL_Surface (picture).
Also this class have a destructor that callas only one function
SDL_FreeSurface(bInfo.bPic);
The "main" program when he (or maybe she? :) created a Block class it gave to block, pointer to picture. Anyway the problem was that i deleted the Block object that mean i called the destructor that freed the picture and then i tried fre this picture again but this picture is not exist! How stupid I'm :\ That what happened when you programming big project alone 8 hours continuously. The destructor is just doing bInfo.bPic = NULL;

Thanks a lot for people who helped :)

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