Archived

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

compfanatic

Starting my frog game...

Recommended Posts

Ok, I have started my frog game. The following code tries to load a background, load a frog animation if the left arrow is pressed, and have the frog jump. This is just the beginning of the game. If you have read "Windows Game Programming For Dummies" or "Tricks of the Windows Game Programming Gurus" you will be able to understand the functions in the following code. I have included all of the libraries needed for the game the source code looks correct, but only a black screen pops up for a split second and disappears. I am extremely frustrated! Could you please tell me what is wrong with the following code? Thanks!
            
#define WIN32_LEAN_AND_MEAN

#include <windows.h>

#include <windowsx.h>

#include <mmsystem.h>

#include <objbase.h>


#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

#include <malloc.h>

#include <memory.h>

#include <string.h>

#include <stdarg.h>

#include <stdio.h>

#include <math.h>

#include <io.h>

#include <fcntl.h>


#include <ddraw.h>


#include "gpdumb1.h"


// DEFINES ////////////////////////////////////////////////////////


#define WINDOW_CLASS_NAME "WINXCLASS"


#define WINDOW_WIDTH	64

#define WINDOW_HEIGHT	48


// defines for player states


#define PLAYER_STATE_DEAD	0

#define PLAYER_STATE_DYING	1

#define PLAYER_STATE_ALIVE	2


#define FROG_MOVE_RIGHT		0

#define FROG_JUMP_RIGHT		1

#define FROG_MOVE_LEFT		2

#define FROG_JUMP_LEFT		3


// frog jump defines

#define INIT_VELOCITY       20

#define GRAVITY_FACTOR      1 

#define MAX_VELOCITY_DOWN   -90 

#define RUN_RATE            8 

#define FLOOR_HEIGHT        480       // if bottom of screen 480 



// PROTOTYPES /////////////////////////////////////////////////////


// game console

int Game_Init(void *parms=NULL);
int Game_Shutdown(void *parms=NULL);
int Game_Main(void *parms=NULL);

// GLOBALS ////////////////////////////////////////////////////////


HWND main_window_handle = NULL;
HINSTANCE main_instance = NULL;
char buffer[80];

BOB frog;

int frog_anims[2][2] = {	{ 0,1 },
							{ 2,3 }	};

BITMAP_IMAGE lake;

// FUNCTIONS //////////////////////////////////////////////////////


LRESULT CALLBACK WindowProc(HWND hwnd, 
						    UINT msg, 
                            WPARAM wparam, 
                            LPARAM lparam)
{
// this is the main message handler of the system

PAINTSTRUCT	ps;		   // used in WM_PAINT

HDC			hdc;	   // handle to a device context


// what is the message 

switch(msg)
	{	
	case WM_CREATE: 
        {
		// do initialization stuff here

		return(0);
		} break;

    case WM_PAINT:
         {
         // start painting

         hdc = BeginPaint(hwnd,&ps);

         // end painting

         EndPaint(hwnd,&ps);
         return(0);
        } break;

	case WM_DESTROY: 
		{
		// kill the application			

		PostQuitMessage(0);
		return(0);
		} break;

	default:break;

    } // end switch


// process any messages that we didn't take care of 

return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc


// WINMAIN ////////////////////////////////////////////////


int WINAPI WinMain(	HINSTANCE hinstance,
					HINSTANCE hprevinstance,
					LPSTR lpcmdline,
					int ncmdshow)
{
// this is the winmain function


WNDCLASS winclass;	// this will hold the class we create

HWND	 hwnd;		// generic window handle

MSG		 msg;		// generic message


// first fill in the window class stucture

winclass.style			= CS_DBLCLKS / CS_OWNDC / 
                          CS_HREDRAW / CS_VREDRAW;
winclass.lpfnWndProc	= WindowProc;
winclass.cbClsExtra		= 0;
winclass.cbWndExtra		= 0;
winclass.hInstance		= hinstance;
winclass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor		= LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground	= GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName	= NULL; 
winclass.lpszClassName	= WINDOW_CLASS_NAME;

// register the window class

if (!RegisterClass(&winclass))
	return(0);

// create the window, note the use of WS_POPUP
if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class

						  "WinX Game Console",	 // title

						  WS_POPUP / WS_VISIBLE,
					 	  0,0,	   // x,y
						  WINDOW_WIDTH,  // width

                          WINDOW_HEIGHT, // height

						  NULL,	   // handle to parent 

						  NULL,	   // handle to menu

						  hinstance,// instance

						  NULL)))
return(0);

// save the window handle and instance in a global

main_window_handle = hwnd;
main_instance      = hinstance;

// perform all game console specific initialization

Game_Init();

// enter main event loop

while(1)
	{
	if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{ 
		// test if this is a quit

        if (msg.message == WM_QUIT)
           break;
	
		// translate any accelerator keys

		TranslateMessage(&msg);

		// send the message to the window proc

		DispatchMessage(&msg);
		} // end if

    
    // main game processing goes here

    Game_Main();

	} // end while


// shutdown game and release all resources

Game_Shutdown();

// return to Windows like this

return(msg.wParam);

} // end WinMain


///////////////////////////////////////////////////////////////////


// WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////////////


