Skip to content

Commit

Permalink
GUI and font rendering fixes for invalid chars.
Browse files Browse the repository at this point in the history
  • Loading branch information
indigoparadox committed Jun 3, 2024
1 parent 9ff7258 commit a44276f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
34 changes: 33 additions & 1 deletion src/retrofnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ void retrofont_blit_glyph(
uint8_t* glyph = retrofont_glyph_at( font, c );
int16_t x_iter, y_iter;

debug_printf( RETROFONT_TRACE_LVL, "blit glyph: %c", c );

for( y_iter = 0 ; font->glyph_h > y_iter ; y_iter++ ) {
for( x_iter = 0 ; font->glyph_w > x_iter ; x_iter++ ) {
if(
Expand Down Expand Up @@ -302,14 +304,37 @@ void retrofont_string(
goto cleanup;
}

/* TODO: Stop at max_w/max_h */

for( i = 0 ; str_sz > i ; i++ ) {
/* Terminate prematurely at null. */
if( '\0' == str[i] ) {
break;
}

/* Handle forced newline. */
if( '\r' == str[i] || '\n' == str[i] ) {
x_iter = x;
y_iter += font->glyph_h;
debug_printf(
RETROFONT_TRACE_LVL,
"newline: " SIZE_T_FMT ", " SIZE_T_FMT, x_iter, y_iter );
continue;
}

/* Filter out characters not present in this font. */
if(
' ' != str[i] && (
str[i] < font->first_glyph ||
str[i] >= font->first_glyph + font->glyphs_count
)
) {
error_printf( "invalid character: 0x%02x", str[i] );
continue;
}

/* TODO: More dynamic way to determine space character? */
if( 32 != str[i] ) {
if( ' ' != str[i] ) {
retrofont_blit_glyph(
target, color, str[i], font, x_iter, y_iter, flags );
}
Expand Down Expand Up @@ -357,6 +382,13 @@ MERROR_RETVAL retrofont_string_sz(
break;
}

/* Handle forced newline. */
if( '\r' == str[i] || '\n' == str[i] ) {
x_iter = 0;
*out_h_p += font->glyph_h;
continue;
}

x_iter += font->glyph_w;

if( *out_w_p <= x_iter ) {
Expand Down
6 changes: 6 additions & 0 deletions src/retrogui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,12 @@ static void retrogui_redraw_LABEL(

/* Draw text. */

#ifdef RETROGXC_PRESENT
assert( 0 <= gui->font_idx );
#else
assert( (MAUG_MHANDLE)NULL != gui->font_h );
#endif /* RETROGXC_PRESENT */

assert( NULL == ctl->LABEL.label );
maug_mlock( ctl->LABEL.label_h, ctl->LABEL.label );
if( NULL == ctl->LABEL.label ) {
Expand Down
42 changes: 33 additions & 9 deletions src/retrow3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#define RETROWIN3D_FLAG_INIT_BMP 0x02

#define retro3dw_win_is_active( win ) \
(RETROWIN3D_FLAG_INIT_GUI == (RETROWIN3D_FLAG_INIT_GUI & (win)->flags))

struct RETROWIN3D {
uint8_t flags;
size_t idc;
Expand All @@ -28,7 +31,7 @@ RETROGUI_IDC retro3dw_poll_win(
struct RETROWIN3D* win_stack, size_t win_stack_sz, RETROGUI_IDC idc,
RETROFLAT_IN_KEY* p_input, struct RETROFLAT_INPUT* input_evt );

void retro3dw_free( struct RETROWIN3D* win );
void retro3dw_free_win( struct RETROWIN3D* win );

MERROR_RETVAL retro3dw_push_win(
struct RETROWIN3D* win_stack, size_t win_stack_sz,
Expand Down Expand Up @@ -127,10 +130,7 @@ MERROR_RETVAL retro3dw_redraw_win_stack(
size_t i = 0;

for( i = 0 ; win_stack_sz > i ; i++ ) {
if(
RETROWIN3D_FLAG_INIT_GUI !=
(RETROWIN3D_FLAG_INIT_GUI & win_stack[i].flags)
) {
if( !retro3dw_win_is_active( &(win_stack[i]) ) ) {
continue;
}

Expand Down Expand Up @@ -158,9 +158,7 @@ RETROGUI_IDC retro3dw_poll_win(

for( i = 0 ; win_stack_sz > i ; i++ ) {
if(
idc != win_stack[i].idc ||
RETROWIN3D_FLAG_INIT_GUI !=
(RETROWIN3D_FLAG_INIT_GUI & win_stack[i].flags)
idc != win_stack[i].idc || !retro3dw_win_is_active( &(win_stack[i]) )
) {
continue;
}
Expand All @@ -187,7 +185,7 @@ RETROGUI_IDC retro3dw_poll_win(
return idc_out;
}

void retro3dw_free( struct RETROWIN3D* win ) {
void retro3dw_free_win( struct RETROWIN3D* win ) {

#ifndef RETROGXC_PRESENT
if( (MAUG_MHANDLE)NULL != win->gui.font_h ) {
Expand All @@ -202,6 +200,8 @@ void retro3dw_free( struct RETROWIN3D* win ) {
if( RETROWIN3D_FLAG_INIT_BMP == (RETROWIN3D_FLAG_INIT_BMP & win->flags) ) {
retroflat_destroy_bitmap( &(win->gui_bmp) );
}

maug_mzero( win, sizeof( struct RETROWIN3D ) );
}

MERROR_RETVAL retro3dw_push_win(
Expand Down Expand Up @@ -252,6 +252,30 @@ MERROR_RETVAL retro3dw_push_win(
return retval;
}

MERROR_RETVAL retro3dw_destroy_win(
struct RETROWIN3D* win_stack, size_t win_stack_sz, size_t idc
) {
size_t i = 0;
MERROR_RETVAL retval = MERROR_OK;

for( i = 0 ; win_stack_sz > i ; i++ ) {
if(
idc != win_stack[i].idc || !retro3dw_win_is_active( &(win_stack[i]) )
) {
continue;
}

debug_printf( RETROWIN3D_TRACE_LVL, "freeing window: " SIZE_T_FMT,
win_stack[i].idc );

retro3dw_free_win( &(win_stack[i]) );

break;
}

return retval;
}

#endif /* RETRO3DW_C */

#endif /* !RETROW3D_H */
Expand Down

0 comments on commit a44276f

Please sign in to comment.