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

📄 hge.h

📁 2D游戏引擎hge的代码
💻 H
字号:
/*
** Haaf's Game Engine 1.4
** Copyright (C) 2003-2004, Relish Games
** hge.relishgames.com
**
** System layer API
*/


#ifndef HGE_H
#define HGE_H


#define HGE_VERSION 0x140

#ifdef HGEDLL
#define EXPORT  __declspec(dllexport)
#else
#define EXPORT
#endif

#define CALL  __stdcall

#if defined(WIN32) || defined(__BORLANDC__)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

#ifdef __BORLANDC__
 #define sqrtf (float)sqrt
 #define acosf (float)acos
 #define atan2f (float)atan2
 #define cosf (float)cos
 #define sinf (float)sin
 #define powf (float)pow
#endif


/*
** Common data types
*/
#ifndef DWORD
typedef unsigned long       DWORD;
typedef unsigned short      WORD;
typedef unsigned char       BYTE;
#endif


/*
** Common math constants
*/
#ifndef M_PI
#define M_PI	3.14159265358979323846f
#define M_PI_2	1.57079632679489661923f
#define M_PI_4	0.785398163397448309616f
#define M_1_PI	0.318309886183790671538f
#define M_2_PI	0.636619772367581343076f
#endif


/*
** HGE Handle types
*/
typedef DWORD HTEXTURE;
typedef DWORD HTARGET;
typedef DWORD HEFFECT;
typedef DWORD HMUSIC;
typedef DWORD HCHANNEL;


/*
** Hardware color macros
*/
#define ARGB(a,r,g,b)	((DWORD(a)<<24) + (DWORD(r)<<16) + (DWORD(g)<<8) + DWORD(b))
#define GETA(col)		((col)>>24)
#define GETR(col)		(((col)>>16) & 0xFF)
#define GETG(col)		(((col)>>8) & 0xFF)
#define GETB(col)		((col) & 0xFF)
#define SETA(col,a)		(((col) & 0x00FFFFFF) + (DWORD(a)<<24))
#define SETR(col,r)		(((col) & 0xFF00FFFF) + (DWORD(r)<<16))
#define SETG(col,g)		(((col) & 0xFFFF00FF) + (DWORD(g)<<8))
#define SETB(col,b)		(((col) & 0xFFFFFF00) + DWORD(b))


/*
** HGE Blending constants
*/
#define	BLEND_COLORADD		1
#define	BLEND_COLORMUL		0
#define	BLEND_ALPHABLEND	2
#define	BLEND_ALPHAADD		0
#define	BLEND_ZWRITE		4
#define	BLEND_NOZWRITE		0


/*
** HGE System state constants
*/
typedef enum
{
	HGE_FRAMEFUNC		= 1,    // bool*()	frame function		(default: NULL) (you MUST set this)
	HGE_FOCUSLOSTFUNC	= 2,    // bool*()	focus lost function	(default: NULL)
	HGE_FOCUSGAINFUNC	= 3,    // bool*()	focus gain function	(default: NULL)
	HGE_EXITFUNC		= 4,    // bool*()	exit function		(default: NULL)

	HGE_ICON			= 6,    // char*	icon resource		(default: NULL)
	HGE_TITLE			= 7,    // char*	window title		(default: "HGE")
	HGE_SCREENWIDTH		= 8,    // int		screen width		(default: 800)
	HGE_SCREENHEIGHT	= 9,    // int		screen height		(default: 600)
	HGE_SCREENBPP		= 10,    // int		screen bitdepth		(default: 32) (desktop bpp in windowed mode)
	HGE_WINDOWED		= 11,   // bool		run in window?		(default: false)
	HGE_ZBUFFER			= 12,   // bool		use z-buffer?		(default: false)

	HGE_RESOURCEFILE	= 14,   // char*	resource file		(default: NULL) (meaning no file)
	HGE_INIFILE			= 15,   // char*	ini file			(default: NULL) (meaning no file)
	HGE_LOGFILE			= 16,   // char*	log file			(default: NULL) (meaning no file)

	HGE_USESOUND		= 18,   // bool		use BASS for sound?	(default: true)
	HGE_SAMPLERATE		= 19,   // int		sample rate			(default: 44100)
	HGE_FXVOLUME		= 20,   // int		global fx volume	(default: 100)
	HGE_MUSVOLUME		= 21,   // int		global music volume	(default: 100)
	
	HGE_FPS				= 23,	// int		fixed fps or 0		(default: 0)
	HGE_DONTSUSPEND		= 24	// bool		focus lost:suspend?	(default: false)
} hgeState;


