Last active
December 13, 2015 23:59
-
-
Save mini3d/4995871 to your computer and use it in GitHub Desktop.
Template for win32 OpenGL app.
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
#define WIN32_LEAN_AND_MEAN | |
#include <windows.h> | |
#include <gl/GL.h> | |
const bool IS_EVENT_DRIVEN = true; // TRUE: Wait for update event. FALSE: 30 fps | |
bool isRunning = true; | |
bool needsRefresh = true; | |
// Window Callback | |
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); | |
// Define Pixel format | |
PIXELFORMATDESCRIPTOR defaultPixelFormatDescriptor = { 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, 0, 0, PFD_MAIN_PLANE }; | |
// Create window class | |
const wchar_t* WNDCLASSNAME = L"TEST_OPENGL_WNDCLASSEX"; | |
const WNDCLASSEX WINDOW_CLASS_TEMPLATE = { sizeof(WNDCLASSEX), CS_OWNDC, &WndProc, 0, 0, GetModuleHandle(NULL), 0, LoadCursor(NULL , IDC_ARROW), 0, 0, WNDCLASSNAME }; | |
ATOM windowClassAtom = RegisterClassEx(&WINDOW_CLASS_TEMPLATE); | |
// Create window | |
HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, WNDCLASSNAME, L"OpenGL Window", WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, 0, 0, GetModuleHandle(NULL), 0); | |
// Set pixel format | |
HDC hDC = GetDC(hWnd); | |
int pixelFormat = ChoosePixelFormat(hDC, &defaultPixelFormatDescriptor); | |
BOOL result = SetPixelFormat(hDC, pixelFormat, &defaultPixelFormatDescriptor); | |
// Create Rendering context | |
HGLRC hRC = wglCreateContext(hDC); | |
// Get WindowSize | |
unsigned int GetWindowWidth(HWND hWnd) { RECT rect; GetClientRect(hWnd, &rect); return rect.right - rect.left; } | |
unsigned int GetWindowHeight(HWND hWnd) { RECT rect; GetClientRect(hWnd, &rect); return rect.bottom - rect.top; } | |
// Paint | |
void Paint() | |
{ | |
// Set the viewport in case the window size has changed | |
glViewport(0,0, GetWindowWidth(hWnd), GetWindowHeight(hWnd)); | |
glClearDepth(1.0); | |
glClearColor(0.5, 0.5, 0.5, 0.0); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
// Set up the drawing surface | |
glLoadIdentity(); | |
glOrtho(0, GetWindowWidth(hWnd), GetWindowHeight(hWnd), 0, 1, 0); | |
// OpenGL drawing goes here! | |
glColor3d(1.0, 1.0, 1.0); | |
glBegin(GL_LINES); | |
glVertex3f(0, 0, 0); | |
glVertex3f(100, 100, 0); | |
glEnd(); | |
// Swap buffers | |
SwapBuffers(hDC); | |
needsRefresh = false; | |
} | |
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) | |
{ | |
switch (msg) { | |
case WM_PAINT: needsRefresh = true; break; | |
case WM_SIZE: needsRefresh = true; break; | |
case WM_CLOSE: isRunning = false; break; | |
case WM_KEYDOWN: isRunning = !(wParam == VK_ESCAPE); break; | |
} | |
return DefWindowProc(hWnd, msg, wParam, lParam); | |
} | |
// Main | |
int main() | |
{ | |
ShowWindow(hWnd, SW_SHOW); | |
wglMakeCurrent(hDC, hRC); | |
while(isRunning == true) | |
{ | |
if (IS_EVENT_DRIVEN) WaitMessage(); | |
else Sleep(1000/30); | |
// Handle Events | |
MSG msg; | |
if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) | |
DispatchMessage(&msg); | |
// Update as needed | |
if (!IS_EVENT_DRIVEN || needsRefresh) | |
Paint(); | |
} | |
wglDeleteContext(hRC); | |
ReleaseDC(hWnd, hDC); | |
DestroyWindow(hWnd); | |
UnregisterClass(WNDCLASSNAME, GetModuleHandle(0)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment