win32中使用cegui的代碼

  1. #include <CEGUI.h> 
  2. #include <RendererModules/directx9GUIRenderer/d3d9renderer.h> 
  3. #include <d3d9.h> 
  4. #define APPLICATION_NAME "My CEGUI Application" 
  5. //----------------------------------------------------------------------------// 
  6. // function prototypes 
  7. bool doWin32Events(bool& idle); 
  8. bool resetDirect3D(void); 
  9. LRESULT CALLBACK myWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); 
  10. //----------------------------------------------------------------------------// 
  11. // global vars 
  12. HWND myWindow; 
  13. LPDIRECT3D9 myD3D; 
  14. D3DPRESENT_PARAMETERS myPPars; 
  15. D3DDISPLAYMODE myDM; 
  16. LPDIRECT3DDEVICE9 my3DDevice; 
  17. CEGUI::DirectX9Renderer* myRenderer; 
  18. //----------------------------------------------------------------------------// 
  19. // app entry point 
  20. int main(int argc, char** argv) 
  21.     WNDCLASS    wndClass;       // structure used to register window class 
  22.     // Initialise WNDCLASS structure. 
  23.     wndClass.style          = 0; 
  24.     wndClass.lpfnWndProc    = myWndProc; 
  25.     wndClass.cbClsExtra     = 0; 
  26.     wndClass.cbWndExtra     = 0; 
  27.     wndClass.hInstance      = GetModuleHandle(0); 
  28.     wndClass.hIcon          = LoadIcon(0, IDI_WINLOGO); 
  29.     wndClass.hCursor        = LoadCursor(0, IDC_ARROW); 
  30.     wndClass.hbrBackground  = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); 
  31.     wndClass.lpszMenuName   = 0; 
  32.     wndClass.lpszClassName  = APPLICATION_NAME; 
  33.     // register class. 
  34.     RegisterClass(&wndClass) 
  35.     // create new window 
  36.     myWindow = CreateWindow(APPLICATION_NAME, APPLICATION_NAME, WS_OVERLAPPEDWINDOW, 
  37.                             0, 0, 800, 600, 0, 0, GetModuleHandle(0), 0); 
  38.     // complete window initialisation 
  39.     ShowWindow(myWindow, SW_NORMAL); 
  40.     UpdateWindow(myWindow); 
  41.     // create main D3D object 
  42.     myD3D = Direct3DCreate9(D3D_SDK_VERSION); 
  43.     // get info about display mode on default adapter 
  44.     myD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &myDM); 
  45.     // fill in presentation params structure 
  46.     ZeroMemory(&myPPars, sizeof(myPPars)); 
  47.     myPPars.BackBufferFormat     = myDM.Format; 
  48.     myPPars.hDeviceWindow        = myWindow; 
  49.     myPPars.SwapEffect           = D3DSWAPEFFECT_DISCARD; 
  50.     myPPars.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; 
  51.     myPPars.Windowed             = true
  52.     // create device 
  53.     myD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, myWindow, 
  54.                                    D3DCREATE_SOFTWARE_VERTEXPROCESSING, myPPars, 
  55.                                    &my3DDevice); 
  56.     // create CEGUI renderer 
  57.     myRenderer = new CEGUI::DirectX9Renderer(my3DDevice); 
  58.     // initialise CEGUI system 
  59.     new CEGUI::System(myRenderer); 
  60.     // load in the scheme file, which auto-loads the TaharezLook imageset 
  61.     CEGUI::SchemeManager::getSingleton().loadScheme( "TaharezLook.scheme" ); 
  62.     // load in a font.  The first font loaded automatically becomes the default font. 
  63.     if(! CEGUI::FontManager::getSingleton().isFontPresent( "Commonwealth-10" ) ) 
  64.     CEGUI::FontManager::getSingleton().createFont( "Commonwealth-10.font" ); 
  65.     System::getSingleton().setDefaultFont( "Commonwealth-10" ); 
  66.     System::getSingleton().setDefaultMouseCursor( "TaharezLook""MouseArrow" ); 
  67.     WindowManager& wmgr = WindowManager::getSingleton(); 
  68.     Window* myRoot = wmgr.createWindow( "DefaultWindow""root" ); 
  69.     System::getSingleton().setGUISheet( myRoot ); 
  70.     FrameWindow* mainwnd = (FrameWindow*)wmgr.createWindow( "TaharezLook/FrameWindow""testWindow" ); 
  71.     myRoot->addChildWindow( mainwnd ); 
  72.     mainwnd->setText( "Hello World!" ); 
  73.     // main windows type message loop 
  74.     bool idle; 
  75.     HRESULT coop; 
  76.     while (doWin32Events(idle)) 
  77.     { 
  78.         if (idle) 
  79.         { 
  80.             // handle D3D lost device stuff 
  81.             coop = my3DDevice->TestCooperativeLevel(); 
  82.             if (coop == D3DERR_DEVICELOST) 
  83.             { 
  84.                 Sleep(500); 
  85.                 continue
  86.             } 
  87.             else if (coop == D3DERR_DEVICENOTRESET) 
  88.             { 
  89.                 if (!resetDirect3D()) 
  90.                     continue
  91.             } 
  92.             if (FAILED(my3DDevice->BeginScene())) 
  93.                 continue
  94.             // draw display 
  95.             my3DDevice->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); 
  96.             CEGUI::System::getSingleton().renderGUI(); 
  97.             my3DDevice->EndScene(); 
  98.             my3DDevice->Present(0, 0, 0, 0); 
  99.         } 
  100.         // check if the application is quitting, and break the loop next time 
  101.         // around if so. 
  102.         if (isQuitting()) 
  103.             PostQuitMessage(0); 
  104.     } 
  105.     return 0; 
  106. //----------------------------------------------------------------------------// 
  107. bool doWin32Events(bool& idle) 
  108.     MSG msg; 
  109.     if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 
  110.     { 
  111.         if (msg.message == WM_QUIT) 
  112.             return false
  113.         TranslateMessage(&msg); 
  114.         DispatchMessage(&msg); 
  115.         idle = false
  116.     } 
  117.     else 
  118.         idle = true
  119.     return true
  120. //----------------------------------------------------------------------------// 
  121. bool resetDirect3D(void
  122.     // perform ops needed prior to reset 
  123.     myRenderer->preD3DReset(); 
  124.     if (SUCCEEDED(my3DDevice->Reset(myPPars))) 
  125.     { 
  126.         // re-build stuff now reset has been done. 
  127.         myRenderer->postD3DReset(); 
  128.         return true
  129.     } 
  130.     return false
  131. //----------------------------------------------------------------------------// 
  132. LRESULT CALLBACK myWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
  133.     switch(message) 
  134.     { 
  135.     case WM_CHAR: 
  136.         CEGUI::System::getSingleton().injectChar((CEGUI::utf32)wParam); 
  137.         break
  138.     case WM_MOUSEMOVE: 
  139.         CEGUI::System::getSingleton().injectMousePosition((float)(LOWORD(lParam)), (float)(HIWORD(lParam))); 
  140.         break
  141.     case WM_LBUTTONDOWN: 
  142.         CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton); 
  143.         break
  144.     case WM_LBUTTONUP: 
  145.         CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton); 
  146.         break
  147.     case WM_RBUTTONDOWN: 
  148.         CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton); 
  149.         break
  150.     case WM_RBUTTONUP: 
  151.         CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton); 
  152.         break
  153.     case WM_MBUTTONDOWN: 
  154.         CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton); 
  155.         break
  156.     case WM_MBUTTONUP: 
  157.         CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton); 
  158.         break
  159.     case 0x020A: // WM_MOUSEWHEEL: 
  160.         CEGUI::System::getSingleton().injectMouseWheelChange(static_cast<float>((short)HIWORD(wParam)) / static_cast<float>(120)); 
  161.         break
  162.     case WM_DESTROY: 
  163.         PostQuitMessage(0); 
  164.         break
  165.     case WM_PAINT: 
  166.     { 
  167.         HDC         hDC; 
  168.         PAINTSTRUCT ps; 
  169.         hDC = BeginPaint(hWnd, &ps); 
  170.         EndPaint(hWnd, &ps); 
  171.         break
  172.     } 
  173.     default
  174.         return(DefWindowProc(hWnd, message, wParam, lParam)); 
  175.         break
  176.     } 
  177.     return 0; 
發佈了18 篇原創文章 · 獲贊 1 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章