GameClient.cpp
上传用户:maxiaolivb
上传日期:2022-06-07
资源大小:915k
文件大小:13k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. #pragma once
  2. #include "extern.h"
  3. #ifndef WM_MOUSEWHEEL
  4. #define WM_MOUSEWHEEL 0x020A
  5. #endif
  6. #define WINDOWS_CLASS_NAME GAME_CNAME
  7. #define CLIENT_WINTH 800
  8. #define CLIENT_HEIGHT 600
  9. bool g_bMaxWindow;
  10. char g_szWindowsTitle[] = "GameClient DEMO";
  11. char g_szIMEText[1024];
  12. // 全局变量:
  13. HINSTANCE g_hInstance; // 当前实例
  14. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  15. // 消息映射
  16. LONG OnSize(HWND,WORD,WORD);
  17. bool InitHGE(HWND hWnd,HINSTANCE hInstance,LONG lStyle,RECT &rcWindow);
  18. bool InitGame(HWND hWnd);
  19. bool ExitGame();
  20. bool LoopGame(HWND);
  21. bool FrameFunc();
  22. bool RenderFunc();
  23. // 程序入口
  24. int APIENTRY _tWinMain(HINSTANCE hInstance,
  25.                      HINSTANCE hPrevInstance,
  26.                      LPTSTR    lpCmdLine,
  27.                      int       nCmdShow)
  28. {
  29. WNDCLASSEX wcex;
  30. wcex.cbSize = sizeof(WNDCLASSEX); 
  31. wcex.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
  32. wcex.lpfnWndProc = (WNDPROC)MainWndProc;
  33. wcex.cbClsExtra = 0;
  34. wcex.cbWndExtra = 0;
  35. wcex.hInstance = hInstance;
  36. wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_GAMECLIENT);
  37. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  38. wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH/*BLACK_BRUSH*/);
  39. wcex.lpszMenuName = NULL;
  40. wcex.lpszClassName = WINDOWS_CLASS_NAME;
  41. wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  42. RegisterClassEx(&wcex);
  43. g_hInstance = hInstance;
  44. int nWindowWidth = CLIENT_WINTH + GetSystemMetrics(SM_CXFIXEDFRAME) * 2;
  45. int nWindowHeight = CLIENT_HEIGHT + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 + GetSystemMetrics(SM_CYCAPTION);
  46. POINT ptWindowStart;
  47. ptWindowStart.x = (GetSystemMetrics(SM_CXSCREEN) - nWindowWidth) / 2;
  48. ptWindowStart.y = (GetSystemMetrics(SM_CYSCREEN) - nWindowHeight) / 2;
  49. DWORD dwWindowStyle = WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE;
  50. HWND hWnd = CreateWindowEx(0, WINDOWS_CLASS_NAME, g_szWindowsTitle, dwWindowStyle,
  51. ptWindowStart.x,ptWindowStart.y, nWindowWidth, nWindowHeight,
  52. NULL, NULL, hInstance, NULL);
  53. if (!hWnd)
  54. return FALSE;
  55. ShowWindow(hWnd, nCmdShow);
  56. //UpdateWindow(hWnd);
  57. MSG msg;
  58. RECT rcWindow;
  59. rcWindow.left = ptWindowStart.x;
  60. rcWindow.top = ptWindowStart.y;
  61. rcWindow.right = ptWindowStart.x + nWindowWidth;
  62. rcWindow.bottom = ptWindowStart.y + nWindowHeight;
  63. if(InitHGE(hWnd,hInstance,dwWindowStyle,rcWindow))
  64. {
  65. g_pHGE->bActive = true;
  66. while (true)
  67. {
  68. if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  69. if(msg.message == WM_QUIT)
  70. break;
  71. TranslateMessage(&msg); //这句不能去掉,不然会收不到中文输入的回车键
  72. DispatchMessage(&msg);
  73. }
  74. else
  75. LoopGame(hWnd);
  76. }
  77. g_pHGE->G_ClearQueue();
  78. g_pHGE->bActive = false;
  79. ExitGame();
  80. }
  81. return (int) msg.wParam;
  82. }
  83. LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  84. {
  85. switch (message) 
  86. {
  87. case WM_IME_STARTCOMPOSITION: //开始组字
  88. {
  89. if (g_pEditCtrl)
  90. g_pEditCtrl->StartComposition();
  91. //strcpy(g_szIMEText,"");
  92. }
  93. break;
  94. case WM_IME_ENDCOMPOSITION: //结束组字
  95. {
  96. if (g_pEditCtrl)
  97. g_pEditCtrl->EndComposition();
  98. }
  99. break;
  100. case WM_IME_CHAR:
  101. {
  102. char szImeChar[3];
  103. if (wParam < 128)
  104. {
  105. szImeChar[0]=(char)wParam;   
  106. szImeChar[1]='';   
  107. }
  108. else
  109. {
  110. szImeChar[0]=(char)(wParam>>8);   
  111. szImeChar[1]=(char)wParam;
  112. szImeChar[2]='';   
  113. }
  114. if (g_pEditCtrl)
  115. g_pEditCtrl->OnImeCharMsg(szImeChar);
  116. //strcat(g_szIMEText,szImeChar);
  117. //SetWindowText(hWnd,g_szIMEText);
  118. }
  119. break;
  120. case WM_ACTIVATEAPP:
  121. if(g_pHGE && g_pHGE->pD3D && g_pHGE->bActive != (wParam == TRUE))
  122. g_pHGE->G_FocusChange(wParam == TRUE);
  123. return FALSE;
  124. case WM_SETCURSOR:
  125. if(g_pHGE->bActive && LOWORD(lParam)==HTCLIENT && g_pHGE->bHideMouse)
  126. SetCursor(NULL);
  127. else
  128. SetCursor(LoadCursor(NULL, IDC_ARROW));
  129. return FALSE;
  130. case WM_SYSKEYDOWN:
  131. if(wParam==VK_F4)
  132. {
  133. if(g_pHGE->procExitFunc)
  134. {
  135. if(g_pHGE->procExitFunc())
  136. return DefWindowProc(hWnd, message, wParam, lParam);
  137. else
  138. return FALSE;
  139. }
  140. else
  141. return DefWindowProc(hWnd, message, wParam, lParam);
  142. }
  143. else
  144. {
  145. g_pHGE->G_BuildEvent(INPUT_KEYDOWN, wParam, HIWORD(lParam) & 0xFF, (lParam & 0x40000000) ? HGEINP_REPEAT:0, -1, -1);
  146. return FALSE;
  147. }
  148. break;
  149. case WM_KEYDOWN:
  150. g_pHGE->G_BuildEvent(INPUT_KEYDOWN, wParam, HIWORD(lParam) & 0xFF, (lParam & 0x40000000) ? HGEINP_REPEAT:0, -1, -1);
  151. return FALSE;
  152. case WM_SYSKEYUP:
  153. g_pHGE->G_BuildEvent(INPUT_KEYUP, wParam, HIWORD(lParam) & 0xFF, 0, -1, -1);
  154. return FALSE;
  155. case WM_KEYUP:
  156. g_pHGE->G_BuildEvent(INPUT_KEYUP, wParam, HIWORD(lParam) & 0xFF, 0, -1, -1);
  157. return FALSE;
  158. case WM_LBUTTONDOWN:
  159. g_pHGE->G_BuildEvent(INPUT_MBUTTONDOWN, HGEK_LBUTTON, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
  160. return FALSE;
  161. case WM_MBUTTONDOWN:
  162. g_pHGE->G_BuildEvent(INPUT_MBUTTONDOWN, HGEK_MBUTTON, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
  163. return FALSE;
  164. case WM_RBUTTONDOWN:
  165. g_pHGE->G_BuildEvent(INPUT_MBUTTONDOWN, HGEK_RBUTTON, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
  166. return FALSE;
  167. case WM_LBUTTONDBLCLK:
  168. g_pHGE->G_BuildEvent(INPUT_MBUTTONDOWN, HGEK_LBUTTON, 0, HGEINP_REPEAT, LOWORDINT(lParam), HIWORDINT(lParam));
  169. return FALSE;
  170. case WM_MBUTTONDBLCLK:
  171. g_pHGE->G_BuildEvent(INPUT_MBUTTONDOWN, HGEK_MBUTTON, 0, HGEINP_REPEAT, LOWORDINT(lParam), HIWORDINT(lParam));
  172. return FALSE;
  173. case WM_RBUTTONDBLCLK:
  174. g_pHGE->G_BuildEvent(INPUT_MBUTTONDOWN, HGEK_RBUTTON, 0, HGEINP_REPEAT, LOWORDINT(lParam), HIWORDINT(lParam));
  175. return FALSE;
  176. case WM_LBUTTONUP:
  177. g_pHGE->G_BuildEvent(INPUT_MBUTTONUP, HGEK_LBUTTON, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
  178. return FALSE;
  179. case WM_MBUTTONUP:
  180. g_pHGE->G_BuildEvent(INPUT_MBUTTONUP, HGEK_MBUTTON, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
  181. return FALSE;
  182. case WM_RBUTTONUP:
  183. g_pHGE->G_BuildEvent(INPUT_MBUTTONUP, HGEK_RBUTTON, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
  184. return FALSE;
  185. case WM_MOUSEMOVE:
  186. g_pHGE->G_BuildEvent(INPUT_MOUSEMOVE, 0, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
  187. return FALSE;
  188. case WM_SYSCOMMAND:
  189. if(wParam==SC_CLOSE)
  190. {
  191. if(g_pHGE->procExitFunc)
  192. {
  193. if(g_pHGE->procExitFunc())
  194. {
  195. g_pHGE->bActive=false;
  196. return DefWindowProc(hWnd, message, wParam, lParam);
  197. }
  198. else
  199. return FALSE;
  200. }
  201. else
  202. {
  203. g_pHGE->bActive=false;
  204. return DefWindowProc(hWnd, message, wParam, lParam);
  205. }
  206. }
  207. break;
  208. case WM_PAINT:
  209. ValidateRect(hWnd,NULL);
  210. break;
  211. case WM_SIZE: //窗口缩放
  212. if(wParam==SIZE_RESTORED)
  213. {
  214. if (g_pHGE)
  215. g_pHGE->G_Resize(LOWORD(lParam), HIWORD(lParam));
  216. }
  217. OnSize(hWnd,LOWORD(lParam),HIWORD(lParam));
  218. break;
  219. case WM_DESTROY: //销毁
  220. PostQuitMessage(0);
  221. return FALSE;
  222. //case WM_NCHITTEST: //使用下面这段可以实现非标题栏的窗口拖动
  223. // if(DefWindowProc(hWnd, message, wParam, lParam) == HTCLIENT)
  224. // return HTCAPTION;
  225. }
  226. return DefWindowProc(hWnd, message, wParam, lParam);
  227. }
  228. LONG OnSize(HWND hWnd,WORD cx,WORD cy)
  229. {
  230. static bool _bInit = false;
  231. if (!_bInit)
  232. {
  233. _bInit = true;
  234. //HWND m_hEditWnd = CreateWindow("Edit","TextEdit",WS_CHILD|WS_VISIBLE| WS_BORDER|SS_LEFT,600,0,100,100,hWnd,NULL,NULL,NULL);
  235. //ShowWindow(m_hEditWnd,SW_SHOW);
  236. }
  237. if(0 == cx || 0 == cy) //这是最小化,不用管
  238. {
  239. g_bMaxWindow = false;
  240. return 0;
  241. }
  242. g_bMaxWindow = true;
  243. // 以下实现了非矩形窗口
  244. // 用这种方式隐藏窗口标题栏,使用HGE来实现标题栏功能
  245. //RECT rcWindow;
  246. //rcWindow.left = GetSystemMetrics(SM_CXFIXEDFRAME);
  247. //rcWindow.top = GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION);
  248. //rcWindow.right = cx + GetSystemMetrics(SM_CXFIXEDFRAME);
  249. //rcWindow.bottom = cy + GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION);
  250. //HRGN hRgn = CreateRoundRectRgn(rcWindow.left,rcWindow.top,rcWindow.right,rcWindow.bottom,0,0);
  251. //SetWindowRgn(hWnd,hRgn,TRUE);
  252. //DeleteObject(hRgn);
  253. return 0;
  254. }
  255. bool InitHGE(HWND hWnd,HINSTANCE hInstance,LONG lStyle,RECT &rcWindow)
  256. {
  257. OSVERSIONINFO os_ver;
  258. SYSTEMTIME tm;
  259. MEMORYSTATUS mem_st;
  260. RECT rcClient;
  261. GetClientRect(hWnd,&rcClient);
  262. char _szLogFile[_MAX_PATH];
  263. sprintf(_szLogFile,"%s.Log",GAME_ENAME);
  264. g_pHGE = (HGE_Impl*)hgeCreate(HGE_VERSION);
  265. g_pHGE->System_SetState(HGE_INIFILE, "config.ini");
  266. g_pHGE->System_SetState(HGE_LOGFILE, _szLogFile);
  267. g_pHGE->System_SetState(HGE_FRAMEFUNC, FrameFunc);
  268. g_pHGE->System_SetState(HGE_RENDERFUNC, RenderFunc);
  269. g_pHGE->System_SetState(HGE_TITLE, GAME_CNAME);
  270. g_pHGE->System_SetState(HGE_SCREENWIDTH, CLIENT_WINTH);
  271. g_pHGE->System_SetState(HGE_SCREENHEIGHT, CLIENT_HEIGHT);
  272. g_pHGE->System_SetState(HGE_SCREENBPP, 32);
  273. g_pHGE->System_SetState(HGE_HIDEMOUSE, false);
  274. g_pHGE->System_SetState(HGE_DONTSUSPEND, true);
  275. g_pHGE->System_SetState(HGE_WINDOWED, true);
  276. g_pHGE->System_SetState(HGE_USESOUND,false);
  277. g_pHGE->System_SetState(HGE_FPS,100);
  278. g_pHGE->System_Log("HGE Started..n");
  279. g_pHGE->System_Log("HGE version: %X.%X", HGE_VERSION>>8, HGE_VERSION & 0xFF);
  280. GetLocalTime(&tm);
  281. g_pHGE->System_Log("Date: %02d.%02d.%d, %02d:%02d:%02dn", tm.wDay, tm.wMonth, tm.wYear, tm.wHour, tm.wMinute, tm.wSecond);
  282. g_pHGE->System_Log("Application: %s",g_pHGE->szWinTitle);
  283. os_ver.dwOSVersionInfoSize=sizeof(os_ver);
  284. GetVersionEx(&os_ver);
  285. g_pHGE->System_Log("OS: Windows %ld.%ld.%ld",os_ver.dwMajorVersion,os_ver.dwMinorVersion,os_ver.dwBuildNumber);
  286. GlobalMemoryStatus(&mem_st);
  287. g_pHGE->System_Log("Memory: %ldK total, %ldK freen",mem_st.dwTotalPhys/1024L,mem_st.dwAvailPhys/1024L);
  288. timeBeginPeriod(1);
  289. g_pHGE->Random_Seed();
  290. g_pHGE->hwnd = hWnd;
  291. g_pHGE->hInstance = hInstance;
  292. g_pHGE->rectW = rcWindow;
  293. g_pHGE->styleW = lStyle;
  294. g_pHGE->rectFS = rcWindow;
  295. g_pHGE->styleFS = WS_POPUP | WS_VISIBLE; //WS_POPUP
  296. if(!g_pHGE->G_GfxInit())
  297. {
  298. g_pHGE->System_Shutdown(); 
  299. return false;
  300. }
  301. if(!g_pHGE->G_SoundInit())
  302. {
  303. g_pHGE->System_Shutdown(); 
  304. return false;
  305. }
  306. g_pHGE->System_Log("Init done.n");
  307. g_pHGE->fTime=0.0f;
  308. g_pHGE->t0=g_pHGE->t0fps=timeGetTime();
  309. g_pHGE->dt=g_pHGE->cfps=0;
  310. g_pHGE->nFPS=0;
  311. return InitGame(hWnd);
  312. }
  313. bool InitGame(HWND hWnd)
  314. {
  315.     char *_pszFontFile = g_pHGE->Ini_GetString("RESOURCE","FONTEN","");
  316. g_pFont = new hgeFont(_pszFontFile);
  317. if (!g_pFont)
  318. {
  319. g_pHGE->System_Log("加载英文字体 %s 错误!",_pszFontFile);
  320. return false;
  321. }
  322. char *_pszFontCNFile = g_pHGE->Ini_GetString("RESOURCE","FONTCN","");
  323. g_pFontCN = new hgeFontCN(_pszFontCNFile);
  324. if (!g_pFontCN)
  325. {
  326. g_pHGE->System_Log("加载中文字体 %s 错误",_pszFontCNFile);
  327. return false;
  328. }
  329. g_pGameFrame = new GameFrame;
  330. if (!g_pGameFrame)
  331. {
  332. g_pHGE->System_Log("GameFrame 创建失败!");
  333. return false;
  334. }
  335. if (!g_pGameFrame->InitGameFrame())
  336. {
  337. g_pHGE->System_Log("GameFrame 初始化失败!");
  338. return false;
  339. }
  340. return true;
  341. }
  342. bool ExitGame()
  343. {
  344. g_pGameFrame->ExitGameFrame();
  345. DELETE_OBJECT(g_pFont)
  346. DELETE_OBJECT(g_pFontCN)
  347. DELETE_OBJECT(g_pGameFrame)
  348. g_pHGE->System_Shutdown();
  349. return true;
  350. }
  351. bool LoopGame(HWND hWnd)
  352. {
  353. POINT pt;
  354. RECT rc;
  355. GetCursorPos(&pt);
  356. GetClientRect(hWnd, &rc);
  357. MapWindowPoints(hWnd, NULL, (LPPOINT)&rc, 2);
  358. if(g_pHGE->bCaptured || (PtInRect(&rc, pt) && WindowFromPoint(pt)==hWnd))
  359. g_pHGE->bMouseOver=true;
  360. else
  361. g_pHGE->bMouseOver=false;
  362. if(g_pHGE->bActive || g_pHGE->bDontSuspend)
  363. {
  364. do
  365. {
  366. g_pHGE->dt=timeGetTime() - g_pHGE->t0;
  367. } while(g_pHGE->dt < 1);
  368. if(g_pHGE->dt >= g_pHGE->nFixedDelta)
  369. {
  370. g_pHGE->fDeltaTime = g_pHGE->dt/1000.0f;
  371. if(g_pHGE->fDeltaTime > 0.2f)
  372. {
  373. if(g_pHGE->nFixedDelta)
  374. g_pHGE->fDeltaTime=g_pHGE->nFixedDelta/1000.0f;
  375. else
  376. g_pHGE->fDeltaTime=0.01f;
  377. }
  378. g_pHGE->fTime+=g_pHGE->fDeltaTime;
  379. g_pHGE->t0=timeGetTime();
  380. if(g_pHGE->t0-g_pHGE->t0fps < 1000)
  381. g_pHGE->cfps++;
  382. else
  383. {
  384. g_pHGE->nFPS=g_pHGE->cfps;
  385. g_pHGE->cfps=0;
  386. g_pHGE->t0fps=g_pHGE->t0;
  387. }
  388. if(g_pHGE->procFrameFunc())
  389. return true;
  390. if(g_pHGE->procRenderFunc)
  391. g_pHGE->procRenderFunc();
  392. g_pHGE->G_ClearQueue();
  393. if(!g_pHGE->bWindowed && g_pHGE->nHGEFPS==HGEFPS_VSYNC)
  394. Sleep(10);
  395. }
  396. else
  397. {
  398. if(g_pHGE->nFixedDelta && g_pHGE->dt+3 < g_pHGE->nFixedDelta)
  399. Sleep(10);
  400. }
  401. }
  402. else
  403. Sleep(10);
  404. return true;
  405. }
  406. bool FrameFunc()
  407. {
  408. return g_pGameFrame->GameFrameRun();
  409. }
  410. bool RenderFunc()
  411. {
  412. if (!g_bMaxWindow)
  413. return false;
  414. g_pHGE->Gfx_BeginScene();
  415. g_pHGE->Gfx_Clear(0);
  416. bool bRet = g_pGameFrame->GameRenderRun();
  417. g_pHGE->Gfx_EndScene();
  418. return bRet;
  419. }