generated from deltabeard/c_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
427 lines (376 loc) · 8.92 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
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
/**
* Renders UI for Haiyajan.
* Copyright (c) 2020-2022 Mahyar Koshkouei
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3, as published by
* the Free Software Foundation.
*/
#ifdef __EMSCRIPTEN__
# include <emscripten.h>
#endif
#include "signal.h"
#include "SDL.h"
#include "ui.h"
void onclick_function_debug(const struct ui_element *element);
static Uint32 quit = 0;
static unsigned ticks_element_num(void *user_ctx)
{
(void) user_ctx;
return 1;
}
static int ticks_elemen_get(unsigned memb,
struct ui_element *element, char *label, unsigned label_sz,
void *user_ctx)
{
(void) user_ctx;
/* There is only one member in this dynamic entry. */
if(memb != 0)
return 0;
SDL_snprintf(label, label_sz, "Ticks: %" SDL_PRIu64, SDL_GetTicks64());
element->type = UI_ELEM_TYPE_LABEL;
element->label = label;
element->elem.label.style = FONT_STYLE_REGULAR;
return 1;
}
static unsigned power_element_num(void *user_ctx)
{
(void) user_ctx;
return 1;
}
static int power_elemen_get(unsigned memb,
struct ui_element *element, char *label, unsigned label_sz,
void *user_ctx)
{
int secs, pct;
SDL_PowerState state;
(void) user_ctx;
/* There is only one member in this dynamic entry. */
if(memb != 0)
return 0;
state = SDL_GetPowerInfo(&secs, &pct);
switch(state)
{
case SDL_POWERSTATE_ON_BATTERY:
SDL_snprintf(label, label_sz, "Running on battery with %d%% "
"remaining", pct);
break;
case SDL_POWERSTATE_NO_BATTERY:
SDL_strlcpy(label, "Running on external power", label_sz);
break;
case SDL_POWERSTATE_CHARGING:
SDL_snprintf(label, label_sz, "Charging battery at %d%%", pct);
break;
case SDL_POWERSTATE_CHARGED:
SDL_strlcpy(label, "Battery fully charged", label_sz);
break;
case SDL_POWERSTATE_UNKNOWN:
default:
SDL_strlcpy(label, "Unknown power state", label_sz);
break;
}
element->type = UI_ELEM_TYPE_LABEL;
element->label = label;
element->elem.label.style = FONT_STYLE_REGULAR;
return 1;
}
extern const struct ui_element ui_elements[];
static const struct ui_element sub_menu_1[] = {
{
.type = UI_ELEM_TYPE_TILE,
.label = "Label Outside Top",
.elem.tile = {
.label_placement = LABEL_PLACEMENT_OUTSIDE_RIGHT_TOP,
.icon = 0xE8B7,
.help = NULL,
.bg = { 0, 0, 0, SDL_ALPHA_OPAQUE},
.fg = { 0xff, 0xff, 0xff, SDL_ALPHA_OPAQUE},
.disabled = SDL_FALSE,
.onclick = {
.action = UI_EVENT_EXECUTE_FUNCTION,
.action_data.execute_function = {
onclick_function_debug
},
},
.user = NULL
}
},
{
.type = UI_ELEM_TYPE_DYNAMIC,
.label = "Battery Status",
.elem.dynamic = {
.number_of_elements = power_element_num,
.get_element = power_elemen_get
}
},
{
.type = UI_ELEM_TYPE_DYNAMIC,
.label = "Ticks",
.elem.dynamic = {
.number_of_elements = ticks_element_num,
.get_element = ticks_elemen_get
}
},
{
.type = UI_ELEM_TYPE_TILE,
.label = "Back",
.elem.tile = {
.label_placement = LABEL_PLACEMENT_OUTSIDE_RIGHT_BOTTOM,
.icon = 0xE8B7,
.help = NULL,
.bg = { 0, 0, 0, SDL_ALPHA_OPAQUE},
.fg = { 0xff, 0xff, 0xff, SDL_ALPHA_OPAQUE},
.disabled = SDL_FALSE,
.onclick = {
.action = UI_EVENT_GOTO_ELEMENT,
.action_data.goto_element = {
ui_elements
},
},
.user = NULL
}
},
{
.type = UI_ELEM_TYPE_END
}
};
const struct ui_element ui_elements[] = {
{
.type = UI_ELEM_TYPE_LABEL,
.label = "Main Menu",
.elem.label = {
.style = FONT_STYLE_HEADER
}
},
{
.type = UI_ELEM_TYPE_TILE,
.label = "Label",
.elem.tile = {
.label_placement = LABEL_PLACEMENT_OUTSIDE_RIGHT_BOTTOM,
.icon = 0xE768,
.help = NULL,
/* Persian Green */
.bg = {.r = 0x00, .g = 0xA3, .b = 0x98, .a = SDL_ALPHA_OPAQUE},
.fg = {.r = 0xFF, .g = 0xFF, .b = 0xFF, .a = SDL_ALPHA_OPAQUE},
.disabled = SDL_FALSE,
.onclick = {
.action = UI_EVENT_EXECUTE_FUNCTION,
.action_data.execute_function = {
onclick_function_debug
},
},
.user = NULL
}
},
{
.type = UI_ELEM_TYPE_TILE,
.label = "Label Outside Middle",
.elem.tile = {
.label_placement = LABEL_PLACEMENT_OUTSIDE_RIGHT_MIDDLE,
.icon = 0xE8B7,
.help = NULL,
/* Persian Blue */
.bg = {.r = 0x1C, .g = 0x39, .b = 0xBB, .a = SDL_ALPHA_OPAQUE},
.fg = {.r = 0xFF, .g = 0xFF, .b = 0xFF, .a = SDL_ALPHA_OPAQUE},
.disabled = SDL_FALSE,
.onclick = {
.action = UI_EVENT_EXECUTE_FUNCTION,
.action_data.execute_function = {
onclick_function_debug
},
},
.user = NULL
}
},
#ifndef __EMSCRIPTEN__
{
.type = UI_ELEM_TYPE_TILE,
.label = "Exit",
.elem.tile = {
.label_placement = LABEL_PLACEMENT_OUTSIDE_RIGHT_BOTTOM,
.icon = 0xE7E8,
.help = NULL,
/* Auburn */
.bg = {.r = 0x9E, .g = 0x2A, .b = 0x2B, .a = SDL_ALPHA_OPAQUE},
.fg = {.r = 0xFF, .g = 0xFF, .b = 0xFF, .a = SDL_ALPHA_OPAQUE},
.disabled = SDL_FALSE,
.onclick = {
.action = UI_EVENT_SET_UNSIGNED_VARIABLE,
.action_data.unsigned_variable = {
.variable = &quit,
.val = 1
},
},
.user = NULL
}
},
#endif
{
.type = UI_ELEM_TYPE_TILE,
.label = "Go to sub-menu",
.elem.tile = {
.label_placement = LABEL_PLACEMENT_OUTSIDE_RIGHT_BOTTOM,
.icon = 0xE8B7,
.help = NULL,
/* Persian Blue */
.bg = {.r = 0x1C, .g = 0x39, .b = 0xBB, .a = SDL_ALPHA_OPAQUE},
.fg = {.r = 0xFF, .g = 0xFF, .b = 0xFF, .a = SDL_ALPHA_OPAQUE},
.disabled = SDL_FALSE,
.onclick = {
.action = UI_EVENT_GOTO_ELEMENT,
.action_data.goto_element = {
sub_menu_1
},
},
.user = NULL
}
},
{
.type = UI_ELEM_TYPE_END,
}
};
struct loop_ctx
{
SDL_Renderer *ren;
ui_ctx_s *ui;
};
static void loop(void *userdata)
{
struct loop_ctx *ctx = userdata;
SDL_Renderer *ren = ctx->ren;
ui_ctx_s *ui = ctx->ui;
SDL_Event e;
SDL_Texture *ui_tex;
while(SDL_PollEvent(&e))
{
if(e.type & UI_EVENT_MASK)
{
ui_process_event(ui, &e);
}
}
ui_tex = ui_render_frame(ui);
SDL_SetRenderTarget(ren, NULL);
/* The UI texture is copied to the entire screen, so a RenderClear is
* not required. */
//SDL_RenderClear(ren);
SDL_RenderCopy(ren, ui_tex, NULL, NULL);
SDL_RenderPresent(ren);
return;
}
void onclick_function_debug(const struct ui_element *element)
{
SDL_Log("Element %p clicked", (void *)element);
}
void print_fps(void)
{
static unsigned frames = 0;
static unsigned tim = 0;
unsigned now;
if(tim == 0)
{
tim = SDL_GetTicks();
return;
}
frames++;
now = SDL_GetTicks();
/* Print FPS every second. */
if(now - tim > 1000)
{
float fps;
fps = ((float)frames/(float)(now - tim)) * 1000.0f;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"FPS: %.2f", fps);
frames = 0;
tim = now;
}
return;
}
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <Windows.h>
# if (_WIN32_WINNT >= 0x0603)
# include <shellscalingapi.h>
# endif
void set_dpi_awareness(void)
{
# if (_WIN32_WINNT >= 0x0605)
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
# elif (_WIN32_WINNT >= 0x0603)
SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
# elif (_WIN32_WINNT >= 0x0600)
SetProcessDPIAware();
# elif defined(__MINGW64__)
SetProcessDPIAware();
# endif
return;
}
#endif
/**
* Process input arguments and initialise libraries.
*/
int main(int argc, char *argv[])
{
SDL_Window *win = NULL;
SDL_Renderer *ren = NULL;
int ret;
ui_ctx_s *ui;
/* Input arguments are currently unused. */
(void)argc;
(void)argv;
#ifdef _WIN32
set_dpi_awareness();
#endif
SDL_SetMainReady();
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
ret = SDL_Init(
SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER |
SDL_INIT_EVENTS | SDL_INIT_TIMER);
if(ret != 0)
goto err;
win = SDL_CreateWindow("Haiyajan UI",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
UI_DEFAULT_WINDOW_WIDTH, UI_DEFAULT_WINDOW_HEIGHT,
SDL_WINDOW_RESIZABLE |
SDL_WINDOW_SHOWN | SDL_WINDOW_MAXIMIZED);
if(win == NULL)
goto err;
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_PRESENTVSYNC |
SDL_RENDERER_TARGETTEXTURE);
if(ren == NULL)
goto err;
/* TODO: Allow even smaller screens. */
SDL_SetWindowMinimumSize(win, UI_MIN_WINDOW_WIDTH,
UI_MIN_WINDOW_HEIGHT);
SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_BLEND);
/* Initialise user interface context. */
ui = ui_init(win, ui_elements);
if(ui == NULL)
goto err;
struct loop_ctx loop_ctx = { .ren = ren, .ui = ui };
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop_arg(loop, &loop_ctx, 0, 1);
#else
while(SDL_QuitRequested() == SDL_FALSE && quit == 0)
loop(&loop_ctx);
#endif
ui_exit(ui);
out:
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return ret;
err:
{
char buf[512];
SDL_snprintf(buf, sizeof(buf),
"A critical error has occurred, and Haiyajan must "
"now close.\n"
"Error %d: %s\n", ret, SDL_GetError());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Critical Error", buf, win);
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Error %d: %s", ret, SDL_GetError());
}
goto out;
}