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

📄 ddapi.h

📁 赤壁之战(游戏原码)
💻 H
字号:
//////////////////////////////
//	DDAPI.h			:	v0039
//	Written by		:	Liu Gang
//	Compiler		:	Microsoft Visual C++ 4.0 & DirectX
//	Library			:	DDraw.Lib
//	Copyright (C)	:	1996 WayAhead Corporation 
//	v0010			:	Aug.26.1996
//	v0011			:	Sep.26.1996
//	v0020			:	Nov.8.1996
//					Warning: the width of the surface cannot larger than 640 pixel
//							when this occurs, there will be only a general error code
//							and return failed.
//	v0021			:	Nov.27.1996
//	v0030			:	Dec.11.1996, upgrade DirectDraw from 1.0 to 2.0
//	v0031			:	Feb.10.1997, fixed a bug that cannot release surfaces entirly
//						add a parameter to DD_EraseFrontScreen(), DD_EraseBckScreen() and CDDSurface::Erase()
//						add CDDSurface::SetColorKeyRGB() and CDDSurface::SetColorKeyPAL(), changed color key method
//	v0032			:	Mar.7.1997, Add a member "m_bInVideo" to CDDSurface
//						To decrease the load time when surface lost
//	v0033			:	Mar.10.1997
//					Warning:	when using Lock()/Unlock() to access surface,
//							please use ddsd.lPitch to stand for the width of 
//							the source surface. Mostly, the value in ddsd.dwWidth is equal to
//                          that in ddsd.lPitch. But if the surface is in video memory, and its
//							width is less than 640, and cannot devied by 4, they do not match before you 
//							draw ddsd.lpSurface to other surfaces.
//	v0034			:	Apr.1.1997, add function FadeOut() and FadeIn(), add surface and palette counter
//							add a member "m_bCreated" and a function IfCreated() to CDDSurface
//							add a member "m_strPalette" and a function Reload() to CDDPalette
//	v0035			:	Apr.9.1997, fixed a bug that generated by last version, did not initialize m_bCreated when 
//							constructing class instance, it took two whole days.
//	v0036			:	Apr.18.1997, add CS_DBLCLKS to window style
//	v0037			:	May.6.1997, add fadein and fadeout functions, and read palette from customed palette file
//	v0038			:	May.10.1997, edited fadein and fadeout functions, to make them easy to use.
//	v0039			:	May.12.1997, changed the global palette to object
//////////////////////////////
// header file

// This file provides basic interfaces for DirectDraw with C++ 
// extension, so that someone can use more easily.
//////////////////////////////

#ifndef	__DDAPI_H__
#define	__DDAPI_H__

//////////////////////////////
#include <DDraw.h>	// Direct Draw objects, needs DDraw.lib
//////////////////////////////

//////////////////////////////
// global control
#define		DD_ERROR_ID		100
// screen size and depth
#define		SCREEN_WIDTH	640
#define		SCREEN_HEIGHT	480
#define		SCREEN_THICK	8
//////////////////////////////

//////////////////////////////
// global major window
#define		WM_DD_SURFACELOST	9999
extern		HWND	hwndGame;	// the only window in this game
//////////////////////////////

// major window
//////////////////////////////
// external initialization
// hInstance	:	handle of application instance
// nCmdShow		:	show major window or not
// strName		:	window name
// return value	:	handle of the window
HWND DD_InitWindow( HANDLE hInstance, int nCmdShow, LPCTSTR strName );

// external main procedure
// main window procedure
// defined in other file
long FAR PASCAL DD_WindowProc( HWND hWnd, UINT message, 
                            WPARAM wParam, LPARAM lParam );
//////////////////////////////

// graphic mode
//////////////////////////////
// initialize graphic system
// hwnd			:	handle of the major window
// nWidth, nHeight, nDepth:	the display mode of screen
//							default is 640x480x8
// bInVideo		:	if 1, should load back buffer surface in video memory
//					if 0, should load back buffer surface in system memory
//					if -1, can load back buffer surface anywhere
// return value	:	TRUE if succeeded
BOOL DD_InitGraph( HWND hwnd, int nWidth=-1, int nHeight=-1, int nDepth=-1, int bInVideo=-1 );

