does my code have problems?

Started by
5 comments, last by omegasyphon 23 years, 7 months ago
can anyone tell me if what problems they see in my code when i run it, it is pretty slow.
        


// INCLUDES ///////////////////////////////////////////////

#define WIN32_LEAN_AND_MEAN  
#include <windows.h>   // include important windows stuff
#include <windowsx.h> 
#include <mmsystem.h>
#include <fstream.h> // includes file iostream and important C/C++ stuff
#include <conio.h>
#include <malloc.h>
#include <memory.h>
#include <io.h>
#include <ddraw.h>  // directX includes
#include <dinput.h>
#include <string.h>
#include <stdio.h>
#include <objbase.h>
// defines for windows 

#define WINDOW_CLASS_NAME "WINXCLASS"  // class name


#define WWIDTH	640              // size of window
#define WHEIGHT	480
#define BPP	16
#define BITMAP_ID   0x4D42 // universal id for a bitmap




// MACROS /////////////////////////////////////////////////


// these read the keyboard asynchronously

#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define DDRAW_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }
// this builds a 16 bit color value in 5.6.5 format (green dominate mode)
#define RGB16BIT(r,g,b) ((b&31) + ((g&63) << 6) + ((r&31) << 11))

// TYPES //////////////////////////////////////////////////


typedef unsigned short USHORT;
typedef unsigned char  UCHAR;
typedef unsigned short WORD;

typedef struct BITMAP_FILE_TAG
{
	BITMAPFILEHEADER bitmapfileheader;
	BITMAPINFOHEADER bitmapinfoheader;
	UCHAR			 *buffer;
} BITMAPFILE, *BITMAPFILEPTR;

typedef struct BITMAPOBJTYP
{
	int x,y;
	LPDIRECTDRAWSURFACE7 image;
} BITMAPOBJ, *BITMAPOBJPTR;

typedef struct MAPOBJTYP
{
	int maplayer1[30][15];
} MAPOBJ, *MAPOBJPTR;

////////// directdraw objects/////////////

LPDIRECTDRAW		lpdd1			= NULL;
LPDIRECTDRAW7		lpdd7			= NULL; 
LPDIRECTDRAWSURFACE7	lpddsprimary	        = NULL;
LPDIRECTDRAWSURFACE7	lpddsback		= NULL;
LPDIRECTDRAWSURFACE7	lpdds1			= NULL;
LPDIRECTDRAWSURFACE7	lpdds2			= NULL;
LPDIRECTDRAWCLIPPER	lpddclipper		= NULL;
LPDIRECTINPUT		lpdi1			= NULL;
LPDIRECTINPUTDEVICE	lpdkey			= NULL;
DDBLTFX			ddbltfx;
DDSURFACEDESC2		ddsd;
DDSCAPS2		ddscaps;
BITMAPFILE		bitmap;
MAPOBJ			map;
BITMAPOBJ		hero;


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


HWND main_window_handle = NULL; // save the window handle

HINSTANCE main_instance = NULL; // save the instance

char buffer[80];                // used to print text

int window_closed = 0;    // tracks if window is closed

int worldx=0;
UCHAR        keyboard_state[256]; // contains keyboard state table

int screenx =0;
int oldfps=0,
	newfps=0;

HWND hwnd;
HDC hdc;
// PROTOTYPES /////////////////////////////////////////////


int Game_Init(void *parms=NULL, int num_parms = 0);
int Game_Shutdown(void *parms=NULL, int num_parms = 0);
int Game_Main(void *parms=NULL, int num_parms = 0);
int loadbitmap(BITMAPFILEPTR bitmap, char *filename);
int unloadbitmap(BITMAPFILEPTR bitmap);
int flipbitmap(UCHAR *image, int bytes_per_line, int height);
LPDIRECTDRAWSURFACE7 createsurface(int width, int height, int mem_flags);
int bltsprite(LPDIRECTDRAWSURFACE7 lpddstemp,LPDIRECTDRAWSURFACE7 lpddstemp2,int sx,int sy,int dx, int dy);
int surfacecopy(LPDIRECTDRAWSURFACE7 lpddstemp,LPDIRECTDRAWSURFACE7 lpddstemp2,int scx,int sy);
int bitmapcopy(LPDIRECTDRAWSURFACE7 lpddstemp,LPDIRECTDRAWSURFACE7 lpddstemp2,int sx,int sy);
int drawtext(void);
int drawmap(void);
int loadmap(void);
int drawhero(void);
int fpscount(void);

} // end WinMain


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

