# SDL - halting event response

This topic is 3247 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i have one problem. i'm trying to develop a board game. basically, it's this: you click on the board and select the position you want to activate. then you wait for the cpu (your opponent) to do the same. then it's you again and it keeps going. the problem, when the cpu is playing i want the game to have a little delay, otherwise it would seem strange if the cpu plays so fast. now, what happens is that if you click while on the delay of the cpu, it goes to the event queue and when it's your turn again he reads the click you made while on the cpu's turn and activates the position you clicked. but i don't want that to happen. any clicks you make while the opponent is "thinking" have to be ignored. i thought about using SDL_PeepEvents or something like that to clear the event queue, but i don't like how it works, since i may lost other events which should be responded and also it causes my program to crash depending on how many clicks i make. what should i do??

##### Share on other sites
I think this is how you'd use SDL_PeepEvents to clear the input queue of mouse events (however, see below):

SDL_Event event;while(SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(SDL_MOUSEBUTTONDOWN) | SDL_EVENTMASK(SDL_MOUSEBUTTONUP)) > 0);

However, there is a better way of getting the AI-fake-thinking delay you want. I'm guessing you're currently using SDL_Delay or similar to implement a blocking delay.

Do you have a game state type and variable already? You want a game state that looks something like this:

enum GameState {    WAITING_FOR_PLAYER,    AI_FAKE_THINKING};GameState state;  // put this somewhere where it is accessible to the routines that will need it, like the input handling code (and only those ones!)

At the start of the game, you set the state to WAITING_FOR_PLAYER, and wait for a mouse input event (or any other kind of event you need to handle). When a click occurs, carry out the player's move, then change state to AI_FAKE_THINKING.

You now go back to waiting for mouse input events (you can use the same routine that was used in the previous state), however when state is AI_FAKE_THINKING, you simply discard the mouse input events (or better yet - handle them by showing a message like "Your competitor is still comtemplating their move!")

After being in the AI_FAKE_THINKING state for a predefined amount of time, you make the AI's move proper, and go back to the WAITING_FOR_PLAYER state (perhaps after announcing a message like "Your move!") Note that to implement this delay in this state you might need to switch to using SDL_PollEvents and checking the time each loop, instead of something like SDL_WaitEvents as I cannot see a version of that that allows you to specify a timeout parameter.

You might have noticed there are only two states in this setup, so you could effectively just have a boolean variable that indicates whether or not the AI is thinking instead. However the nice thing about having a enumerated state type is that it can be easily extended later on if you decide to get more complex, and have more than 2 game states.

##### Share on other sites
hum, thanks, now i understand.
i had already thought about the gamestate flag, but i was simply disabling SDL_PooLEvents() during the IA movement, so it just wouldn't work anyway, thanks.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
14
5. 5

• 12
• 9
• 9
• 17
• 20
• ### Forum Statistics

• Total Topics
632600
• Total Posts
3007339

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!