ShellRenderInterfaceExtensionsOpenGL_Win32.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include <ShellRenderInterfaceExtensions.h>
  29. #include <ShellRenderInterfaceOpenGL.h>
  30. #include <windows.h>
  31. #include <RmlUi/Core.h>
  32. #include <RmlUi/Core/Platform.h>
  33. #include <Shell.h>
  34. void ShellRenderInterfaceOpenGL::SetContext(void *context)
  35. {
  36. m_rmlui_context = context;
  37. }
  38. void ShellRenderInterfaceOpenGL::SetViewport(int width, int height)
  39. {
  40. if(m_width != width || m_height != height) {
  41. Rml::Core::Matrix4f projection, view;
  42. m_width = width;
  43. m_height = height;
  44. glViewport(0, 0, width, height);
  45. projection = Rml::Core::Matrix4f::ProjectOrtho(0, (float)width, (float)height, 0, -1, 1);
  46. glMatrixMode(GL_PROJECTION);
  47. glLoadMatrixf(projection.data());
  48. view = Rml::Core::Matrix4f::Identity();
  49. glMatrixMode(GL_MODELVIEW);
  50. glLoadMatrixf(view.data());
  51. if(m_rmlui_context != nullptr)
  52. {
  53. ((Rml::Core::Context*)m_rmlui_context)->SetDimensions(Rml::Core::Vector2i(width, height));
  54. ((Rml::Core::Context*)m_rmlui_context)->ProcessProjectionChange(projection);
  55. ((Rml::Core::Context*)m_rmlui_context)->ProcessViewChange(view);
  56. }
  57. }
  58. }
  59. bool ShellRenderInterfaceOpenGL::AttachToNative(void *nativeWindow)
  60. {
  61. this->render_context = nullptr;
  62. this->window_handle = (HWND)nativeWindow;
  63. this->device_context = GetDC(this->window_handle);
  64. if (this->device_context == nullptr)
  65. {
  66. Shell::DisplayError("Could not get device context.");
  67. return false;
  68. }
  69. PIXELFORMATDESCRIPTOR pixel_format_descriptor;
  70. memset(&pixel_format_descriptor, 0, sizeof(pixel_format_descriptor));
  71. pixel_format_descriptor.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  72. pixel_format_descriptor.nVersion = 1;
  73. pixel_format_descriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  74. pixel_format_descriptor.iPixelType = PFD_TYPE_RGBA;
  75. pixel_format_descriptor.cColorBits = 32;
  76. pixel_format_descriptor.cRedBits = 8;
  77. pixel_format_descriptor.cGreenBits = 8;
  78. pixel_format_descriptor.cBlueBits = 8;
  79. pixel_format_descriptor.cAlphaBits = 8;
  80. pixel_format_descriptor.cDepthBits = 24;
  81. pixel_format_descriptor.cStencilBits = 8;
  82. int pixel_format = ChoosePixelFormat(this->device_context, &pixel_format_descriptor);
  83. if (pixel_format == 0)
  84. {
  85. Shell::DisplayError("Could not choose 32-bit pixel format.");
  86. return false;
  87. }
  88. if (SetPixelFormat(this->device_context, pixel_format, &pixel_format_descriptor) == FALSE)
  89. {
  90. Shell::DisplayError("Could not set pixel format.");
  91. return false;
  92. }
  93. this->render_context = wglCreateContext(this->device_context);
  94. if (this->render_context == nullptr)
  95. {
  96. Shell::DisplayError("Could not create OpenGL rendering context.");
  97. return false;
  98. }
  99. // Activate the rendering context.
  100. if (wglMakeCurrent(this->device_context, this->render_context) == FALSE)
  101. {
  102. Shell::DisplayError("Unable to make rendering context current.");
  103. return false;
  104. }
  105. // Set up the GL state.
  106. glClearColor(0, 0, 0, 1);
  107. glEnableClientState(GL_VERTEX_ARRAY);
  108. glEnableClientState(GL_COLOR_ARRAY);
  109. glEnable(GL_BLEND);
  110. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  111. glMatrixMode(GL_PROJECTION);
  112. glLoadIdentity();
  113. RECT crect;
  114. GetClientRect(this->window_handle, &crect);
  115. glOrtho(0, (crect.right - crect.left), (crect.bottom - crect.top), 0, -1, 1);
  116. glMatrixMode(GL_MODELVIEW);
  117. glLoadIdentity();
  118. return true;
  119. }
  120. void ShellRenderInterfaceOpenGL::DetachFromNative()
  121. {
  122. // Shutdown OpenGL
  123. if (this->render_context != nullptr)
  124. {
  125. wglMakeCurrent(nullptr, nullptr);
  126. wglDeleteContext(this->render_context);
  127. this->render_context = nullptr;
  128. }
  129. if (this->device_context != nullptr)
  130. {
  131. ReleaseDC(this->window_handle, this->device_context);
  132. this->device_context = nullptr;
  133. }
  134. }
  135. void ShellRenderInterfaceOpenGL::PrepareRenderBuffer()
  136. {
  137. glClear(GL_COLOR_BUFFER_BIT);
  138. }
  139. void ShellRenderInterfaceOpenGL::PresentRenderBuffer()
  140. {
  141. // Flips the OpenGL buffers.
  142. SwapBuffers(this->device_context);
  143. }