comparison examples/example/trace_hud.c @ 414:6947c1fefe5c

misc: extreme cleanup - Remove of Javascript binding for many reasons. - Put examples and tests in GNU make. - Create a library with shared example code.
author David Demelier <markand@malikania.fr>
date Sun, 09 Oct 2022 13:51:03 +0200
parents examples/example-trace/trace_hud.c@460c78706989
children 8f59201dc76b
comparison
equal deleted inserted replaced
413:222045c513ec 414:6947c1fefe5c
1 /*
2 * trace_hud.c -- on screen hud
3 *
4 * Copyright (c) 2020-2022 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include <core/action.h>
24 #include <core/font.h>
25 #include <core/trace.h>
26 #include <core/window.h>
27
28 #include <ui/label.h>
29 #include <ui/theme.h>
30
31 #include "trace_hud.h"
32
33 #define LINES_MAX (4)
34 #define THEME(t) ((t) ? (t) : theme_default())
35
36 static struct {
37 char lines[LINES_MAX + 1][TRACE_LINE_MAX];
38 unsigned int elapsed;
39 } data;
40
41 struct trace_hud trace_hud = {
42 .timeout = TRACE_HUD_TIMEOUT_DEFAULT
43 };
44
45 void
46 trace_hud_handler(const char *str)
47 {
48 assert(str);
49
50 /* 1.Try to find an empty line. */
51 for (size_t i = 0; i < LINES_MAX; ++i) {
52 if (data.lines[i][0] == '\0') {
53 snprintf(data.lines[i], sizeof (data.lines[i]), "%s", str);
54 return;
55 }
56 }
57
58 /* 2. All lines are full, put in last one and move other. */
59 memmove(&data.lines[0], &data.lines[1], sizeof (data.lines[0]) * LINES_MAX - 1);
60 snprintf(data.lines[LINES_MAX - 1], sizeof (data.lines[0]), "%s", str);
61
62 /* 3. Reset elapsed time now. */
63 data.elapsed = 0;
64 }
65
66 void
67 trace_hud_update(unsigned int ticks)
68 {
69 data.elapsed += ticks;
70
71 /*
72 * We have an empty line in the data.lines at LINES_MAX, so we simply so
73 * to move the whole array.
74 *
75 * [0] = "abc"
76 * [1] = "def"
77 * [2] = "xyz"
78 * [3] = "zef"
79 * [n] = "ldkf"
80 * [LINES_MAX + 1] = "\0"
81 */
82 if (data.elapsed >= trace_hud.timeout) {
83 data.elapsed = 0;
84 memmove(&data.lines[0], &data.lines[1], sizeof (data.lines[0]) * LINES_MAX);
85 }
86 }
87
88 void
89 trace_hud_draw(void)
90 {
91 struct theme *th;
92 int x, y;
93
94 th = THEME(trace_hud.theme);
95 x = th->padding;
96 y = th->padding;
97
98 for (int i = 0; i < LINES_MAX && data.lines[i][0]; ++i) {
99 label_draw(&(struct label) {
100 .x = x,
101 .y = y,
102 .text = data.lines[i],
103 .theme = th,
104 .flags = LABEL_FLAGS_SHADOW
105 });
106
107 y += font_height(th->fonts[THEME_FONT_INTERFACE]);
108 y += th->padding;
109 }
110 }
111
112 void
113 trace_hud_clear(void)
114 {
115 memset(&data, 0, sizeof (data));
116 }
117
118 static int
119 update(struct action *a, unsigned int ticks)
120 {
121 (void)a;
122
123 trace_hud_update(ticks);
124
125 return 0;
126 }
127
128 static void
129 draw(struct action *a)
130 {
131 (void)a;
132
133 trace_hud_draw();
134 }
135
136 struct action *
137 trace_hud_action(void)
138 {
139 static struct action a = {
140 .update = update,
141 .draw = draw
142 };
143
144 return &a;
145 }