// quit graphic system
void DD_QuitGraph( void );			

// when init error occurs			
// hwnd			:	handle of the parent window
// err			:	error ID
// return value	:	Always FALSE
BOOL DD_initFail( HWND hwnd, int err );

// get front buffer pointer
// return value	:	pointer of front buffer
extern LPDIRECTDRAWSURFACE2 DD_GetFrontBuffer( void );

// get back buffer pointer
// return value	:	pointer of back buffer
extern LPDIRECTDRAWSURFACE2 DD_GetBackBuffer( void );
//////////////////////////////

// screen
//////////////////////////////
// if surface is lost, run retore
// return value	:	TRUE if succeeded
BOOL DD_RestoreScreen( void );

// draw from back buffer to front buffer
// pRect		:	rectangle region to update, full surface if it is NULL
// return value	:	TRUE if succeeded
BOOL DD_UpdateScreen( RECT *pRect=NULL );

// fill black color to front screen
// nColor		:	color index you want to fill, default is BLACK
// pRect		:	rectangle region to erase, full surface if it is NULL
// return value	:	TRUE if succeeded
BOOL DD_EraseFrontScreen( int nColor=0, RECT *pRect=NULL );

// fill black color to a given surface
// pSurface		:	surface to be erased
// nColor		:	color index you want to fill, default is BLACK
// pRect		:	rectangle region to erase, full surface if it is NULL
// return value	:	TRUE if succeeded
BOOL DD_EraseScreen( LPDIRECTDRAWSURFACE2 pSurface, int nColor, RECT *pRect/*=NULL*/ );
//////////////////////////////

// fade screen
//////////////////////////////
// fill black color to back screen
// nColor		:	color index you want to fill, default is BLACK
// pRect		:	rectangle region to erase, full surface if it is NULL
// return value	:	TRUE if succeeded
BOOL DD_EraseBackScreen( int nColor=0, RECT *pRect=NULL );

// fade in or fade out sczz
// bIn			:	TRUE for fade in screen, else for fade out screen
// nMaxStep		:	ranges from 1 to 255, set to 0 if to reset this function
// return value	:	TRUE for end session
BOOL DD_FadeScreen( BOOL bIn, int nStep=20 );

// fade out 
// nMaxStep		:	ranges from 1 to 255, set to 0 if to reset this function
// return value	:	TRUE for end session
BOOL DD_FadeOut( int nStep=20 );

// fade in 
// nMaxStep		:	ranges from 1 to 255, set to 0 if to reset this function
// return value	:	TRUE for end session
BOOL DD_FadeIn(PALETTEENTRY	 * pe,PALETTEENTRY	* peFadeSave,int nMaxStep );

// fade out screen, from color to black
// nStep		:	how much steps fade from begin to end
// nDelay		:	time each step should pause
void DD_FadeOutScreen( int nMaxStep, int nDelay );

// fade in screen, from black to color
// nStep		:	how much steps fade from begin to end
// nDelay		:	time each step should pause
// pPal			:	the palette you want to display at last
void DD_FadeInScreen( int nMaxStep, int nDelay );

// Restore saved palette from "peFadeSave" to spcecified palette 
// pointer. Usually used after DD_FadeOutScreen(), to reset the 
// palette at last, or maybe not useful after v0039.
// pPal			:	palette pointer to be restored
void DD_FadeRestore( LPDIRECTDRAWPALETTE pPal );

// Clear the palette pointer to whole black. Usually used before 
// DD_FadeInScreen(), to set a palette first and save the origional 
// value to "peFadeSave"
// pPal			:	palette to be cleared
void DD_FadeClear( LPDIRECTDRAWPALETTE pPal );
//////////////////////////////

