-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
479 lines (387 loc) · 11.6 KB
/
main.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include "module.hpp"
#include "modules/screensaver.hpp"
#include "modules/mainmenu.hpp"
#include "modules/lightroom.hpp"
#include "modules/tramview.hpp"
#include "modules/powerview.hpp"
#include "modules/mateview.hpp"
#include "modules/infoview.hpp"
#include "modules/eventsview.hpp"
#include "fontrenderer.hpp"
#include "rendering.hpp"
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <chrono>
#include <ctime>
#include <algorithm>
using namespace std::chrono;
SDL_Renderer * renderer;
SDL_Window * window;
SDL_Texture * home_icon;
static module * current_module;
static module * next_module;
static module * previous_module;
double total_time;
double time_step;
glm::ivec2 screen_size { 1280, 1024 };
std::filesystem::path resource_root;
void module::activate(module * other)
{
next_module = other;
}
static SDL_Texture * splash_icon;
struct splash
{
double progress = 0.0;
SDL_Point center;
SDL_Color color = { 0xFF, 0xFF, 0xFF, 0xFF };
};
int main()
{
resource_root = std::filesystem::current_path() / "resources";
srand(time(nullptr));
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
die("Failed to initialize SDL: %s", SDL_GetError());
atexit(SDL_Quit);
if(IMG_Init(IMG_INIT_PNG) == 0)
die("Failed to initialize SDL_image: %s", IMG_GetError());
atexit(IMG_Quit);
if(TTF_Init() < 0)
die("Failed to initialize TTF_image: %s", TTF_GetError());
atexit(TTF_Quit);
window = SDL_CreateWindow(
"Kiosk v5.0",
0, 0,
1280, 1024,
SDL_WINDOW_SHOWN // SDL_WINDOW_FULLSCREEN_DESKTOP
);
if(window == nullptr)
die("Failed to create window: %s", SDL_GetError());
SDL_SetHintWithPriority("SDL_HINT_RENDER_VSYNC", "0", SDL_HINT_OVERRIDE);
renderer = SDL_CreateRenderer(
window,
-1, // best possible
SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE
);
if(renderer == nullptr)
die("Failed to create renderer: %s", SDL_GetError());
{
SDL_RendererInfo info;
if(SDL_GetRendererInfo(renderer, &info) < 0)
die("Failed to query renderer info: %s", SDL_GetError());
fprintf(stdout,
"Renderer: %s\n"
"Max. Texture Size: %d*%d\n",
info.name,
info.max_texture_width,
info.max_texture_height
);
}
SDL_ShowCursor(1);
splash_icon = IMG_LoadTexture(renderer, (resource_root / "splash.png").c_str());
if(splash_icon == nullptr)
die("Failed to load splash.png: %s", SDL_GetError());
SDL_SetTextureBlendMode(splash_icon, SDL_BLENDMODE_BLEND);
home_icon = IMG_LoadTexture(renderer, (resource_root / "icons" / "home.png").c_str());
if(home_icon == nullptr)
die("Failed to load home.png: %s", SDL_GetError());
// preload all resources
module::get<screensaver>();
module::get<mainmenu>();
module::get<lightroom>();
module::get<tramview>();
module::get<powerview>();
module::get<mateview>();
module::get<infoview>();
module::get<eventsview>();
// then activate screensaver as initial screen
module::activate<screensaver>();
std::vector<splash> splashes;
SDL_Texture * frontbuffer = nullptr;
SDL_Texture * backbuffer = nullptr;
auto const recreate_rendertargets = [&]()
{
int dx, dy;
// SDL_GetWindowSize(window, &dx, &dy);
dx = 1280;
dy = 1024;
if(frontbuffer != nullptr)
SDL_DestroyTexture(frontbuffer);
if(backbuffer != nullptr)
SDL_DestroyTexture(backbuffer);
frontbuffer = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_RGBA32,
SDL_TEXTUREACCESS_TARGET,
dx, dy
);
if(frontbuffer == nullptr)
die("Failed to create frontbuffer: %s", SDL_GetError());
backbuffer = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_RGBA32,
SDL_TEXTUREACCESS_TARGET,
dx, dy
);
if(backbuffer == nullptr)
die("Failed to create backbuffer: %s", SDL_GetError());
SDL_SetTextureBlendMode(frontbuffer, SDL_BLENDMODE_BLEND);
SDL_SetTextureBlendMode(backbuffer, SDL_BLENDMODE_BLEND);
};
recreate_rendertargets();
rendering::big_font.emplace(
renderer,
TTF_OpenFont((resource_root / "fonts" / "Roboto-Regular.ttf" ).c_str(), 50)
);
rendering::medium_font.emplace(
renderer,
TTF_OpenFont((resource_root / "fonts" / "Roboto-Regular.ttf" ).c_str(), 40)
);
rendering::small_font.emplace(
renderer,
TTF_OpenFont((resource_root / "fonts" / "Roboto-Regular.ttf" ).c_str(), 25)
);
int pixel_transition_matrix[20][25];
for(auto & row : pixel_transition_matrix)
for(auto & value : row)
value = rand() % 64;
int current_transition;
double transition_progress;
auto const next_transition = [&]()
{
// current_transition = rand() % 3;
current_transition = 1; // slider
transition_progress = 0.0;
};
next_transition();
auto const startup = high_resolution_clock::now();
auto last_frame = startup;
auto last_event = startup;
while(next_module != nullptr)
{
int scr_x, scr_y;
SDL_GetWindowSize(window, &scr_x, &scr_y);
screen_size = { 1280, 1024 };
SDL_Rect const actual_screen = {
(scr_x - screen_size.x) / 2,
(scr_y - screen_size.y) / 2,
screen_size.x,
screen_size.y,
};
if(current_module != next_module)
{
if(current_module != nullptr)
current_module->leave();
previous_module = current_module;
current_module = next_module;
transition_progress = 0.0;
if(current_module != nullptr)
current_module->enter();
}
SDL_Event ev;
bool quitting = false;
while(SDL_PollEvent(&ev))
{
if((ev.type == SDL_MOUSEBUTTONDOWN) or (ev.type == SDL_KEYDOWN))
last_event = high_resolution_clock::now();
splash * splash = nullptr;
if(ev.type == SDL_MOUSEBUTTONDOWN)
{
splash = &splashes.emplace_back();
splash->center.x = ev.button.x;
splash->center.y = ev.button.y;
}
switch(ev.type)
{
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEBUTTONDOWN:
ev.button.x -= actual_screen.x;
ev.button.y -= actual_screen.y;
break;
case SDL_MOUSEMOTION:
ev.motion.x -= actual_screen.x;
ev.motion.y -= actual_screen.y;
break;
case SDL_MOUSEWHEEL:
ev.wheel.x -= actual_screen.x;
ev.wheel.y -= actual_screen.y;
break;
}
if(ev.type == SDL_QUIT)
{
next_module = nullptr;
quitting = true;
}
else if(previous_module == nullptr) // if no transition is in progress
{
auto const result = current_module->notify(ev);
if(splash != nullptr)
{
if(result == failure)
splash->color = { 0xFF, 0x80, 0x80, 0xFF };
}
}
}
auto const now = high_resolution_clock::now();
if(not quitting and (now - last_event) > minutes(5))
module::activate<screensaver>();
total_time = duration_cast<milliseconds>(now - startup).count() / 1000.0;
time_step = duration_cast<milliseconds>(now - last_frame).count() / 1000.0;
last_frame = now;
for(auto & sp : splashes)
sp.progress += time_step;
// Render transition
auto const start_time = high_resolution_clock::now();
if(previous_module != nullptr)
{
SDL_SetRenderTarget(renderer, backbuffer);
SDL_SetRenderDrawColor(renderer, 0x30, 0x30, 0x30, 0xFF);
SDL_RenderClear(renderer);
previous_module->render();
SDL_SetRenderTarget(renderer, frontbuffer);
SDL_SetRenderDrawColor(renderer, 0x30, 0x30, 0x30, 0xFF);
SDL_RenderClear(renderer);
current_module->render();
SDL_SetRenderTarget(renderer, nullptr);
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0xFF, 0xFF);
SDL_RenderClear(renderer);
switch(current_transition)
{
case 0: // alpha blend
{
SDL_RenderCopy(renderer, backbuffer, nullptr, &actual_screen);
SDL_SetTextureAlphaMod(frontbuffer, std::clamp(255.0 * pow(transition_progress, 2.0), 0.0, 255.0));
SDL_RenderCopy(renderer, frontbuffer, nullptr, &actual_screen);
transition_progress += 5.0 * time_step;
break;
}
case 1: // vertical sliding
{
int pos_y = screen_size.y * glm::smoothstep(0.0, 1.0, transition_progress);
SDL_Rect src_position { 0, 0, screen_size.x, pos_y };
SDL_Rect dst_position { actual_screen.x, actual_screen.y, screen_size.x, pos_y };
SDL_RenderCopy(renderer, frontbuffer, &src_position, &dst_position);
src_position.y = src_position.h;
src_position.h = screen_size.y - src_position.y;
dst_position = { actual_screen.x + src_position.x, actual_screen.y + src_position.y, src_position.w, src_position.h };
SDL_RenderCopy(renderer, backbuffer, &src_position, &dst_position);
dst_position.y = actual_screen.y + pos_y - 2;
dst_position.h = 5;
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderFillRect(renderer, &dst_position);
transition_progress += 2.0 * time_step;
break;
}
case 2: // pixels
{
int const w = screen_size.x / 25;
int const h = screen_size.y / 20;
int progress = 64 * transition_progress;
for(int x = 0; x < 25; x++)
{
for(int y = 0; y < 20; y++)
{
SDL_Rect src_pos = {
w * x,
h * y,
w,
h
};
SDL_Rect dst_pos = {
actual_screen.x + src_pos.x,
actual_screen.y + src_pos.y,
src_pos.w,
src_pos.h,
};
if(progress > pixel_transition_matrix[y][x])
SDL_RenderCopy(renderer, frontbuffer, &src_pos, &dst_pos);
else
SDL_RenderCopy(renderer, backbuffer, &src_pos, &dst_pos);
}
}
transition_progress += 4.5 * time_step;
break;
}
}
if(transition_progress >= 1.0)
{
next_transition();
previous_module = nullptr;
}
}
else
{
SDL_SetRenderTarget(renderer, frontbuffer);
SDL_SetRenderDrawColor(renderer, 0x30, 0x30, 0x30, 0xFF);
SDL_RenderClear(renderer);
current_module->render();
SDL_SetTextureBlendMode(frontbuffer, SDL_BLENDMODE_NONE);
SDL_SetRenderTarget(renderer, nullptr);
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0xFF, 0xFF);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, frontbuffer, nullptr, &actual_screen);
}
for(auto const & splash : splashes)
{
int size = 100 * pow(splash.progress, 0.5);
SDL_Rect rekt = {
splash.center.x - size/2,
splash.center.y - size/2,
size, size
};
int x = std::max<int>(0, 255 - 255 * pow(splash.progress, 0.5));
SDL_SetTextureAlphaMod(splash_icon, x);
SDL_SetTextureColorMod(splash_icon, splash.color.r, splash.color.g, splash.color.b);
SDL_RenderCopy(
renderer,
splash_icon,
nullptr,
&rekt
);
}
splashes.erase(
std::remove_if(splashes.begin(), splashes.end(), [](splash const & s) { return s.progress >= 1.0; }),
splashes.end()
);
auto const end_time = high_resolution_clock::now();
auto const frame_time = duration_cast<microseconds>(end_time - start_time).count();
rendering::small_font->render(
{ 10, 10, 150, 50 },
std::to_string(frame_time / 1000.0) + " ms",
FontRenderer::Left | FontRenderer::Middle,
{ 0xFF, 0x00, 0xFF, 0xFF }
);
rendering::small_font->render(
{ 10, 60, 150, 50 },
std::to_string(rendering::small_font->cache.size()) + ", "
+ std::to_string(rendering::medium_font->cache.size()) + ", "
+ std::to_string(rendering::big_font->cache.size()),
FontRenderer::Left | FontRenderer::Middle,
{ 0xFF, 0x00, 0xFF, 0xFF }
);
SDL_RenderPresent(renderer);
if(frame_time >= 16000)
fprintf(stdout, "%f ms\n", frame_time / 1000.0 );
rendering::big_font->collect_garbage();
rendering::medium_font->collect_garbage();
rendering::small_font->collect_garbage();
}
rendering::big_font.reset();
rendering::medium_font.reset();
rendering::small_font.reset();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
return 0;
}
[[noreturn]] void __die(char const * file, int lineno, char const * msg, ...)
{
va_list list;
va_start(list, msg);
fprintf(stderr, "%s:%d:", file, lineno);
vfprintf(stderr, msg, list);
fprintf(stderr, "\n");
fflush(stderr);
va_end(list);
exit(1);
}