HGE 初始化函数注释:System_Initiate
1
//
HGE初始化
2 bool CALL HGE_Impl::System_Initiate()
3 {
4 OSVERSIONINFO os_ver; // 操作系统版本信息
5 SYSTEMTIME tm; // 系统时间
6 MEMORYSTATUS mem_st; // 内存状态
7 WNDCLASS winclass;
8 int width, height;
9
10 // Log system info
11
12 System_Log( " HGE Started..\n " );
13
14 System_Log( " HGE version: %X.%X " , HGE_VERSION >> 8 , HGE_VERSION & 0xFF );
15 GetLocalTime( & tm); // 取本地时间 UTC格式,并显示到日志中。
16 System_Log( " Date: %02d.%02d.%d, %02d:%02d:%02d\n " , tm.wDay, tm.wMonth, tm.wYear, tm.wHour, tm.wMinute, tm.wSecond);
17
18 System_Log( " Application: %s " ,szWinTitle);
19 os_ver.dwOSVersionInfoSize = sizeof (os_ver);
20 GetVersionEx( & os_ver); // 取当前操作系统的版本信息
21 System_Log( " OS: Windows %ld.%ld.%ld " ,os_ver.dwMajorVersion,os_ver.dwMinorVersion,os_ver.dwBuildNumber);
22
23 GlobalMemoryStatus( & mem_st); // 取内存状态
24 System_Log( " Memory: %ldK total, %ldK free\n " ,mem_st.dwTotalPhys / 1024L ,mem_st.dwAvailPhys / 1024L );
25
26
27 // Register window class
28 /* *
29 窗口样式
30 CS_DBLCLKS 支持双击消息
31 CS_HREDRAW 水平移动或变化时候,重画
32 CS_VREDRAW 垂直移动或变化时候,重画
33 */
34 winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
35
36 winclass.lpfnWndProc = WindowProc; // 消息处理过程
37 winclass.cbClsExtra = 0 ;
38 winclass.cbWndExtra = 0 ;
39 winclass.hInstance = hInstance;
40 winclass.hCursor = LoadCursor(NULL, IDC_ARROW); // 装载光标
41 winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
42 winclass.lpszMenuName = NULL;
43 winclass.lpszClassName = WINDOW_CLASS_NAME;
44 if (szIcon) winclass.hIcon = LoadIcon(hInstance, szIcon); // 装载图标
45 else winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // 应用程序图标
46
47 if ( ! RegisterClass( & winclass)) {
48 _PostError( " Can't register window class " );
49 return false ;
50 }
51
52 // Create window
53
54 width = nScreenWidth + GetSystemMetrics(SM_CXFIXEDFRAME) * 2 ; // SM_CXFIXEDFRAME垂直边框的宽度
55 height = nScreenHeight + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 + GetSystemMetrics(SM_CYCAPTION); // SM_CYFIXEDFRAME 水平边框与标题的宽度
56 // 下面的定义,是使自己处于屏幕的中心
57 // 这里定义的是窗口模式显示的样式
58 rectW.left = (GetSystemMetrics(SM_CXSCREEN) - width) / 2 ;
59 rectW.top = (GetSystemMetrics(SM_CYSCREEN) - height) / 2 ;
60 rectW.right = rectW.left + width;
61 rectW.bottom = rectW.top + height;
62 styleW = WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE; // WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX;
63 // 这里定义的是全屏模式显示的样式
64 rectFS.left = 0 ;
65 rectFS.top = 0 ;
66 rectFS.right = nScreenWidth;
67 rectFS.bottom = nScreenHeight;
68 styleFS = WS_POPUP | WS_VISIBLE; // WS_POPUP
69 // 如果存在父窗口,则定义为子窗口样式
70 if (hwndParent)
71 {
72 rectW.left = 0 ;
73 rectW.top = 0 ;
74 rectW.right = nScreenWidth;
75 rectW.bottom = nScreenHeight;
76 styleW = WS_CHILD | WS_VISIBLE;
77 bWindowed = true ;
78 }
79
80 if (bWindowed) // 窗口模式
81 hwnd = CreateWindowEx( 0 , WINDOW_CLASS_NAME, szWinTitle, styleW,
82 rectW.left, rectW.top, rectW.right - rectW.left, rectW.bottom - rectW.top,
83 hwndParent, NULL, hInstance, NULL);
84 else // 全屏模式
85 hwnd = CreateWindowEx(WS_EX_TOPMOST, WINDOW_CLASS_NAME, szWinTitle, styleFS,
86 0 , 0 , 0 , 0 ,
87 NULL, NULL, hInstance, NULL);
88
89 if ( ! hwnd) // 如果创建失败
90 {
91 _PostError( " Can't create window " );
92 return false ;
93 }
94
95 ShowWindow(hwnd, SW_SHOW); // 显示
96
97 // Init subsystems
98
99 timeBeginPeriod( 1 ); // 设置多媒体时间粒度,在这里是1毫秒
100 Random_Seed(); // 初始化随机数种子
101 _InitPowerStatus();
102 _InputInit(); // 输入初始化
103 if ( ! _GfxInit()) { System_Shutdown(); return false ; } // D3D8初始化
104 if ( ! _SoundInit()) { System_Shutdown(); return false ; } // bass.dll初始化
105
106 System_Log( " Init done.\n " );
107
108
109 fTime = 0.0f ; // HGE计时器
110 t0 = t0fps = timeGetTime(); // 帧率计时器 初始化
111 dt = cfps = 0 ; // 帧间计时器 初始化
112 nFPS = 0 ; // 帧率 初始化
113
114 // Show splash
115
116 #ifdef DEMO
117
118 bool ( * func)();
119 bool ( * rfunc)();
120 HWND hwndTmp;
121
122 if (pHGE -> bDMO)
123 {
124 Sleep( 200 );
125 func = ( bool ( * )())pHGE -> System_GetStateFunc(HGE_FRAMEFUNC);
126 rfunc = ( bool ( * )())pHGE -> System_GetStateFunc(HGE_RENDERFUNC);
127 hwndTmp = hwndParent; hwndParent = 0 ;
128 pHGE -> System_SetStateFunc(HGE_FRAMEFUNC, DFrame);
129 pHGE -> System_SetStateFunc(HGE_RENDERFUNC, 0 );
130 DInit();
131 pHGE -> System_Start();
132 DDone();
133 hwndParent = hwndTmp;
134 pHGE -> System_SetStateFunc(HGE_FRAMEFUNC, func);
135 pHGE -> System_SetStateFunc(HGE_RENDERFUNC, rfunc);
136 }
137
138 #endif
139
140 // Done
141
142 return true ;
143 }
2 bool CALL HGE_Impl::System_Initiate()
3 {
4 OSVERSIONINFO os_ver; // 操作系统版本信息
5 SYSTEMTIME tm; // 系统时间
6 MEMORYSTATUS mem_st; // 内存状态
7 WNDCLASS winclass;
8 int width, height;
9
10 // Log system info
11
12 System_Log( " HGE Started..\n " );
13
14 System_Log( " HGE version: %X.%X " , HGE_VERSION >> 8 , HGE_VERSION & 0xFF );
15 GetLocalTime( & tm); // 取本地时间 UTC格式,并显示到日志中。
16 System_Log( " Date: %02d.%02d.%d, %02d:%02d:%02d\n " , tm.wDay, tm.wMonth, tm.wYear, tm.wHour, tm.wMinute, tm.wSecond);
17
18 System_Log( " Application: %s " ,szWinTitle);
19 os_ver.dwOSVersionInfoSize = sizeof (os_ver);
20 GetVersionEx( & os_ver); // 取当前操作系统的版本信息
21 System_Log( " OS: Windows %ld.%ld.%ld " ,os_ver.dwMajorVersion,os_ver.dwMinorVersion,os_ver.dwBuildNumber);
22
23 GlobalMemoryStatus( & mem_st); // 取内存状态
24 System_Log( " Memory: %ldK total, %ldK free\n " ,mem_st.dwTotalPhys / 1024L ,mem_st.dwAvailPhys / 1024L );
25
26
27 // Register window class
28 /* *
29 窗口样式
30 CS_DBLCLKS 支持双击消息
31 CS_HREDRAW 水平移动或变化时候,重画
32 CS_VREDRAW 垂直移动或变化时候,重画
33 */
34 winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
35
36 winclass.lpfnWndProc = WindowProc; // 消息处理过程
37 winclass.cbClsExtra = 0 ;
38 winclass.cbWndExtra = 0 ;
39 winclass.hInstance = hInstance;
40 winclass.hCursor = LoadCursor(NULL, IDC_ARROW); // 装载光标
41 winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
42 winclass.lpszMenuName = NULL;
43 winclass.lpszClassName = WINDOW_CLASS_NAME;
44 if (szIcon) winclass.hIcon = LoadIcon(hInstance, szIcon); // 装载图标
45 else winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // 应用程序图标
46
47 if ( ! RegisterClass( & winclass)) {
48 _PostError( " Can't register window class " );
49 return false ;
50 }
51
52 // Create window
53
54 width = nScreenWidth + GetSystemMetrics(SM_CXFIXEDFRAME) * 2 ; // SM_CXFIXEDFRAME垂直边框的宽度
55 height = nScreenHeight + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 + GetSystemMetrics(SM_CYCAPTION); // SM_CYFIXEDFRAME 水平边框与标题的宽度
56 // 下面的定义,是使自己处于屏幕的中心
57 // 这里定义的是窗口模式显示的样式
58 rectW.left = (GetSystemMetrics(SM_CXSCREEN) - width) / 2 ;
59 rectW.top = (GetSystemMetrics(SM_CYSCREEN) - height) / 2 ;
60 rectW.right = rectW.left + width;
61 rectW.bottom = rectW.top + height;
62 styleW = WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE; // WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX;
63 // 这里定义的是全屏模式显示的样式
64 rectFS.left = 0 ;
65 rectFS.top = 0 ;
66 rectFS.right = nScreenWidth;
67 rectFS.bottom = nScreenHeight;
68 styleFS = WS_POPUP | WS_VISIBLE; // WS_POPUP
69 // 如果存在父窗口,则定义为子窗口样式
70 if (hwndParent)
71 {
72 rectW.left = 0 ;
73 rectW.top = 0 ;
74 rectW.right = nScreenWidth;
75 rectW.bottom = nScreenHeight;
76 styleW = WS_CHILD | WS_VISIBLE;
77 bWindowed = true ;
78 }
79
80 if (bWindowed) // 窗口模式
81 hwnd = CreateWindowEx( 0 , WINDOW_CLASS_NAME, szWinTitle, styleW,
82 rectW.left, rectW.top, rectW.right - rectW.left, rectW.bottom - rectW.top,
83 hwndParent, NULL, hInstance, NULL);
84 else // 全屏模式
85 hwnd = CreateWindowEx(WS_EX_TOPMOST, WINDOW_CLASS_NAME, szWinTitle, styleFS,
86 0 , 0 , 0 , 0 ,
87 NULL, NULL, hInstance, NULL);
88
89 if ( ! hwnd) // 如果创建失败
90 {
91 _PostError( " Can't create window " );
92 return false ;
93 }
94
95 ShowWindow(hwnd, SW_SHOW); // 显示
96
97 // Init subsystems
98
99 timeBeginPeriod( 1 ); // 设置多媒体时间粒度,在这里是1毫秒
100 Random_Seed(); // 初始化随机数种子
101 _InitPowerStatus();
102 _InputInit(); // 输入初始化
103 if ( ! _GfxInit()) { System_Shutdown(); return false ; } // D3D8初始化
104 if ( ! _SoundInit()) { System_Shutdown(); return false ; } // bass.dll初始化
105
106 System_Log( " Init done.\n " );
107
108
109 fTime = 0.0f ; // HGE计时器
110 t0 = t0fps = timeGetTime(); // 帧率计时器 初始化
111 dt = cfps = 0 ; // 帧间计时器 初始化
112 nFPS = 0 ; // 帧率 初始化
113
114 // Show splash
115
116 #ifdef DEMO
117
118 bool ( * func)();
119 bool ( * rfunc)();
120 HWND hwndTmp;
121
122 if (pHGE -> bDMO)
123 {
124 Sleep( 200 );
125 func = ( bool ( * )())pHGE -> System_GetStateFunc(HGE_FRAMEFUNC);
126 rfunc = ( bool ( * )())pHGE -> System_GetStateFunc(HGE_RENDERFUNC);
127 hwndTmp = hwndParent; hwndParent = 0 ;
128 pHGE -> System_SetStateFunc(HGE_FRAMEFUNC, DFrame);
129 pHGE -> System_SetStateFunc(HGE_RENDERFUNC, 0 );
130 DInit();
131 pHGE -> System_Start();
132 DDone();
133 hwndParent = hwndTmp;
134 pHGE -> System_SetStateFunc(HGE_FRAMEFUNC, func);
135 pHGE -> System_SetStateFunc(HGE_RENDERFUNC, rfunc);
136 }
137
138 #endif
139
140 // Done
141
142 return true ;
143 }