// blt
//////////////////////////////
// draw from one surface to another
// ptDest		:	destination position in destination surface
// lprcSrc		:	source rectangle in source surface, full surface if it is NULL
// dwFlags		:	DDBLTFAST_NOCOLORKEY or DDBLTFAST_SRCCOLORKEY
// return value	:	TRUE if succeeded
BOOL DD_BltSurface( POINT ptDest, LPDIRECTDRAWSURFACE2 lpDDSDest, RECT *lprcSrc, LPDIRECTDRAWSURFACE2 lpDDSSrc, DWORD dwFlags );

// get global palette pointer
LPDIRECTDRAWPALETTE DD_GetPalette();
//////////////////////////////


//////////////////////////////
// DirectDraw surface class
class	CDDSurface
{
	LPDIRECTDRAWSURFACE2 m_lpSurface;
	LPDIRECTDRAWPALETTE m_lpPalette;

	// creation flag
	BOOL m_bCreated;	// TRUE if the surface has been created

	// bitmap attributes
	BOOL m_bHasBitmap;
	char m_strBitmap[64];
	SIZE m_szBitmap;	// size of surface

	// color key
	DWORD m_dwDrawFlag;	// with color key or not
	// if in video memory, only used when surface lost
	BOOL m_bInVideo;	// -1, auto; 0, not; 1 yes

public:
	// contructor
	CDDSurface();
	// destructor
	~CDDSurface();

// attributes
	// get size of bitmap
	SIZE GetSize(){ return m_szBitmap; }
	// get draw flag
	DWORD GetDrawFlag(){ return m_dwDrawFlag; }

	// the only way to get surface pointer
	LPDIRECTDRAWSURFACE2 GetSurface(){ return m_lpSurface; }
	// get palette of the surface
	LPDIRECTDRAWPALETTE GetPalette( void ){ return m_lpPalette; }

	// test if the surface has been created
	BOOL IfCreated(){ return m_bCreated; }

// contruction
	// create surface
	// width, height:	size of surface to create
	// bColorkey	:	TRUE if want color key, usually BLACK
	// bInVideo		:	1, should load surface in video memory
	//					0, should load surface in system memory
	//					-1, load surface anywhere possible
	// return value	:	TRUE if succeeded
	BOOL Create( int width, int height, BOOL bColorKey=FALSE, BOOL bInVideo=-1 );

	// create surface to store a bitmap, same as create()
	// strBitmap	:	bitmap filename to load
	// bColorKey	:	TRUE if want color key, usually BLACK
	// bInVideo		:	1, should load surface in video memory
	//					0, should load surface in system memory
	//					-1, load surface anywhere possible
	// return value	:	TRUE if succeeded
	BOOL LoadBitmap( LPCTSTR strBitmap, BOOL bColorKey=TRUE, BOOL bInVideo=-1 );

	// if surface is lost, run restore
	// there maybe a bug, if bitmap was loaded by Auto memory detect, 
	// and load into video memory, but got surface lost, the bitmap may
	// not be reloaded.
	// return value	:	TRUE if it is succeeded
	BOOL Restore( void );

	// reload a bitmap to an existing surface, called when surface lost
	// strBitmap:	new bitmap file name
	// return value:	TRUE if succeeded
	BOOL ReLoadBitmap( LPCTSTR strBitmap = NULL);

// color key
	// actually, you need not use these two functions to set color key, 
	// if you want color key, by default, BLACK is the color key

	// they all can run correctly on 256 color, exclusive mode.
	// in unexclusive mode, surface created by Create(), 
	//		SetColorKeyPAL() and SetColorKeyRGB() are not correct.
	// in high color mode, surface created by Create(), 
	//		SetColorKeyPAL() and SetColorKeyRGB() are not correct.
	// if surface created by LoadBitmap(), they are all correct.
	// only SetColorKeyDEF() can run correctly in any condition.

