Use of CreateEvent() in Quake

462 views Asked by At

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?

1

There are 1 answers

3
Raymond Chen On BEST ANSWER

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.