-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
134 lines (113 loc) · 3.99 KB
/
main.c
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
#include <Windows.h>
#include "graphic.h"
// Global entry for HWND
HWND globalHWND;
HDC backDC;
BOOL SIZE_CHANGED = 0;
LRESULT WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void rendering();
int WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nShowCmd
) {
WNDCLASSEX wnd = { 0 };
wnd.cbSize = sizeof(wnd);
wnd.hInstance = 0;
wnd.lpszClassName = L"wnd";
wnd.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wnd.lpfnWndProc = WndProc;
wnd.hCursor = LoadCursor(0, IDC_ARROW);
wnd.hIcon = LoadIcon(0, IDI_APPLICATION);
RegisterClassEx(&wnd);
globalHWND = CreateWindowEx(0, L"wnd", L"Test", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, 0, 0, 0, 0);
CreateThread(0, 0, rendering, 0, 0, 0);
MSG msg = { 0 };
while (GetMessage(&msg, globalHWND, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps = { 0 };
HDC hdc;
static RECT clientRect;
switch (msg) {
case WM_CREATE:
break;
case WM_DESTROY:
ExitProcess(0);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
//GetClientRect(hWnd, &clientRect);
EndPaint(hWnd, &ps);
break;
case WM_ERASEBKGND:
return 1;
case WM_SIZING:
SIZE_CHANGED = 1;
return DefWindowProc(hWnd, msg, wParam, lParam);
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
void renderRoutine(Canvas* canvas) {
srand((unsigned int)time(NULL)); // Èíèöèàëèçàöèÿ ãåíåðàòîðà ñëó÷àéíûõ ÷èñåë
POINT pt0 = { 0, 0 }; // Íà÷àëüíàÿ òî÷êà â öåíòðå
POINT ptRandom; // Ñëó÷àéíàÿ òî÷êà
RECT clientRect = canvas_getClientRect(canvas);
// Ðèñóåì 1000 ñëó÷àéíûõ ëèíèé
for (int i = 0; i < 10; ++i) {
// Ãåíåðèðóåì ñëó÷àéíûå êîîðäèíàòû äëÿ êîíå÷íîé òî÷êè
ptRandom.x = rand() % (clientRect.right) - (clientRect.right / 2);
ptRandom.y = rand() % (clientRect.bottom) - (clientRect.bottom / 2);
// Âûáèðàåì ñëó÷àéíûé öâåò äëÿ êàæäîé ëèíèè
COLORREF randomColor = RGB(rand() % 256, rand() % 256, rand() % 256);
// Ðèñóåì ëèíèþ
drawLine(canvas, pt0, ptRandom, randomColor);
}
}
void rendering() {
HDC hdc = GetDC(globalHWND);
RECT clientRect;
GetClientRect(globalHWND, &clientRect);
backDC = CreateCompatibleDC(hdc);
HBITMAP bmpMem = CreateCompatibleBitmap(hdc, clientRect.right, clientRect.bottom);
DeleteObject(SelectObject(backDC, bmpMem));
HBRUSH hbrBkg = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
Canvas* canvas = createCanvas(globalHWND, backDC);
// Èíèöèàëèçàöèÿ òàéìåðà äëÿ FPS
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
LARGE_INTEGER t1, t2;
double elapsedTime;
TCHAR fpsString[50];
BOOL running = TRUE;
while (running) {
QueryPerformanceCounter(&t1);
if (SIZE_CHANGED) {
GetClientRect(globalHWND, &clientRect);
updateRect_canvas(canvas, &clientRect);
bmpMem = CreateCompatibleBitmap(hdc, clientRect.right, clientRect.bottom);
DeleteObject(SelectObject(backDC, bmpMem));
SIZE_CHANGED = 0;
}
FillRect(backDC, &clientRect, hbrBkg);
renderRoutine(canvas);
// Âû÷èñëåíèå è îòîáðàæåíèå FPS
QueryPerformanceCounter(&t2);
elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
int frameRate = (int)(1000.0 / elapsedTime + 0.5);
wsprintf(fpsString, TEXT("FPS: %d"), frameRate);
SetTextColor(backDC, RGB(0, 0, 0));
SetBkMode(backDC, TRANSPARENT);
TextOut(backDC, 10, 10, fpsString, lstrlen(fpsString));
BitBlt(hdc, 0, 0, clientRect.right, clientRect.bottom, backDC, 0, 0, SRCCOPY);
}
DeleteObject(hbrBkg);
DeleteObject(bmpMem);
DeleteDC(backDC);
ReleaseDC(globalHWND, hdc);
}