ShellRenderInterfaceExtensionsOpenGL_MacOSX.cpp 3.6 KB

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