/*
** HGE System state value class
*/
class hgeValue
{
public:
	hgeValue(int value)				{ int_value = value; }
	hgeValue(bool value)			{ bool_value = value; }
	hgeValue(char *value)			{ string_value = value; }
	hgeValue(bool (*value)())		{ func_value = value; }

	int								int_value;
	bool							bool_value;
	char							*string_value;
	bool							(*func_value)();
};


/*
** HGE Vertex structure
*/
struct hgeVertex
{
	float			x, y;		// screen position    
	float			z;			// Z-buffer depth    
	float			reserved;	// reciprocal homogeneous W
	DWORD			col;		// color
	float			tx, ty;		// texture coordinates
};


/*
** HGE Quad structure
*/
struct hgeQuad
{
	hgeVertex		v[4];
	HTEXTURE		tex;
	int				blend;
};


/*
** HGE Input Event structure
*/
struct hgeInputEvent
{
	int		type;			// event type
	int		key;			// key code
	int		flags;			// event flags
	int		chr;			// character code
	int		wheel;			// wheel shift
	float	x;				// mouse cursor x-coordinate
	float	y;				// mouse cursor y-coordinate
};


/*
** HGE Input Event type constants
*/
#define INPUT_KEYDOWN		1
#define INPUT_KEYUP			2
#define INPUT_MBUTTONDOWN	3
#define INPUT_MBUTTONUP		4
#define INPUT_MOUSEMOVE		5
#define INPUT_MOUSEWHEEL	6


/*
** HGE Input Event flags
*/
#define HGEINP_SHIFT		1
#define HGEINP_CTRL			2
#define HGEINP_ALT			4
#define HGEINP_CAPSLOCK		8
#define HGEINP_SCROLLLOCK	16
#define HGEINP_NUMLOCK		32
#define HGEINP_REPEAT		64


/*
** HGE Interface class
*/
class HGE
{
public:
	virtual	void		CALL	Release() = 0;

	virtual bool		CALL	System_Initiate() = 0;
	virtual void		CALL	System_Shutdown() = 0;
	virtual bool		CALL	System_Start() = 0;
	virtual void		CALL	System_SetState(hgeState state, hgeValue value) = 0;
	virtual hgeValue	CALL	System_GetState(hgeState state) = 0;
	virtual char*		CALL	System_GetErrorMessage() = 0;
	virtual	void		CALL	System_Log(char *format, ...) = 0;
	virtual float		CALL	System_Rand(float min, float max) = 0;

	virtual void*		CALL	Resource_Load(char *filename, DWORD *size=0) = 0;
	virtual void		CALL	Resource_Free(void *res) = 0;

	virtual	void		CALL	Ini_SetInt(char *section, char *name, int value) = 0;
	virtual	int			CALL	Ini_GetInt(char *section, char *name, int def_val) = 0;
	virtual	void		CALL	Ini_SetFloat(char *section, char *name, float value) = 0;
	virtual	float		CALL	Ini_GetFloat(char *section, char *name, float def_val) = 0;
	virtual	void		CALL	Ini_SetString(char *section, char *name, char *value) = 0;
	virtual	char*		CALL	Ini_GetString(char *section, char *name, char *def_val, char *dest, int dest_size) = 0;

