Created
January 26, 2017 20:21
-
-
Save TheServer201/49ff1f6a15c9062d47b39a0613b1c8ff to your computer and use it in GitHub Desktop.
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
// gcc -Os -s -municode -nostartfiles -mfpmath=both -march=core2 -e __main -Wl,-gc-sections win.c -o win -luser32 -lkernel32 -lgdi32 -lopengl32 | |
#include <windows.h> | |
#include <stdint.h> | |
#include <gl/gl.h> | |
#include <stdio.h> | |
#include <time.h> | |
typedef struct { | |
GLfloat x, y; | |
} Vector2; | |
Vector2 CalculateBezierPoint(float t, Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3){ | |
float tt = t * t, ttt = tt * t, u = 1.0 - t, uu = u * u, uuu = uu * u; | |
Vector2 p; | |
p.x = p0.x * uuu + 3.0 * p1.x * t * uu + 3.0 * p2.x * tt * u + p3.x * ttt; | |
p.y = p0.y * uuu + 3.0 * p1.y * t * uu + 3.0 * p2.y * tt * u + p3.y * ttt; | |
return p; | |
} | |
void DrawRectangle(Vector2 *Vertex, Vector2 Coords[2], GLfloat Radius){ | |
if(Radius){ | |
Vertex[0].x = (Coords[0].x + Coords[1].x) / 2.0; | |
Vertex[0].y = (Coords[0].y + Coords[1].y) / 2.0; | |
Vector2 p0, p1, p2; | |
p2.x = Coords[1].x - Radius * (Coords[0].y - Coords[1].y); | |
p2.y = Coords[0].y; | |
p1.x = Coords[1].x; | |
p1.y = Coords[0].y; | |
p0.x = Coords[1].x; | |
p0.y = Coords[1].y + (1.0 - Radius) * (Coords[0].y - Coords[1].y); | |
for(uint8_t i = 0; i < 16; i++){ | |
GLfloat t = i / 15.0; | |
Vertex[i + 1] = CalculateBezierPoint(t, p0, p1, p1, p2); | |
} | |
for(uint8_t i = 17; i < 34; i++){ | |
Vertex[i].x = -Vertex[33 - i].x; | |
Vertex[i].y = +Vertex[33 - i].y; | |
} | |
for(uint8_t i = 34; i < 67; i++){ | |
Vertex[i].x = +Vertex[67 - i].x; | |
Vertex[i].y = -Vertex[67 - i].y; | |
} | |
Vertex[67] = Vertex[32]; | |
Vertex[68] = Vertex[35]; | |
Vertex[69] = Vertex[66]; | |
Vertex[70] = Vertex[1]; | |
} else { | |
Vertex[0] = Coords[0]; | |
Vertex[1].x = Coords[1].x; | |
Vertex[1].y = Coords[0].y; | |
Vertex[2] = Coords[1]; | |
Vertex[3].x = Coords[0].x; | |
Vertex[3].y = Coords[1].y; | |
} | |
} | |
HDC hDc; | |
HGLRC hGlrc; | |
float Wheel = 0.0; | |
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){ | |
switch(uMsg){ | |
case WM_CHAR: | |
if(wParam == 0x1B) | |
goto Done; | |
wprintf(L"0x%x -> %lc\n", wParam, wParam); | |
break; | |
case WM_MOUSEWHEEL: | |
{ | |
float Temp = Wheel; | |
Wheel += (float)GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA / 25; | |
if(Wheel < 0.0 || Wheel > 0.6) | |
Wheel = Temp; | |
break; | |
} | |
case WM_SIZE: | |
glViewport(0, 0, LOWORD(lParam), HIWORD(lParam)); | |
break; | |
case WM_CLOSE: Done: | |
PostQuitMessage(0); | |
break; | |
default: | |
return DefWindowProc(hwnd, uMsg, wParam, lParam); | |
} | |
} | |
uint8_t Num = 0; | |
VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime){ | |
glClear(GL_COLOR_BUFFER_BIT); | |
glColor4f(0.0, 0.0, 0.0, 0.0); | |
Vector2 Coords[] = {-0.4, +0.4, +0.4, -0.4},//{-0.8, +0.8, -0.2, +0.2}, | |
*Quads = malloc(71 * sizeof(Vector2)); | |
DrawRectangle(Quads, Coords, Wheel); | |
glEnableClientState(GL_VERTEX_ARRAY); | |
glVertexPointer(2, GL_FLOAT, 0, Quads); | |
glDrawArrays(GL_TRIANGLE_FAN, 0, 71); | |
Num = (Num + 1) % 72; | |
free(Quads); | |
glFlush(); | |
SwapBuffers(hDc); | |
} | |
void _main(){ | |
WNDCLASSEX WndClassEx = {0}; | |
WndClassEx.cbSize = sizeof(WndClassEx); | |
WndClassEx.lpfnWndProc = WindowProc; | |
WndClassEx.lpszClassName = L"RotMG"; | |
RegisterClassEx(&WndClassEx); | |
HWND hWnd = CreateWindowEx(WS_EX_LEFT, WndClassEx.lpszClassName, NULL, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 640, 640, 0, 0, NULL, NULL); | |
hDc = GetDC(hWnd); | |
PIXELFORMATDESCRIPTOR PixelFormatDescriptor = {0}; | |
PixelFormatDescriptor.nSize = sizeof(PixelFormatDescriptor); | |
PixelFormatDescriptor.nVersion = 1; | |
PixelFormatDescriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; | |
PixelFormatDescriptor.iPixelType = PFD_TYPE_RGBA; | |
PixelFormatDescriptor.cColorBits = 24; | |
int iPixelFormat = ChoosePixelFormat(hDc, &PixelFormatDescriptor); | |
SetPixelFormat(hDc, iPixelFormat, &PixelFormatDescriptor); | |
hGlrc = wglCreateContext(hDc); | |
wglMakeCurrent(hDc, hGlrc); | |
glClearColor(1.0, 1.0, 1.0, 1.0); | |
srand(time(NULL)); | |
UINT_PTR IDEvent = SetTimer(NULL, 0, 0, TimerProc); | |
MSG Msg; | |
while(GetMessage(&Msg, NULL, 0, 0)){ | |
TranslateMessage(&Msg); | |
DispatchMessage(&Msg); | |
} | |
KillTimer(hWnd, IDEvent); | |
wglDeleteContext(hGlrc); | |
ReleaseDC(hWnd, hDc); | |
DestroyWindow(hWnd); | |
UnregisterClass(WndClassEx.lpszClassName, NULL); | |
ExitProcess(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment