1

I'm curious about the use of the CreateEvent() function in Quake.

We have the following global in the c file that WinMain() is defined in:

static HANDLE tevent;

In the WinMain() function itself, we see this:

tevent = CreateEvent(NULL, FALSE, FALSE, NULL);

The game exits if this creation fails.

It then appears in a following function:

void SleepUntilInput (int time)
{
    MsgWaitForMultipleObjects (1, &tevent, FALSE, time, QS_ALLINPUT);
}

And finally CloseHandle(tevent) is called in the Quit function.

I'm unfamiliar with WinAPI, so this leaves me with two questions.

-What does this use of CreateEvent() return?

-How does this make the SleepUntilInput() function work properly?

Philip
  • 373
  • 2
  • 11

1 Answers1

2

CreateEvent returns the handle to a newly-created event.

SleepUntilInput uses the `MsgWaitForMultipleObjcts function to wait until one of three things happens:

  1. The event is signaled, or
  2. Input is available, or
  3. time milliseconds have elapsed.

Since the code never signals the event, condition (1) never occurs, so the only things that will wake it up are (2) or (3).

Note that the event is not actually necessary. The code could have done

void SleepUntilInput (int time) 
{ 
    MsgWaitForMultipleObjects (0, NULL, FALSE, time, QS_ALLINPUT); 
} 

But whoever wrote the code didn't think of that, so they instead passed a dummy event.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • Surely the event is signalled in the real code? The intention must have been that: if the user wants to quit, then stop waiting for stuff and quit. 3rd parameter of WaitForMultipleObjects() is WaitAll = FALSE, meaning that if any things it waits for occurs, it will stop waiting (as opposed to all the things it waits for). – Lundin Aug 16 '12 at 15:08
  • there are no other references to tevent anywhere in the code, so unless there's some implicit use I'm unaware of, then no, it's not referenced. Maybe Carmack stripped something out before shipping - I've found a number of examples of that. – Philip Aug 16 '12 at 15:17
  • The MSDN page for [MsgWaitForMultipleObjects](http://msdn.microsoft.com/en-us/library/windows/desktop/ms684242(v=vs.85).aspx) doesn't state what the expected behavior is if no objects are passed in - and also specifies that pHandles should be "An array of object handles", and NULL isn't an array; perhaps he was being conservative rather then relying on undocumented behavior? – BrendanMcK Aug 17 '12 at 05:57