Last active
April 27, 2024 06:22
-
-
Save skeeto/96a8121fa95a299faf79294d7f5e7c3c to your computer and use it in GitHub Desktop.
Draw a triangle on Windows using OpenGL 1.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Draw a triangle on Windows using OpenGL 1.1 | |
// $ gcc -mwindows -o triangle triangle.c -lopengl32 | |
// This is free and unencumbered software released into the public domain. | |
#define WIN32_LEAN_AND_MEAN | |
#include <windows.h> | |
#include <GL/gl.h> | |
#define countof(a) (int)(sizeof(a) / (sizeof(*(a)))) | |
static LRESULT CALLBACK handler(HWND h, UINT msg, WPARAM wparam, LPARAM lparam) | |
{ | |
switch (msg) { | |
case WM_CLOSE: | |
PostQuitMessage(0); | |
return 0; | |
} | |
return DefWindowProcA(h, msg, wparam, lparam); | |
} | |
int WINAPI WinMain(HINSTANCE h, HINSTANCE p, LPSTR cmd, int show) | |
{ | |
WNDCLASSA wc = {0}; | |
wc.lpfnWndProc = handler; | |
wc.lpszClassName = "gl"; | |
RegisterClassA(&wc); | |
DWORD style = WS_MINIMIZEBOX | WS_SYSMENU; | |
HWND wnd = CreateWindowA( | |
"gl", "Demo", style, | |
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, | |
0, 0, 0, 0 | |
); | |
PIXELFORMATDESCRIPTOR pfd = {0}; | |
pfd.nSize = sizeof(pfd); | |
pfd.nVersion = 1; | |
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; | |
HDC dc = GetDC(wnd); | |
int format = ChoosePixelFormat(dc, &pfd); | |
SetPixelFormat(dc, format, &pfd); | |
HGLRC ctx = wglCreateContext(dc); | |
wglMakeCurrent(dc, ctx); | |
ShowWindow(wnd, SW_NORMAL); | |
for (;;) { | |
for (MSG msg; PeekMessageA(&msg, 0, 0, 0, TRUE);) { | |
if (msg.message == WM_QUIT) { | |
ExitProcess(0); | |
} | |
TranslateMessage(&msg); | |
DispatchMessageA(&msg); | |
} | |
glClearColor(0, 0, 0, 0); | |
glClear(GL_COLOR_BUFFER_BIT); | |
float triangle[] = {-0.5f,-0.5f, 0.0f,0.5f, 0.5f,-0.5f}; | |
glVertexPointer(2, GL_FLOAT, 0, triangle); | |
glColor3f(1, 0, 1); | |
glEnableClientState(GL_VERTEX_ARRAY); | |
glDrawArrays(GL_TRIANGLES, 0, countof(triangle)/2); | |
SwapBuffers(dc); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment