diff --git a/src/common/api.c b/src/common/api.c index f62c4e4..4579994 100644 --- a/src/common/api.c +++ b/src/common/api.c @@ -316,24 +316,78 @@ SDL_Surface* GFX_getBufferCopy(void) { // must be freed by caller memcpy(copy->pixels, (gfx.map + gfx.buffer_size * buffer), (SCREEN_HEIGHT * SCREEN_PITCH)); return copy; } -int GFX_truncateDisplayName(const char* in_name, char* out_name, int max_width) { +int GFX_truncateText(TTF_Font* font, const char* in_name, char* out_name, int max_width) { int text_width; strcpy(out_name, in_name); - TTF_SizeUTF8(font.large, out_name, &text_width, NULL); + TTF_SizeUTF8(font, out_name, &text_width, NULL); text_width += SCALE1(12*2); while (text_width>max_width) { int len = strlen(out_name); - out_name[len-1] = '\0'; - out_name[len-2] = '.'; - out_name[len-3] = '.'; - out_name[len-4] = '.'; - TTF_SizeUTF8(font.large, out_name, &text_width, NULL); + strcpy(&out_name[len-4], "...\0"); + TTF_SizeUTF8(font, out_name, &text_width, NULL); text_width += SCALE1(12*2); } return text_width; } +int GFX_wrapText(TTF_Font* font, char* str, int max_width, int max_lines) { + // TODO: this is kinda buggy mess + + if (!str) return 0; + + int line_width; + int max_line_width; + char* line = str; + char buffer[MAX_PATH]; + + TTF_SizeUTF8(font, line, &line_width, NULL); + if (line_width<=max_width) { + line_width = GFX_truncateText(font,line,buffer,max_width); + strcpy(line,buffer); + return line_width; + } + + char* prev; + char* tmp = line; + int lines = 1; + int i = 0; + while (!max_lines || lines=max_width) { // wrap + if (line_width>max_line_width) max_line_width = line_width; + tmp[0] = ' '; + tmp += 1; + prev[0] = '\n'; + prev += 1; + line = prev; + lines += 1; + } + else { // continue + tmp[0] = ' '; + prev = tmp; + tmp += 1; + } + i += 1; + } + + line_width = GFX_truncateText(font,line,buffer,max_width); + strcpy(line,buffer); + + if (line_width>max_line_width) max_line_width = line_width; + return max_line_width; +} void GFX_blitAsset(int asset, SDL_Rect* src_rect, SDL_Surface* dst, SDL_Rect* dst_rect) { SDL_Rect* rect = &asset_rects[asset]; @@ -630,7 +684,7 @@ int GFX_blitButtonGroup(char** pairs, SDL_Surface* dst, int align_right) { return ow; } -#define MAX_TEXT_LINES 3 +#define MAX_TEXT_LINES 16 void GFX_sizeText(TTF_Font* font, char* str, int leading, int* w, int* h) { char* lines[MAX_TEXT_LINES]; int count = 0; @@ -638,7 +692,7 @@ void GFX_sizeText(TTF_Font* font, char* str, int leading, int* w, int* h) { char* tmp; lines[count++] = str; while ((tmp=strchr(lines[count-1], '\n'))!=NULL) { - if (count+1>MAX_TEXT_LINES) break; // TODO: bail + if (count+1>MAX_TEXT_LINES) break; // TODO: bail? lines[count++] = tmp+1; } *h = count * leading; @@ -674,7 +728,7 @@ void GFX_blitText(TTF_Font* font, char* str, int leading, SDL_Color color, SDL_S char* tmp; lines[count++] = str; while ((tmp=strchr(lines[count-1], '\n'))!=NULL) { - if (count+1>MAX_TEXT_LINES) break; // TODO: bail + if (count+1>MAX_TEXT_LINES) break; // TODO: bail? lines[count++] = tmp+1; } int x = dst_rect->x; diff --git a/src/common/api.h b/src/common/api.h index cdec57c..2d129b0 100644 --- a/src/common/api.h +++ b/src/common/api.h @@ -88,7 +88,8 @@ int GFX_getVsync(void); void GFX_setVsync(int vsync); SDL_Surface* GFX_getBufferCopy(void); // must be freed by caller -int GFX_truncateDisplayName(const char* in_name, char* out_name, int max_width); // returns final width (including pill padding) +int GFX_truncateText(TTF_Font* font, const char* in_name, char* out_name, int max_width); // returns final width (including pill padding) +int GFX_wrapText(TTF_Font* font, char* str, int max_width, int max_lines); // NOTE: all dimensions should be pre-scaled void GFX_blitAsset(int asset, SDL_Rect* src_rect, SDL_Surface* dst, SDL_Rect* dst_rect); diff --git a/src/minarch/main.c b/src/minarch/main.c index c8a39bf..73e1b90 100644 --- a/src/minarch/main.c +++ b/src/minarch/main.c @@ -783,7 +783,10 @@ static void OptionList_init(const struct retro_core_option_definition *defs) { if (def->info) { len = strlen(def->info) + 1; item->desc = calloc(len, sizeof(char)); - strcpy(item->desc, def->info); + strncpy(item->desc, def->info, len); + item->desc[len-1] = '\0'; + + GFX_wrapText(font.tiny, item->desc, SCALE1(240), 2); // TODO magic number! (this is more about chars per line than pixel width so it's not going to be relative to the screen size, only the scale) } item->visible = 1; diff --git a/src/minui/main.c b/src/minui/main.c index e3df1fc..fdb26d4 100644 --- a/src/minui/main.c +++ b/src/minui/main.c @@ -1384,7 +1384,7 @@ int main (int argc, char *argv[]) { SDL_Color text_color = COLOR_WHITE; char display_name[256]; - int text_width = GFX_truncateDisplayName(entry_name, display_name, max_width); + int text_width = GFX_truncateText(font.large, entry_name, display_name, max_width); max_width = MIN(max_width, text_width); if (j==selected_row) { GFX_blitPill(ASSET_WHITE_PILL, screen, &(SDL_Rect){