⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dxlibrary.h

📁 A Star 算法 的C++实现, 有很好的类实现
💻 H
📖 第 1 页 / 共 5 页
字号:
//-----------------------------------------------------------------------------
// DIRECTX LIBRARY
// This include file contains functions that access Direct X.
// By Patrick Lester, pwlester@policyalmanac.org

// You also will need the Direct X 7.0 SDK, and to install the lib
// and include files from the SDK to your C++ lib and include
// folders. You can get the DirectX 7.0 SDK (large!) here:
// http://download.microsoft.com/download/win98SE/SDK/7.0/W9X/EN-US/dx7sdk-700.1.exe
//-----------------------------------------------------------------------------

#include <ddraw.h> //Direct Draw header
#define DIRECTINPUT_VERSION 0x0700
#include <dinput.h>
#define D3D_OVERLOADS
#include <d3d.h>
#include <string> //used by Debuglog


//-----------------------------------------------------------------------------
// Name: Function prototypes (where needed) 
//-----------------------------------------------------------------------------
DWORD ColorMatch(LPDIRECTDRAWSURFACE7 pdds, COLORREF rgb);
HRESULT LoadPalette(LPDIRECTDRAWPALETTE* ppPalette,const TCHAR* strBMP );
int ReleaseObjects(void);
static HRESULT CALLBACK TextureSearchCallback(DDPIXELFORMAT* pddpf, VOID* param);
void UpdateMouse (void);
void Debuglog (char* pDebuglog);
void Debuglog (int number);
int SetBrush (int r, int g, int b, int pattern=0, int style=0);
int SetFontColor(int r,int g,int b);
int SetPen (int r, int g, int b, int thickness=1, int style=0);



//-----------------------------------------------------------------------------
// Name: Class Declarations
//-----------------------------------------------------------------------------
class cImage //class containing info about an image
{
public:
	LPDIRECTDRAWSURFACE7 pSurface; //pointer to the image
	int imageWidth;
	int imageHeight;
	int xHandle;
	int yHandle;
	bool isMasked; //0 = no, 1 = yes
	int convertedMaskColor;//mask color converted to used pixel format
	byte memLocation; //0 = VRAM, 1 = RAM
	bool imageIs3D; //0 = false, 1 = true

	cImage* previousImage;//pointer to previous item in linked list
	cImage* nextImage;//pointer to next item in linked list

	//constructor member function
	cImage()
	{
	previousImage = NULL;
	nextImage = NULL;
	}
};


//Concatening string class, CSTR: This class is used to combine
//strings and numbers into a single, usable string for the
//Debuglog and Text commands.
#define combine g_strString-
class CSTR 
{
	public:
		char actualString[255];
		CSTR& operator-(char* nextString);
		CSTR& operator-(int number);
		CSTR& operator-(double number);
		CSTR& operator+(char* nextString);
		CSTR& operator+(int number);
		CSTR& operator+(double number);
};
CSTR g_strString; //declare global CSTR

CSTR& CSTR::operator-(char* nextString){
	strcpy (this->actualString,nextString); 
	return g_strString;}

CSTR& CSTR::operator-(int number){
	char temp [33];
	itoa (number,temp,10); 
	strcpy (this->actualString,temp);
	return g_strString;}

CSTR& CSTR::operator-(double number){
	char temp [20];
	gcvt (number,10,temp);
	strcpy (this->actualString,temp);
	return g_strString;}

CSTR& CSTR::operator+(char* nextString){
	strcat (this->actualString,nextString); 
	return g_strString;}

CSTR& CSTR::operator+(int number){
	char temp [33];
	itoa (number,temp,10); 
	strcat (this->actualString,temp); 
	return g_strString;}
	
CSTR& CSTR::operator+(double number){
	char temp [20];
	gcvt (number,10,temp);
	strcat (this->actualString,temp);
	return g_strString;}

void Debuglog (CSTR pStringDebuglog);

