ShellRenderInterfaceExtensionsOpenGL_MacOSX.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <Carbon/Carbon.h>
  31. #include <RmlUi/Core/Context.h>
  32. #include <RmlUi/Core.h>
  33. #include <RmlUi/Core/Platform.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, width, height, 0, -1, 1);
  46. glMatrixMode(GL_PROJECTION);
  47. glLoadMatrixf(projection);
  48. view = Rml::Core::Matrix4f::Identity();
  49. glMatrixMode(GL_MODELVIEW);
  50. glLoadMatrixf(view);
  51. aglUpdateContext(gl_context);
  52. if(m_rmlui_context != nullptr)
  53. {
  54. ((Rml::Core::Context*)m_rmlui_context)->SetDimensions(Rml::Core::Vector2i(width, height));
  55. ((Rml::Core::Context*)m_rmlui_context)->ProcessProjectionChange(projection);
  56. ((Rml::Core::Context*)m_rmlui_context)->ProcessViewChange(view);
  57. }
  58. }
  59. }
  60. bool ShellRenderInterfaceOpenGL::AttachToNative(void *nativeWindow)
  61. {
  62. WindowRef window = (WindowRef)nativeWindow;
  63. static GLint attributes[] =
  64. {
  65. AGL_RGBA,
  66. AGL_DOUBLEBUFFER,
  67. AGL_ALPHA_SIZE, 8,
  68. AGL_DEPTH_SIZE, 24,
  69. AGL_STENCIL_SIZE, 8,
  70. AGL_ACCELERATED,
  71. AGL_NONE
  72. };
  73. AGLPixelFormat pixel_format = aglChoosePixelFormat(nullptr, 0, attributes);
  74. if (pixel_format == nullptr)
  75. return false;
  76. CGrafPtr window_port = GetWindowPort(window);
  77. if (window_port == nullptr)
  78. return false;
  79. this->gl_context = aglCreateContext(pixel_format, nullptr);
  80. if (this->gl_context == nullptr)
  81. return false;
  82. aglSetDrawable(this->gl_context, window_port);
  83. aglSetCurrentContext(this->gl_context);
  84. aglDestroyPixelFormat(pixel_format);
  85. // Set up the GL state.
  86. glClearColor(0, 0, 0, 1);
  87. glEnableClientState(GL_VERTEX_ARRAY);
  88. glEnableClientState(GL_COLOR_ARRAY);
  89. glEnable(GL_BLEND);
  90. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  91. glMatrixMode(GL_PROJECTION);
  92. glLoadIdentity();
  93. Rect crect;
  94. GetWindowBounds(window, kWindowContentRgn, &crect);
  95. glOrtho(0, (crect.right - crect.left), (crect.bottom - crect.top), 0, -1, 1);
  96. glMatrixMode(GL_MODELVIEW);
  97. glLoadIdentity();
  98. }
  99. void ShellRenderInterfaceOpenGL::DetachFromNative()
  100. {
  101. // Shutdown OpenGL if necessary.
  102. aglSetCurrentContext(nullptr);
  103. aglSetDrawable(this->gl_context, nullptr);
  104. aglDestroyContext(this->gl_context);
  105. }
  106. void ShellRenderInterfaceOpenGL::PrepareRenderBuffer()
  107. {
  108. glClear(GL_COLOR_BUFFER_BIT);
  109. }
  110. void ShellRenderInterfaceOpenGL::PresentRenderBuffer()
  111. {
  112. // Flips the OpenGL buffers.
  113. aglSwapBuffers(this->gl_context);
  114. }