Skip to content

Instantly share code, notes, and snippets.

@zao
Created July 8, 2014 08:25
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 zao/bed34529ee2760781c5c to your computer and use it in GitHub Desktop.
Save zao/bed34529ee2760781c5c to your computer and use it in GitHub Desktop.
winless on Windows
#define NOMINMAX 1
#include <Windows.h>
#include <GL/gl.h>
#include <stdio.h>
#pragma comment (lib, "opengl32.lib")
typedef struct WindowData
{
HDC dc;
HGLRC rc;
} WindowData;
LRESULT CALLBACK WindowProcedure(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_CREATE:
{
WindowData* wd = calloc(1, sizeof(WindowData));
SetWindowLongPtr(wnd, GWLP_USERDATA, (LONG_PTR)(void*)wd);
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24,
8,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
HDC dc = GetDC(wnd);
wd->dc = dc;
int pf = ChoosePixelFormat(dc, &pfd);
SetPixelFormat(dc, pf, &pfd);
wd->rc = wglCreateContext(dc);
wglMakeCurrent(dc, wd->rc);
char buf[2048];
sprintf_s(buf, sizeof(buf), "OpenGL version %s\n", glGetString(GL_VERSION));
OutputDebugStringA(buf);
break;
}
case WM_DESTROY:
{
WindowData* wd = (WindowData*)(void*)GetWindowLongPtr(wnd, GWLP_USERDATA);
wglMakeCurrent(wd->dc, NULL);
wglDeleteContext(wd->rc);
free(wd);
PostQuitMessage(0);
break;
}
}
return DefWindowProc(wnd, msg, wparam, lparam);
}
HWND MakeWindow()
{
WNDCLASSEX wc;
ATOM classAtom;
HMODULE mod;
GetModuleHandleEx(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCWSTR)&WindowProcedure,
&mod);
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(wc);
wc.style = CS_OWNDC;
wc.lpfnWndProc = &WindowProcedure;
wc.hInstance = (HINSTANCE)mod;
wc.lpszClassName = L"windowless-opengl";
classAtom = RegisterClassEx(&wc);
return CreateWindowEx(0, (LPCWSTR)classAtom, L"windowless-opengl",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 256, 256,
NULL, NULL, (HINSTANCE)mod, NULL);
}
int WINAPI WinMain(HINSTANCE appInstance, HINSTANCE prevInstance, LPSTR commandLine, int showWindow)
{
HWND wnd;
wnd = MakeWindow();
GLint tex;
DestroyWindow(wnd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment