main.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <Rocket/Core.h>
  2. #include <Rocket/Debugger.h>
  3. #include <Input.h>
  4. #include <Shell.h>
  5. #include "RenderInterfaceDirectX10.h"
  6. // Because we're a windows app
  7. #include <windows.h>
  8. // For _T unicode/mbcs macro
  9. #include <tchar.h>
  10. //Our device for this sample
  11. static ID3D10Device * pD3D10Device=NULL;
  12. //Swap Chain
  13. static IDXGISwapChain * pSwapChain=NULL;
  14. //Render Target
  15. static ID3D10RenderTargetView * pRenderTargetView=NULL;
  16. static Rocket::Core::Context* context = NULL;
  17. bool InitialiseDirectX()
  18. {
  19. //get the size of the window
  20. RECT windowRect;
  21. GetClientRect((HWND) Shell::GetWindowHandle(),&windowRect);
  22. UINT width=windowRect.right-windowRect.left;
  23. UINT height=windowRect.bottom-windowRect.top;
  24. //put the device into debug if we are in a debug build
  25. UINT createDeviceFlags=0;
  26. #ifdef _DEBUG
  27. createDeviceFlags|=D3D10_CREATE_DEVICE_DEBUG;
  28. #endif
  29. //Setup swap chain
  30. DXGI_SWAP_CHAIN_DESC sd;
  31. ZeroMemory( &sd, sizeof( sd ) );
  32. sd.BufferCount=1;
  33. sd.OutputWindow = (HWND) Shell::GetWindowHandle();
  34. sd.Windowed = TRUE;
  35. sd.SampleDesc.Count = 1;
  36. sd.SampleDesc.Quality = 0;
  37. sd.BufferDesc.Width = width;
  38. sd.BufferDesc.Height = height;
  39. sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  40. sd.BufferDesc.RefreshRate.Numerator = 60;
  41. sd.BufferDesc.RefreshRate.Denominator = 1;
  42. sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  43. //Create device and swapchain
  44. if (FAILED(D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, D3D10_SDK_VERSION, &sd, &pSwapChain, &pD3D10Device)))
  45. {
  46. MessageBox(NULL, _T("D3D10CreateDeviceAndSwapChain failed for D3D10_DRIVER_TYPE_HARDWARE."), _T("Could not intialized DirectX 10"), MB_OK|MB_ICONERROR);
  47. return false;
  48. }
  49. //Create Render Target
  50. ID3D10Texture2D *pBackBuffer;
  51. if ( FAILED (pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D),(void**)&pBackBuffer)))
  52. {
  53. MessageBox(NULL, _T("SwapChain->GetBuffer failed."), _T("Could not intialized DirectX 10"), MB_OK|MB_ICONERROR);
  54. return false;
  55. }
  56. if (FAILED(pD3D10Device->CreateRenderTargetView( pBackBuffer, NULL, &pRenderTargetView )))
  57. {
  58. pBackBuffer->Release();
  59. MessageBox(NULL, _T("D3D10Device->CreateRenderTargetView failed."), _T("Could not intialized DirectX 10"), MB_OK|MB_ICONERROR);
  60. return false;
  61. }
  62. pBackBuffer->Release();
  63. pD3D10Device->OMSetRenderTargets(1,&pRenderTargetView,NULL);
  64. D3D10_VIEWPORT vp;
  65. vp.Width = width;
  66. vp.Height = height;
  67. vp.MinDepth = 0.0f;
  68. vp.MaxDepth = 1.0f;
  69. vp.TopLeftX = 0;
  70. vp.TopLeftY = 0;
  71. pD3D10Device->RSSetViewports( 1, &vp );
  72. return true;
  73. }
  74. void ShutdownDirectX()
  75. {
  76. if (pD3D10Device)
  77. pD3D10Device->ClearState();
  78. if (pRenderTargetView)
  79. pRenderTargetView->Release();
  80. if (pSwapChain)
  81. pSwapChain->Release();
  82. if (pD3D10Device)
  83. pD3D10Device->Release();
  84. }
  85. void GameLoop()
  86. {
  87. float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f };
  88. pD3D10Device->ClearRenderTargetView( pRenderTargetView,ClearColor );
  89. context->Update();
  90. context->Render();
  91. pSwapChain->Present( 0, 0 );
  92. }
  93. int APIENTRY WinMain(HINSTANCE ROCKET_UNUSED_PARAMETER(instance_handle), HINSTANCE ROCKET_UNUSED_PARAMETER(previous_instance_handle), char* ROCKET_UNUSED_PARAMETER(command_line), int ROCKET_UNUSED_PARAMETER(command_show))
  94. {
  95. ROCKET_UNUSED(instance_handle);
  96. ROCKET_UNUSED(previous_instance_handle);
  97. ROCKET_UNUSED(command_line);
  98. ROCKET_UNUSED(command_show);
  99. // Generic OS initialisation, creates a window and does not attach OpenGL.
  100. if (!Shell::Initialise("../Samples/basic/directx/") ||
  101. !Shell::OpenWindow("DirectX 10 Sample", false))
  102. {
  103. Shell::Shutdown();
  104. return -1;
  105. }
  106. // DirectX initialisation.
  107. if (!InitialiseDirectX())
  108. {
  109. Shell::CloseWindow();
  110. Shell::Shutdown();
  111. return -1;
  112. }
  113. // Install our DirectX render interface into Rocket.
  114. RenderInterfaceDirectX10 directx_renderer(pD3D10Device,1024,768);
  115. Rocket::Core::SetRenderInterface(&directx_renderer);
  116. ShellSystemInterface system_interface;
  117. Rocket::Core::SetSystemInterface(&system_interface);
  118. Rocket::Core::Initialise();
  119. // Create the main Rocket context and set it on the shell's input layer.
  120. context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(1024, 768));
  121. if (context == NULL)
  122. {
  123. Rocket::Core::Shutdown();
  124. Shell::Shutdown();
  125. return -1;
  126. }
  127. Rocket::Debugger::Initialise(context);
  128. Input::SetContext(context);
  129. Shell::LoadFonts("../../assets/");
  130. // Load and show the tutorial document.
  131. Rocket::Core::ElementDocument* document = context->LoadDocument("../../assets/demo.rml");
  132. if (document != NULL)
  133. {
  134. document->Show();
  135. document->RemoveReference();
  136. }
  137. Shell::EventLoop(GameLoop);
  138. // Shutdown Rocket.
  139. context->RemoveReference();
  140. Rocket::Core::Shutdown();
  141. ShutdownDirectX();
  142. Shell::CloseWindow();
  143. Shell::Shutdown();
  144. return 0;
  145. }