context_gl_win.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*************************************************************************/
  2. /* context_gl_win.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #if defined(OPENGL_ENABLED) || defined(LEGACYGL_ENABLED) || defined(GLES2_ENABLED)
  31. //
  32. // C++ Implementation: context_gl_x11
  33. //
  34. // Description:
  35. //
  36. //
  37. // Author: Juan Linietsky <[email protected]>, (C) 2008
  38. //
  39. // Copyright: See COPYING file that comes with this distribution
  40. //
  41. //
  42. #include "context_gl_win.h"
  43. //#include "drivers/opengl/glwrapper.h"
  44. //#include "ctxgl_procaddr.h"
  45. #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
  46. #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
  47. #define WGL_CONTEXT_FLAGS_ARB 0x2094
  48. #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
  49. typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int *);
  50. void ContextGL_Win::release_current() {
  51. wglMakeCurrent(hDC, NULL);
  52. }
  53. void ContextGL_Win::make_current() {
  54. wglMakeCurrent(hDC, hRC);
  55. }
  56. int ContextGL_Win::get_window_width() {
  57. return OS::get_singleton()->get_video_mode().width;
  58. }
  59. int ContextGL_Win::get_window_height() {
  60. return OS::get_singleton()->get_video_mode().height;
  61. }
  62. void ContextGL_Win::swap_buffers() {
  63. SwapBuffers(hDC);
  64. }
  65. /*
  66. static GLWrapperFuncPtr wrapper_get_proc_address(const char* p_function) {
  67. print_line(String()+"getting proc of: "+p_function);
  68. GLWrapperFuncPtr func=(GLWrapperFuncPtr)get_gl_proc_address(p_function);
  69. if (!func) {
  70. print_line("Couldn't find function: "+String(p_function));
  71. print_line("error: "+itos(GetLastError()));
  72. }
  73. return func;
  74. }
  75. */
  76. void ContextGL_Win::set_use_vsync(bool p_use) {
  77. if (wglSwapIntervalEXT) {
  78. wglSwapIntervalEXT(p_use ? 1 : 0);
  79. }
  80. use_vsync = p_use;
  81. }
  82. bool ContextGL_Win::is_using_vsync() const {
  83. return use_vsync;
  84. }
  85. #define _WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
  86. Error ContextGL_Win::initialize() {
  87. static PIXELFORMATDESCRIPTOR pfd = {
  88. sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
  89. 1,
  90. PFD_DRAW_TO_WINDOW | // Format Must Support Window
  91. PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  92. PFD_DOUBLEBUFFER,
  93. PFD_TYPE_RGBA,
  94. 24,
  95. 0, 0, 0, 0, 0, 0, // Color Bits Ignored
  96. 0, // No Alpha Buffer
  97. 0, // Shift Bit Ignored
  98. 0, // No Accumulation Buffer
  99. 0, 0, 0, 0, // Accumulation Bits Ignored
  100. 24, // 24Bit Z-Buffer (Depth Buffer)
  101. 0, // No Stencil Buffer
  102. 0, // No Auxiliary Buffer
  103. PFD_MAIN_PLANE, // Main Drawing Layer
  104. 0, // Reserved
  105. 0, 0, 0 // Layer Masks Ignored
  106. };
  107. hDC = GetDC(hWnd);
  108. if (!hDC) {
  109. MessageBox(NULL, "Can't Create A GL Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  110. return ERR_CANT_CREATE; // Return FALSE
  111. }
  112. pixel_format = ChoosePixelFormat(hDC, &pfd);
  113. if (!pixel_format) // Did Windows Find A Matching Pixel Format?
  114. {
  115. MessageBox(NULL, "Can't Find A Suitable pixel_format.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  116. return ERR_CANT_CREATE; // Return FALSE
  117. }
  118. BOOL ret = SetPixelFormat(hDC, pixel_format, &pfd);
  119. if (!ret) // Are We Able To Set The Pixel Format?
  120. {
  121. MessageBox(NULL, "Can't Set The pixel_format.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  122. return ERR_CANT_CREATE; // Return FALSE
  123. }
  124. hRC = wglCreateContext(hDC);
  125. if (!hRC) // Are We Able To Get A Rendering Context?
  126. {
  127. MessageBox(NULL, "Can't Create A Temporary GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  128. return ERR_CANT_CREATE; // Return FALSE
  129. }
  130. wglMakeCurrent(hDC, hRC);
  131. if (opengl_3_context) {
  132. int attribs[] = {
  133. WGL_CONTEXT_MAJOR_VERSION_ARB, 3, //we want a 3.3 context
  134. WGL_CONTEXT_MINOR_VERSION_ARB, 3,
  135. //and it shall be forward compatible so that we can only use up to date functionality
  136. WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB | _WGL_CONTEXT_DEBUG_BIT_ARB,
  137. 0
  138. }; //zero indicates the end of the array
  139. PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = NULL; //pointer to the method
  140. wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  141. if (wglCreateContextAttribsARB == NULL) //OpenGL 3.0 is not supported
  142. {
  143. MessageBox(NULL, "Cannot get Proc Address for CreateContextAttribs", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  144. wglDeleteContext(hRC);
  145. return ERR_CANT_CREATE;
  146. }
  147. HGLRC new_hRC = wglCreateContextAttribsARB(hDC, 0, attribs);
  148. if (!new_hRC) {
  149. wglDeleteContext(hRC);
  150. MessageBox(NULL, "Can't Create An OpenGL 3.3 Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  151. return ERR_CANT_CREATE; // Return false
  152. }
  153. wglMakeCurrent(hDC, NULL);
  154. wglDeleteContext(hRC);
  155. hRC = new_hRC;
  156. if (!wglMakeCurrent(hDC, hRC)) // Try To Activate The Rendering Context
  157. {
  158. MessageBox(NULL, "Can't Activate The GL 3.3 Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  159. return ERR_CANT_CREATE; // Return FALSE
  160. }
  161. printf("Activated GL 3.3 context");
  162. }
  163. wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
  164. //glWrapperInit(wrapper_get_proc_address);
  165. return OK;
  166. }
  167. ContextGL_Win::ContextGL_Win(HWND hwnd, bool p_opengl_3_context) {
  168. opengl_3_context = p_opengl_3_context;
  169. hWnd = hwnd;
  170. use_vsync = false;
  171. }
  172. ContextGL_Win::~ContextGL_Win() {
  173. }
  174. #endif