Original Post
can anyone tell me if what problems they see in my code when i run it, it is pretty slow.
Edited by - omegasyphon on 8/31/00 4:36:38 PM
// 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;
}
|