HGE引擎适用于MFC的修改
?打開(kāi)hge181/src/core/system.cpp
找到System_Initiate()函數(shù),可以看見(jiàn)里面有段代碼是用于創(chuàng)建窗口。
?
// Register window classwinclass.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 windowwidth=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_POPUPif(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);elsehwnd = 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);shi
?
這段代碼是創(chuàng)建窗口。
但因?yàn)槲覀兪莿?chuàng)建基于MFC窗口消息的圖形,其窗口創(chuàng)建MFC已經(jīng)幫其做好了。所以需要將其刪除。
?
?
?
再找到SystemSetStatusHwnd函數(shù),修改代碼:
void CALL HGE_Impl::System_SetStateHwnd(hgeHwndState state, HWND value) {switch(state){case HGE_HWND: if(!hwnd) hwnd = value; break; case HGE_HWNDPARENT: if(!hwnd) hwndParent=value; break;} }
?
這樣做是方便我們?cè)O(shè)置渲染的窗口。比如對(duì)話(huà)框之類(lèi)的。
?
?
?
接下來(lái)在HGE_IMP.H文件中定義成員?
?
?RECT flipSrcRect; RECT flipDstRect;
?
并將其初始化為0.
打開(kāi)hge181/src/core/graphics.cpp文件修改Gfx_EndScene函數(shù)修改代碼如下:
?
void CALL HGE_Impl::Gfx_EndScene(bool use_fliRect /*= 0*/) {_render_batch(true);pD3DDevice->EndScene();if(!pCurTarget){if (!use_fliRect){pD3DDevice->Present( NULL, NULL, NULL, NULL );return;}pD3DDevice->Present(&flipSrcRect, &flipDstRect, NULL,NULL);} }
?
修改_GfxInit()在?pD3D=Direct3DCreate8(120); // D3D_SDK_VERSION這句代碼前面添加如下代碼:
if( bWindowed ){int width=nScreenWidth + GetSystemMetrics(SM_CXFIXEDFRAME)*2;int 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;if(!hwnd){_PostError("hwnd is NULL");return false;}styleW=GetWindowLong(hwnd,GWL_STYLE);}else{rectFS.left=0;rectFS.top=0;rectFS.right=nScreenWidth;rectFS.bottom=nScreenHeight;styleFS = WS_POPUP|WS_VISIBLE;}
?
rectW和rectFS顧名思義分別設(shè)置渲染子窗口區(qū)域,渲染整個(gè)屏幕區(qū)域。
?
?
并添加一個(gè)接口
?
void CALL HGE_Impl::Gfx_SetFlipRect(int top, int left, int right, int bottom) {flipSrcRect.top = top;flipSrcRect.left = left;flipSrcRect.right =right;flipSrcRect.bottom = bottom;flipDstRect.top = top;flipDstRect.left = left;flipDstRect.right =right;flipDstRect.bottom = bottom; }
這幾句代碼的作用是為了兼容MFC繪制模式。我們?cè)谶@里的pD3DDevice->Present(&flipSrcRect, &flipDstRect, NULL,NULL);函數(shù)的作用是為了繪制到指定的我們的渲染區(qū)域。
?
?
?
調(diào)用過(guò)程如下。創(chuàng)建對(duì)話(huà)框,在初始化對(duì)話(huà)框上面初始化Render
具體代碼如下:
CRect rect;HWND hwnd = GetDlgItem(IDC_STATIC_RENDER)->GetSafeHwnd();GetDlgItem(IDC_STATIC_RENDER)->GetClientRect(&rect);hge = hgeCreate(HGE_VERSION);hge->System_SetState(HGE_SCREENWIDTH, rect.Width());hge->System_SetState(HGE_SCREENHEIGHT, rect.Height());hge->System_SetState(HGE_WINDOWED, true);//hge->System_SetState(HGE_HWNDPARENT, GetSafeHwnd());hge->System_SetState(HGE_HWND,hwnd);hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);hge->System_SetState(HGE_FPS,60);hge->System_SetState(HGE_DONTSUSPEND, true);hge->System_SetState(HGE_TEXTUREFILTER, false);hge->System_SetState(HGE_SCREENBPP, 32);hge->System_SetState(HGE_LOGFILE, "log.txt");hge->System_Initiate();hge->Gfx_SetFlipRect(25, 25, rect.Width()-50, rect.Height()-50);m_htexture = hge->Texture_Load("minimap.dds");m_pic = new hgeSprite(hge, m_htexture);//這里的接口稍稍做一點(diǎn)變動(dòng),涉及到后面四個(gè)參數(shù)的操作可以屏蔽掉//因?yàn)檫@步就可以重新設(shè)置圖片大小m_pic->SetTextureRect( 0, 0, hge->Texture_GetWidth(m_htexture), hge->Texture_GetHeight(m_htexture) );
?
?后面創(chuàng)建圖片并具體渲染,我們可以模仿hgeSprite這種方式創(chuàng)建。然后在OnTimer里面進(jìn)行渲染。
?
void CDxTestDlg::OnPaint() {/// render to targetm_render->Gfx_Clear(0);m_render->Gfx_BeginScene();m_pic->RenderEx(100, 100, 0.0f, 0.5, 0.5);m_render->Gfx_EndScene(true); }?
總結(jié)
以上是生活随笔為你收集整理的HGE引擎适用于MFC的修改的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java反射基础(一)--Class对象
- 下一篇: 调试某游戏副本中的加亮提示信息思路