Sunday, January 27, 2013

Dev Zero - Actually getting that window on the screen - [Part 3]

The goal this time around is to determine what we need to do in order to get a proper Window on-screen. What does it take to get a window on-screen in the land of Microsoft? Well it breaks down into a couple of API calls:

  • RegisterClassEx() to register the appropriate systems to Windows and to create a message pump to handle the OS events.
  • CreateWindow(), ShowWindow() and UpdateWindow() to actually create and show the window.
  • The actual message pump that handles all the OS events.

So, where do we put that? Well, a fair bit of it could live in our window.cpp file, as a set of file-global vars and functions. And that would work OK. It's simple and relatively easy to read. Let's try that and see what we end up with.

What's of note here? Well, there's a couple of things. First off, we don't end up passing into the window class any information about the HINSTANCE. Which we need to be able to register the class. But there's a little trick that works well and allows you to get the instance of the current thread:

That, with one call, gives us everything we need to instantiate our application. However, it doesn't allow us to properly update.

If you look at the Window::Update method, you can see that I use PeekMessage(), instead of GetMessage(). This is good, because GetMessage is a blocking call; we don't ever return until we get a message. For a game, that's bad, because we still want our application to process, even if we don't get any OS level events. However, this leaves us with havint to find a way to determine when the application has actually terminated. It's a little harder, because we have to resort to using that global s_ActiveWindow to truly determine if our application is live. But it works. And we end up with our application running and behaving as we would expect!

This, however could be implemented in a cleaner fashion. In my next post, I'll show you how to do that.

Note that this time around, I haven't posted any code. Mostly because this isn't a 'good' implementation. But the goal for tonight was to get a live application running (window up and visible). And we've accomplished that.

No comments: