One Last ThingYou may not realize it, but you now have enough knowledge of Windows programming to create a working game based on Windows GDI! You can create the window, and you can show the graphics. The game logic is just the same old C you've come to know and love. You can even use the mouse by processing the messages it generates. There's just one thing you're missing, and even though it's off-topic for this article, I can't leave without mentioning it, and that's keyboard support. Windows has a great function for determining the state of the keys on the keyboard: GetAsyncKeyState(). It returns a 16-bit value, the high bit of which indicates whether the key is currently depressed or not. Here's the prototype:
The parameter is an identifier for a key on the keyboard, and takes constants beginning with VK_ for "virtual key." Some of the most common ones are VK_RETURN, VK_ESCAPE, VK_UP, VK_LEFT, VK_RIGHT, and VK_DOWN. You can even use VK_LBUTTON and VK_RBUTTON for the mouse buttons! How convenient. Just mask out the high bit, and if it's 1, the key is being pressed. A useful macro for doing this that I always use is:
If you're scratching your head over that, you probably haven't seen the conditional operator (?) before. This operator -- the only ternary operator in the C language -- evaluates the expression on its left. If the expression is true, the expression evaluates to the value on the left side of the colon. If the expression is false, the expression evaluates to the value on the right side of the colon. Got it? Cool, hey? A few notes: first, the method I have just shown you only tells whether the key is up or down. It doesn't tell you when the key was pressed. So if you want to test for discrete keypresses rather than continuous ones, you'll have to make up some logic to do this. GetAsyncKeyState() also uses the low bit of its return value to tell whether the key has been pressed since the last call to the function, but that may not be good enough. One solution is to create a table of the 256 possible values the function can take, then call each one every frame, and compare the new values to the old ones. ClosingNow you've got all it takes for a GDI-based game, and I've taken this article a lot longer than I wanted it to go. Congratulations to both of us. :) Since I covered a lot today, I have developed a sample program for you to look at. It recreates the starfield screensaver that comes with Windows, only in an .EXE format and in a window instead of fullscreen. The program illustrates the creation of a window, the processing of several key messages, and using a device context for pixel-plotting. The download includes the source and the compiled program, and is available here. Any questions? I'm always happy to help out. You can reach me by E-mail at ironblayde@aeon-software.com, or on ICQ at UIN #53210499. Now that we've covered all this Windows stuff, next time I'll be taking you on a magical journey into the wonderful world of DirectX. See you then! Copyright © 2000 by Joseph D. Farrell. All rights reserved. |