	// set colorkey by default
	// color on top left corner of the surface to be color key
	// must be used after Create() or LoadBitmap()
	// if used before, no effects
	// return	:	TRUE if successful
	BOOL SetColorKeyDEF();

	// set colorkey by given RGB value
	// must be used after Create() or LoadBitmap()
	// if used before, no effects
	// nColor	:	color key value
	// return	:	TRUE if successful
	BOOL SetColorKeyRGB( DWORD dwColor );

	// set colorkey by given palette entry
	// must be used after Create() or LoadBitmap()
	// if used before, no effects
	// if in high/true color mode, this functions is obsolete
	// nColor	:	color key value
	// return	:	TRUE if successful
	BOOL SetColorKeyPAL( BYTE nColor );

// draw
	// fill black color to the surface, full surface if it is NULL
	// nColor		:	color you want to fill, default is BLACK
	// pRect		:	region to fill, default is the entire surface
	// return value	:	TRUE if succeeded
	BOOL Erase( int nColor=0, RECT *pRect=NULL );

	// blt surface to front buffer
	// 贴图直接到显示面上
	// ptDest	:	destination position, top-left
	// lpercSrc	:	source rectangle, full surface if it is NULL
	// return value	:	TRUE if succeeded
	BOOL BltToFront( POINT ptDest, LPRECT lprcSrc );

	// blt my surface to back surface
	// 贴图到背景面上
	// ptDest	:	destination position, top-left
	// lprcSrc	:	source rectangle, full surface if it is NULL
	// return value	:	TRUE if succeeded
	BOOL BltToBack( POINT ptDest, LPRECT lprcSrc );

	// blt given surface to my surface
	// if created by Create(), flags must be DDBLTFAST_NOCOLORKEY
	// if created by LoadBitmap( .., FALSE ), flags must be DDBLTFAST_NOCOLORKEY
	// if created by LoadBitmap( .., [TRUE]), flags must be DDBLTFAST_SRCCOLORKEY
	// x,y			:	Destinaiton position
	// lpsrcSurface	:	source surface
	// src			:	source rectangle, full surface if it is NULL
	// flags		:	blting flag, DDBLTFAST_NOCOLORKEY or DDBLTFAST_SRCCOLORKEY
	// return value	:	TRUE if succeeded
	BOOL CDDSurface::BltSurface( int x, int y,
		LPDIRECTDRAWSURFACE2 lpDesSurface, LPRECT src, DWORD flags );

// destruction
	// release DirectDraw object
	void Release();
};
//////////////////////////////


//////////////////////////////
// DirectDraw Palette class
#define	DD_PALTYPE_BMP	0
#define	DD_PALTYPE_PAL	1
class CDDPalette
{
	LPDIRECTDRAWPALETTE	m_lpPalette;
	int  m_nType;			// palette file type, 0 is bitmap file, 1 is palette file
	char m_strPalette[64];	// bitmap file name contains the palette

public:
	// contructor
	CDDPalette();
	// destructor
	~CDDPalette();

// attributes
	// only way to get palette
	LPDIRECTDRAWPALETTE GetPalette( void ){ return m_lpPalette; }

// construction
	// create palette object
	// strBitmap	:	bitmap file name
	// bSet			:	TRUE if should set palette to 
	//					the primary surface right after creation
	BOOL LoadPalette( LPCTSTR strBitmap , BOOL bSet=FALSE);

	// create palette object
	// strPal		:	palette file name
	// bSet			:	TRUE if should set palette to 
	//					the primary surface right after creation
	BOOL LoadPalettePAL( LPCTSTR strPal , BOOL bSet=TRUE);

// reload
	// reload palette, first release the current, then read palette from file
	// return		:	TRUE if successful
	BOOL ReloadPalette();

// set
	// set palette to primary surface
	// if color depth is higher than 256, this may result in an unkown error
	// return value	:	TRUE if succeeded
	BOOL SetPalette( void );

// destruction
	// release palette
	void Release( void );

};
//////////////////////////////
#endif



⌨️ 快捷键说明

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