ShellRenderInterfaceExtensionsOpenGL_Win32.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <windows.h>
  30. #include <Rocket/Core.h>
  31. #include <Rocket/Core/Platform.h>
  32. #include <Shell.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. }
  49. if(m_rocket_context != NULL)
  50. {
  51. ((Rocket::Core::Context*)m_rocket_context)->SetDimensions(Rocket::Core::Vector2i(width, height));
  52. }
  53. }
  54. bool ShellRenderInterfaceOpenGL::AttachToNative(void *nativeWindow)
  55. {
  56. this->render_context = NULL;
  57. this->window_handle = (HWND)nativeWindow;
  58. this->device_context = GetDC(this->window_handle);
  59. if (this->device_context == NULL)
  60. {
  61. Shell::DisplayError("Could not get device context.");
  62. return false;
  63. }
  64. PIXELFORMATDESCRIPTOR pixel_format_descriptor;
  65. memset(&pixel_format_descriptor, 0, sizeof(pixel_format_descriptor));
  66. pixel_format_descriptor.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  67. pixel_format_descriptor.nVersion = 1;
  68. pixel_format_descriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  69. pixel_format_descriptor.iPixelType = PFD_TYPE_RGBA;
  70. pixel_format_descriptor.cColorBits = 32;
  71. pixel_format_descriptor.cRedBits = 8;
  72. pixel_format_descriptor.cGreenBits = 8;
  73. pixel_format_descriptor.cBlueBits = 8;
  74. pixel_format_descriptor.cAlphaBits = 8;
  75. pixel_format_descriptor.cDepthBits = 24;
  76. pixel_format_descriptor.cStencilBits = 8;
  77. int pixel_format = ChoosePixelFormat(this->device_context, &pixel_format_descriptor);
  78. if (pixel_format == 0)
  79. {
  80. Shell::DisplayError("Could not choose 32-bit pixel format.");
  81. return false;
  82. }
  83. if (SetPixelFormat(this->device_context, pixel_format, &pixel_format_descriptor) == FALSE)
  84. {
  85. Shell::DisplayError("Could not set pixel format.");
  86. return false;
  87. }
  88. this->render_context = wglCreateContext(this->device_context);
  89. if (this->render_context == NULL)
  90. {
  91. Shell::DisplayError("Could not create OpenGL rendering context.");
  92. return false;
  93. }
  94. // Activate the rendering context.
  95. if (wglMakeCurrent(this->device_context, this->render_context) == FALSE)
  96. {
  97. Shell::DisplayError("Unable to make rendering context current.");
  98. return false;
  99. }
  100. // Set up the GL state.
  101. glClearColor(0, 0, 0, 1);
  102. glEnableClientState(GL_VERTEX_ARRAY);
  103. glEnableClientState(GL_COLOR_ARRAY);
  104. glEnable(GL_BLEND);
  105. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  106. glMatrixMode(GL_PROJECTION);
  107. glLoadIdentity();
  108. glOrtho(0, 1024, 768, 0, -1, 1);
  109. glMatrixMode(GL_MODELVIEW);
  110. glLoadIdentity();
  111. return true;
  112. }
  113. void ShellRenderInterfaceOpenGL::DetachFromNative()
  114. {
  115. // Shutdown OpenGL
  116. if (this->render_context != NULL)
  117. {
  118. wglMakeCurrent(NULL, NULL);
  119. wglDeleteContext(this->render_context);
  120. this->render_context = NULL;
  121. }
  122. if (this->device_context != NULL)
  123. {
  124. ReleaseDC(this->window_handle, this->device_context);
  125. this->device_context = NULL;
  126. }
  127. }
  128. void ShellRenderInterfaceOpenGL::PrepareRenderBuffer()
  129. {
  130. glClear(GL_COLOR_BUFFER_BIT);
  131. }
  132. void ShellRenderInterfaceOpenGL::PresentRenderBuffer()
  133. {
  134. // Flips the OpenGL buffers.
  135. SwapBuffers(this->device_context);
  136. }