display: add a maximum size parameter to dtext_opt()

This parameter controls the maximum number of glyphs to print.

For backwards compatibility, it is automatically inserted by a macro in
older calls with only 7 parameters.
This commit is contained in:
Lephe 2021-02-15 17:32:45 +01:00
parent cb2d067967
commit a086510885
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
6 changed files with 34 additions and 21 deletions

View file

@ -303,9 +303,16 @@ enum {
fx-CG 50: Any R5G6B6 color, or C_NONE
@halign Where x should be relative to the rendered string
@valign Where y should be relative to the rendered string
@str String to display */
@str String to display
@size Maximum number of bytes to display (negative for no limit) */
void dtext_opt(int x, int y, int fg, int bg, int halign, int valign,
char const *str);
char const *str, int size);
/* The last parameter @size was added in gint 2.4; this macro will add it
automatically with a value of -1 if the call is made with only 7 parameters.
This is for backwards compatibility. */
#define dtext_opt8(x,y,fg,bg,h,v,str,sz,...) dtext_opt(x,y,fg,bg,h,v,str,sz)
#define dtext_opt(...) dtext_opt8(__VA_ARGS__, -1)
/* dtext(): Simple version of dtext_opt() with defaults
Calls dtext_opt() with bg=C_NONE, halign=DTEXT_LEFT and valign=DTEXT_TOP. */

View file

@ -4,7 +4,7 @@
/* gtext_opt(): Display a string of text */
void gtext_opt(int x, int y, int fg, int bg, int halign, int valign,
char const *str)
char const *str, int size)
{
uint32_t *light, *dark;
dgray_getvram(&light, &dark);
@ -21,5 +21,5 @@ void gtext_opt(int x, int y, int fg, int bg, int halign, int valign,
}
topti_render(x, y, str, topti_font, topti_asm_text[fg],
topti_asm_text[bg], light, dark);
topti_asm_text[bg], light, dark, size);
}

View file

@ -43,9 +43,10 @@ static void topti_glyph(uint16_t *vram, uint32_t const * data, int left,
}
static void topti_render(int x, int y, char const *str_char, font_t const *f,
int fg, int bg)
int fg, int bg, int size)
{
uint8_t const *str = (void *)str_char;
uint8_t const *str0 = str;
/* Raw glyph data */
uint32_t const *data = f->data;
@ -68,7 +69,7 @@ static void topti_render(int x, int y, char const *str_char, font_t const *f,
while(1)
{
uint32_t code_point = topti_utf8_next(&str);
if(!code_point) break;
if(!code_point || (size >= 0 && str - str0 > size)) break;
int glyph = topti_glyph_index(f, code_point);
if(glyph < 0) continue;
@ -107,12 +108,12 @@ static void topti_render(int x, int y, char const *str_char, font_t const *f,
/* 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)
char const *str, int size)
{
if(halign != DTEXT_LEFT || valign != DTEXT_TOP)
{
int w, h;
dsize(str, topti_font, &w, &h);
dnsize(str, size, topti_font, &w, &h);
if(halign == DTEXT_RIGHT) x -= w - 1;
if(halign == DTEXT_CENTER) x -= (w >> 1);
@ -120,5 +121,5 @@ void dtext_opt(int x, int y, int fg, int bg, int halign, int valign,
if(valign == DTEXT_MIDDLE) y -= (h >> 1);
}
topti_render(x, y, str, topti_font, fg, bg);
topti_render(x, y, str, topti_font, fg, bg, size);
}

View file

@ -74,7 +74,7 @@ struct rendering_mode
/* Text and image rendering */
void (*dtext_opt)
(int x, int y, int fg, int bg, int halign, int valign,
char const *str);
char const *str, int size);
void (*dsubimage)
(bopti_image_t const *image, struct rbox *r, int flags);
};
@ -89,9 +89,8 @@ void grect(int x1, int y1, int x2, int y2, color_t color);
void gpixel(int x, int y, color_t color);
void gint_ghline(int x1, int x2, int y, int color);
void gint_gvline(int y1, int y2, int x, int color);
void gtext_opt
(int x, int y, int fg, int bg, int halign, int valign,
char const *str);
void gtext_opt(int x, int y, int fg, int bg, int halign, int valign,
char const *str, int size);
void gsubimage(bopti_image_t const *image, struct rbox *r, int flags);
/* Short macro to call the alternate rendering function when available */

View file

@ -22,8 +22,10 @@ extern asm_text_t *topti_asm_text[8];
@asm_fg Assembler function for text rendering
@asm_bg Assembler function for background rendering
@v1 Monochrome VRAM or light gray VRAM
@v2 Monochrome or dark gray VRAM */
@v2 Monochrome or dark gray VRAM
@size Maximum number of characters to render */
void topti_render(int x, int y, char const *str, font_t const *f,
asm_text_t *asm_fg, asm_text_t *asm_bg, uint32_t *v1, uint32_t *v2);
asm_text_t *asm_fg, asm_text_t *asm_bg, uint32_t *v1, uint32_t *v2,
int size);
#endif /* GINT_RENDERFX_TOPTIASM */

View file

@ -6,6 +6,8 @@
#include "render-fx.h"
#include "topti-asm.h"
#undef dtext_opt
/* Default font */
extern font_t gint_font5x7;
font_t const * gint_default_font = &gint_font5x7;
@ -83,9 +85,11 @@ static int topti_split(uint32_t const * glyph, int width, int height, int free,
/* topti_render(): Render a string on the VRAM */
void topti_render(int x, int y, char const *str_char, font_t const *f,
asm_text_t *asm_fg, asm_text_t *asm_bg, uint32_t *v1, uint32_t *v2)
asm_text_t *asm_fg, asm_text_t *asm_bg, uint32_t *v1, uint32_t *v2,
int size)
{
uint8_t const *str = (void *)str_char;
uint8_t const *str0 = str;
/* Storage height and number of free bits in operators[] */
int height = f->data_height, free;
@ -121,7 +125,7 @@ void topti_render(int x, int y, char const *str_char, font_t const *f,
while(1)
{
uint32_t code_point = topti_utf8_next(&str);
if(!code_point) break;
if(!code_point || (size >= 0 && str - str0 > size)) break;
int glyph = topti_glyph_index(f, code_point);
if(glyph < 0) continue;
@ -175,16 +179,16 @@ void topti_render(int x, int y, char const *str_char, font_t const *f,
/* 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)
char const *str, int size)
{
if((uint)fg >= 8 || (uint)bg >= 8) return;
DMODE_OVERRIDE(dtext_opt, x, y, fg, bg, halign, valign, str);
DMODE_OVERRIDE(dtext_opt, x, y, fg, bg, halign, valign, str, size);
if(halign != DTEXT_LEFT || valign != DTEXT_TOP)
{
int w, h;
dsize(str, topti_font, &w, &h);
dnsize(str, size, topti_font, &w, &h);
if(halign == DTEXT_RIGHT) x -= w - 1;
if(halign == DTEXT_CENTER) x -= (w >> 1);
@ -193,5 +197,5 @@ void dtext_opt(int x, int y, int fg, int bg, int halign, int valign,
}
topti_render(x, y, str, topti_font, topti_asm_text[fg],
topti_asm_text[bg], gint_vram, gint_vram);
topti_asm_text[bg], gint_vram, gint_vram, size);
}