To be clear, I've extensively tested this code and found the issue is somewhere with the code written prior to the WGL code. It's precisely in the WIN32 code. Now I think it could partially be caused by calling gluOrth2D but even then, that shouldn't be the primary cause as far I as I understand. I may figure this out myself just by messing with stuff but considering this issue (that occured in a much larger project) has taken up a lot of my time I thought it was worth posting this encase anyone else runs into the same problem. I'm hoping for an explanation as well as fix/correction.
#include <GL/glew.h>
#include <glfw3.h>
#define GLFW_EXPOSE_NATIVE_WGL
#define GLFW_EXPOSE_NATIVE_WIN32
#include <glfw3native.h>
constexpr int width = 1000, height = 1000;
bool running = true;
LRESULT CALLBACK WIN32_processMessage(HWND hwnd, UINT msg,
WPARAM wparam, LPARAM lparam)
{
return DefWindowProcA(hwnd, msg, wparam, lparam);
}
int main()
{
HINSTANCE hinst = GetModuleHandleA(NULL);
HICON icon = LoadIcon(hinst, IDI_APPLICATION);
WNDCLASSA* wc = (WNDCLASSA*)memset(malloc(sizeof(WNDCLASSA)), 0, sizeof(WNDCLASSA));
if (wc == NULL)
{
return -1;
}
wc->style = CS_DBLCLKS;
wc->lpfnWndProc = WIN32_processMessage;
wc->cbClsExtra = 0;
wc->cbWndExtra = 0;
wc->hInstance = hinst;
wc->hIcon = icon;
wc->hCursor = LoadCursor(NULL, IDC_ARROW);
wc->hbrBackground = NULL;
wc->lpszClassName = "toast_window_class";
if (!RegisterClassA(wc))
{
return 1;
}
UINT32 windowX = 100;
UINT32 windowY = 100;
UINT32 windowWidth = width;
UINT32 windowHeight = height;
UINT32 windowStyle = WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION;
UINT32 windowExStyle = WS_EX_APPWINDOW;
windowStyle |= WS_MAXIMIZEBOX;
windowStyle |= WS_MINIMIZEBOX;
windowStyle |= WS_THICKFRAME;
RECT borderRect = { 0,0,0,0 };
AdjustWindowRectEx(&borderRect, windowStyle, 0, windowExStyle);
windowX += borderRect.left;
windowY += borderRect.top;
windowWidth += borderRect.right - borderRect.left;
windowWidth += borderRect.bottom - borderRect.top;
HWND window = CreateWindowExA(windowExStyle, "toast_window_class", (LPCSTR)"test",
windowStyle, windowX, windowY, windowWidth, windowHeight, 0, 0, hinst, 0);
if (window == 0)
{
return 2;
}
bool shouldActivate = true;
const int showWindowCommandFlags = shouldActivate ? SW_SHOW : SW_SHOWNOACTIVATE;
ShowWindow(window, showWindowCommandFlags);
// WGL -----------------------------------------------------------
HDC device = GetDC(window);
PIXELFORMATDESCRIPTOR pfd;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cAlphaBits = 0;
pfd.cAccumBits = 0;
pfd.cDepthBits = 0;
pfd.cStencilBits = 0;
pfd.cAuxBuffers = 0;
pfd.iLayerType = PFD_MAIN_PLANE;
SetPixelFormat(device, ChoosePixelFormat(device, &pfd), &pfd);
HGLRC render = wglCreateContext(device);
wglMakeCurrent(device, render);
// GLEW ---------------------------------------------------------
const GLint res = glewInit();
if (GLEW_OK != res)
{
return 1;
}
// setup OpenGL --------------------------------------------------
gluOrtho2D(0, width, 0, height);
// main loop -----------------------------------------------------
while (running)
{
glClear(GL_COLOR_BUFFER_BIT);
// rendering
glBegin(GL_POINTS);
for (int x = 10; x < width - 10; ++x)
{
for (int y = 10; y < height - 10; ++y)
{
glVertex2f(x, y);
}
}
glEnd();
SwapBuffers(device);
}
return 0;
}