//-----------------------------------------------------------------------------
// Name: Globals
//-----------------------------------------------------------------------------
HWND                        g_hWndGame=NULL; //game's Window handle
LPDIRECTDRAW7               g_pDirectDraw = NULL;
LPDIRECTDRAWSURFACE7        g_pFrontBuffer= NULL; 
LPDIRECTDRAWSURFACE7        g_pBackBuffer = NULL;
LPDIRECTDRAWPALETTE         g_pPalette = NULL;
PALETTEENTRY                g_PaletteEntry[256];
LPDIRECT3D7                 g_pD3D = NULL;
LPDIRECT3DDEVICE7           g_pD3DDevice = NULL;
D3DTLVERTEX                 g_Vertex[4]; //quad vertices for 3D images
LPDIRECTINPUT7              g_pDirectInput = NULL;
LPDIRECTINPUTDEVICE         g_pMouse=NULL;
DIMOUSESTATE                g_pMouseData;
int                         g_MouseX=0;
int                         g_MouseY=0;
bool                        g_MouseDown1=false;
bool                        g_MouseDown2=false;
int                         g_MouseHit1=0;
int                         g_MouseHit2=0;
int                         g_MouseSpeed=2;
DWORD                       g_ClsColor=0;
COLORREF                    g_FontColor=RGB(255,255,255);
HBRUSH                      g_hBrush=NULL;
HFONT                       g_hFont=NULL;
HPEN                        g_hPen=NULL;
int                         g_screenWidth = 0;
int                         g_screenHeight = 0;
int                         g_screenDepth = 0;
short                       g_screenFormat16Bit = 0;
bool                        g_AutoMidHandle=false;
int                         g_Pitch=0;
void*                       g_pSurface = NULL;

//Globals used to keep track of current drawing buffer
char                        g_pCurrentBuffer = 2;//1=front, 2 = back, 3 = image
cImage*                     g_pCurrentImage = NULL;//pointer to the cImage in buffer
LPDIRECTDRAWSURFACE7        g_pCurrentSurface = NULL;//pointer to the surface in buffer

//Globals used by cImage class	
cImage*                     g_pFirstImage = NULL;//pointer to first item in linked list
cImage*                     g_pLastImage = NULL;//pointer to last item in linked list


//-----------------------------------------------------------------------------
// Name: AutoMidHandle
// Desc: Automatically sets the image handle of loaded images to the 
//	middle of the image. Only affects images loaded or created 
//	after this command is called.
//-----------------------------------------------------------------------------
void AutoMidHandle (bool inValue)
{ 	
	g_AutoMidHandle = inValue;
}


//-----------------------------------------------------------------------------
// Name: AvailSysMem()
// Desc: Returns the available system memory (RAM) in bytes.
//  - Example = Draw3DImage demo
//-----------------------------------------------------------------------------
int AvailSysMem(void) 
{ 	
	MEMORYSTATUS stat;
	GlobalMemoryStatus (&stat);
	return(stat.dwAvailPhys);
}


//-----------------------------------------------------------------------------
// Name: AvailVidMem()
// Desc: Returns the available video memory in bytes
//  - Example = FreeImage demo
//-----------------------------------------------------------------------------
int AvailVidMem(void) 
{
	DWORD totalVRAM=0;
	DWORD freeVRAM=0;
	DDSCAPS2 ddscaps;
	ZeroMemory(&ddscaps, sizeof(ddscaps));//clear memory block for structure
	ddscaps.dwCaps = DDSCAPS_VIDEOMEMORY;  
	g_pDirectDraw->GetAvailableVidMem(&ddscaps,&totalVRAM,&freeVRAM);
	return freeVRAM;
} 


//-----------------------------------------------------------------------------
// Name: BackBuffer
// Desc: Returns a "pointer" to the back buffer
//-----------------------------------------------------------------------------
int BackBuffer (void)
{
	return 2;
}


//-----------------------------------------------------------------------------
// Name: Bin()
// Desc: Converts an unsigned integer to its binary equivalent in the 
//	form of a string. It is stored in a global string.
//	Example = ReadPixel
//-----------------------------------------------------------------------------
char g_BinaryString[33];//global string
void Bin(unsigned int number) 
{
	strcpy (g_BinaryString,"00000000000000000000000000000000");
	for (int x=1;x<32;x++)
	{
		unsigned int a = (unsigned int) pow(2,x);
		unsigned int b = (unsigned int) pow(2,x-1);
		if ((number % a)/b == 1) g_BinaryString[32-x] = 49;
	}
}


//-----------------------------------------------------------------------------
// Name: Cls
// Desc: Clears the screen with the designated CLS color, which is black
// unless changed by the ClsColor() command.
//-----------------------------------------------------------------------------
int Cls (void)
{
	DDBLTFX ddbltfx;
	ZeroMemory( &ddbltfx, sizeof(ddbltfx));
	ddbltfx.dwSize      = sizeof(ddbltfx);
	ddbltfx.dwFillColor = g_ClsColor;

	// Blit the entire current buffer with the specified fill color
	HRESULT hr = g_pCurrentSurface->Blt(
	NULL, //destination rectangle of blit (0 = blit entire surface)
	NULL, //address of source surface blitted from (0 used here)
	NULL, //source rectangle of blit (0 = blit from entire surface)
	DDBLT_COLORFILL | DDBLT_WAIT, //blit flags; DDBLT_COLORFILL = use dwFillColor member
	// of the DDBLTFX structure as the RGB color that fills the destination
	&ddbltfx );//address of special effects ddbltfx structure created above

	if (hr == DD_OK)
		return 1;
	else
		return hr;
}



