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.
This commit is contained in:
Lephe 2020-06-18 18:31:13 +02:00
parent 06345967fd
commit 230b796196
Signed by untrusted user: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
5 changed files with 103 additions and 23 deletions

View File

@ -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
<gint/std/stdio.h> 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)

View File

@ -5,8 +5,8 @@
#include <gint/defs/attributes.h>
#include <gint/hardware.h>
#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)

View File

@ -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);
}

View File

@ -2,10 +2,29 @@
#include <display/common.h>
#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);
}

View File

@ -2,7 +2,8 @@
#include <gint/std/stdio.h>
/* 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);
}