Mercurial > molko
diff libmlk-adventure/adventure/trace_hud.c @ 243:71b3b7036de7
misc: lot of cleanups,
- prefix libraries with libmlk,
- move assets from source directories closes #2520,
- prefix header guards closes #2519
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 28 Nov 2020 22:37:30 +0100 |
parents | libadventure/adventure/trace_hud.c@a43e79d489ea |
children | 8d41e317c07b |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmlk-adventure/adventure/trace_hud.c Sat Nov 28 22:37:30 2020 +0100 @@ -0,0 +1,115 @@ +/* + * trace_hud.c -- on screen hud + * + * Copyright (c) 2020 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <assert.h> +#include <stdio.h> +#include <string.h> + +#include <core/font.h> +#include <core/trace.h> +#include <core/window.h> + +#include <ui/label.h> +#include <ui/theme.h> + +#include "trace_hud.h" + +#define LINES_MAX (4) +#define THEME(t) ((t) ? (t) : theme_default()) + +static struct { + char lines[LINES_MAX + 1][TRACE_LINE_MAX]; + unsigned int elapsed; +} data; + +struct trace_hud trace_hud = { + .timeout = TRACE_HUD_TIMEOUT_DEFAULT +}; + +void +trace_hud_handler(const char *str) +{ + assert(str); + + /* 1.Try to find an empty line. */ + for (size_t i = 0; i < LINES_MAX; ++i) { + if (data.lines[i][0] == '\0') { + snprintf(data.lines[i], sizeof (data.lines[i]), "%s", str); + return; + } + } + + /* 2. All lines are full, put in last one and move other. */ + memmove(&data.lines[0], &data.lines[1], sizeof (data.lines[0]) * LINES_MAX - 1); + snprintf(data.lines[LINES_MAX - 1], sizeof (data.lines[0]), "%s", str); + + /* 3. Reset elapsed time now. */ + data.elapsed = 0; +} + +void +trace_hud_update(unsigned int ticks) +{ + data.elapsed += ticks; + + /* + * We have an empty line in the data.lines at LINES_MAX, so we simply so + * to move the whole array. + * + * [0] = "abc" + * [1] = "def" + * [2] = "xyz" + * [3] = "zef" + * [n] = "ldkf" + * [LINES_MAX + 1] = "\0" + */ + if (data.elapsed >= trace_hud.timeout) { + data.elapsed = 0; + memmove(&data.lines[0], &data.lines[1], sizeof (data.lines[0]) * LINES_MAX); + } +} + +void +trace_hud_draw(void) +{ + struct theme *th; + int x, y; + + th = THEME(trace_hud.theme); + x = th->padding; + y = th->padding; + + for (int i = 0; i < LINES_MAX && data.lines[i][0]; ++i) { + label_draw(&(struct label) { + .x = x, + .y = y, + .text = data.lines[i], + .theme = th, + .flags = LABEL_FLAGS_SHADOW + }); + + y += font_height(th->fonts[THEME_FONT_INTERFACE]); + y += th->padding; + } +} + +void +trace_hud_clear(void) +{ + memset(&data, 0, sizeof (data)); +}