	virtual float		CALL	Timer_GetDelta() = 0;
	virtual int			CALL	Timer_GetFPS() = 0;

	virtual HEFFECT		CALL	Effect_Load(char *filename, DWORD size=0) = 0;
	virtual void		CALL	Effect_Free(HEFFECT eff) = 0;
	virtual HCHANNEL	CALL 	Effect_Play(HEFFECT eff) = 0;
	virtual HCHANNEL	CALL	Effect_PlayEx(HEFFECT eff, int volume=100, int pan=0, float pitch=1.0f, bool loop=false) = 0;

	virtual HMUSIC		CALL	Music_Load(char *filename, DWORD size=0) = 0;
	virtual void		CALL	Music_Free(HMUSIC mus) = 0;
	virtual HCHANNEL	CALL	Music_Play(HMUSIC mus) = 0;

	virtual void		CALL	Channel_SetPanning(HCHANNEL chn, int pan) = 0;
	virtual void		CALL 	Channel_SetVolume(HCHANNEL chn, int volume) = 0;
	virtual void		CALL 	Channel_SetPitch(HCHANNEL chn, float pitch) = 0;
	virtual void		CALL 	Channel_Pause(HCHANNEL chn) = 0;
	virtual void		CALL 	Channel_Resume(HCHANNEL chn) = 0;
	virtual void		CALL 	Channel_Stop(HCHANNEL chn) = 0;
	virtual void		CALL 	Channel_StopAll() = 0;

	virtual void		CALL	Input_GetMousePos(float *x, float *y) = 0;
	virtual void		CALL	Input_SetMousePos(float x, float y) = 0;
	virtual int			CALL	Input_GetMouseWheel() = 0;
	virtual bool		CALL	Input_GetKeyState(int key) = 0;
	virtual char*		CALL	Input_GetKeyName(int key) = 0;
	virtual int			CALL	Input_GetKey() = 0;
	virtual int			CALL	Input_GetChar() = 0;
	virtual bool		CALL	Input_GetEvent(hgeInputEvent *event) = 0;

	virtual bool		CALL	Gfx_BeginScene(HTARGET target=0) = 0;
	virtual void		CALL	Gfx_EndScene() = 0;
	virtual void		CALL	Gfx_Clear(DWORD color) = 0;
	virtual void		CALL	Gfx_RenderQuad(hgeQuad *quad) = 0;
	virtual void		CALL	Gfx_RenderLine(float x1, float y1, float x2, float y2, DWORD color=0xFFFFFFFF, float z=0.5f) = 0;
	virtual bool		CALL	Gfx_SetClipping(int x=0, int y=0, int w=0, int h=0) = 0;

	virtual HTARGET		CALL	Target_Create(int width, int height, bool zbuffer) = 0;
	virtual void		CALL	Target_Free(HTARGET target) = 0;
	virtual HTEXTURE	CALL	Target_GetTexture(HTARGET target) = 0;

	virtual HTEXTURE	CALL	Texture_Create(int width, int height) = 0;
	virtual HTEXTURE	CALL	Texture_Load(char *filename, DWORD size=0) = 0;
	virtual void		CALL	Texture_Free(HTEXTURE tex) = 0;
	virtual int			CALL	Texture_GetWidth(HTEXTURE tex) = 0;
	virtual int			CALL	Texture_GetHeight(HTEXTURE tex) = 0;
	virtual DWORD*		CALL	Texture_Lock(HTEXTURE tex, bool bReadOnly=true, int left=0, int top=0, int width=0, int height=0) = 0;
	virtual void		CALL	Texture_Unlock(HTEXTURE tex) = 0;
};

extern "C" { EXPORT HGE * CALL hgeCreate(int ver); }


/*
** HGE Virtual-key codes
*/
#define HGEK_LBUTTON	0x01
#define HGEK_RBUTTON	0x02
#define HGEK_MBUTTON	0x04

