Here’s another fairly trivial code snippet. I’ve stumbled across some
borked attempts at initializing and maintaining rendering windows for
games lately. Most failed to properly respond to window messages, either
ignoring WM_CLOSE
outright or letting DefWindowProc()
call DestroyWindow()
when WM_CLOSE
was received, thereby not giving the rest of
the game’s code any time to cleanly shut down before the window handle
becomes invalid.
So I’ll provide a clean and well-behaved window class here. It doesn’t
use any global variables – in fact, you could create any number of windows
from any number of threads. WM_CLOSE
simply causes the class’
WasCloseRequested()
method to return true, so by polling this
method you can first shut down graphics and input devices and then destroy
the window in an orderly fashion.
For your convenience I also added some helper methods: one resizes the window in a way that ensures the client area will actually end up with the exact pixel size requested. Another will center the window on the screen without messing up if the user has extended his desktop over multiple monitors.
Read More