-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI_impl_win.c
200 lines (164 loc) · 4.44 KB
/
GUI_impl_win.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#ifdef _WIN32
#include "GUI_impl.h"
static HANDLE Thread;
static HBITMAP bmp;
static HDC wndhdc, hdc;
static RECT screen_rect;
static HWND hwnd;
static HINSTANCE hinstance;
void Sleep_while_active(size_t ms)
{
size_t iter = ms/SLEEP_RES;
for(int i=0; current_state.conf->active && i<iter; i++){
Sleep(SLEEP_RES);
}
}
// IO functions
#ifndef CONSCREEN
struct Rect get_screen_dimensions()
{
RECT rc;
GetClientRect(hwnd, &rc);
return (struct Rect){0, 0, rc.right, rc.bottom};
}
void Draw_begin()
{
GetClientRect(hwnd, &screen_rect);
bmp = CreateCompatibleBitmap(wndhdc,screen_rect.right,screen_rect.bottom);
SelectObject(hdc,bmp);
// Clear Bitmap
FillRect(hdc,&screen_rect,GetStockObject(BLACK_BRUSH));
}
void Draw_Rect(struct Rect *rect, struct Color col)
{
RECT rc = {rect->right, rect->top, rect->left, rect->bottom};
HBRUSH brush = CreateSolidBrush(RGB(col.r,col.g,col.b));
FillRect(hdc,&rc,brush);
DeleteObject(brush);
}
/*void Draw_Text(int x, int y, const char *text, size_t len, int size, struct Color col)
{
HFONT font = CreateFontA(size,0,0,0,0,0,0,0,DEFAULT_CHARSET,OUT_CHARACTER_PRECIS,CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY,FF_DONTCARE,0);
SelectObject(hdc,font);
SetTextColor(hdc, RGB(col.r, col.g, col.b));
RECT rc = {x,y,screen_rect.right,screen_rect.bottom};
DrawTextA(hdc, text, len, &rc,0);
DeleteObject(font);
}*/
void Draw_end()
{
BitBlt(wndhdc,0,0,screen_rect.right,screen_rect.bottom,hdc,0,0,SRCCOPY);
DeleteObject(bmp);
ValidateRect(hwnd,&screen_rect);
}
static LRESULT CALLBACK _GUI_tread_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg)
{
case WM_CREATE:
{
//Init Objects
wndhdc= GetDC(hwnd);
hdc = CreateCompatibleDC(wndhdc);
RECT rc;
GetClientRect(hwnd,&rc);
//Configure HDC
SetBkColor(hdc,RGB(0,0,0));
SetBkMode(hdc,TRANSPARENT);
// Set UpdateTimer
SetTimer(hwnd,1,30,0);
break;
}
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
{
//Free
ReleaseDC(hwnd,hdc);
ReleaseDC(hwnd,wndhdc);
KillTimer(hwnd,1);
PostQuitMessage(0);
break;
}
case WM_ERASEBKGND:
break;
case WM_PAINT:
{
if(!current_state.rendering){
// UpdateScreen
GUI_render();
}
break;
}
case WM_TIMER:
{
InvalidateRgn(hwnd,0,0);
break;
}
case WM_KEYDOWN:
{
enum INPUT event=NOTHING;
switch(wParam)
{
case 'P':
event=TOGGLE_PAUSE; break;
case VK_OEM_MINUS:
case VK_SUBTRACT:
event=SPEED_UP; break;
case VK_OEM_PLUS:
case VK_ADD:
event=SPEED_DOWN; break;
}
handleInput(event);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 1;
}
static DWORD WINAPI GUI_thread_proc(LPVOID unused){
// Create Windowclass
const char* Name="SortTest";
WNDCLASSEXA wc = {0};
wc.cbSize=sizeof(wc);
wc.lpfnWndProc=_GUI_tread_proc;
wc.lpszClassName=Name;
wc.lpszMenuName=Name;
wc.hbrBackground = GetStockObject(0);
if(RegisterClassExA(&wc)){
// Create and maintain Window
hwnd = CreateWindowExA(WS_EX_OVERLAPPEDWINDOW, Name, Name, WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hinstance, NULL);
if(hwnd){
current_state.conf->active=1;
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
UnregisterClassA(Name,hinstance);
}
current_state.conf->active=0;
return 0;
}
int GUI_impl_create()
{
SECURITY_ATTRIBUTES sa = {0};
hinstance=GetModuleHandle(0);
Thread = CreateThread(&sa,0,GUI_thread_proc, NULL,0,0);
return 0;
}
int GUI_impl_destroy()
{
PostMessageA(hwnd,WM_DESTROY,0,0);
WaitForSingleObject(Thread,INFINITE);
return 0;
}
void GUI_impl_update()
{
//PostMessageA(hwnd,WM_PAINT,0,0);
}
#endif
#endif