Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to stop events from being detected?

This thread is locked; no one can reply to it. rss feed Print
How to stop events from being detected?
SomeoneConfused
Member #15,890
February 2015

Hello allegro masters!

I have been creating small and fun games using allegro 5.0.10 since 5 months.
I can't say that i haven't had some difficulties with it, but i got used to it.

Now I'm currently writing a game that includes a pause option. So when you click a button on the screen, it pauses the game.

The way that i set it up is that when i get a click on the cordinates on my button, the current timer gets stopped, and a pause menu function gets called which starts another event queue and displays some buttons to keep playing or get back to the main menu. After the person clicks the button to keep playing, the timer of the game loop gets started again.

The part of my code where i describe what is happening is this:
[code]if(events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN){
if(mouseX<=width-10 && mouseX>=width-35 && mouseY>=10 && mouseY<=40){ //if the click is on the pause button
al_stop_timer(timer);
PauseMenu(); //call the pause function
al_start_timer(timer);
}
}[/code]

My problem: When the pause menu gets displayed, any actions i do ( like clicking on the display ) get registered as events in my main game loop. So when the player un-pauses the game, all clicks that were made in the pause menu take effect. I do not want that. Is there any way to avoid events getting detected by my main game loop where i use this pause function?

I didn't show you my whole code because it is 1100+ lines

Edgar Reynaldo
Member #8,592
May 2007
avatar

You can either use al_unregister_event_source or you can create another event queue that subscribes to the relevant sources in the second. Actually, you should pass an ALLEGRO_EVENT_QUEUE* (your queue) into your PauseMenu() function and have it monitor the events instead. That way you can still ignore the ones you don't need and you can use the ones that you do.

Edit
Use al_pause_event_queue

Thomas Fjellstrom
Member #476
June 2000
avatar

I might be lazy and just tell the main game event handling to ignore events if(paused) continue; kind of thing.

But that al_pause_event_queue function looks handy.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

SomeoneConfused
Member #15,890
February 2015

Thanks for the suggestions about the al_pause_event_queue function. But I'm using the latest recommended build of allegro ( 5.0.10 ) and this function is included in 5.1. I do not know if i should update it or insert my pause function in my main game loop, and not as additional function creating additional events loop.

EDIT: i did my code that way:
else if(events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN){
if(mouseX<=width-10 && mouseX>=width-35 && mouseY>=10 && mouseY<=40){
al_stop_timer(timer);
al_unregister_event_source(event_queue,al_get_keyboard_event_source());
al_unregister_event_source(event_queue,al_get_mouse_event_source());
PauseMenu();
al_register_event_source(event_queue,al_get_keyboard_event_source());
al_register_event_source(event_queue,al_get_mouse_event_source());
al_start_timer(timer);
}
}

I do not know however if constantly unregistering and registering sources again always when a person pauses the game is a good and very efficient process.

Thomas Fjellstrom
Member #476
June 2000
avatar

I don't think its too inefficient. I think it'll work fine. It's a nice quick solution to the problem.

Though, I would probably slap each game mode into its own event handler, so the main event loop just dispatches to the event handlers for each active mode (say the game, and the menu). In pause mode the game mode would know not to actually handle events related to updating certain aspects of the game.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Member #8,592
May 2007
avatar

SomeoneConfused
Member #15,890
February 2015

the al_flush_event_queue() seems really good function. I can use it instead of removing and adding back event sources. Currently my code is this:

if(mouseX<=width-10 && mouseX>=width-35 && mouseY>=10 && mouseY<=40){
al_stop_timer(timer);
PauseMenu();
al_flush_event_queue(event_queue);
al_start_timer(timer);
}

If this metodh is hardware efficient and does not take much processing to complete, i will leave it that way. But I'm always open for more suggestions. Thanks for the ones above me ^

Also, do i need to stop my game loop's timer before and start it after the pause menu if i flush the event queue? Or it will flush all timer events since the pause menu? Because if the timer isn't stopped, the code that runs when there is timer event happening, runs about 100-200 times at once, depending on how long I paused the game, so that is why i stop the timer. Is there al_pause_timer or the only way to pause a timer is to stop it and restart it?

bamccaig
Member #7,536
July 2006
avatar

I agree with Thomas. I think that the solution here is neither to stop nor flush the main loop event queue, but rather to restructure the program so that the main loop handles the different program modes efficiently. I'm confused though. Is the "pause menu" running on a different thread? It seems if that call blocked then you should be more or less fine. I still think restructuring the code is better. Have a single timer, a single main loop, and use a game "mode" and optionally function pointers to control the logic at runtime.

Thomas Fjellstrom
Member #476
June 2000
avatar

the al_flush_event_queue() seems really good function. I can use it instead of removing and adding back event sources. Currently my code is this:

Assuming you can use al_pause_event_queue that might be an even better solution. Don't think you even have to start/stop the timer in that case, it will keep firing in the background, but it won't insert events into your queue. One function call instead of three.

bamccaig said:

It seems if that call blocked then you should be more or less fine. I still think restructuring the code is better.

Allegro uses a thread to pump timer events onto your queue. so it'll fill your queue with events even if your main thread blocks.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

bamccaig
Member #7,536
July 2006
avatar

Allegro uses a thread to pump timer events onto your queue. so it'll fill your queue with events even if your main thread blocks.

Ah, yes, that hadn't occurred to me. Seems he has already reached that solution by flushing the queue. Mostly I say restructure.

SomeoneConfused
Member #15,890
February 2015

Like i said above Thomas, i use allegro 5.0.10, the latest stable build of allegro. The al_pause_event_queue is included in allegro 5.1.

So you're saying that i don't need to stop and start the timer again, if i just flush the event_queue after my pause menu?

Thomas Fjellstrom
Member #476
June 2000
avatar

Like i said above Thomas, i use allegro 5.0.10, the latest stable build of allegro. The al_pause_event_queue is included in allegro 5.1.

Doh. Sorry about that.

Quote:

So you're saying that i don't need to stop and start the timer again, if i just flush the event_queue after my pause menu?

I think its better to stop the timer and flush the queue if you can't pause the queue. Could cause some excess memory use when paused if it is paused for a long time.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: