• Advertisement


This topic is now archived and is closed to further replies.

need help on Zong! from the Zen book...

This topic is 5687 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

// Zong!

// Author: Adrian Walker

// Date: June 27, 2002 ... early morning


// the main windows includes

#include <windows.h>
#include <stdlib.h>

void DrawBat();				// draws the bat

void DrawBall( HDC hDC );	// draws the ball

void MoveBall();			// moves the ball

void DrawScore();			// draws the score

void Pause( int MilliSecs ); // pause function

int GameInit();				// initialization

int GameLoop();				// the game loop

int GameShutDown();			// shutdown

// constants for variables in pixels

const int BAT_WIDTH = 10;		
const int BAT_HEIGHT = 50;
const int BAT_INITIAL_X = 15;
const int BAT_SPEED = 6;
const int BALL_SPEED = 3;
const int BALL_RADIUS = 7;

// variable holding the speed of the ball

int g_BallSpeed = BALL_SPEED;

// used for redrawing code

BOOL g_bRedrawBat = TRUE;
BOOL g_bRedrawBall = TRUE;

// the score holder, initialized to three

int g_Score = 3;

// a glocal device context because we're drawing alot

HDC g_hWndMain;

// a structure to hold info about the ball

typedef struct
	int x, y;		// position

	int radius;		// size


// a structure to hold info about the bat

typedef struct
	int x, y;		// position

	int Width, Height; // size


// the global bat structure

// the global ball structure


// the windows procedure to handle events

long CALLBACK WndProc( HWND hWnd, UINT uMessage,
					  WPARAM wParam, LPARAM lParam )
	PAINTSTRUCT PaintStruct;		// struture used during windows painting

	HDC hDC;						// handle to device context for painting

	// handle to the background bitmap

	static HBITMAP hBitmap = 0;

	// switch the windows message to figure out what it is

	switch( uMessage )
	case WM_CREATE:			// the CreateWindows() was just called

			// load the background bitmap

			hBitmap = (HBITMAP)LoadImage( 0, "ZONG.BMP", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
			return 0;
			switch( wParam )
				// exit if the escape key is pressed

			case VK_ESCAPE:
					if( MessageBox( g_hWndMain,
						"Do you really want to quit the game of the great ones?",
						"Bye Bye", MB_YESNO ) == IDYES )
					PostMessage( g_hWndMain, WM_DESTROY, 0, 0 );
			return 0;

	case WM_PAINT:	// redraw the background bitmap

			// tell windows we want to start updating the window

			hDC = BeginPaint( hWnd, &PaintStruct );

			// structure to hold info about the bitmap

			BITMAP Bitmap;

			// get information about the bitmap

			GetObject( hBitmap, sizeof( BITMAP ), &Bitmap );

			// create a compatible DC for the bitmap

			HDC hBitmapDC = CreateCompatibleDC( hDC );

			// select the bitmap into the new DC

			SelectObject( hBitmapDC, hBitmap );

			// copy the bitmap to the main DC

			BitBlt( hDC, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, hBitmapDC, 0, 0, SRCCOPY );

			// get rid of the new DC

			DeleteDC( hBitmapDC );

			// tell windows we have finished updating the window

			EndPaint( hWnd, &PaintStruct );

			// the window has just been redrawn so we should not redraw the bat and ball

			// to erase the previous ones. They are already erased! Otherwise you get artifacts

			g_bRedrawBat = FALSE;
			g_bRedrawBall = FALSE;

			return 0;

	case WM_DESTROY:	// the window is about to be closed

			// delete the background bitmap

			DeleteObject( hBitmap );

			// our main window is closing, which means we want out app to exit

			// tell windows to put a WM_QUIT message in our message queue

			PostQuitMessage( 0 );
			return 0;

			// let windows handle this message

			return DefWindowProc( hWnd, uMessage, wParam, lParam );
lines 92 and 93 give me errors...
C:\Program Files\Microsoft Visual Studio\MyProjects\Zong\main.cpp(92) : error C2664: 'MessageBoxA' : cannot convert parameter 1 from 'struct HDC__ *' to 'struct HWND__ *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\Zong\main.cpp(93) : error C2664: 'PostMessageA' : cannot convert parameter 1 from 'struct HDC__ *' to 'struct HWND__ *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
lines 92 and 93 are in the VK_ESCPAPE switch under WM_KEYDOWN I'm not super experienced with Windows programming, but I don't get why it doesn't work, 'cause I copied it right out of the book! Did I miscopy something? The program isn't finished, but I included enough code that I think that it shouldn't get an error from that... but I could be wrong... Your help would be greatly appreciated! The multiverse is sloppy... I just make it neat [edited by - zer0wolf on June 28, 2002 12:56:02 PM]

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
You global window handle was declared as an HDC not an HWND.
MessageBox and PostMessage functions take an HWND as their first parameter. If you meant for it to be a global device context, that is fine, but the MessageBox and PostMessage functions don''t take an HDC as their first parameter.

Share this post

Link to post
Share on other sites
Change this line:

HDC g_hWndMain;


HWND g_hWndMain;

BTW.. Using the MessageBox function:

The first paramter is the handle to the wondow in which the Message box belongs to. If you have only one window, you may set this parameter to NULL. If you have mulitiple Windows, you will need to specify which window the MessageBox Belongs to. However, if oyou change the type from HDC to HWND, it will still work.

Your comments indicate that you want a global Device Context. If this is the case, then you will need to declare:

HDC g_hDC;

as well as the HWND...

Share this post

Link to post
Share on other sites

  • Advertisement