changeset 154:2252f9efac9a

ui: remove style from debug_report, closes #2499 While here add an example of usage and rename the function similar to trace.
author David Demelier <markand@malikania.fr>
date Fri, 16 Oct 2020 13:04:48 +0200
parents aa6e70e330a1
children 13b7a1a4a72c
files examples/CMakeLists.txt examples/example-debug.c libcore/core/font.c libcore/core/font.h librpg/rpg/map_state.c libui/ui/debug.c libui/ui/debug.h libui/ui/theme.c
diffstat 8 files changed, 163 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/examples/CMakeLists.txt	Thu Oct 15 18:45:27 2020 +0200
+++ b/examples/CMakeLists.txt	Fri Oct 16 13:04:48 2020 +0200
@@ -29,6 +29,13 @@
 )
 
 molko_define_executable(
+	TARGET example-debug
+	SOURCES example-debug.c
+	FOLDER examples
+	LIBRARIES libui
+)
+
+molko_define_executable(
 	TARGET example-font
 	SOURCES example-font.c
 	LIBRARIES libui
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/example-debug.c	Fri Oct 16 13:04:48 2020 +0200
@@ -0,0 +1,102 @@
+/*
+ * example-debug.c -- example on how to use debug interface
+ *
+ * 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 <core/clock.h>
+#include <core/event.h>
+#include <core/sys.h>
+#include <core/window.h>
+#include <core/painter.h>
+#include <core/panic.h>
+#include <core/trace.h>
+#include <core/util.h>
+
+#include <ui/debug.h>
+#include <ui/theme.h>
+
+#define W 1280
+#define H 720
+
+static void
+init(void)
+{
+	if (!sys_init() ||
+	    !window_init("Example - Debug", W, H) ||
+	    !theme_init())
+		panic();
+
+	debug_options.enable = true;
+}
+
+static void
+run(void)
+{
+	struct clock clock = {0};
+	int x = 0, y = 0;
+
+	clock_start(&clock);
+
+	for (;;) {
+		struct debug_report report = {0};
+		union event ev;
+		unsigned int elapsed = clock_elapsed(&clock);
+
+		clock_start(&clock);
+
+		while (event_poll(&ev)) {
+			switch (ev.type) {
+			case EVENT_MOUSE:
+				x = ev.mouse.x;
+				y = ev.mouse.y;
+				break;
+			case EVENT_QUIT:
+				return;
+			default:
+				break;
+			}
+		}
+
+		painter_set_color(0x4f8fbaff);
+		painter_clear();
+		debug(&report, "Game running.");
+		debug(&report, "mouse: %d, %d", x, y);
+		painter_present();
+
+		if ((elapsed = clock_elapsed(&clock)) < 20)
+			delay(20 - elapsed);
+	}
+}
+
+static void
+quit(void)
+{
+	theme_finish();
+	window_finish();
+	sys_finish();
+}
+
+int
+main(int argc, char **argv)
+{
+	(void)argc;
+	(void)argv;
+
+	init();
+	run();
+	quit();
+}
+
--- a/libcore/core/font.c	Thu Oct 15 18:45:27 2020 +0200
+++ b/libcore/core/font.c	Fri Oct 16 13:04:48 2020 +0200
@@ -17,6 +17,7 @@
  */
 
 #include <assert.h>
+#include <string.h>
 
 #include <SDL_ttf.h>
 
@@ -54,6 +55,15 @@
 	return true;
 }
 
+void
+font_shallow(struct font *dst, const struct font *src)
+{
+	assert(dst);
+	assert(src);
+
+	memcpy(dst, src, sizeof (*src));
+}
+
 bool
 font_ok(const struct font *font)
 {
--- a/libcore/core/font.h	Thu Oct 15 18:45:27 2020 +0200
+++ b/libcore/core/font.h	Fri Oct 16 13:04:48 2020 +0200
@@ -76,6 +76,23 @@
 font_openmem(struct font *font, const void *buffer, size_t buflen, unsigned int size);
 
 /**
+ * Convenient shortcut to shallow copy src into dst.
+ *
+ * Use this function when you want your own local font to update its attributes
+ * without changing the calling site.
+ *
+ * This is a shortcut to `memcpy(dst, src, sizeof (*src))`.
+ *
+ * \pre dst != NULL
+ * \param dst the destination font
+ * \param src the source font (may be NULL)
+ * \note Internal handle points to the same pointer, do not use \ref font_finish
+ *       on the destination.
+ */
+void
+font_shallow(struct font *dst, const struct font *src);
+
+/**
  * Tells if the font was properly opened.
  *
  * \pre font != NULL
--- a/librpg/rpg/map_state.c	Thu Oct 15 18:45:27 2020 +0200
+++ b/librpg/rpg/map_state.c	Fri Oct 16 13:04:48 2020 +0200
@@ -335,9 +335,9 @@
 		PLAYER()->x - VIEW()->x,
 		PLAYER()->y - VIEW()->y);
 
-	debug_printf(&report, "position: %d, %d", PLAYER()->x,
+	debug(&report, "position: %d, %d", PLAYER()->x,
 	    PLAYER()->y);
-	debug_printf(&report, "view: %d, %d", VIEW()->x,
+	debug(&report, "view: %d, %d", VIEW()->x,
 	    VIEW()->y);
 }
 
--- a/libui/ui/debug.c	Thu Oct 15 18:45:27 2020 +0200
+++ b/libui/ui/debug.c	Fri Oct 16 13:04:48 2020 +0200
@@ -24,9 +24,6 @@
 #include "debug.h"
 #include "theme.h"
 
-#define PADDING_X 5
-#define PADDING_Y 5
-
 struct debug_options debug_options = {
 #if !defined(NDEBUG)
 	.enable = true
@@ -34,7 +31,7 @@
 };
 
 void
-debug_printf(struct debug_report *report, const char *fmt, ...)
+debug(struct debug_report *report, const char *fmt, ...)
 {
 	assert(report);
 	assert(fmt);
@@ -45,12 +42,12 @@
 	va_list ap;
 
 	va_start(ap, fmt);
-	debug_vprintf(report, fmt, ap);
+	vdebug(report, fmt, ap);
 	va_end(ap);
 }
 
 void
-debug_vprintf(struct debug_report *report, const char *fmt, va_list ap)
+vdebug(struct debug_report *report, const char *fmt, va_list ap)
 {
 	assert(report);
 	assert(fmt);
@@ -60,23 +57,27 @@
 
 	char line[DEBUG_LINE_MAX];
 	struct theme *theme;
-	struct font *font;
+	struct font font;
 	struct texture tex;
-	unsigned int gapy;
+	int x, y;
 
 	vsnprintf(line, sizeof (line), fmt, ap);
 
+	/* Get the font and modify its style. */
 	theme = report->theme ? report->theme : theme_default();
-	font = theme->fonts[THEME_FONT_DEBUG];
-	font->color = report->color;
 
-	if (!font_render(font, &tex, line))
+	/* Update font style. */
+	font_shallow(&font, theme->fonts[THEME_FONT_DEBUG]);
+	font.style = FONT_STYLE_NONE;
+	font.color = theme->colors[THEME_COLOR_DEBUG];
+
+	if (!font_render(&font, &tex, line))
 		return;
 
-	gapy = (PADDING_Y * (report->count)) +
-	    (font_height(font) * (report->count));
+	x = theme->padding;
+	y = (theme->padding * (report->count + 1)) + (tex.h * (report->count));
 	report->count++;
 
-	texture_draw(&tex, PADDING_X, gapy);
+	texture_draw(&tex, x, y);
 	texture_finish(&tex);
 }
