0
\$\begingroup\$

I'm new to game development, and I was wondering about a architecture question.

The window creation code in C++ is given by doing the following:

  1. Fill out a WNDCLASS struct

  2. Register the window class using the struct by calling RegisterWindow(WNDCLASS)

  3. Get a handle to the window from calling CreateWindow()

  4. Handle Windows events in your WndProc function, and call TranslateMessage, and DispatchMessage to forward messages in the message queue when retrieved from PeekMessage().

Then, in a game you might have a Game engine which contains a simple game loop like so:

while (running)
{
    ProcessInput();
    UpdateState();
    RenderStates();
}

My question is how do you include the window creation with the game engine. Do you decouple the two? If you decouple the two, how do you handle input to the WndProc function if it has no idea about the GameEngine class to set particular flags about keys that are pressed?

\$\endgroup\$
  • \$\begingroup\$ If you go ahead with the creation of the window for the user, make sure you also allow the users to create the window themselves and pass the window handle to you; in some context they'll want to embed engine in some other infrastructure and such. \$\endgroup\$ – Vaillancourt Apr 20 '16 at 19:46
  • \$\begingroup\$ Take a look at the directx-vs-templates for an example of decoupling the 'game' from the 'main window' at least for some Microsoft platforms. \$\endgroup\$ – Chuck Walbourn Apr 21 '16 at 0:11
2
\$\begingroup\$

Whether or not you decouple your window creation from your engine code is up to you. You can do it both ways. If you don't handle window creation in your code, you'll expect the user of your code to handle it. That will include forwarding window events to you somehow -- probably by calling interface methods on your game engine object you define for them.

But if you're going to handle it yourself, you can associate an instance of your game engine with a HWND using SetWindowLongPtr and GetWindowLongPtr with the GWLP_USERDATA flag. These functions let you store and recover a pointer-sized blob of data with a specific HWND, so once you create your window, use them to shove the game engine pointer in there, and then you can recover the game engine pointer from that window in the WNDPROC when handling appropriate messages and call the appropriate game engine functions.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.