Creating a Windows Game Loop With C and the Win32 API (part 1)
25 Jul 2021
Hello my friends, I’m so excited for this post!
In this post we’re going to put together a game loop using the Win32 API and C. If you’ve always been curious about
digging into the basics of a Windows game loop, then hopefully this satisfies your cravings.
The best thing about this is that once we go through and create it, we can use it for just about any project we want
to for Windows games; more specifically, I’m going to be referencing this same tutorial in some future content
I have planned.
Any version of Visual Studio or CLion
A cup of coffee
Introducing the Windows Event Model
While you don’t actually need to dig too deeply into the nitty gritty depths of the Win32 API (or Windows itself),
the important thing to learn and keep in mind, is that the softw…
The WinMain entry point
Without further ado, let’s get into the entry point of every Windows program; WinMain
Define a WindowProc
Back in the setup of our winclass, you might notice that we needed to define a function pointer for winclass.lpfnWndProc.
But what is that?
Remember that Windows applications function as programs that respond to Windows event messages. The WindowProc is this
main message event receiver for our application.
For the purposes of game development, we really don’t need to respond to anything other then an actual WM_QUIT event
message, which is sent when the user closes your application via the “X” button. (How dare they!?)
Otherwise, just funnel every event message we didn’t need to the default event message handler with DefWindowProc.
Here’s the full function for you.
Define our (empty) stub functions
Now that we have the bare bones Win32 specific code defined, you might notice that we left a few “stub” game
functions that we are responsible for defining in any game program we create with this code.
For now, we can define the 3 functions with empty bodies.
They can either go in the same file, or in a new one, such as game.cpp for instance:
I feel that for these lower-level Win32 functions, it’s a lot easier to work with straight “C”.
The main benefit of this, is that we leave the option available of dropping in any C++ classes we might want to use,
or just keep using C for everything.