--- a/libui/ui/debug.h	Thu Oct 15 18:45:27 2020 +0200
+++ b/libui/ui/debug.h	Fri Oct 16 13:04:48 2020 +0200
@@ -32,38 +32,16 @@
  * debug_options.enabled variable is set to true. However, you need to specify
  * the font to use in order to work.
  *
- * Each call to \ref debug_printf or \ref debug_vprintf automatically adjust
+ * Each call to \ref debug or \ref vdebug automatically adjust
  * next coordinate for rendering the text. As such, user may simply print
  * several lines of text without having to deal with that manually.
- *
- * Example, activate global debugging.
- *
- * \code
- * struct font *f = font_openf("foo.ttf", 10);
- *
- * if (!f)
- * 	error_fatal();
- *
- * debug_options.default_font = f;
- * debug_options.enabled = true;
- * \endcode
- *
- * Example, print debugging information manually.
- *
- * \code
- * struct debug_report report = {
- * 	.color = 0x00ff00ff,
- * 	.font = myfont          // Optional if debug_options.default_font is set.
- * };
- *
- * debug_printf("current position: %d, %d\n", x, y);
- * \endcode
  */
 
 #include <stdbool.h>
 #include <stdarg.h>
 
 #include <core/font.h>
+#include <core/plat.h>
 
 /**
  * Maximum content length per report.
@@ -89,7 +67,6 @@
  */
 struct debug_report {
 	struct theme *theme;            /*!< (+&?) Theme to use. */
-	unsigned long color;            /*!< (+) Font foreground color to use. */
 	unsigned int count;             /*!< (-) Number of messages already printed. */
 };
 
@@ -99,15 +76,6 @@
 extern struct debug_options debug_options;
 
 /**
- * Convenient macro to initialize \ref debug_report with default values.
- */
-#define DEBUG_INIT_DEFAULTS {                   \
-        .font = debug_options.default_font,     \
-        .color = debug_options.default_color,   \
-        .style = debug_options.default_style    \
-}
-
-/**
  * Print debugging information into the screen.
  *
  * \pre report != NULL
@@ -118,14 +86,17 @@
  *       result is truncated.
  */
 void
-debug_printf(struct debug_report *report, const char *fmt, ...);
+debug(struct debug_report *report, const char *fmt, ...) PLAT_PRINTF(2, 3);
 
 /**
- * Similar to \ref debug_printf but with a va_list object.
+ * Similar to \ref debug with a va_list arguments pointer.
  *
- * \see \ref debug_printf
+ * \pre fmt != NULL
+ * \param report the reporting context
+ * \param fmt the printf(3) format string
+ * \param ap the argument list
  */
 void
-debug_vprintf(struct debug_report *report, const char *fmt, va_list ap);
+vdebug(struct debug_report *report, const char *fmt, va_list ap) PLAT_PRINTF(2, 0);
 
 #endif /* !MOLKO_DEBUG_H */
--- a/libui/ui/theme.c	Thu Oct 15 18:45:27 2020 +0200
+++ b/libui/ui/theme.c	Fri Oct 16 13:04:48 2020 +0200
@@ -46,6 +46,7 @@
 /* Default theme. */
 static struct theme default_theme = {
 	.colors = {
+		[THEME_COLOR_DEBUG]     = 0xff0000ff,
 		[THEME_COLOR_NORMAL]    = 0xffffffff,
 		[THEME_COLOR_SELECTED]  = 0x006554ff,
 		[THEME_COLOR_SHADOW]    = 0x000000ff