//-----------------------------------------------------------------------------
// Name: ClsColor
// Desc: Changes the color for the Cls command. Default is black.
//-----------------------------------------------------------------------------
void ClsColor (int r, int g, int b)
{
	COLORREF rgb = RGB(r,g,b);
	if (rgb == 0) g_ClsColor=0;
	if (rgb != 0) g_ClsColor = ColorMatch(g_pFrontBuffer, rgb);
}


//-----------------------------------------------------------------------------
// Name: Color()
// Desc: Changes the current drawing color for text writing and GUI draw
//	commands like Line, Oval, and Rect. You can set each of these
//	separately using SetFontColor, SetBrush and SetPen. 
//-----------------------------------------------------------------------------
void Color(int r, int g, int b)
{
	SetFontColor(r,g,b);
	SetBrush(r,g,b);
	SetPen(r,g,b);
}


//-----------------------------------------------------------------------------
// Name: ColorMatch()
// Desc: This function translates a specified rgb color value into a format
//	appropriate to the color depth being used. For example, if 8 bit color
//	depth is used, it translates it into the appropriate palette entry. 
//	If 16 bit color depth is used, it translates the rgb into a 16 bit 
//	color, etc.
//	
//	Used by MaskImage and Cls
//-----------------------------------------------------------------------------
DWORD ColorMatch(LPDIRECTDRAWSURFACE7 pdds, COLORREF rgb)
{
    COLORREF                rgbT;
    HDC                     hdc;
    DWORD                   dw = CLR_INVALID;
    DDSURFACEDESC2          ddsd;
    HRESULT                 hres;

    // Use GDI SetPixel to color match for us
    if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
    {
        rgbT = GetPixel(hdc, 0, 0);     // Save current pixel value
        SetPixel(hdc, 0, 0, rgb);       // Set our value
        pdds->ReleaseDC(hdc);
    }

    //Now lock the surface so we can read back the converted color
    ddsd.dwSize = sizeof(ddsd);
    while ((hres = pdds->Lock(NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING);
    if (hres == DD_OK)
    {
        dw = *(DWORD *) ddsd.lpSurface;// Get DWORD
        if (ddsd.ddpfPixelFormat.dwRGBBitCount < 32)
            dw &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount) - 1;// Mask it to bpp
        pdds->Unlock(NULL);
    }

    //Now put the color that was there back.
    if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
    {
        SetPixel(hdc, 0, 0, rgbT);
        pdds->ReleaseDC(hdc);
    }
    return dw;
}



//-----------------------------------------------------------------------------
// Name: CopyImage
// Desc: Creates a new image that is a copy of another, pre-existing
//	image.
//	Example = CopyRect demo
//-----------------------------------------------------------------------------
cImage* CopyImage (cImage* pImageSource, int loadTo=0)
{
	HRESULT                     hr;

	//Error check
	if(pImageSource == NULL) return 0;
	if(pImageSource->pSurface == NULL) return 0;

	//Set up the Direct Draw surface description
	DDSURFACEDESC2 ddsd; //surface description data structure
	ZeroMemory(&ddsd, sizeof(ddsd));// zero-out the memory area
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags = DDSD_CAPS | //ddsCaps structure is valid (see below
                 DDSD_HEIGHT | //dwHeight member is valid
                 DDSD_WIDTH;   //dwWidth member is valid
	if (loadTo == 0) ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; 
	if (loadTo == 1) ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN 
		| DDSCAPS_SYSTEMMEMORY; //load to RAM if loadTo == 1
	ddsd.dwWidth = pImageSource->imageWidth;
	ddsd.dwHeight = pImageSource->imageHeight;

	//Create the Direct Draw surface in specified memory area
	LPDIRECTDRAWSURFACE7 pSurfaceNew; //pointer to Direct Draw surface
	if (g_pDirectDraw->CreateSurface(
	&ddsd, //address of above DDSURFACEDESC2 structure that describes surface
	&pSurfaceNew, //address of the variable pointing to the surface
	NULL)  //advanced COM stuff
	!= DD_OK) return 0;

	//Compute destination rectangle
	RECT rectDest;
	rectDest.left=0;
	rectDest.right=pImageSource->imageWidth;
	rectDest.top=0;
	rectDest.bottom=pImageSource->imageHeight;

	//Blit the existing image to the new image copy.
	hr = pSurfaceNew->Blt(
	&rectDest, //Destination RECT
	pImageSource->pSurface,  //lpDDSrcSurface
	NULL, //Source RECT or NULL for entire surface
	DDBLT_WAIT, //flags
	NULL ); //special effects

	if (hr != DD_OK)
	{
		pSurfaceNew->Release();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -