Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Created November 20, 2012 09:46
Show Gist options
  • Select an option

  • Save neuro-sys/5fb74d795259a1a58f84 to your computer and use it in GitHub Desktop.

Select an option

Save neuro-sys/5fb74d795259a1a58f84 to your computer and use it in GitHub Desktop.
WinAPI barebones skeleton with OpenGL context set up.
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#define WIN32_LEAN_AND_MEAN
#define WCLASSNAME "HEDE"
#define W 640
#define H 480
#define PI (3.141592653589793)
#define A2R(A) A* PI / 180
HWND hwnd;
HDC hdc;
HGLRC hglrc;
int i;
int fps;
double factorial(int num){
double fact = 1;
for(int i = 1; i <= num; i++){
fact *= i;
}
return fact;
}
double pow(float base, float exp)
{
double r = 1.;
int i;
for (i=0; i<exp; i++) r*= (exp>0)?base:1./base;
return r;
}
double sin(double num){
double value = 0;
for(int n = 0; n < 19; n++){
value += pow(-1.0, n) * pow(num, 2*n+1) / factorial(2*n + 1);
}
return value;
}
double cos(double num) {
return sin(num + PI/2.);
}
void render(void)
{
double iS = sin(A2R(i++));
double t = 1 + iS;
double t_normal = t / 2; /* (t - min) / (max - min) */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glRotatef(i, 1, 1, 1);
glBegin(GL_QUADS);
glColor3f(t_normal, (1 + cos(t_normal)) / 2, (1 + cos(t_normal)) / 2); glVertex3f( 1.0f, 1.0f, 0.0f );
glColor3f((1 + sin(t_normal / 2)) / 2, t_normal, (1 + cos(t_normal / 2)) / 2); glVertex3f( 1.0f, -1.0f, 0.0f );
glColor3f((1 + cos(t_normal)) / 2, (1 + sin(t_normal)) / 2, t_normal); glVertex3f( -1.0f, -1.0f, 0.0f );
glColor3f((1 + cos(t_normal)) / 2, (1 + sin(t_normal)) / 2, t_normal); glVertex3f( -1.0f, 1.0f, 0.0f );
glEnd();
glPopMatrix();
SwapBuffers(hdc);
}
void initGL(void)
{
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA;
hdc = GetDC(hwnd);
SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd);
hglrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hglrc);
glPolygonMode(GL_FRONT_AND_BACK, GL_FLAT);
glMatrixMode(GL_VIEWPORT);
glLoadIdentity;
glViewport(0, 0, W, H);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65, (float) W/H, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 3,
0, 0, 0,
0, 1, 0);
}
void destroyGL(void)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hglrc);
ReleaseDC(hwnd, hdc);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg) {
case WM_CREATE:
break;
case WM_PAINT:
return 0;
case WM_DESTROY:
destroyGL();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
int frame_count;
long then;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASS wc;
MSG msg;
ZeroMemory(&wc, sizeof(WNDCLASS));
wc.style = CS_OWNDC | WS_OVERLAPPED;
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.lpszClassName = WCLASSNAME;
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
if (!(RegisterClass(&wc)))
return 0;
hwnd = CreateWindow(WCLASSNAME, "meep", WS_OVERLAPPED | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT,
W, H, HWND_DESKTOP, NULL, hInstance, NULL);
if (!hwnd)
return 0;
initGL();
ShowWindow(hwnd, SW_SHOW);
then = GetTickCount() + 1000;
while (1)
{
long t1, t2;
t1 = GetTickCount();
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
render();
t2 = GetTickCount() - t1;
frame_count++;
if (GetTickCount() > then) {
then = GetTickCount() + 1000;
fps = frame_count;
frame_count = 0;
}
#define FPS 60.
if (t2 <= 1000 / FPS) Sleep(1000 / FPS - t2);
}
return 0;
}
int WINAPI WinMainCRTStartup(void)
{
ExitProcess(WinMain(GetModuleHandle(NULL), NULL, NULL, 0));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment