![]() |
Multimedia »
GDI »
Beginners
Beginner
License: The Code Project Open License (CPOL)
A Simple Win32 Game: Bicho HuntingBy Dr. Emmett BrownThis article describes how to easily transform an idea into a game, and some of the possibilities that the GDI offers to do this. |
C++, Windows (WinXP), GDI
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
After Googling something related to C++, I found a very nice IDE called CodeBlocks. So I decided to refresh my Win32 knowledge, but this time I started playing with WM_PAINT. This is the result of a funny GDI weekend.
I'm used to program OO, but I just wanted to change, so mixed programming techniques. I only used a few classes to make it clear.
The main technique used to improve the animation quality is double buffering. We create the memory dc as soon as we have the screen dc, and we use it all through the drawing process. After we have the new image in memory, we copy it into the screen dc. This way, the screen dc is only modified once.
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT rect;
GetClientRect(hwnd, &rect);
// Create a 1x1 dc:
HDC hdcDoubleBuffering = CreateCompatibleDC(hdc);
// Create a bitmap with the windows size:
HBITMAP bmDoubleBuffering =
CreateCompatibleBitmap(hdc, rect.right, rect.bottom);
// Select the bitmap in the new dc, so its size adapts to the screen:
SelectObject(hdcDoubleBuffering, bmDoubleBuffering);
// Draw in hdcDoubleBuffering
...
// Copy the memory dc to the original dc:
BitBlt(hdc, 0, 0, rect.right, rect.bottom, hdcDoubleBuffering, 0, 0, SRCCOPY);
// Free resources:
DeleteDC(hdcDoubleBuffering);
DeleteObject(bmDoubleBuffering);
}
Also, all bitmaps are loaded when the application starts. This way, we improve drawing speed.
This is a very easy thing to do, and makes the game a lot more funny. There are a lot of CodeProject articles that show how to use the winmm.lib library. I just needed to play a sound. To do this, I linked winmm.lib and called the sndPlaySound function.
In resources:
ID_SOUNDLASER WAVE "laser.wav"
When loading:
HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(ID_SOUNDLASER), "WAVE");
HANDLE sndLaser = LoadResource(hInstance, hResInfo);
In the middle of the game:
const char* sndRes = (const char*)LockResource(sndLaser);
if (sndRes)
{
sndPlaySound(sndRes, SND_MEMORY|SND_ASYNC|SND_NODEFAULT);
UnlockResource(sndRes);
}
I wanted to add a HighScores table, but I didn't want to add configuration files to the application. So I used the Windows registry. It consists of 15 registry keys at HKEY_CURENT_USER\Software\RotterSoftware\BichoHunting\. You can easily hack it now. :)
I did all this with my registry key wrapper class. Maybe it's useful to you.
The rest of the code are counters, states, timer ticks, random numbers, etc. I did my best to make it clear, but to me, performance and a good user experience are always more important than the code.
I hope you enjoy the game and I wait to see your API games soon. Have fun!
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 8 Aug 2008 Editor: Deeksha Shenoy |
Copyright 2008 by Dr. Emmett Brown Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |