Created
November 23, 2012 15:54
-
-
Save neuro-sys/4136228 to your computer and use it in GitHub Desktop.
gl2d
This file contains hidden or 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
| #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 | |
| RECT rect; | |
| HANDLE hndc; | |
| HWND hwnd; | |
| HDC hdc; | |
| HGLRC hglrc; | |
| int i; | |
| int fps; | |
| void draw_pixels(void) | |
| { | |
| int x, y; | |
| glBegin(GL_POINTS); | |
| for (y = 0; y < H; y++) | |
| for (x = 0; x < W; x++) { | |
| glColor3b(x, y, 100); | |
| glVertex2f(x, y); | |
| } | |
| glEnd(); | |
| } | |
| void render(void) | |
| { | |
| glClear(GL_COLOR_BUFFER_BIT); | |
| glMatrixMode(GL_MODELVIEW); | |
| glLoadIdentity(); | |
| draw_pixels(); | |
| 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); | |
| glDisable(GL_DEPTH_TEST); | |
| glClear(GL_COLOR_BUFFER_BIT); | |
| glMatrixMode(GL_PROJECTION); | |
| glLoadIdentity(); | |
| glOrtho(0, W, H, 0, -1, 1); | |
| 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) | |
| { | |
| switch(msg) { | |
| case WM_CREATE: | |
| break; | |
| case WM_PAINT: | |
| return 0; | |
| case WM_MOUSEMOVE: | |
| 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); | |
| } | |
| void _ZeroMemory(void *dest, int count) | |
| { | |
| __asm { | |
| mov ecx, count | |
| mov edx, dest | |
| xorps xmm0, xmm0 | |
| @loop: | |
| movdqa [edx], xmm0 | |
| add edx, 16 | |
| sub ecx, 16 | |
| jg @loop | |
| ret | |
| } | |
| } | |
| 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); | |
| GetWindowRect(hwnd, &rect); | |
| 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