2013-04-09 13:31:46 +00:00
|
|
|
#include "opengl/opengl.hpp"
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
|
|
|
|
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
|
|
|
|
|
2015-06-20 05:44:05 +00:00
|
|
|
struct VideoWGL : Video, OpenGL {
|
2017-07-24 05:23:40 +00:00
|
|
|
VideoWGL() { initialize(); }
|
|
|
|
~VideoWGL() { terminate(); }
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto ready() -> bool { return _ready; }
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto context() -> uintptr { return _context; }
|
|
|
|
auto blocking() -> bool { return _blocking; }
|
|
|
|
auto smooth() -> bool { return _smooth; }
|
|
|
|
auto shader() -> string { return _shader; }
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto setContext(uintptr context) -> bool {
|
|
|
|
if(_context == context) return true;
|
|
|
|
_context = context;
|
|
|
|
return initialize();
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto setBlocking(bool blocking) -> bool {
|
|
|
|
if(_blocking == blocking) return true;
|
|
|
|
_blocking = blocking;
|
|
|
|
if(wglSwapInterval) wglSwapInterval(_blocking);
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto setSmooth(bool smooth) -> bool {
|
|
|
|
if(_smooth == smooth) return true;
|
|
|
|
_smooth = smooth;
|
|
|
|
if(!_shader) OpenGL::filter = _smooth ? GL_LINEAR : GL_NEAREST;
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto setShader(string shader) -> bool {
|
|
|
|
if(_shader == shader) return true;
|
|
|
|
OpenGL::shader(_shader = shader);
|
|
|
|
if(!_shader) OpenGL::filter = _smooth ? GL_LINEAR : GL_NEAREST;
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto clear() -> void {
|
|
|
|
if(!ready()) return;
|
|
|
|
OpenGL::clear();
|
|
|
|
SwapBuffers(_display);
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 10:38:03 +00:00
|
|
|
auto lock(uint32_t*& data, uint& pitch, uint width, uint height) -> bool {
|
2017-07-24 05:23:40 +00:00
|
|
|
if(!ready()) return false;
|
2013-04-09 13:31:46 +00:00
|
|
|
OpenGL::size(width, height);
|
2010-08-09 13:28:56 +00:00
|
|
|
return OpenGL::lock(data, pitch);
|
|
|
|
}
|
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
auto unlock() -> void {
|
2017-07-24 05:23:40 +00:00
|
|
|
if(!ready()) return;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto output() -> void {
|
|
|
|
if(!ready()) return;
|
|
|
|
RECT rectangle;
|
|
|
|
GetClientRect((HWND)_context, &rectangle);
|
|
|
|
OpenGL::outputWidth = rectangle.right - rectangle.left;
|
|
|
|
OpenGL::outputHeight = rectangle.bottom - rectangle.top;
|
|
|
|
OpenGL::output();
|
|
|
|
SwapBuffers(_display);
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
private:
|
|
|
|
auto initialize() -> bool {
|
|
|
|
terminate();
|
|
|
|
if(!_context) return false;
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
PIXELFORMATDESCRIPTOR descriptor = {};
|
|
|
|
descriptor.nSize = sizeof(PIXELFORMATDESCRIPTOR);
|
|
|
|
descriptor.nVersion = 1;
|
|
|
|
descriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
|
|
|
descriptor.iPixelType = PFD_TYPE_RGBA;
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
_display = GetDC((HWND)_context);
|
|
|
|
GLuint pixelFormat = ChoosePixelFormat(_display, &descriptor);
|
|
|
|
SetPixelFormat(_display, pixelFormat, &descriptor);
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
_wglContext = wglCreateContext(_display);
|
|
|
|
wglMakeCurrent(_display, _wglContext);
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
wglCreateContextAttribs = (HGLRC (APIENTRY*)(HDC, HGLRC, const int*))glGetProcAddress("wglCreateContextAttribsARB");
|
|
|
|
wglSwapInterval = (BOOL (APIENTRY*)(int))glGetProcAddress("wglSwapIntervalEXT");
|
|
|
|
|
|
|
|
if(wglCreateContextAttribs) {
|
2017-07-24 05:23:40 +00:00
|
|
|
int attributeList[] = {
|
2013-07-29 09:42:45 +00:00
|
|
|
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
|
|
|
WGL_CONTEXT_MINOR_VERSION_ARB, 2,
|
|
|
|
0
|
|
|
|
};
|
2017-07-24 05:23:40 +00:00
|
|
|
HGLRC context = wglCreateContextAttribs(_display, 0, attributeList);
|
2013-07-29 09:42:45 +00:00
|
|
|
if(context) {
|
2017-07-09 02:23:17 +00:00
|
|
|
wglMakeCurrent(nullptr, nullptr);
|
2017-07-24 05:23:40 +00:00
|
|
|
wglDeleteContext(_wglContext);
|
|
|
|
wglMakeCurrent(_display, _wglContext = context);
|
2013-07-29 09:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
if(wglSwapInterval) wglSwapInterval(_blocking);
|
|
|
|
return _ready = OpenGL::initialize();
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto terminate() -> void {
|
|
|
|
_ready = false;
|
|
|
|
OpenGL::terminate();
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
if(_wglContext) {
|
|
|
|
wglDeleteContext(_wglContext);
|
|
|
|
_wglContext = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-24 05:23:40 +00:00
|
|
|
|
|
|
|
auto (APIENTRY* wglCreateContextAttribs)(HDC, HGLRC, const int*) -> HGLRC = nullptr;
|
|
|
|
auto (APIENTRY* wglSwapInterval)(int) -> BOOL = nullptr;
|
|
|
|
|
|
|
|
bool _ready = false;
|
|
|
|
uintptr _context = 0;
|
|
|
|
bool _blocking = false;
|
|
|
|
bool _smooth = true;
|
|
|
|
string _shader;
|
|
|
|
|
|
|
|
HDC _display = nullptr;
|
|
|
|
HGLRC _wglContext = nullptr;
|
|
|
|
HINSTANCE _glWindow = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
};
|