#define HGEK_ESCAPE		0x1B
#define HGEK_BACKSPACE	0x08
#define HGEK_TAB		0x09
#define HGEK_ENTER		0x0D
#define HGEK_SPACE		0x20

#define HGEK_SHIFT		0x10
#define HGEK_CTRL		0x11
#define HGEK_ALT		0x12

#define HGEK_LWIN		0x5B
#define HGEK_RWIN		0x5C
#define HGEK_APPS		0x5D

#define HGEK_PAUSE		0x13
#define HGEK_CAPSLOCK	0x14
#define HGEK_NUMLOCK	0x90
#define HGEK_SCROLLLOCK	0x91

#define HGEK_PGUP		0x21
#define HGEK_PGDN		0x22
#define HGEK_HOME		0x24
#define HGEK_END		0x23
#define HGEK_INSERT		0x2D
#define HGEK_DELETE		0x2E

#define HGEK_LEFT		0x25
#define HGEK_UP			0x26
#define HGEK_RIGHT		0x27
#define HGEK_DOWN		0x28

#define HGEK_0			0x30
#define HGEK_1			0x31
#define HGEK_2			0x32
#define HGEK_3			0x33
#define HGEK_4			0x34
#define HGEK_5			0x35
#define HGEK_6			0x36
#define HGEK_7			0x37
#define HGEK_8			0x38
#define HGEK_9			0x39

#define HGEK_A			0x41
#define HGEK_B			0x42
#define HGEK_C			0x43
#define HGEK_D			0x44
#define HGEK_E			0x45
#define HGEK_F			0x46
#define HGEK_G			0x47
#define HGEK_H			0x48
#define HGEK_I			0x49
#define HGEK_J			0x4A
#define HGEK_K			0x4B
#define HGEK_L			0x4C
#define HGEK_M			0x4D
#define HGEK_N			0x4E
#define HGEK_O			0x4F
#define HGEK_P			0x50
#define HGEK_Q			0x51
#define HGEK_R			0x52
#define HGEK_S			0x53
#define HGEK_T			0x54
#define HGEK_U			0x55
#define HGEK_V			0x56
#define HGEK_W			0x57
#define HGEK_X			0x58
#define HGEK_Y			0x59
#define HGEK_Z			0x5A

#define HGEK_GRAVE		0xC0
#define HGEK_MINUS		0xBD
#define HGEK_EQUALS		0xBB
#define HGEK_BACKSLASH	0xDC
#define HGEK_LBRACKET	0xDB
#define HGEK_RBRACKET	0xDD
#define HGEK_SEMICOLON	0xBA
#define HGEK_APOSTROPHE	0xDE
#define HGEK_COMMA		0xBC
#define HGEK_PERIOD		0xBE
#define HGEK_SLASH		0xBF

#define HGEK_NUMPAD0	0x60
#define HGEK_NUMPAD1	0x61
#define HGEK_NUMPAD2	0x62
#define HGEK_NUMPAD3	0x63
#define HGEK_NUMPAD4	0x64
#define HGEK_NUMPAD5	0x65
#define HGEK_NUMPAD6	0x66
#define HGEK_NUMPAD7	0x67
#define HGEK_NUMPAD8	0x68
#define HGEK_NUMPAD9	0x69

#define HGEK_MULTIPLY	0x6A
#define HGEK_DIVIDE		0x6F
#define HGEK_ADD		0x6B
#define HGEK_SUBTRACT	0x6D
#define HGEK_DECIMAL	0x6E

#define HGEK_F1			0x70
#define HGEK_F2			0x71
#define HGEK_F3			0x72
#define HGEK_F4			0x73
#define HGEK_F5			0x74
#define HGEK_F6			0x75
#define HGEK_F7			0x76
#define HGEK_F8			0x77
#define HGEK_F9			0x78
#define HGEK_F10		0x79
#define HGEK_F11		0x7A
#define HGEK_F12		0x7B


#endif

⌨️ 快捷键说明

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