HGE引擎适用于MFC的修改

 打开hge181/src/core/system.cpp

找到System_Initiate()函数,可以看见里面有段代码是用于创建窗口。

 

// 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);shi

 

这段代码是创建窗口。

但因为我们是创建基于MFC窗口消息的图形,其窗口创建MFC已经帮其做好了。所以需要将其删除。

 

 

 

再找到SystemSetStatusHwnd函数,修改代码:

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; } }

 

这样做是方便我们设置渲染的窗口。比如对话框之类的。

 

 

 

接下来在HGE_IMP.H文件中定义成员 

 

 RECT flipSrcRect; RECT flipDstRect;

 

并将其初始化为0.

打开hge181/src/core/graphics.cpp文件修改Gfx_EndScene函数修改代码如下:

 

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顾名思义分别设置渲染子窗口区域,渲染整个屏幕区域。

 

 

并添加一个接口

 

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绘制模式。我们在这里的pD3DDevice->Present(&flipSrcRect, &flipDstRect, NULL,NULL);函数的作用是为了绘制到指定的我们的渲染区域。

 

 

 

调用过程如下。创建对话框,在初始化对话框上面初始化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);//这里的接口稍稍做一点变动,涉及到后面四个参数的操作可以屏蔽掉 //因为这步就可以重新设置图片大小 m_pic->SetTextureRect( 0, 0, hge->Texture_GetWidth(m_htexture), hge->Texture_GetHeight(m_htexture) );

 

 后面创建图片并具体渲染,我们可以模仿hgeSprite这种方式创建。然后在OnTimer里面进行渲染。

 

void CDxTestDlg::OnPaint() { /// render to target m_render->Gfx_Clear(0); m_render->Gfx_BeginScene(); m_pic->RenderEx(100, 100, 0.0f, 0.5, 0.5); m_render->Gfx_EndScene(true); } 

你可能感兴趣的:(HGE2D引擎)