Click here to Skip to main content
6,595,854 members and growing! (18,759 online)
Email Password   helpLost your password?
Multimedia » GDI » Beginners     Beginner License: The Code Project Open License (CPOL)

A Simple Win32 Game: Bicho Hunting

By Dr. Emmett Brown

This 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
Version:2 (See All)
Posted:8 Aug 2008
Views:15,167
Bookmarked:39 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 5.84 Rating: 4.49 out of 5

1
1 vote, 5.0%
2
1 vote, 5.0%
3
4 votes, 20.0%
4
14 votes, 70.0%
5
Sample Image

Introduction

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.

Programming Style

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.

Improving Animation Performance

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.

Adding Sound

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);
}

HighScores

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

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!

History

  • 8th August, 2008: Initial post 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dr. Emmett Brown


Member

Occupation: Software Developer
Location: Uruguay Uruguay

Other popular GDI articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
QuestionIssue with CodeBlocks PinmemberSergio Fonseca9:56 28 Sep '09  
AnswerRe: Issue with CodeBlocks PinmemberDr. Emmett Brown17:06 28 Sep '09  
Generalgood Pinmemberhusoso5:53 3 Sep '08  
GeneralBinary works also in Vista SP1 PinmemberT800G3:01 17 Aug '08  
GeneralGreat Article PinmemberGigy5:53 11 Aug '08  
GeneralPerfect Pinmemberfetag3:41 11 Aug '08  
GeneralRules? Pinmembersupercat919:22 9 Aug '08  
GeneralRe: Rules? Pinmemberrotter5125:38 10 Aug '08  
GeneralGood Job Pinmemberqiutao from wuhan3:38 9 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin 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