context_gl_win.cpp 6.9 KB

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