hge源码注释 2 hge引擎的启动(2)

启动引擎的第二步一般是调用System_SetState,设置一些系统状态,它的定义如下

	virtual void		CALL	System_SetStateBool  (hgeBoolState   state, bool        value) = 0;
	virtual void		CALL	System_SetStateFunc  (hgeFuncState   state, hgeCallback value) = 0;
	virtual void		CALL	System_SetStateHwnd  (hgeHwndState   state, HWND        value) = 0;
	virtual void		CALL	System_SetStateInt   (hgeIntState    state, int         value) = 0;
	virtual void		CALL	System_SetStateString(hgeStringState state, const char *value) = 0;
	inline void					System_SetState(hgeBoolState   state, bool        value) { System_SetStateBool  (state, value); }
	inline void					System_SetState(hgeFuncState   state, hgeCallback value) { System_SetStateFunc  (state, value); }
	inline void					System_SetState(hgeHwndState   state, HWND        value) { System_SetStateHwnd  (state, value); }
	inline void					System_SetState(hgeIntState    state, int         value) { System_SetStateInt   (state, value); }
	inline void					System_SetState(hgeStringState state, const char *value) { System_SetStateString(state, value); }


System_SetState的第一个参数类型都是枚举,代表设置的含义

/*
** HGE System state constants  hge系统状态常量 枚举
*/
enum hgeBoolState
{
	HGE_WINDOWED		= 1,    // bool		run in window?		(default: false)  窗口化
	HGE_ZBUFFER			= 2,    // bool		use z-buffer?		(default: false)  使用Z 缓存
	HGE_TEXTUREFILTER	= 3,    // bool		texture filtering?	(default: true)  纹理过滤
	
	HGE_USESOUND		= 4,    // bool		use BASS for sound?	(default: true)  使用声音
	
	HGE_DONTSUSPEND		= 5,	// bool		focus lost:suspend?	(default: false)窗口失去焦点时不暂停
	HGE_HIDEMOUSE		= 6,	// bool		hide system cursor?	(default: true)  隐藏鼠标

	HGE_SHOWSPLASH		= 7,	// bool		hide system cursor?	(default: true) 显示分屏

	HGEBOOLSTATE_FORCE_DWORD = 0x7FFFFFFF  //使枚举为32位
};

enum hgeFuncState
{
	HGE_FRAMEFUNC		= 8,    // bool*()	frame function		(default: NULL) (you MUST set this)帧渲染函数
	HGE_RENDERFUNC		= 9,    // bool*()	render function		(default: NULL) 帧逻辑函数
	HGE_FOCUSLOSTFUNC	= 10,   // bool*()	focus lost function	(default: NULL) 窗口失去焦点时调用
	HGE_FOCUSGAINFUNC	= 11,   // bool*()	focus gain function	(default: NULL) 窗口得到焦点时调用
	HGE_GFXRESTOREFUNC	= 12,   // bool*()	exit function		(default: NULL) 有内容丢失需要恢复时调用
	HGE_EXITFUNC		= 13,   // bool*()	exit function		(default: NULL) 试图关闭窗口时调用
	
	HGEFUNCSTATE_FORCE_DWORD = 0x7FFFFFFF
};

enum hgeHwndState
{
	HGE_HWND			= 15,	// int		window handle: read only   窗口句柄
	HGE_HWNDPARENT		= 16,	// int		parent win handle	(default: 0)  父窗口句柄
	
	HGEHWNDSTATE_FORCE_DWORD = 0x7FFFFFFF
};

enum hgeIntState
{
	HGE_SCREENWIDTH		= 17,   // int		screen width		(default: 800) 屏幕高
	HGE_SCREENHEIGHT	= 18,   // int		screen height		(default: 600) 屏幕宽
	HGE_SCREENBPP		= 19,   // int		screen bitdepth		(default: 32) (desktop bpp in windowed mode) 位深度
	
	HGE_SAMPLERATE		= 20,   // int		sample rate			(default: 44100) 声音采样率
	HGE_FXVOLUME		= 21,   // int		global fx volume	(default: 100)  全局音效的音量
	HGE_MUSVOLUME		= 22,   // int		global music volume	(default: 100)  全局音乐的音量
	HGE_STREAMVOLUME	= 23,   // int		global music volume	(default: 100) 同上
	
	HGE_FPS				= 24,	// int		fixed fps			(default: HGEFPS_UNLIMITED) 屏幕刷新率

	HGE_POWERSTATUS		= 25,   // int		battery life percent + status 电源状态
	
	HGEINTSTATE_FORCE_DWORD = 0x7FFFFFF
};

enum hgeStringState
{
	HGE_ICON			= 26,   // char*	icon resource		(default: NULL) 图标
	HGE_TITLE			= 27,   // char*	window title		(default: "HGE")  窗口标题
	
	HGE_INIFILE			= 28,   // char*	ini file			(default: NULL) (meaning no file) 配置文件
	HGE_LOGFILE			= 29,   // char*	log file			(default: NULL) (meaning no file) 日志文件

	HGESTRINGSTATE_FORCE_DWORD = 0x7FFFFFFF
};

下一步调用 System_Initiate,根据刚才的设置进行初始化,该函数在HGE类中是个纯虚函数,在HGE_Impl被实现


//system.cpp
//系统初始化
bool CALL HGE_Impl::System_Initiate()
{
	OSVERSIONINFO	os_ver; //操作系统版本
	SYSTEMTIME		tm; //时间
	MEMORYSTATUS	mem_st;  //内存信息
	WNDCLASS		winclass; //窗口类
	int				width, height;  //窗口宽高

	// Log system info

	System_Log("HGE Started..\n");

	System_Log("HGE version: %X.%X", HGE_VERSION>>8, HGE_VERSION & 0xFF);
	GetLocalTime(&tm);  //获取当地的当前系统日期和时间
	System_Log("Date: %02d.%02d.%d, %02d:%02d:%02d\n", tm.wDay, tm.wMonth, tm.wYear, tm.wHour, tm.wMinute, tm.wSecond);

	System_Log("Application: %s",szWinTitle);
	os_ver.dwOSVersionInfoSize=sizeof(os_ver);
	GetVersionEx(&os_ver);  //获取操作系统版本
	System_Log("OS: Windows %ld.%ld.%ld",os_ver.dwMajorVersion,os_ver.dwMinorVersion,os_ver.dwBuildNumber);

	GlobalMemoryStatus(&mem_st);  //获取内存信息
	System_Log("Memory: %ldK total, %ldK free\n",mem_st.dwTotalPhys/1024L,mem_st.dwAvailPhys/1024L);


	// Register window class  注册窗口类
	
	winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; //窗口可以检测到双击事件,
	//窗口拥有自己的设备内容,当窗口水平、垂直方向的宽度变化时重绘整个窗口
	winclass.lpfnWndProc	= WindowProc;
	winclass.cbClsExtra		= 0;
	winclass.cbWndExtra		= 0;
	winclass.hInstance		= hInstance;
	winclass.hCursor		= LoadCursor(NULL, IDC_ARROW);
	winclass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	winclass.lpszMenuName	= NULL; 
	winclass.lpszClassName	= WINDOW_CLASS_NAME;
	if(szIcon) winclass.hIcon = LoadIcon(hInstance, szIcon);
	else winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	
	if (!RegisterClass(&winclass)) {
		_PostError("Can't register window class");
		return false;
	}

	// Create window
	//窗口居中
	width=nScreenWidth + GetSystemMetrics(SM_CXFIXEDFRAME)*2;
	height=nScreenHeight + GetSystemMetrics(SM_CYFIXEDFRAME)*2 + GetSystemMetrics(SM_CYCAPTION);
	//窗口化时窗口矩形
	rectW.left=(GetSystemMetrics(SM_CXSCREEN)-width)/2;
	rectW.top=(GetSystemMetrics(SM_CYSCREEN)-height)/2;
	rectW.right=rectW.left+width;
	rectW.bottom=rectW.top+height;
	styleW=WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|WS_VISIBLE; //WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX;
	//全屏时屏幕矩形
	rectFS.left=0;
	rectFS.top=0;
	rectFS.right=nScreenWidth;
	rectFS.bottom=nScreenHeight;
	styleFS=WS_POPUP|WS_VISIBLE; //WS_POPUP

	if(hwndParent)
	{
		rectW.left=0;
		rectW.top=0;
		rectW.right=nScreenWidth;
		rectW.bottom=nScreenHeight;
		styleW=WS_CHILD|WS_VISIBLE; 
		bWindowed=true;
	}
	//创建窗口,分是否全屏
	if(bWindowed)
		hwnd = CreateWindowEx(0, WINDOW_CLASS_NAME, szWinTitle, styleW,
				rectW.left, rectW.top, rectW.right-rectW.left, rectW.bottom-rectW.top,
				hwndParent, NULL, hInstance, NULL);
	else
		hwnd = CreateWindowEx(WS_EX_TOPMOST, WINDOW_CLASS_NAME, szWinTitle, styleFS,
				0, 0, 0, 0,
				NULL, NULL, hInstance, NULL);
	if (!hwnd)
	{
		_PostError("Can't create window");
		return false;
	}

	ShowWindow(hwnd, SW_SHOW);

	// Init subsystems

	timeBeginPeriod(1);  //设置定时器精度 1ms
	Random_Seed();  //初始化随机数种子为0
	_InitPowerStatus();  //初始化电源状态
	_InputInit();	//初始化输入
	if(!_GfxInit()) { System_Shutdown(); return false; }  //初始化d3d
	if(!_SoundInit()) { System_Shutdown(); return false; } //初始化声音

	System_Log("Init done.\n");
	//初始化时间
	fTime=0.0f;   
	t0=t0fps=timeGetTime();  
	dt=cfps=0;
	nFPS=0;

	// Show splash

#ifdef DEMO

	bool			(*func)();
	bool			(*rfunc)();
	HWND			hwndTmp;

	if(pHGE->bDMO)  //如果开头显示hge的logo
	{
		Sleep(200);
		func=(bool(*)())pHGE->System_GetStateFunc(HGE_FRAMEFUNC);
		rfunc=(bool(*)())pHGE->System_GetStateFunc(HGE_RENDERFUNC);
		hwndTmp=hwndParent; hwndParent=0;
		pHGE->System_SetStateFunc(HGE_FRAMEFUNC, DFrame);
		pHGE->System_SetStateFunc(HGE_RENDERFUNC, 0);
		DInit();
		pHGE->System_Start();
		DDone();
		hwndParent=hwndTmp;
		pHGE->System_SetStateFunc(HGE_FRAMEFUNC, func);
		pHGE->System_SetStateFunc(HGE_RENDERFUNC, rfunc);
	}

#endif

	// Done

	return true;
}


你可能感兴趣的:(hge)