main.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include <Rocket/Core.h>
  28. #include <Rocket/Debugger.h>
  29. #include <Input.h>
  30. #include <Shell.h>
  31. #include "RenderInterfaceDirectX.h"
  32. #include <d3d9.h>
  33. #include <d3dx9.h>
  34. static LPDIRECT3D9 g_pD3D = NULL;
  35. static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
  36. static Rocket::Core::Context* context = NULL;
  37. bool InitialiseDirectX()
  38. {
  39. g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
  40. if (g_pD3D == NULL)
  41. return false;
  42. D3DPRESENT_PARAMETERS d3dpp;
  43. ZeroMemory(&d3dpp, sizeof(d3dpp));
  44. d3dpp.Windowed = TRUE;
  45. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  46. d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  47. if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
  48. D3DDEVTYPE_HAL,
  49. (HWND) Shell::GetWindowHandle(),
  50. D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  51. &d3dpp,
  52. &g_pd3dDevice)))
  53. {
  54. return false;
  55. }
  56. // Set up an orthographic projection.
  57. D3DXMATRIX projection;
  58. D3DXMatrixOrthoOffCenterLH(&projection, 0, 1024, 768, 0, -1, 1);
  59. g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &projection);
  60. // Switch to clockwise culling instead of counter-clockwise culling; Rocket generates counter-clockwise geometry,
  61. // so you can either reverse the culling mode when Rocket is rendering, or reverse the indices in the render
  62. // interface.
  63. g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
  64. // Enable alpha-blending for Rocket.
  65. g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  66. g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  67. g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  68. // Set up the texture stage states for the diffuse texture.
  69. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  70. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  71. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  72. g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
  73. g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  74. g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
  75. g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
  76. // Disable lighting for Rocket.
  77. g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
  78. return true;
  79. }
  80. void ShutdownDirectX()
  81. {
  82. if (g_pd3dDevice != NULL)
  83. {
  84. // Release the last resources we bound to the device.
  85. g_pd3dDevice->SetTexture(0, NULL);
  86. g_pd3dDevice->SetStreamSource(0, NULL, 0, 0);
  87. g_pd3dDevice->SetIndices(NULL);
  88. g_pd3dDevice->Release();
  89. g_pd3dDevice = NULL;
  90. }
  91. if (g_pD3D != NULL)
  92. {
  93. g_pD3D->Release();
  94. g_pD3D = NULL;
  95. }
  96. }
  97. void GameLoop()
  98. {
  99. g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  100. g_pd3dDevice->BeginScene();
  101. context->Update();
  102. context->Render();
  103. g_pd3dDevice->EndScene();
  104. g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
  105. }
  106. #if defined ROCKET_PLATFORM_WIN32
  107. #include <windows.h>
  108. int APIENTRY WinMain(HINSTANCE ROCKET_UNUSED(instance_handle), HINSTANCE ROCKET_UNUSED(previous_instance_handle), char* ROCKET_UNUSED(command_line), int ROCKET_UNUSED(command_show))
  109. #else
  110. int main(int ROCKET_UNUSED(argc), char** ROCKET_UNUSED(argv))
  111. #endif
  112. {
  113. // Generic OS initialisation, creates a window and does not attach OpenGL.
  114. if (!Shell::Initialise("../Samples/basic/directx/") ||
  115. !Shell::OpenWindow("DirectX Sample", false))
  116. {
  117. Shell::Shutdown();
  118. return -1;
  119. }
  120. // DirectX initialisation.
  121. if (!InitialiseDirectX())
  122. {
  123. Shell::CloseWindow();
  124. Shell::Shutdown();
  125. return -1;
  126. }
  127. // Install our DirectX render interface into Rocket.
  128. RenderInterfaceDirectX directx_renderer(g_pD3D, g_pd3dDevice);
  129. Rocket::Core::SetRenderInterface(&directx_renderer);
  130. ShellSystemInterface system_interface;
  131. Rocket::Core::SetSystemInterface(&system_interface);
  132. Rocket::Core::Initialise();
  133. // Create the main Rocket context and set it on the shell's input layer.
  134. context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(1024, 768));
  135. if (context == NULL)
  136. {
  137. Rocket::Core::Shutdown();
  138. Shell::Shutdown();
  139. return -1;
  140. }
  141. Rocket::Debugger::Initialise(context);
  142. Input::SetContext(context);
  143. Shell::LoadFonts("../../assets/");
  144. // Load and show the tutorial document.
  145. Rocket::Core::ElementDocument* document = context->LoadDocument("../../assets/demo.rml");
  146. if (document != NULL)
  147. {
  148. document->Show();
  149. document->RemoveReference();
  150. }
  151. Shell::EventLoop(GameLoop);
  152. // Shutdown Rocket.
  153. context->RemoveReference();
  154. Rocket::Core::Shutdown();
  155. ShutdownDirectX();
  156. Shell::CloseWindow();
  157. Shell::Shutdown();
  158. return 0;
  159. }