Skip to content

Instantly share code, notes, and snippets.

@nlguillemot
Created August 30, 2015 02:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlguillemot/62ca78c412cad9daf268 to your computer and use it in GitHub Desktop.
Save nlguillemot/62ca78c412cad9daf268 to your computer and use it in GitHub Desktop.
BasicGL
#include <Windows.h>
#include <GL/gl.h>
#pragma comment(lib, "OpenGL32.lib")
LRESULT CALLBACK MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CLOSE:
ExitProcess(0);
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(wc));
wc.cbSize = sizeof(wc);
wc.style = CS_OWNDC;
wc.lpfnWndProc = MyWndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
wc.lpszClassName = TEXT("WindowClass");
RegisterClassEx(&wc);
RECT wr = { 0, 0, 640, 480 };
AdjustWindowRect(&wr, 0, FALSE);
HWND hWnd = CreateWindowEx(
0, TEXT("WindowClass"),
TEXT("BasicGL"),
WS_OVERLAPPEDWINDOW,
0, 0, wr.right - wr.left, wr.bottom - wr.top,
0, 0, hInstance, 0);
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
pfd.iLayerType = PFD_MAIN_PLANE;
HDC hDC = GetDC(hWnd);
int chosenPixelFormat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, chosenPixelFormat, &pfd);
HGLRC hGLRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hGLRC);
ShowWindow(hWnd, SW_SHOWNORMAL);
while (1)
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3ub(255, 0, 0);
glVertex2f(-1.0f, -1.0f);
glColor3ub(0, 255, 0);
glVertex2f(1.0f, -1.0f);
glColor3ub(0, 0, 255);
glVertex2f(0.0f, 1.0f);
glEnd();
SwapBuffers(hDC);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment