From 230b796196b46dc9a7c5524fadbe6b675036004c Mon Sep 17 00:00:00 2001 From: Lephe Date: Thu, 18 Jun 2020 18:31:13 +0200 Subject: [PATCH] render: add alignment options to dtext_opt() and dprint_opt() This change introduces two new functions dtext_opt() and dprint_opt() that have both color and alignment options. The regular dtext() and dprint() have been changed to always used bg=C_NONE which is what most calls want. --- include/gint/display.h | 59 +++++++++++++++++++++++++++++++----------- src/core/exch.c | 4 +-- src/render-cg/topti.c | 22 ++++++++++++++-- src/render-fx/dtext.c | 23 ++++++++++++++-- src/render/dprint.c | 18 +++++++++++-- 5 files changed, 103 insertions(+), 23 deletions(-) diff --git a/include/gint/display.h b/include/gint/display.h index 2173cf2..3c00c0c 100644 --- a/include/gint/display.h +++ b/include/gint/display.h @@ -201,32 +201,61 @@ font_t const *dfont(font_t const * font); @w @h Set to the width and height of the rendered text, may be NULL */ void dsize(char const *str, font_t const * font, int *w, int *h); -/* dtext(): Display a string of text +/* Alignment settings for dtext_opt() and dprint_opt(). Combining a vertical + and a horizontal alignment option specifies where a given point (x,y) should + be relative to the rendered string. */ +enum { + /* Horizontal settings: default in dtext() is DTEXT_LEFT */ + DTEXT_LEFT = 0, + DTEXT_CENTER = 1, + DTEXT_RIGHT = 2, + /* Vertical settings: default in dtext() is DTEXT_TOP */ + DTEXT_TOP = 0, + DTEXT_MIDDLE = 1, + DTEXT_BOTTOM = 2, +}; + +/* dtext_opt(): Display a string of text Draws some text in the video RAM using the font set with dfont() (or gint's - default if no such font was set). + default if no such font was set). This function has a lot of parameters, + see dtext() for a simpler version. On fx9860g, due to the particular design of topti, this function performs drastic rendering optimizations using the line structure of the VRAM and is able to render several characters at once. This is not a printf()-family function so [str] cannot contain formats like - "%d" and you cannot pass additional arguments. + "%d" and you cannot pass additional arguments. See dprint_opt() and dprint() + for that. - @x @y Coordinates of top-left corner of the rendered string - @str String to display - @fg Text color - fx9860g: white, black, none, invert - fxcg50: Any R5G6B6 color, or C_NONE - @bg Background color - fx9860g: white, black, none, invert - fxcg50: Any R5G6B5 color, or C_NONE */ -void dtext(int x, int y, char const *str, int fg, int bg); + @x @y Coordinates of top-left corner of the rendered string + @fg Text color + fx9860g: white, black, none, invert + fxcg50: Any R5G6B6 color, or C_NONE + @bg Background color + fx9860g: white, black, none, invert + fxcg50: Any R5G6B5 color, or C_NONE + @halign Where x should be relative to the rendered string (see above enum) + @valign Where y should be relative to the rendered string (see above enum) + @str String to display */ +void dtext_opt(int x, int y, int fg, int bg, int halign, int valign, + char const *str); -/* dprint(): Display a formatted string - Much like dtext(), but accepts printf-like formats with arguments. See +/* dtext(): Simple version of dtext_opt() with defaults + This is exactly dtext_opt() with bg=C_NONE, halign=DTEXT_LEFT and + valign=DTEXT_TOP. */ +void dtext(int x, int y, int fg, char const *str); + +/* dprint_opt(): Display a formatted string + Much like dtext_opt(), but accepts printf-like formats with arguments. See for a detailed view of what this format supports. */ -void dprint(int x, int y, int fg, int bg, char const *format, ...); +void dprint_opt(int x, int y, int fg, int bg, int halign, int valign, + char const *format, ...); + +/* dprint(): Simple version of dprint_op() with defaults + Like dtext() with formatted printing. */ +void dprint(int x, int y, int fg, char const *format, ...); //--- // Image rendering (bopti) diff --git a/src/core/exch.c b/src/core/exch.c index 8ea0ddb..4ae4415 100644 --- a/src/core/exch.c +++ b/src/core/exch.c @@ -5,8 +5,8 @@ #include #include -#define dprint(x, y, ...) dprint(x, y, C_BLACK, C_NONE, __VA_ARGS__) -#define dtext(x, y, str) dtext (x, y, str, C_BLACK, C_NONE) +#define dprint(x, y, ...) dprint(x, y, C_BLACK, __VA_ARGS__) +#define dtext(x, y, str) dtext (x, y, C_BLACK, str) /* gint_panic_default(): Default panic handler */ GNORETURN static void gint_default_panic(GUNUSED uint32_t code) diff --git a/src/render-cg/topti.c b/src/render-cg/topti.c index 95d8c8f..e1d699a 100644 --- a/src/render-cg/topti.c +++ b/src/render-cg/topti.c @@ -116,8 +116,26 @@ void topti_render(int x, int y, char const *str, size_t size, font_t const *f, } } -/* dtext() - display a string of text */ -void dtext(int x, int y, char const *str, int fg, int bg) +/* dtext_opt(): Display a string of text */ +void dtext_opt(int x, int y, int fg, int bg, int halign, int valign, + char const *str) { + if(halign != DTEXT_LEFT || valign != DTEXT_TOP) + { + int w, h; + dsize(str, topti_font, &w, &h); + + if(halign == DTEXT_RIGHT) x -= w; + if(halign == DTEXT_CENTER) x -= ((w+1) >> 1); + if(valign == DTEXT_BOTTOM) y -= h; + if(valign == DTEXT_MIDDLE) y -= ((h+1) >> 1); + } + topti_render(x, y, str, strlen(str), topti_font, fg, bg); } + +/* dtext(): Simple version of dtext_opt() with defaults */ +void dtext(int x, int y, int fg, char const *str) +{ + dtext_opt(x, y, fg, C_NONE, DTEXT_LEFT, DTEXT_TOP, str); +} diff --git a/src/render-fx/dtext.c b/src/render-fx/dtext.c index e388b52..7496281 100644 --- a/src/render-fx/dtext.c +++ b/src/render-fx/dtext.c @@ -2,10 +2,29 @@ #include #include "topti-asm.h" -/* dtext(): Display a string of text */ -void dtext(int x, int y, char const *str, int fg, int bg) +/* dtext_opt(): Display a string of text */ +void dtext_opt(int x, int y, int fg, int bg, int halign, int valign, + char const *str) { if((uint)fg >= 8 || (uint)bg >= 8) return; + + if(halign != DTEXT_LEFT || valign != DTEXT_TOP) + { + int w, h; + dsize(str, topti_font, &w, &h); + + if(halign == DTEXT_RIGHT) x -= w; + if(halign == DTEXT_CENTER) x -= ((w+1) >> 1); + if(valign == DTEXT_BOTTOM) y -= h; + if(valign == DTEXT_MIDDLE) y -= ((h+1) >> 1); + } + topti_render(x, y, str, topti_font, topti_asm_text[fg], topti_asm_text[bg], gint_vram, gint_vram); } + +/* dtext(): Simple version of dtext_opt() with defaults */ +void dtext(int x, int y, int fg, char const *str) +{ + dtext_opt(x, y, fg, C_NONE, DTEXT_LEFT, DTEXT_TOP, str); +} diff --git a/src/render/dprint.c b/src/render/dprint.c index 256c741..eac0160 100644 --- a/src/render/dprint.c +++ b/src/render/dprint.c @@ -2,7 +2,8 @@ #include /* dprint(): Display a formatted string */ -void dprint(int x, int y, int fg, int bg, char const *format, ...) +void dprint_opt(int x, int y, int fg, int bg, int halign, int valign, + char const *format, ...) { char str[512]; va_list args; @@ -11,5 +12,18 @@ void dprint(int x, int y, int fg, int bg, char const *format, ...) vsnprintf(str, 512, format, args); va_end(args); - dtext(x, y, str, fg, bg); + dtext_opt(x, y, fg, bg, halign, valign, str); +} + +/* dprint(): Simple version of dprint_op() with defaults */ +void dprint(int x, int y, int fg, char const *format, ...) +{ + char str[512]; + va_list args; + + va_start(args, format); + vsnprintf(str, 512, format, args); + va_end(args); + + dtext(x, y, fg, str); }