-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.c
More file actions
149 lines (127 loc) · 5.15 KB
/
window.c
File metadata and controls
149 lines (127 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <windows.h>
#include <tchar.h>
#include <math.h>
#include <stdio.h>
#define PI 3.13159265358979323846
// Window Procedure: Handles messages sent to the window
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static int rotX = 100;
static int rotY = 100;
static int posX = 100;
static int posY = 100;
static double angle = 0.0;
RECT rcrect;
switch (uMsg)
{
case WM_DESTROY:
KillTimer(hwnd, 1);
PostQuitMessage(0); // Signal that the application should exit
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// get window's client rectangle. We need this for bitmap creation.
// create memory DC and memory bitmap where we shall do our drawing
HDC memDC = CreateCompatibleDC(hdc);
// get window's client rectangle. We need this for bitmap creation.
GetClientRect(hwnd, &rcrect);
// now we can create bitmap where we shall do our drawing
HBITMAP bmp = CreateCompatibleBitmap(hdc,
rcrect.right - rcrect.left,
rcrect.bottom - rcrect.top);
// we need to save original bitmap, and select it back when we are done,
// in order to avoid GDI leaks!
HBITMAP oldBmp = (HBITMAP)SelectObject(memDC, bmp);
HBRUSH hBrush = CreateSolidBrush(RGB(255, 0, 0));
SelectObject(memDC, hBrush);
//----------------
FillRect(memDC, &rcrect, (HBRUSH)(COLOR_WINDOW + 1));
int cx = (rcrect.right - rcrect.left) / 2;
int cy = (rcrect.bottom - rcrect.top) / 2;
// Use static angle, calculate rotating endpoint
double radius = 200; // Fixed radius for visible rotation
double offset = 100;
double x2 = cx + radius * cos(angle);
double y2 = cy + radius * sin(angle);
double x3 = cx + radius * cos(angle + PI);
double y3 = cy + radius * sin(angle + PI);
double x4 = cx + radius * cos(angle + (PI / 2));
double y4 = cy + radius * sin(angle + (PI / 2));
MoveToEx(memDC, (int)x3, (int)y3, NULL); // Start at center
LineTo(memDC, (int)x2, (int)y2); // Draw to rotating endpoint
MoveToEx(memDC, (int)x3, (int)y3, NULL); // Start at center
LineTo(memDC, (int)x4, (int)y4); // Draw to rotating endpoint
//-----------------------------------------------------
// OK, everything is drawn into memory DC,
// now is the time to draw that final result into our target DC
BitBlt(hdc, 0, 0, rcrect.right - rcrect.left,
rcrect.bottom - rcrect.top, memDC, 0, 0, SRCCOPY);
// all done, now we need to cleanup
DeleteObject(hBrush);
SelectObject(memDC, oldBmp); // select back original bitmap
DeleteObject(bmp); // delete bitmap since it is no longer required
DeleteDC(memDC); // delete memory DC since it is no longer required
EndPaint(hwnd, &ps);
return 0;
}
case WM_CREATE:
// ID + time in ms
SetTimer(hwnd, 1, 16, NULL);
return 0;
case WM_TIMER:
angle += 0.05;
InvalidateRect(hwnd, NULL, FALSE);
return 0;
default:
// Let Windows handle any messages we don't explicitly handle
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
// Entry point for a graphical Windows application
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
char appInName[] = "WindowApp";
char appName[] = "TestGame";
// 1. Define and register the window class
WNDCLASSEX wc = {0}; // Initialize to zero
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WindowProc; // Pointer to our window procedure
wc.hInstance = hInstance;
wc.lpszClassName = appInName;
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error", MB_ICONERROR | MB_OK);
return 0;
}
// 2. Create the window
HWND hwnd = CreateWindowEx(
0, // Optional window styles
appInName, // Window class name
appName, // Window title
WS_OVERLAPPEDWINDOW | WS_VISIBLE, // Window style
CW_USEDEFAULT, CW_USEDEFAULT, // Position (x, y)
800, 600, // Size (width, height)
NULL, // Parent window handle
NULL, // Menu handle
hInstance, // Instance handle
NULL // Additional creation parameters
);
if (hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error", MB_ICONERROR | MB_OK);
return 0;
}
// 3. Show the window
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// 4. Message loop: Process messages until WM_QUIT is received
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}