This repository was archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
124 lines (96 loc) · 2.81 KB
/
main.cpp
File metadata and controls
124 lines (96 loc) · 2.81 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
#include <windows.h>
#include <vector>
#include <cstdlib>
#include "object.cpp"
std::vector<Object> objects;
void initialize(int count, int width, int height) {
for (int i = 0; i < count; ++i) {
Point position = Point(
rand() % (width - 50),
rand() % (height - 50));
float dx = (rand() % 5) + 1;
float dy = (rand() % 5) + 1;
objects.emplace_back(position, -1, -1);
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
static HDC hdc_buffer;
static HBITMAP hbm_buffer;
static HBRUSH background_brush;
switch (uMsg) {
case WM_CREATE: {
HDC hdc = GetDC(hwnd);
RECT rect;
GetClientRect(hwnd, &rect);
hdc_buffer = CreateCompatibleDC(hdc);
hbm_buffer = CreateCompatibleBitmap(hdc, rect.right, rect.bottom);
SelectObject(hdc_buffer, hbm_buffer);
background_brush = CreateSolidBrush(RGB(255, 255, 255));
ReleaseDC(hwnd, hdc);
break;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT rect;
GetClientRect(hwnd, &rect);
FillRect(hdc_buffer, &rect, background_brush);
for (const auto& object : objects) {
object.draw(hdc);
}
BitBlt(hdc, 0, 0, rect.right, rect.bottom, hdc_buffer, 0, 0, SRCCOPY);
EndPaint(hwnd, &ps);
return 0;
}
case WM_TIMER: {
RECT rect;
GetClientRect(hwnd, &rect);
for (auto& obj : objects) {
obj.update();
obj.bounce(rect.right, rect.bottom);
}
InvalidateRect(hwnd, nullptr, TRUE); // Trigger WM_PAINT
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
// Entry Point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
const char CLASS_NAME[] = "Object Demonstration";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
"Moving Objects",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
800, 600,
nullptr, nullptr, hInstance, nullptr
);
if (hwnd == nullptr) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
initialize(3, 800, 600);
SetTimer(hwnd, 1, 16, nullptr);
MSG msg = {};
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
extern "C" {
int main() {
HINSTANCE hInstance = GetModuleHandle(nullptr);
return WinMain(hInstance, nullptr, GetCommandLineA(), SW_SHOWNORMAL);
}
}