int Game_Init(void *parms)
{
	char filename[80];

	DD_Init(SCREEN_WIDTH, SCREEN_WIDTH, SCREEN_BPP);

	// LAKE //

	Load_Bitmap_File(&bitmap8bit, "lake.BMP");
	
	Set_Palette(bitmap8bit.palette);

	Create_Bitmap(&lake, 0,0,640,480);
	Load_Image_Bitmap(&lake,&bitmap8bit,0,0,BITMAP_EXTRACT_MODE_ABS);

	Unload_Bitmap_File(&bitmap8bit);

	// FROG //

	Create_BOB(&frog, 0,0,153,145, 4,
			   BOB_ATTR_VISIBLE / BOB_ATTR_MULTI_FRAME,
			   DDSCAPS_SYSTEMMEMORY);

	for (int direction=0; direction < 2; direction++)
	{
		sprintf(filename, "frog%d.BMP",direction);

		Load_Bitmap_File(&bitmap8bit, filename);

		Set_Palette(bitmap8bit.palette);

		Load_Frame_BOB(&frog,&bitmap8bit,0+direction*4,0,0,BITMAP_EXTRACT_MODE_CELL);  
		Load_Frame_BOB(&frog,&bitmap8bit,0+direction*4,1,0,BITMAP_EXTRACT_MODE_CELL);  

		Unload_Bitmap_File(&bitmap8bit);

		Load_Animation_BOB(&frog,direction,2,frog_anims[direction]);

	} // end for direction


	Set_Animation_BOB(&frog,0);
	Set_Pos_BOB(&frog,320,350);
	Set_Vel_BOB(&frog,0,0);
	Set_Anim_Speed_BOB(&frog,7);
	
	Unload_Bitmap_File(&bitmap8bit);

	RECT screen_rect = {0,0,SCREEN_WIDTH,SCREEN_HEIGHT};
	lpddclipper = DD_Attach_Clipper(lpddsback,1,&screen_rect);

	ShowCursor(FALSE);

	return 1;

} // end Game_Init


///////////////////////////////////////////////////////////////////


int Game_Shutdown(void *parms)
{
	Destroy_Bitmap(&lake);

	Destroy_BOB(&frog);

	DD_Shutdown();

	return 1;

} // end Game_Shutdown


///////////////////////////////////////////////////////////////////


int Game_Main(void *parms)
{
	static int player_moving;

	if (KEY_DOWN(VK_ESCAPE))
		PostMessage(main_window_handle,WM_DESTROY,0,0);

	DD_Fill_Surface(lpddsback,0);

	if (KEY_DOWN(VK_LEFT))
	{
		player_moving = 1;

		frog.yv = INIT_VELOCITY;   
	
		do {       
			       
			frog.x -= RUN_RATE;       
	
			frog.y -= frog.yv;       
			frog.yv -= GRAVITY_FACTOR;    
			
		} while(frog.height >= FLOOR_HEIGHT);

		if (frog.curr_animation != FROG_MOVE_LEFT)
			Set_Animation_BOB(&frog,FROG_MOVE_LEFT);
	
	} // end move left       



	if (frog.x < 0)
		frog.x = 0;
	else
	if (frog.x > (SCREEN_WIDTH - frog.width))
		frog.x = (SCREEN_WIDTH - frog.width);

	if (frog.y < 0)
		frog.y = 0;
	else
	if (frog.y > (SCREEN_HEIGHT - 32))
		frog.y = (SCREEN_HEIGHT - 32);

	if (player_moving)
		Animate_BOB(&frog);

	DD_Lock_Back_Surface();
	
	Draw_Bitmap(&lake, back_buffer, back_lpitch, 0);

	DD_Unlock_Back_Surface();

	Draw_BOB(&frog, lpddsback);

	DD_Flip();

	return 1;

} // end Game_Main



            
Thanks again!!! -Alex (Just put the code inside source tags to make it more readable - WitchLord) Edited by - WitchLord on 6/19/00 12:45:39 PM

Share this post


Link to post
Share on other sites
Hmmm.... I don''t know how many people will actually read that. Anyhow it seems to be a couple of things. First off you NEED to have some error handling especially now, as the DirectDraw portion of a game seems to be the buggiest, hardest, part of a game to program. So you need to be checking after all your calls and displaying message boxes as to what the problem is. You can always take out some message boxes once it''s completely working too.

The first thing I noticed is that you init DX w/ SCREEN_WIDTH, SCREEN_HEIGHT, and SCREEN_BPP which don''t seems to be defined. Also many of your includes did not show up in the post.
But try defining those constants.
See ya,
Ben

Share this post


Link to post
Share on other sites
Groovy! Someone else who''s read that book....

I''d agree with cyberben, you''ve get your window width and height, but no screen vars defined. Also, I suspect that 64, 48 thing is a misprint.. it should be 640 and 480.

Are you getting those warnings with the GetStockObj() lines? I hate those warnings! You''d think a stock object would be in the form its used in. All you need to do to stop them is recast them as HBRUSH. eg:

winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

that should clear up them damned stoopid errors, and it works for HPEN s aswell. (course, they could just be MSVC++ 6.0 compile oddities)

I only mention it as it annoyed me while I was working through the book.

stay frosty
Keez

Share this post


Link to post
Share on other sites
I don''t get any errors, it''s just the program won''t execute correctly. What happens is a black window is launched to full screen and then disappears in a split second. I''ll try your suggestions about defining the window height and window width, and also making the message boxes.

Thanks!!!
-Alex

Share this post


Link to post
Share on other sites
compfanatic.........have I got the solution for you
I have just finished writing a super simplified wrapper that would allow a 7 year old to use direct Draw and direct Input.....
you can read more about it in the thread called
simpleDirectInput and simpleDirectDraw in this same section......
if you would like a copy of my library then give me an email
I would like for someone to try it out and see what they think
djsteffe@unity.ncsu.edu


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Share this post


Link to post
Share on other sites