Project Alice
Loading...
Searching...
No Matches
opengl_wrapper_win.cpp
Go to the documentation of this file.
1#include "wglew.h"
2
3#include <cassert>
4
5#ifndef UNICODE
6#define UNICODE
7#endif
8#define NOMINMAX
9#define WIN32_LEAN_AND_MEAN
10
11#include "Windows.h"
12
13namespace ogl {
14
16 assert(state.win_ptr && state.win_ptr->hwnd && !state.open_gl.context);
17
18 HDC window_dc = state.win_ptr->opengl_window_dc;
19
20 PIXELFORMATDESCRIPTOR pfd;
21 ZeroMemory(&pfd, sizeof(pfd));
22 pfd.nSize = sizeof(pfd);
23 pfd.nVersion = 1;
24 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
25 pfd.iPixelType = PFD_TYPE_RGBA;
26 pfd.cColorBits = 32;
27 pfd.cDepthBits = 24;
28 pfd.cStencilBits = 8;
29 pfd.iLayerType = PFD_MAIN_PLANE;
30 int const pixel_format = ChoosePixelFormat(window_dc, &pfd);
31 SetPixelFormat(window_dc, pixel_format, &pfd);
32
33 auto handle_to_ogl_dc = wglCreateContext(window_dc);
34 wglMakeCurrent(window_dc, handle_to_ogl_dc);
35
36 if(glewInit() != 0) {
37 window::emit_error_message("GLEW failed to initialize", true);
38 }
39
40 if(!wglewIsSupported("WGL_ARB_create_context")) {
41 window::emit_error_message("WGL_ARB_create_context not supported", true);
42 }
43
44 // Explicitly request for OpenGL 3.1
45 static const int attribs_3_1[] = {
46 WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
47 WGL_CONTEXT_MINOR_VERSION_ARB, 1,
48 WGL_CONTEXT_FLAGS_ARB,
49#ifndef NDEBUG
50 WGL_CONTEXT_DEBUG_BIT_ARB |
51#endif
52 0,
53 WGL_CONTEXT_PROFILE_MASK_ARB,
54 WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
55 0
56 };
57 state.open_gl.context = wglCreateContextAttribsARB(window_dc, nullptr, attribs_3_1);
58 if(state.open_gl.context == nullptr) {
59 window::emit_error_message("Unable to create WGL context", true);
60 }
61
62 wglMakeCurrent(window_dc, HGLRC(state.open_gl.context));
63 wglDeleteContext(handle_to_ogl_dc);
64
65 // wglMakeCurrent(window_dc, HGLRC(state.open_gl.context));
66#ifndef NDEBUG
67 glDebugMessageCallback(debug_callback, nullptr);
68 glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
69 glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_OTHER, GL_DEBUG_SEVERITY_LOW, 0, nullptr, GL_FALSE);
70#endif
71
72 if(wglewIsSupported("WGL_EXT_swap_control_tear") == 1) {
73 wglSwapIntervalEXT(-1);
74 } else if(wglewIsSupported("WGL_EXT_swap_control") == 1) {
75 wglSwapIntervalEXT(1);
76 } else {
77 MessageBoxW(state.win_ptr->hwnd, L"WGL_EXT_swap_control_tear and WGL_EXT_swap_control not supported", L"OpenGL error", MB_OK);
78 }
79}
80
81void shutdown_opengl(sys::state& state) {
82 assert(state.win_ptr && state.win_ptr->hwnd && state.open_gl.context);
83 wglMakeCurrent(state.win_ptr->opengl_window_dc, nullptr);
84 wglDeleteContext(HGLRC(state.open_gl.context));
85 state.open_gl.context = nullptr;
86}
87} // namespace ogl
#define assert(condition)
Definition: debug.h:74
Definition: color.hpp:6
void create_opengl_context(sys::state &state)
void shutdown_opengl(sys::state &state)
void debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei, GLchar const *message, void const *)
void emit_error_message(std::string const &content, bool fatal)
Definition: window_nix.cpp:355