Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Created November 21, 2012 17:25
Show Gist options
  • Select an option

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

Select an option

Save neuro-sys/4126221 to your computer and use it in GitHub Desktop.
foo
#include <windows.h>
#include <stdio.h>
#include <math.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
HANDLE hndc;
HWND hwnd;
HDC hdc;
HGLRC hglrc;
int i;
int fps;
int omx, omy, mx, my;
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);
glLoadIdentity();
gluLookAt(0, 0, 2,
0, 0, 0,
0, 1, 0);
glPushMatrix();
glTranslatef(0, 0, -5);
//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);
if (i >= 360) i = 0;
}
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();
}
void destroyGL(void)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hglrc);
ReleaseDC(hwnd, hdc);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
char text[255];
switch(msg) {
case WM_CREATE:
break;
case WM_PAINT:
return 0;
case WM_MOUSEMOVE:
omx = mx;
omy = my;
mx = LOWORD(lparam);
my = HIWORD(lparam);
if (mx & 1 << 15) mx -= (1 << 16);
if (my & 1 << 15) my -= (1 << 16);
sprintf(text, "mx: %d\tmy: %d\n", mx, my);
WriteConsole(hndc, text, strlen(text), NULL, NULL);
return 0;
case WM_CHAR:
switch(wparam) {
case 27:
PostQuitMessage(0);
break;
}
return 0;
case WM_DESTROY:
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();
AllocConsole();
hndc = GetStdHandle(STD_OUTPUT_HANDLE);
ShowWindow(hwnd, SW_SHOW);
WriteConsole(hndc, "hello", strlen("hello"), NULL, NULL);
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 100.
if (t2 <= 1000 / FPS) Sleep(1000 / FPS - t2);
}
destroyGL();
return 0;
}
int WINAPI StubWinMainCRTStartup(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