Advertisement
Tkap1

Untitled

Jun 8th, 2024
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.20 KB | None | 0 0
  1. static void create_window(int width, int height)
  2. {
  3.     const char* class_name = "RocketJump!_CLASS";
  4.     HINSTANCE instance = GetModuleHandle(NULL);
  5.  
  6.     PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = NULL;
  7.     PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = NULL;
  8.  
  9.     // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv     dummy start     vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  10.     {
  11.         WNDCLASSEX window_class = {};
  12.         window_class.cbSize = sizeof(window_class);
  13.         window_class.style = CS_OWNDC;
  14.         window_class.lpfnWndProc = DefWindowProc;
  15.         window_class.lpszClassName = class_name;
  16.         window_class.hInstance = instance;
  17.         check(RegisterClassEx(&window_class));
  18.  
  19.         HWND dummy_window = CreateWindowEx(
  20.             0,
  21.             class_name,
  22.             "dummy",
  23.             WS_OVERLAPPEDWINDOW,
  24.             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  25.             NULL,
  26.             NULL,
  27.             instance,
  28.             NULL
  29.         );
  30.         assert(dummy_window != INVALID_HANDLE_VALUE);
  31.  
  32.         HDC dc = GetDC(dummy_window);
  33.  
  34.         PIXELFORMATDESCRIPTOR pfd = {};
  35.         pfd.nSize = sizeof(pfd);
  36.         pfd.nVersion = 1;
  37.         pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;
  38.         pfd.cColorBits = 24;
  39.         pfd.cDepthBits = 24;
  40.         pfd.iLayerType = PFD_MAIN_PLANE;
  41.         int format = ChoosePixelFormat(dc, &pfd);
  42.         check(DescribePixelFormat(dc, format, sizeof(pfd), &pfd));
  43.         check(SetPixelFormat(dc, format, &pfd));
  44.  
  45.         HGLRC glrc = wglCreateContext(dc);
  46.         check(wglMakeCurrent(dc, glrc));
  47.  
  48.         wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)load_gl_func("wglCreateContextAttribsARB");
  49.         wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)load_gl_func("wglChoosePixelFormatARB");
  50.  
  51.         check(wglMakeCurrent(NULL, NULL));
  52.         check(wglDeleteContext(glrc));
  53.         check(ReleaseDC(dummy_window, dc));
  54.         check(DestroyWindow(dummy_window));
  55.         check(UnregisterClass(class_name, instance));
  56.  
  57.     }
  58.     // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^     dummy end       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  59.  
  60.     // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv     window start        vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  61.     {
  62.         WNDCLASSEX window_class = {};
  63.         window_class.cbSize = sizeof(window_class);
  64.         window_class.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
  65.         window_class.lpfnWndProc = window_proc;
  66.         window_class.lpszClassName = class_name;
  67.         window_class.hInstance = instance;
  68.         window_class.hCursor = LoadCursor(NULL, IDC_ARROW);
  69.         window_class.hIcon = LoadIcon(instance, MAKEINTRESOURCE(MY_ICON));
  70.         check(RegisterClassEx(&window_class));
  71.  
  72.         DWORD style = (WS_OVERLAPPEDWINDOW) & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX;
  73.         RECT rect = {};
  74.         rect.right = width;
  75.         rect.bottom = height;
  76.         AdjustWindowRect(&rect, style, false);
  77.  
  78.         g_window.handle = CreateWindowEx(
  79.             0,
  80.             class_name,
  81.             "Rocket Jump!",
  82.             style,
  83.             CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top,
  84.             NULL,
  85.             NULL,
  86.             instance,
  87.             NULL
  88.         );
  89.         assert(g_window.handle != INVALID_HANDLE_VALUE);
  90.  
  91.         center_window();
  92.  
  93.         g_window.dc = GetDC(g_window.handle);
  94.  
  95.         int pixel_attribs[] = {
  96.             WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
  97.             WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
  98.             WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
  99.             WGL_SWAP_METHOD_ARB, WGL_SWAP_COPY_ARB,
  100.             WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
  101.             WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
  102.             WGL_COLOR_BITS_ARB, 32,
  103.             WGL_DEPTH_BITS_ARB, 24,
  104.             WGL_STENCIL_BITS_ARB, 8,
  105.  
  106.             #if m_multisample
  107.             WGL_SAMPLE_BUFFERS_ARB, GL_TRUE,
  108.             WGL_SAMPLES_ARB, 4,
  109.             #endif
  110.  
  111.             0
  112.         };
  113.  
  114.         PIXELFORMATDESCRIPTOR pfd = {};
  115.         pfd.nSize = sizeof(pfd);
  116.         int format;
  117.         u32 num_formats;
  118.         check(wglChoosePixelFormatARB(g_window.dc, pixel_attribs, NULL, 1, &format, &num_formats));
  119.         check(DescribePixelFormat(g_window.dc, format, sizeof(pfd), &pfd));
  120.         SetPixelFormat(g_window.dc, format, &pfd);
  121.  
  122.         int gl_attribs[] = {
  123.             WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  124.             WGL_CONTEXT_MINOR_VERSION_ARB, 3,
  125.             WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  126.             // WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB,
  127.             0
  128.         };
  129.         HGLRC glrc = wglCreateContextAttribsARB(g_window.dc, NULL, gl_attribs);
  130.         assert(glrc);
  131.         check(wglMakeCurrent(g_window.dc, glrc));
  132.     }
  133.     // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^     window end      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement