main.cpp 4.2 KB

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