wgl.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "opengl/opengl.hpp"
  2. #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
  3. #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
  4. static LRESULT CALLBACK VideoOpenGL32_WindowProcedure(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
  5. if(msg == WM_SYSKEYDOWN && wparam == VK_F4) return false;
  6. return DefWindowProc(hwnd, msg, wparam, lparam);
  7. }
  8. struct VideoWGL : VideoDriver, OpenGL {
  9. VideoWGL& self = *this;
  10. VideoWGL(Video& super) : VideoDriver(super) { construct(); }
  11. ~VideoWGL() { destruct(); }
  12. auto create() -> bool override {
  13. return initialize();
  14. }
  15. auto driver() -> string override { return "OpenGL 3.2"; }
  16. auto ready() -> bool override { return _ready; }
  17. auto hasFullScreen() -> bool override { return true; }
  18. auto hasMonitor() -> bool override { return true; }
  19. auto hasContext() -> bool override { return true; }
  20. auto hasBlocking() -> bool override { return true; }
  21. auto hasFlush() -> bool override { return true; }
  22. auto hasShader() -> bool override { return true; }
  23. auto setFullScreen(bool fullScreen) -> bool override {
  24. return initialize();
  25. }
  26. auto setMonitor(string monitor) -> bool override {
  27. return initialize();
  28. }
  29. auto setContext(uintptr context) -> bool override {
  30. return initialize();
  31. }
  32. auto setBlocking(bool blocking) -> bool override {
  33. if(wglSwapInterval) wglSwapInterval(blocking);
  34. return true;
  35. }
  36. auto setFlush(bool flush) -> bool override {
  37. return true;
  38. }
  39. auto setShader(string shader) -> bool override {
  40. OpenGL::setShader(self.shader);
  41. return true;
  42. }
  43. auto focused() -> bool override {
  44. if(self.fullScreen && self.exclusive) return true;
  45. auto focused = GetFocus();
  46. return _context == focused || IsChild(_context, focused);
  47. }
  48. auto clear() -> void override {
  49. OpenGL::clear();
  50. SwapBuffers(_display);
  51. }
  52. auto size(uint& width, uint& height) -> void override {
  53. if(self.fullScreen) {
  54. width = _monitorWidth;
  55. height = _monitorHeight;
  56. } else {
  57. RECT rectangle;
  58. GetClientRect(_context, &rectangle);
  59. width = rectangle.right - rectangle.left;
  60. height = rectangle.bottom - rectangle.top;
  61. }
  62. }
  63. auto acquire(uint32_t*& data, uint& pitch, uint width, uint height) -> bool override {
  64. OpenGL::size(width, height);
  65. return OpenGL::lock(data, pitch);
  66. }
  67. auto release() -> void override {
  68. }
  69. auto output(uint width, uint height) -> void override {
  70. uint windowWidth, windowHeight;
  71. size(windowWidth, windowHeight);
  72. OpenGL::absoluteWidth = width;
  73. OpenGL::absoluteHeight = height;
  74. OpenGL::outputX = 0;
  75. OpenGL::outputY = 0;
  76. OpenGL::outputWidth = windowWidth;
  77. OpenGL::outputHeight = windowHeight;
  78. OpenGL::output();
  79. SwapBuffers(_display);
  80. if(self.flush) glFinish();
  81. }
  82. private:
  83. auto construct() -> void {
  84. WNDCLASS windowClass{};
  85. windowClass.cbClsExtra = 0;
  86. windowClass.cbWndExtra = 0;
  87. windowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  88. windowClass.hCursor = LoadCursor(0, IDC_ARROW);
  89. windowClass.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
  90. windowClass.hInstance = GetModuleHandle(0);
  91. windowClass.lpfnWndProc = VideoOpenGL32_WindowProcedure;
  92. windowClass.lpszClassName = L"VideoOpenGL32_Window";
  93. windowClass.lpszMenuName = 0;
  94. windowClass.style = CS_HREDRAW | CS_VREDRAW;
  95. RegisterClass(&windowClass);
  96. }
  97. auto destruct() -> void {
  98. terminate();
  99. }
  100. auto initialize() -> bool {
  101. terminate();
  102. if(!self.fullScreen && !self.context) return false;
  103. auto monitor = Video::monitor(self.monitor);
  104. _monitorX = monitor.x;
  105. _monitorY = monitor.y;
  106. _monitorWidth = monitor.width;
  107. _monitorHeight = monitor.height;
  108. if(self.fullScreen) {
  109. _context = _window = CreateWindowEx(WS_EX_TOPMOST, L"VideoOpenGL32_Window", L"", WS_VISIBLE | WS_POPUP,
  110. _monitorX, _monitorY, _monitorWidth, _monitorHeight,
  111. nullptr, nullptr, GetModuleHandle(0), nullptr);
  112. } else {
  113. _context = (HWND)self.context;
  114. }
  115. PIXELFORMATDESCRIPTOR descriptor{};
  116. descriptor.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  117. descriptor.nVersion = 1;
  118. descriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  119. descriptor.iPixelType = PFD_TYPE_RGBA;
  120. _display = GetDC(_context);
  121. GLuint pixelFormat = ChoosePixelFormat(_display, &descriptor);
  122. SetPixelFormat(_display, pixelFormat, &descriptor);
  123. _wglContext = wglCreateContext(_display);
  124. wglMakeCurrent(_display, _wglContext);
  125. wglCreateContextAttribs = (HGLRC (APIENTRY*)(HDC, HGLRC, const int*))glGetProcAddress("wglCreateContextAttribsARB");
  126. wglSwapInterval = (BOOL (APIENTRY*)(int))glGetProcAddress("wglSwapIntervalEXT");
  127. if(wglCreateContextAttribs) {
  128. int attributeList[] = {
  129. WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  130. WGL_CONTEXT_MINOR_VERSION_ARB, 2,
  131. 0
  132. };
  133. HGLRC context = wglCreateContextAttribs(_display, 0, attributeList);
  134. if(context) {
  135. wglMakeCurrent(nullptr, nullptr);
  136. wglDeleteContext(_wglContext);
  137. wglMakeCurrent(_display, _wglContext = context);
  138. }
  139. }
  140. if(wglSwapInterval) wglSwapInterval(self.blocking);
  141. return _ready = OpenGL::initialize(self.shader);
  142. }
  143. auto terminate() -> void {
  144. _ready = false;
  145. OpenGL::terminate();
  146. if(_wglContext) {
  147. wglDeleteContext(_wglContext);
  148. _wglContext = nullptr;
  149. }
  150. if(_window) {
  151. DestroyWindow(_window);
  152. _window = nullptr;
  153. }
  154. _context = nullptr;
  155. }
  156. auto (APIENTRY* wglCreateContextAttribs)(HDC, HGLRC, const int*) -> HGLRC = nullptr;
  157. auto (APIENTRY* wglSwapInterval)(int) -> BOOL = nullptr;
  158. bool _ready = false;
  159. int _monitorX = 0;
  160. int _monitorY = 0;
  161. int _monitorWidth = 0;
  162. int _monitorHeight = 0;
  163. HWND _window = nullptr;
  164. HWND _context = nullptr;
  165. HDC _display = nullptr;
  166. HGLRC _wglContext = nullptr;
  167. HINSTANCE _glWindow = nullptr;
  168. };