- #include <CEGUI.h>
- #include <RendererModules/directx9GUIRenderer/d3d9renderer.h>
- #include <d3d9.h>
- #define APPLICATION_NAME "My CEGUI Application"
- //----------------------------------------------------------------------------//
- // function prototypes
- bool doWin32Events(bool& idle);
- bool resetDirect3D(void);
- LRESULT CALLBACK myWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
- //----------------------------------------------------------------------------//
- // global vars
- HWND myWindow;
- LPDIRECT3D9 myD3D;
- D3DPRESENT_PARAMETERS myPPars;
- D3DDISPLAYMODE myDM;
- LPDIRECT3DDEVICE9 my3DDevice;
- CEGUI::DirectX9Renderer* myRenderer;
- //----------------------------------------------------------------------------//
- // app entry point
- int main(int argc, char** argv)
- {
- WNDCLASS wndClass; // structure used to register window class
- // Initialise WNDCLASS structure.
- wndClass.style = 0;
- wndClass.lpfnWndProc = myWndProc;
- wndClass.cbClsExtra = 0;
- wndClass.cbWndExtra = 0;
- wndClass.hInstance = GetModuleHandle(0);
- wndClass.hIcon = LoadIcon(0, IDI_WINLOGO);
- wndClass.hCursor = LoadCursor(0, IDC_ARROW);
- wndClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
- wndClass.lpszMenuName = 0;
- wndClass.lpszClassName = APPLICATION_NAME;
- // register class.
- RegisterClass(&wndClass)
- // create new window
- myWindow = CreateWindow(APPLICATION_NAME, APPLICATION_NAME, WS_OVERLAPPEDWINDOW,
- 0, 0, 800, 600, 0, 0, GetModuleHandle(0), 0);
- // complete window initialisation
- ShowWindow(myWindow, SW_NORMAL);
- UpdateWindow(myWindow);
- // create main D3D object
- myD3D = Direct3DCreate9(D3D_SDK_VERSION);
- // get info about display mode on default adapter
- myD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &myDM);
- // fill in presentation params structure
- ZeroMemory(&myPPars, sizeof(myPPars));
- myPPars.BackBufferFormat = myDM.Format;
- myPPars.hDeviceWindow = myWindow;
- myPPars.SwapEffect = D3DSWAPEFFECT_DISCARD;
- myPPars.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
- myPPars.Windowed = true;
- // create device
- myD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, myWindow,
- D3DCREATE_SOFTWARE_VERTEXPROCESSING, myPPars,
- &my3DDevice);
- // create CEGUI renderer
- myRenderer = new CEGUI::DirectX9Renderer(my3DDevice);
- // initialise CEGUI system
- new CEGUI::System(myRenderer);
- // load in the scheme file, which auto-loads the TaharezLook imageset
- CEGUI::SchemeManager::getSingleton().loadScheme( "TaharezLook.scheme" );
- // load in a font. The first font loaded automatically becomes the default font.
- if(! CEGUI::FontManager::getSingleton().isFontPresent( "Commonwealth-10" ) )
- CEGUI::FontManager::getSingleton().createFont( "Commonwealth-10.font" );
- System::getSingleton().setDefaultFont( "Commonwealth-10" );
- System::getSingleton().setDefaultMouseCursor( "TaharezLook", "MouseArrow" );
- WindowManager& wmgr = WindowManager::getSingleton();
- Window* myRoot = wmgr.createWindow( "DefaultWindow", "root" );
- System::getSingleton().setGUISheet( myRoot );
- FrameWindow* mainwnd = (FrameWindow*)wmgr.createWindow( "TaharezLook/FrameWindow", "testWindow" );
- myRoot->addChildWindow( mainwnd );
- mainwnd->setText( "Hello World!" );
- // main windows type message loop
- bool idle;
- HRESULT coop;
- while (doWin32Events(idle))
- {
- if (idle)
- {
- // handle D3D lost device stuff
- coop = my3DDevice->TestCooperativeLevel();
- if (coop == D3DERR_DEVICELOST)
- {
- Sleep(500);
- continue;
- }
- else if (coop == D3DERR_DEVICENOTRESET)
- {
- if (!resetDirect3D())
- continue;
- }
- if (FAILED(my3DDevice->BeginScene()))
- continue;
- // draw display
- my3DDevice->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
- CEGUI::System::getSingleton().renderGUI();
- my3DDevice->EndScene();
- my3DDevice->Present(0, 0, 0, 0);
- }
- // check if the application is quitting, and break the loop next time
- // around if so.
- if (isQuitting())
- PostQuitMessage(0);
- }
- return 0;
- }
- //----------------------------------------------------------------------------//
- bool doWin32Events(bool& idle)
- {
- MSG msg;
- if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
- {
- if (msg.message == WM_QUIT)
- return false;
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- idle = false;
- }
- else
- idle = true;
- return true;
- }
- //----------------------------------------------------------------------------//
- bool resetDirect3D(void)
- {
- // perform ops needed prior to reset
- myRenderer->preD3DReset();
- if (SUCCEEDED(my3DDevice->Reset(myPPars)))
- {
- // re-build stuff now reset has been done.
- myRenderer->postD3DReset();
- return true;
- }
- return false;
- }
- //----------------------------------------------------------------------------//
- LRESULT CALLBACK myWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch(message)
- {
- case WM_CHAR:
- CEGUI::System::getSingleton().injectChar((CEGUI::utf32)wParam);
- break;
- case WM_MOUSEMOVE:
- CEGUI::System::getSingleton().injectMousePosition((float)(LOWORD(lParam)), (float)(HIWORD(lParam)));
- break;
- case WM_LBUTTONDOWN:
- CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);
- break;
- case WM_LBUTTONUP:
- CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton);
- break;
- case WM_RBUTTONDOWN:
- CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);
- break;
- case WM_RBUTTONUP:
- CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton);
- break;
- case WM_MBUTTONDOWN:
- CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);
- break;
- case WM_MBUTTONUP:
- CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton);
- break;
- case 0x020A: // WM_MOUSEWHEEL:
- CEGUI::System::getSingleton().injectMouseWheelChange(static_cast<float>((short)HIWORD(wParam)) / static_cast<float>(120));
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_PAINT:
- {
- HDC hDC;
- PAINTSTRUCT ps;
- hDC = BeginPaint(hWnd, &ps);
- EndPaint(hWnd, &ps);
- break;
- }
- default:
- return(DefWindowProc(hWnd, message, wParam, lParam));
- break;
- }
- return 0;
- }
win32中使用cegui的代码
最新推荐文章于 2015-03-27 10:22:08 发布