ShellRenderInterfaceExtensionsDirectX_Win32.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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) 2014 David Wimsey
  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 "RenderInterfaceDirectX.h"
  28. #include <Rocket/Core.h>
  29. #include <d3dx9.h>
  30. void RenderInterfaceDirectX::SetContext(void *context)
  31. {
  32. m_rocket_context = context;
  33. }
  34. void RenderInterfaceDirectX::SetViewport(int width, int height)
  35. {
  36. Rocket::Core::Matrix4f rocket_projection;
  37. if(g_pd3dDevice != NULL)
  38. {
  39. D3DXMATRIX projection;
  40. D3DXMatrixOrthoOffCenterLH(&projection, 0, (float)width, (float)height, 0, -1, 1);
  41. g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &projection);
  42. rocket_projection = projection;
  43. rocket_projection = rocket_projection.Transpose();
  44. if(m_rocket_context != NULL)
  45. {
  46. ((Rocket::Core::Context*)m_rocket_context)->SetDimensions(Rocket::Core::Vector2i(width, height));
  47. ((Rocket::Core::Context*)m_rocket_context)->ProcessProjectionChange();
  48. //((Rocket::Core::Context*)m_rocket_context)->ProcessViewChange();
  49. }
  50. }
  51. }
  52. bool RenderInterfaceDirectX::AttachToNative(void *nativeWindow)
  53. {
  54. RECT clientRect;
  55. if(!GetClientRect((HWND) nativeWindow, &clientRect))
  56. {
  57. // if we can't lookup the client rect, abort, something is seriously wrong
  58. return false;
  59. }
  60. g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
  61. if (g_pD3D == NULL)
  62. return false;
  63. D3DPRESENT_PARAMETERS d3dpp;
  64. ZeroMemory(&d3dpp, sizeof(d3dpp));
  65. d3dpp.Windowed = TRUE;
  66. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  67. d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  68. g_pd3dDevice = NULL;
  69. if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
  70. D3DDEVTYPE_HAL,
  71. (HWND) nativeWindow,
  72. D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  73. &d3dpp,
  74. &g_pd3dDevice)))
  75. {
  76. this->DetachFromNative();
  77. return false;
  78. }
  79. // Set up an orthographic projection.
  80. D3DXMATRIX projection;
  81. D3DXMatrixOrthoOffCenterLH(&projection, 0, (FLOAT)clientRect.right, (FLOAT)clientRect.bottom, 0, -1, 1);
  82. g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &projection);
  83. // Switch to clockwise culling instead of counter-clockwise culling; Rocket generates counter-clockwise geometry,
  84. // so you can either reverse the culling mode when Rocket is rendering, or reverse the indices in the render
  85. // interface.
  86. g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
  87. // Enable alpha-blending for Rocket.
  88. g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  89. g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  90. g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  91. // Set up the texture stage states for the diffuse texture.
  92. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  93. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  94. g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  95. g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
  96. g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  97. g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
  98. g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
  99. // Disable lighting for Rocket.
  100. g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
  101. return true;
  102. }
  103. void RenderInterfaceDirectX::DetachFromNative()
  104. {
  105. if (g_pd3dDevice != NULL)
  106. {
  107. // Release the last resources we bound to the device.
  108. g_pd3dDevice->SetTexture(0, NULL);
  109. g_pd3dDevice->SetStreamSource(0, NULL, 0, 0);
  110. g_pd3dDevice->SetIndices(NULL);
  111. g_pd3dDevice->Release();
  112. g_pd3dDevice = NULL;
  113. }
  114. if (g_pD3D != NULL)
  115. {
  116. g_pD3D->Release();
  117. g_pD3D = NULL;
  118. }
  119. }
  120. void RenderInterfaceDirectX::PrepareRenderBuffer()
  121. {
  122. if(g_pd3dDevice != NULL)
  123. {
  124. g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  125. g_pd3dDevice->BeginScene();
  126. }
  127. }
  128. void RenderInterfaceDirectX::PresentRenderBuffer()
  129. {
  130. if(g_pd3dDevice != NULL)
  131. {
  132. g_pd3dDevice->EndScene();
  133. g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
  134. }
  135. }