Mercurial > molko
comparison libadventure/adventure/trace_hud.c @ 141:4eeeccf2b732
core: add trace/vtrace functions, closes #2493
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 14 Oct 2020 10:29:53 +0200 |
parents | |
children | 7f1af54bb35a |
comparison
equal
deleted
inserted
replaced
140:453651d76f7c | 141:4eeeccf2b732 |
---|---|
1 /* | |
2 * trace_hud.c -- on screen hud | |
3 * | |
4 * Copyright (c) 2020 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/label.h> | |
24 #include <core/font.h> | |
25 #include <core/theme.h> | |
26 #include <core/trace.h> | |
27 | |
28 #include "trace_hud.h" | |
29 | |
30 #define LINES_MAX (4) | |
31 #define THEME(t) ((t) ? (t) : theme_default()) | |
32 | |
33 static struct { | |
34 char lines[LINES_MAX + 1][TRACE_LINE_MAX]; | |
35 unsigned int elapsed; | |
36 } data; | |
37 | |
38 struct trace_hud trace_hud = { | |
39 .timeout = TRACE_HUD_TIMEOUT_DEFAULT | |
40 }; | |
41 | |
42 void | |
43 trace_hud_handler(const char *str) | |
44 { | |
45 assert(str); | |
46 | |
47 /* 1.Try to find an empty line. */ | |
48 for (size_t i = 0; i < LINES_MAX; ++i) { | |
49 if (data.lines[i][0] == '\0') { | |
50 snprintf(data.lines[i], sizeof (data.lines[i]), "%s", str); | |
51 return; | |
52 } | |
53 } | |
54 | |
55 /* 2. All lines are full, put in last one and move other. */ | |
56 memmove(&data.lines[0], &data.lines[1], sizeof (data.lines[0]) * LINES_MAX - 1); | |
57 snprintf(data.lines[LINES_MAX - 1], sizeof (data.lines[0]), "%s", str); | |
58 | |
59 /* 3. Reset elapsed time now. */ | |
60 data.elapsed = 0; | |
61 } | |
62 | |
63 void | |
64 trace_hud_update(unsigned int ticks) | |
65 { | |
66 data.elapsed += ticks; | |
67 | |
68 /* | |
69 * We have an empty line in the data.lines at LINES_MAX, so we simply so | |
70 * to move the whole array. | |
71 * | |
72 * [0] = "abc" | |
73 * [1] = "def" | |
74 * [2] = "xyz" | |
75 * [3] = "zef" | |
76 * [n] = "ldkf" | |
77 * [LINES_MAX + 1] = "\0" | |
78 */ | |
79 if (data.elapsed >= trace_hud.timeout) { | |
80 data.elapsed = 0; | |
81 memmove(&data.lines[0], &data.lines[1], sizeof (data.lines[0]) * LINES_MAX); | |
82 } | |
83 } | |
84 | |
85 void | |
86 trace_hud_draw(void) | |
87 { | |
88 struct theme *th; | |
89 int x, y; | |
90 | |
91 th = THEME(trace_hud.theme); | |
92 x = th->padding; | |
93 y = th->padding; | |
94 | |
95 for (int i = 0; i < LINES_MAX && data.lines[i][0]; ++i) { | |
96 label_draw(&(struct label) { | |
97 .x = x, | |
98 .y = y, | |
99 .text = data.lines[i], | |
100 .theme = th, | |
101 .flags = LABEL_NO_HCENTER | |
102 }); | |
103 | |
104 y += font_height(th->fonts[THEME_FONT_INTERFACE]); | |
105 y += th->padding; | |
106 } | |
107 } | |
108 | |
109 void | |
110 trace_hud_clear(void) | |
111 { | |
112 memset(&data, 0, sizeof (data)); | |
113 } |