int Game_Init(void *parms, int num_parms)
{

// first create base IDirectDraw interface

if (FAILED(DirectDrawCreate(NULL, &lpdd1, NULL)))
   return(0);

// now query for IDirectDraw7

if (FAILED(lpdd1->QueryInterface(IID_IDirectDraw7,
                               (LPVOID *)&lpdd7)))
   return(0);

// set cooperation to full screen

if (FAILED(lpdd7->SetCooperativeLevel(main_window_handle, 
                                      DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX | 
                                      DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
   return(0);

// first create the direct input object

if (DirectInputCreate(main_instance,DIRECTINPUT_VERSION,&lpdi1,NULL)!=DI_OK)
   return(0);

// create a keyboard device  //////////////////////////////////

if (lpdi1->CreateDevice(GUID_SysKeyboard, &lpdkey, NULL)!=DI_OK)
   return(0);

// set data format

if (lpdkey->SetDataFormat(&c_dfDIKeyboard)!=DI_OK)
   return(0);

// acquire the keyboard

if (lpdkey->Acquire()!=DI_OK)
   return(0);

// set display mode

if (FAILED(lpdd7->SetDisplayMode(WWIDTH,WHEIGHT,BPP,0,0)))
   return(0);


DDRAW_INIT_STRUCT(ddsd);

ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.dwBackBufferCount =1;

ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;

//create primary surface

if (FAILED(lpdd7->CreateSurface(&ddsd, &lpddsprimary, NULL)))
return 0;
//create offscreen surface

lpdds1 = createsurface(960,480,DDSCAPS_VIDEOMEMORY);
lpdds2 = createsurface(640,480,DDSCAPS_VIDEOMEMORY);
hero.image = createsurface(32,32,DDSCAPS_VIDEOMEMORY);
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
if (FAILED(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsback)))
  return(0);

if (!loadbitmap(&bitmap,"lvl1tiles.bmp"))
  return(0);

lpdds1->Lock(NULL,&ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);
// get video pointer to primary surfce

UCHAR *image_buffer = (UCHAR *)ddsd.lpSurface;
	for (int x=0;x<WHEIGHT;x++)
	{
		memcpy(ℑ_buffer[x*ddsd.lPitch],&bitmap.buffer[x*WWIDTH*2],WWIDTH*2);
	}
if (FAILED(lpdds1->Unlock(NULL)))
return 0;
loadmap();

unloadbitmap(&bitmap);
hero.y=384;
hero.x=0;

return 1;
} // end Game_Init


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


int Game_Main(void *parms, int num_parms)
{

// make sure this isn't executed again

if (window_closed)
   return(0);

// for now test if user is hitting ESC and send WM_CLOSE

if (KEYDOWN(VK_ESCAPE))
   {
   PostMessage(main_window_handle,WM_CLOSE,0,0);
   window_closed = 1;
   } // end if


	drawmap();
	//surfacecopy(lpdds2,lpdds1,0,30);
	drawhero();
	drawtext();
	// get the keyboard data

lpdkey->GetDeviceState(256, (LPVOID)keyboard_state);
if (keyboard_state[DIK_RIGHT]) 
   {
	if (hero.x<960)
		hero.x+=2;



   } // end if

else
if (keyboard_state[DIK_LEFT])  
   {
	if (hero.x>0)
		hero.x-=2;
   } // end if




lpddsprimary->Flip(NULL, DDFLIP_WAIT);

  return 1;
} // end Game_Main


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



int Game_Shutdown(void *parms, int num_parms)
{
	// release keyboard

lpdkey->Unacquire();
lpdkey->Release();
lpdi1->Release();


// now the primary surface

if (lpddsprimary)
   {
   lpddsprimary->Release();
   lpddsprimary = NULL;
   } // end if



// release the directdraw object

if (lpdd7!=NULL)
   lpdd7->Release();
	return 1;

// release the directdraw object

if (lpdds1!=NULL)
   lpdds1->Release();
	return 1;
}
        
Edited by - omegasyphon on 8/31/00 4:36:38 PM
Advertisement
Thats a lotta code! I''m not going to bother reading it all (sorry). Check the functions that are being run in real time. See if they can be improved. What is running slow about it?

Jesus, that''s a lot of code. Maybe you could run a profiler and determine what procedures are taking the most time, then send that code to us to take a look at. No one is going to go through all that code for you.
Lemme guess... You read LaMothe''s book? Just a hunch...

Immediately, I don''t see a problem however I notice you''re not releasing the HDC in DrawText which might be causing a problem. Also, I don''t see after skimming the code why in draw hero you blt twice?
hmm... definetly trim this down. Looking at this much code boggles the mind, pal, and most people (like me) just can''t focus over this much without spending a lot a minutes on it, which no ones gonna do. oooh, the world is spinning....

==============================
"You don''t know the power of the Dark Side..."

- Darth Vader, Episode VI
==============================

Drew Sikora
Executive Producer
GameDev.net

Dude, you need to learn the value of the Enter key.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
ok ill slim it down but also how do i run a profiler on it?

This topic is closed to new replies.

Advertisement