GameClient.cpp
上传用户:maxiaolivb
上传日期:2022-06-07
资源大小:915k
文件大小:13k
- #pragma once
- #include "extern.h"
- #ifndef WM_MOUSEWHEEL
- #define WM_MOUSEWHEEL 0x020A
- #endif
- #define WINDOWS_CLASS_NAME GAME_CNAME
- #define CLIENT_WINTH 800
- #define CLIENT_HEIGHT 600
- bool g_bMaxWindow;
- char g_szWindowsTitle[] = "GameClient DEMO";
- char g_szIMEText[1024];
- // 全局变量:
- HINSTANCE g_hInstance; // 当前实例
- LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
- // 消息映射
- LONG OnSize(HWND,WORD,WORD);
- bool InitHGE(HWND hWnd,HINSTANCE hInstance,LONG lStyle,RECT &rcWindow);
- bool InitGame(HWND hWnd);
- bool ExitGame();
- bool LoopGame(HWND);
- bool FrameFunc();
- bool RenderFunc();
- // 程序入口
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = (WNDPROC)MainWndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_GAMECLIENT);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH/*BLACK_BRUSH*/);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = WINDOWS_CLASS_NAME;
- wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
- RegisterClassEx(&wcex);
- g_hInstance = hInstance;
- int nWindowWidth = CLIENT_WINTH + GetSystemMetrics(SM_CXFIXEDFRAME) * 2;
- int nWindowHeight = CLIENT_HEIGHT + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 + GetSystemMetrics(SM_CYCAPTION);
- POINT ptWindowStart;
- ptWindowStart.x = (GetSystemMetrics(SM_CXSCREEN) - nWindowWidth) / 2;
- ptWindowStart.y = (GetSystemMetrics(SM_CYSCREEN) - nWindowHeight) / 2;
- DWORD dwWindowStyle = WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE;
- HWND hWnd = CreateWindowEx(0, WINDOWS_CLASS_NAME, g_szWindowsTitle, dwWindowStyle,
- ptWindowStart.x,ptWindowStart.y, nWindowWidth, nWindowHeight,
- NULL, NULL, hInstance, NULL);
- if (!hWnd)
- return FALSE;
- ShowWindow(hWnd, nCmdShow);
- //UpdateWindow(hWnd);
- MSG msg;
- RECT rcWindow;
- rcWindow.left = ptWindowStart.x;
- rcWindow.top = ptWindowStart.y;
- rcWindow.right = ptWindowStart.x + nWindowWidth;
- rcWindow.bottom = ptWindowStart.y + nWindowHeight;
- if(InitHGE(hWnd,hInstance,dwWindowStyle,rcWindow))
- {
- g_pHGE->bActive = true;
- while (true)
- {
- if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- if(msg.message == WM_QUIT)
- break;
- TranslateMessage(&msg); //这句不能去掉,不然会收不到中文输入的回车键
- DispatchMessage(&msg);
- }
- else
- LoopGame(hWnd);
- }
- g_pHGE->G_ClearQueue();
- g_pHGE->bActive = false;
- ExitGame();
- }
- return (int) msg.wParam;
- }
- LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_IME_STARTCOMPOSITION: //开始组字
- {
- if (g_pEditCtrl)
- g_pEditCtrl->StartComposition();
- //strcpy(g_szIMEText,"");
- }
- break;
- case WM_IME_ENDCOMPOSITION: //结束组字
- {
- if (g_pEditCtrl)
- g_pEditCtrl->EndComposition();
- }
- break;
- case WM_IME_CHAR:
- {
- char szImeChar[3];
-
- if (wParam < 128)
- {
- szImeChar[0]=(char)wParam;
- szImeChar[1]=' ';
- }
- else
- {
- szImeChar[0]=(char)(wParam>>8);
- szImeChar[1]=(char)wParam;
- szImeChar[2]=' ';
- }
- if (g_pEditCtrl)
- g_pEditCtrl->OnImeCharMsg(szImeChar);
- //strcat(g_szIMEText,szImeChar);
- //SetWindowText(hWnd,g_szIMEText);
- }
- break;
- case WM_ACTIVATEAPP:
- if(g_pHGE && g_pHGE->pD3D && g_pHGE->bActive != (wParam == TRUE))
- g_pHGE->G_FocusChange(wParam == TRUE);
- return FALSE;
- case WM_SETCURSOR:
- if(g_pHGE->bActive && LOWORD(lParam)==HTCLIENT && g_pHGE->bHideMouse)
- SetCursor(NULL);
- else
- SetCursor(LoadCursor(NULL, IDC_ARROW));
- return FALSE;
- case WM_SYSKEYDOWN:
- if(wParam==VK_F4)
- {
- if(g_pHGE->procExitFunc)
- {
- if(g_pHGE->procExitFunc())
- return DefWindowProc(hWnd, message, wParam, lParam);
- else
- return FALSE;
- }
- else
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- else
- {
- g_pHGE->G_BuildEvent(INPUT_KEYDOWN, wParam, HIWORD(lParam) & 0xFF, (lParam & 0x40000000) ? HGEINP_REPEAT:0, -1, -1);
- return FALSE;
- }
- break;
- case WM_KEYDOWN:
- g_pHGE->G_BuildEvent(INPUT_KEYDOWN, wParam, HIWORD(lParam) & 0xFF, (lParam & 0x40000000) ? HGEINP_REPEAT:0, -1, -1);
- return FALSE;
- case WM_SYSKEYUP:
- g_pHGE->G_BuildEvent(INPUT_KEYUP, wParam, HIWORD(lParam) & 0xFF, 0, -1, -1);
- return FALSE;
- case WM_KEYUP:
- g_pHGE->G_BuildEvent(INPUT_KEYUP, wParam, HIWORD(lParam) & 0xFF, 0, -1, -1);
- return FALSE;
- case WM_LBUTTONDOWN:
- g_pHGE->G_BuildEvent(INPUT_MBUTTONDOWN, HGEK_LBUTTON, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
- return FALSE;
- case WM_MBUTTONDOWN:
- g_pHGE->G_BuildEvent(INPUT_MBUTTONDOWN, HGEK_MBUTTON, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
- return FALSE;
- case WM_RBUTTONDOWN:
- g_pHGE->G_BuildEvent(INPUT_MBUTTONDOWN, HGEK_RBUTTON, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
- return FALSE;
- case WM_LBUTTONDBLCLK:
- g_pHGE->G_BuildEvent(INPUT_MBUTTONDOWN, HGEK_LBUTTON, 0, HGEINP_REPEAT, LOWORDINT(lParam), HIWORDINT(lParam));
- return FALSE;
- case WM_MBUTTONDBLCLK:
- g_pHGE->G_BuildEvent(INPUT_MBUTTONDOWN, HGEK_MBUTTON, 0, HGEINP_REPEAT, LOWORDINT(lParam), HIWORDINT(lParam));
- return FALSE;
- case WM_RBUTTONDBLCLK:
- g_pHGE->G_BuildEvent(INPUT_MBUTTONDOWN, HGEK_RBUTTON, 0, HGEINP_REPEAT, LOWORDINT(lParam), HIWORDINT(lParam));
- return FALSE;
- case WM_LBUTTONUP:
- g_pHGE->G_BuildEvent(INPUT_MBUTTONUP, HGEK_LBUTTON, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
- return FALSE;
- case WM_MBUTTONUP:
- g_pHGE->G_BuildEvent(INPUT_MBUTTONUP, HGEK_MBUTTON, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
- return FALSE;
- case WM_RBUTTONUP:
- g_pHGE->G_BuildEvent(INPUT_MBUTTONUP, HGEK_RBUTTON, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
- return FALSE;
- case WM_MOUSEMOVE:
- g_pHGE->G_BuildEvent(INPUT_MOUSEMOVE, 0, 0, 0, LOWORDINT(lParam), HIWORDINT(lParam));
- return FALSE;
- case WM_SYSCOMMAND:
- if(wParam==SC_CLOSE)
- {
- if(g_pHGE->procExitFunc)
- {
- if(g_pHGE->procExitFunc())
- {
- g_pHGE->bActive=false;
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- else
- return FALSE;
- }
- else
- {
- g_pHGE->bActive=false;
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- }
- break;
- case WM_PAINT:
- ValidateRect(hWnd,NULL);
- break;
- case WM_SIZE: //窗口缩放
- if(wParam==SIZE_RESTORED)
- {
- if (g_pHGE)
- g_pHGE->G_Resize(LOWORD(lParam), HIWORD(lParam));
- }
- OnSize(hWnd,LOWORD(lParam),HIWORD(lParam));
- break;
- case WM_DESTROY: //销毁
- PostQuitMessage(0);
- return FALSE;
- //case WM_NCHITTEST: //使用下面这段可以实现非标题栏的窗口拖动
- // if(DefWindowProc(hWnd, message, wParam, lParam) == HTCLIENT)
- // return HTCAPTION;
- }
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- LONG OnSize(HWND hWnd,WORD cx,WORD cy)
- {
- static bool _bInit = false;
- if (!_bInit)
- {
- _bInit = true;
- //HWND m_hEditWnd = CreateWindow("Edit","TextEdit",WS_CHILD|WS_VISIBLE| WS_BORDER|SS_LEFT,600,0,100,100,hWnd,NULL,NULL,NULL);
- //ShowWindow(m_hEditWnd,SW_SHOW);
- }
- if(0 == cx || 0 == cy) //这是最小化,不用管
- {
- g_bMaxWindow = false;
- return 0;
- }
- g_bMaxWindow = true;
- // 以下实现了非矩形窗口
- // 用这种方式隐藏窗口标题栏,使用HGE来实现标题栏功能
- //RECT rcWindow;
- //rcWindow.left = GetSystemMetrics(SM_CXFIXEDFRAME);
- //rcWindow.top = GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION);
- //rcWindow.right = cx + GetSystemMetrics(SM_CXFIXEDFRAME);
- //rcWindow.bottom = cy + GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION);
- //HRGN hRgn = CreateRoundRectRgn(rcWindow.left,rcWindow.top,rcWindow.right,rcWindow.bottom,0,0);
- //SetWindowRgn(hWnd,hRgn,TRUE);
- //DeleteObject(hRgn);
- return 0;
- }
- bool InitHGE(HWND hWnd,HINSTANCE hInstance,LONG lStyle,RECT &rcWindow)
- {
- OSVERSIONINFO os_ver;
- SYSTEMTIME tm;
- MEMORYSTATUS mem_st;
- RECT rcClient;
- GetClientRect(hWnd,&rcClient);
- char _szLogFile[_MAX_PATH];
- sprintf(_szLogFile,"%s.Log",GAME_ENAME);
- g_pHGE = (HGE_Impl*)hgeCreate(HGE_VERSION);
- g_pHGE->System_SetState(HGE_INIFILE, "config.ini");
- g_pHGE->System_SetState(HGE_LOGFILE, _szLogFile);
- g_pHGE->System_SetState(HGE_FRAMEFUNC, FrameFunc);
- g_pHGE->System_SetState(HGE_RENDERFUNC, RenderFunc);
- g_pHGE->System_SetState(HGE_TITLE, GAME_CNAME);
- g_pHGE->System_SetState(HGE_SCREENWIDTH, CLIENT_WINTH);
- g_pHGE->System_SetState(HGE_SCREENHEIGHT, CLIENT_HEIGHT);
- g_pHGE->System_SetState(HGE_SCREENBPP, 32);
- g_pHGE->System_SetState(HGE_HIDEMOUSE, false);
- g_pHGE->System_SetState(HGE_DONTSUSPEND, true);
- g_pHGE->System_SetState(HGE_WINDOWED, true);
- g_pHGE->System_SetState(HGE_USESOUND,false);
- g_pHGE->System_SetState(HGE_FPS,100);
- g_pHGE->System_Log("HGE Started..n");
- g_pHGE->System_Log("HGE version: %X.%X", HGE_VERSION>>8, HGE_VERSION & 0xFF);
- GetLocalTime(&tm);
- g_pHGE->System_Log("Date: %02d.%02d.%d, %02d:%02d:%02dn", tm.wDay, tm.wMonth, tm.wYear, tm.wHour, tm.wMinute, tm.wSecond);
- g_pHGE->System_Log("Application: %s",g_pHGE->szWinTitle);
- os_ver.dwOSVersionInfoSize=sizeof(os_ver);
- GetVersionEx(&os_ver);
- g_pHGE->System_Log("OS: Windows %ld.%ld.%ld",os_ver.dwMajorVersion,os_ver.dwMinorVersion,os_ver.dwBuildNumber);
- GlobalMemoryStatus(&mem_st);
- g_pHGE->System_Log("Memory: %ldK total, %ldK freen",mem_st.dwTotalPhys/1024L,mem_st.dwAvailPhys/1024L);
- timeBeginPeriod(1);
- g_pHGE->Random_Seed();
- g_pHGE->hwnd = hWnd;
- g_pHGE->hInstance = hInstance;
- g_pHGE->rectW = rcWindow;
- g_pHGE->styleW = lStyle;
- g_pHGE->rectFS = rcWindow;
- g_pHGE->styleFS = WS_POPUP | WS_VISIBLE; //WS_POPUP
- if(!g_pHGE->G_GfxInit())
- {
- g_pHGE->System_Shutdown();
- return false;
- }
- if(!g_pHGE->G_SoundInit())
- {
- g_pHGE->System_Shutdown();
- return false;
- }
- g_pHGE->System_Log("Init done.n");
- g_pHGE->fTime=0.0f;
- g_pHGE->t0=g_pHGE->t0fps=timeGetTime();
- g_pHGE->dt=g_pHGE->cfps=0;
- g_pHGE->nFPS=0;
- return InitGame(hWnd);
- }
- bool InitGame(HWND hWnd)
- {
- char *_pszFontFile = g_pHGE->Ini_GetString("RESOURCE","FONTEN","");
- g_pFont = new hgeFont(_pszFontFile);
- if (!g_pFont)
- {
- g_pHGE->System_Log("加载英文字体 %s 错误!",_pszFontFile);
- return false;
- }
- char *_pszFontCNFile = g_pHGE->Ini_GetString("RESOURCE","FONTCN","");
- g_pFontCN = new hgeFontCN(_pszFontCNFile);
- if (!g_pFontCN)
- {
- g_pHGE->System_Log("加载中文字体 %s 错误",_pszFontCNFile);
- return false;
- }
- g_pGameFrame = new GameFrame;
- if (!g_pGameFrame)
- {
- g_pHGE->System_Log("GameFrame 创建失败!");
- return false;
- }
- if (!g_pGameFrame->InitGameFrame())
- {
- g_pHGE->System_Log("GameFrame 初始化失败!");
- return false;
- }
- return true;
- }
- bool ExitGame()
- {
- g_pGameFrame->ExitGameFrame();
- DELETE_OBJECT(g_pFont)
- DELETE_OBJECT(g_pFontCN)
- DELETE_OBJECT(g_pGameFrame)
- g_pHGE->System_Shutdown();
- return true;
- }
- bool LoopGame(HWND hWnd)
- {
- POINT pt;
- RECT rc;
- GetCursorPos(&pt);
- GetClientRect(hWnd, &rc);
- MapWindowPoints(hWnd, NULL, (LPPOINT)&rc, 2);
- if(g_pHGE->bCaptured || (PtInRect(&rc, pt) && WindowFromPoint(pt)==hWnd))
- g_pHGE->bMouseOver=true;
- else
- g_pHGE->bMouseOver=false;
- if(g_pHGE->bActive || g_pHGE->bDontSuspend)
- {
- do
- {
- g_pHGE->dt=timeGetTime() - g_pHGE->t0;
- } while(g_pHGE->dt < 1);
- if(g_pHGE->dt >= g_pHGE->nFixedDelta)
- {
- g_pHGE->fDeltaTime = g_pHGE->dt/1000.0f;
- if(g_pHGE->fDeltaTime > 0.2f)
- {
- if(g_pHGE->nFixedDelta)
- g_pHGE->fDeltaTime=g_pHGE->nFixedDelta/1000.0f;
- else
- g_pHGE->fDeltaTime=0.01f;
- }
- g_pHGE->fTime+=g_pHGE->fDeltaTime;
- g_pHGE->t0=timeGetTime();
- if(g_pHGE->t0-g_pHGE->t0fps < 1000)
- g_pHGE->cfps++;
- else
- {
- g_pHGE->nFPS=g_pHGE->cfps;
- g_pHGE->cfps=0;
- g_pHGE->t0fps=g_pHGE->t0;
- }
- if(g_pHGE->procFrameFunc())
- return true;
- if(g_pHGE->procRenderFunc)
- g_pHGE->procRenderFunc();
- g_pHGE->G_ClearQueue();
- if(!g_pHGE->bWindowed && g_pHGE->nHGEFPS==HGEFPS_VSYNC)
- Sleep(10);
- }
- else
- {
- if(g_pHGE->nFixedDelta && g_pHGE->dt+3 < g_pHGE->nFixedDelta)
- Sleep(10);
- }
- }
- else
- Sleep(10);
- return true;
- }
- bool FrameFunc()
- {
- return g_pGameFrame->GameFrameRun();
- }
- bool RenderFunc()
- {
- if (!g_bMaxWindow)
- return false;
- g_pHGE->Gfx_BeginScene();
- g_pHGE->Gfx_Clear(0);
- bool bRet = g_pGameFrame->GameRenderRun();
- g_pHGE->Gfx_EndScene();
- return bRet;
- }