I am writing a video game in C#, and I would like to handle certain events (e.g. keyboard/mouse events) only at a specific point in my game loop. For example, is there a way to write something like the following:
void gameLoop()
{
// do updates
handleAllEvents();
render();
}
If an event occurs at some other point in the loop, I would like to wait until handleAllEvents()
to handle them. By "events" I mean the standard C# event system, for example:
public event Action<object, KeyboardEventArgs> KeyPressed;
Please let me know if the question is not phrased clearly. Thanks!
Remove the call to Application.Run()
, and build up your own message loop. For example like this:
void GameLoop()
{
while (!exitSignal)
{
Application.DoEvents();
render();
}
}
You must then ensure, that render will not stay there for so long.
For more info I suggest you to study the Application
class, especially methods Run()
and DoEvents()
.