Skip to content

Instantly share code, notes, and snippets.

@AdjWang
Last active December 13, 2023 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdjWang/808422011aee43312f87b15b69e238c6 to your computer and use it in GitHub Desktop.
Save AdjWang/808422011aee43312f87b15b69e238c6 to your computer and use it in GitHub Desktop.
GLAD with wgl
cmake_minimum_required(VERSION 3.8)
# .
# ├── CMakeLists.txt
# ├── GladWGL.cpp
# └── glad
# ├── include
# │   ├── KHR
# │   │   └── khrplatform.h
# │   └── glad
# │   └── glad.h
# └── src
# └── glad.c
# 5 directories, 5 files
include_directories(glad/include)
# Add source to this project's executable.
add_executable (GladWGL WIN32 "GladWGL.cpp" "glad/src/glad.c")
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET GladWGL PROPERTY CXX_STANDARD 20)
endif()
// GladWGL.cpp : Defines the entry point for the application.
// https://www.khronos.org/opengl/wiki/Creating_an_OpenGL_Context_%28WGL%29
#include <windows.h>
// #include <GL/GL.h>
#include <glad/glad.h>
#pragma comment (lib, "opengl32.lib")
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
HINSTANCE hinst;
HWND hwndMain;
// https://www.khronos.org/opengl/wiki/Load_OpenGL_Functions
void *GetAnyGLFuncAddress(const char *name)
{
void *p = (void *)wglGetProcAddress(name);
if (p == 0 ||
(p == (void*)0x1) || (p == (void*)0x2) || (p == (void*)0x3) ||
(p == (void*)-1) )
{
HMODULE module = LoadLibraryA("opengl32.dll");
p = (void *)GetProcAddress(module, name);
}
return p;
}
int WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd)
{
hinst = hInstance;
MSG msg = { 0 };
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wc.lpszClassName = "oglversionchecksample";
wc.style = CS_OWNDC;
if (!RegisterClass(&wc))
return 1;
hwndMain = CreateWindow(wc.lpszClassName, "openglversioncheck", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 640, 480, 0, 0, hInstance, 0);
if (!hwndMain)
return FALSE;
ShowWindow(hwndMain, nShowCmd);
UpdateWindow(hwndMain);
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //Flags
PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette.
32, // Colordepth of the framebuffer.
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24, // Number of bits for the depthbuffer
8, // Number of bits for the stencilbuffer
0, // Number of Aux buffers in the framebuffer.
PFD_MAIN_PLANE,
0,
0, 0, 0
};
HDC ourWindowHandleToDeviceContext = GetDC(hWnd);
int letWindowsChooseThisPixelFormat;
letWindowsChooseThisPixelFormat = ChoosePixelFormat(ourWindowHandleToDeviceContext, &pfd);
SetPixelFormat(ourWindowHandleToDeviceContext, letWindowsChooseThisPixelFormat, &pfd);
HGLRC ourOpenGLRenderingContext = wglCreateContext(ourWindowHandleToDeviceContext);
wglMakeCurrent(ourWindowHandleToDeviceContext, ourOpenGLRenderingContext);
if (!gladLoadGLLoader((GLADloadproc)GetAnyGLFuncAddress)) {
MessageBoxA(0, "Failed to initialize GLAD", "Error", 0);
PostQuitMessage(0);
} else {
MessageBoxA(0, (char *)glGetString(GL_VERSION), "OPENGL VERSION", 0);
}
//wglMakeCurrent(ourWindowHandleToDeviceContext, NULL); Unnecessary; wglDeleteContext will make the context not current
wglDeleteContext(ourOpenGLRenderingContext);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment