Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func s_opengl_window init_opengl_window(s_launch_args args)
- {
- s_opengl_window window = zero;
- HINSTANCE hinstance = GetModuleHandle(null);
- PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
- PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = null;
- // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv REGISTER CLASS START vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- {
- WNDCLASS my_class = zero;
- // my_class.cbSize = sizeof(my_class);
- my_class.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
- my_class.lpfnWndProc = window_proc;
- // my_class.cbClsExtra = ;
- // my_class.cbWndExtra = ;
- my_class.hInstance = hinstance;
- #ifndef m_debug_internal
- my_class.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(MY_ICON));
- #endif // m_debug_internal
- // my_class.hCursor = ;
- // @TODO(tkap): Is this correct? We probably want to hide the cursor anyway...
- my_class.hCursor = LoadCursor(null, IDC_ARROW);
- // my_class.hbrBackground = ;
- // my_class.lpszMenuName = ;
- my_class.lpszClassName = class_name;
- // my_class.hIconSm = ;
- if(!RegisterClass(&my_class))
- {
- print_win32_error();
- platform_exit();
- }
- }
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ REGISTER CLASS END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv FAKE WINDOW START vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- {
- HWND fake_window = CreateWindowEx(
- 0,
- class_name,
- "fakewindow",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- 0,
- null,
- hinstance,
- null
- );
- if(!fake_window)
- {
- print_win32_error();
- platform_exit();
- }
- HDC fake_dc = GetDC(fake_window);
- if(!fake_dc)
- {
- print_win32_error();
- platform_exit();
- }
- PIXELFORMATDESCRIPTOR pfd = zero;
- pfd.nSize = sizeof(pfd);
- pfd.nVersion = 1;
- pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 32;
- pfd.cAlphaBits = 8;
- pfd.cDepthBits = 24;
- int pixel_format = ChoosePixelFormat(fake_dc, &pfd);
- if(!pixel_format)
- {
- print_win32_error();
- platform_exit();
- }
- if(!SetPixelFormat(fake_dc, pixel_format, &pfd))
- {
- print_win32_error();
- platform_exit();
- }
- HGLRC fake_glrc = wglCreateContext(fake_dc);
- if(!fake_glrc)
- {
- print_win32_error();
- platform_exit();
- }
- if(!wglMakeCurrent(fake_dc, fake_glrc))
- {
- print_win32_error();
- platform_exit();
- }
- wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)get_gl_proc("wglChoosePixelFormatARB");
- wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)get_gl_proc("wglCreateContextAttribsARB");
- wglMakeCurrent(fake_dc, null);
- wglDeleteContext(fake_glrc);
- ReleaseDC(fake_window, fake_dc);
- DestroyWindow(fake_window);
- }
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FAKE WINDOW END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv CREATE WINDOW START vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- {
- window.handle = CreateWindowEx(
- 0,
- class_name,
- "Window",
- WS_POPUP | WS_VISIBLE,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- args.window_w ? args.window_w : g_window_width,
- args.window_h ? args.window_h : g_window_height,
- 0,
- null,
- hinstance,
- null
- );
- if(!window.handle)
- {
- print_win32_error();
- platform_exit();
- }
- HMONITOR monitor = MonitorFromWindow(
- window.handle,
- MONITOR_DEFAULTTONEAREST
- );
- MONITORINFO monitor_info = zero;
- monitor_info.cbSize = sizeof(monitor_info);
- GetMonitorInfoA(
- monitor,
- &monitor_info
- );
- int monitor_w = monitor_info.rcMonitor.right - monitor_info.rcMonitor.left;
- int monitor_h = monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top;
- g_window_width = args.window_w ? args.window_w : monitor_w;
- g_window_height = args.window_h ? args.window_h : monitor_h;
- SetWindowPos(
- window.handle,
- 0,
- args.window_x,
- args.window_y,
- g_window_width,
- g_window_height,
- 0
- );
- }
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ CREATE WINDOW END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv CREATE CONTEXT START vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- {
- HGLRC gldc = 0;
- window.dc = GetDC(window.handle);
- if(!window.dc)
- {
- print_win32_error();
- platform_exit();
- }
- int pixelFormat[32];
- UINT numFormats;
- const int pixelAttribs[] = {
- WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
- WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
- WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
- WGL_SWAP_METHOD_ARB, WGL_SWAP_COPY_ARB, // @Note(tkap, 16/07/2024): If we don't have this, using the zoomit program's pencil feature, we get an outdated image of the game
- // WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, // @Note(tkap, 16/07/2024): It appears we don't need this?
- WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
- // WGL_COLOR_BITS_ARB, 32,
- WGL_COLOR_BITS_ARB, 24,
- WGL_DEPTH_BITS_ARB, 24,
- WGL_ALPHA_BITS_ARB, 8,
- // WGL_SAMPLE_BUFFERS_ARB, GL_TRUE,
- // WGL_SAMPLES_ARB, 4,
- 0
- };
- int contextAttributes[] = {
- WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
- WGL_CONTEXT_MINOR_VERSION_ARB, 3,
- WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
- #ifdef m_debug
- WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB,
- #endif
- 0
- };
- if(!wglChoosePixelFormatARB(window.dc, pixelAttribs, null, 1, pixelFormat, &numFormats))
- {
- print_win32_error();
- platform_exit();
- }
- assert(numFormats);
- PIXELFORMATDESCRIPTOR pfd = zero;
- DescribePixelFormat(window.dc, pixelFormat[0], sizeof(PIXELFORMATDESCRIPTOR), &pfd);
- if(!SetPixelFormat(window.dc, pixelFormat[0], &pfd))
- {
- print_win32_error();
- platform_exit();
- }
- gldc = wglCreateContextAttribsARB(window.dc, 0, contextAttributes);
- if(!gldc)
- {
- print_win32_error();
- platform_exit();
- }
- if(!wglMakeCurrent(window.dc, gldc))
- {
- print_win32_error();
- platform_exit();
- }
- }
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ CREATE CONTEXT END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- #ifdef m_debug
- init_gl_func(glDebugMessageCallback);
- glDebugMessageCallback(&gl_debug_callback, null);
- glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
- // glEnable(GL_DEBUG_OUTPUT);
- #endif
- PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
- if(wglGetExtensionsStringARB) {
- const char* extensions = wglGetExtensionsStringARB(window.dc);
- g_platform_shared.is_adaptive_vsync_supported = strstr(extensions, "WGL_EXT_swap_control_tear") != null;
- }
- return window;
- }
Add Comment
Please, Sign In to add comment