From 3f3c238227132e89ad5364538fa789726d76141e Mon Sep 17 00:00:00 2001 From: Nathaniel Walizer Date: Fri, 17 Jan 2025 17:30:55 -0800 Subject: [PATCH] Add colors to menus --- src/menu.c | 29 ++++++++++++++++++++++------- src/render.h | 7 ++++--- src/sdl_overlay.c | 13 +++++++++++-- src/sdl_overlay.h | 3 ++- src/sdl_render.c | 5 +++-- 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/menu.c b/src/menu.c index 6727d18..34efb5c 100644 --- a/src/menu.c +++ b/src/menu.c @@ -8,6 +8,11 @@ #include "timer.h" +#define color_menu (0xFFFFFFFFU) +#define color_highlight (0xFFeeee00U) +#define color_error (0xFFff4444U) + + static int get_input(nes_Input_Reader* reader, nes_input* input, int *last) { int status = nes_input_update(reader, input); @@ -138,9 +143,13 @@ static void show_menu(const menu_state* menu, int dim, int x, ( (menu->top == n && 0 != menu->top) || (bottom == n && files->count - 1 > bottom) ) ? "..." : filename, - x, y); + x, y, + (menu->cursor == n) ? + color_highlight : color_menu + ); if (menu->cursor == n) { - nes_draw_text(rend, ">", x - 10, y); + nes_draw_text(rend, ">", x - 10, y, + color_highlight); } } @@ -216,8 +225,11 @@ char* run_main_menu(menu_state* state, nes_Renderer* rend, if (NULL == dir) { nes_draw_last_frame(rend, NULL != sys->cart.mapper); - nes_draw_text(rend, "No ROMS found!", 10, 10); - nes_draw_text(rend, "Press any key to exit", 10, 21); + nes_draw_text( + rend, + "No ROMS found!\nPress any key to exit", + 10, 21, color_error + ); nes_draw_done(rend); wait_for_input(input, &sys->input); @@ -309,11 +321,14 @@ int modal_popup(const char* message, nes_Renderer* rend, nes_text_size(rend, message, &w, &h); - int x = (nes_ppu_render_w - w) / 2; - int y = (nes_ppu_render_h - h) / 2; + int x = ((int)nes_ppu_render_w - w) / 2; + int y = ((int)nes_ppu_render_h - h) / 2; + + if (x < 5) x = 5; + if (y < 5) y = 5; nes_draw_last_frame(rend, NULL != sys->cart.mapper); - nes_draw_text(rend, message, x, y); + nes_draw_text(rend, message, x, y, color_error); nes_draw_done(rend); return wait_for_input(input, &sys->input); diff --git a/src/render.h b/src/render.h index 8c77543..60eba80 100644 --- a/src/render.h +++ b/src/render.h @@ -11,7 +11,7 @@ typedef struct nes_Renderer_t { int (*render)(struct nes_Renderer_t*, nes_ppu*); void (*draw_last_frame)(struct nes_Renderer_t*, int dim); - void (*draw_text)(struct nes_Renderer_t*, const char*, int x, int y); + void (*draw_text)(struct nes_Renderer_t*, const char*, int x, int y, uint32_t color); void (*text_size)(struct nes_Renderer_t*, const char*, int* w, int* h); void (*draw_done)(struct nes_Renderer_t*); @@ -43,8 +43,9 @@ static inline void nes_text_size(nes_Renderer* rend, } static inline void nes_draw_text(nes_Renderer* rend, - const char* str, int x, int y) { - rend->draw_text(rend, str, x, y); + const char* str, int x, int y, + uint32_t color) { + rend->draw_text(rend, str, x, y, color); } static inline void nes_draw_done(nes_Renderer* rend) { diff --git a/src/sdl_overlay.c b/src/sdl_overlay.c index 75db898..5eb7951 100644 --- a/src/sdl_overlay.c +++ b/src/sdl_overlay.c @@ -119,7 +119,8 @@ void measure_string(sdl_overlay_font* font, const char* string, void render_string(SDL_Renderer* rend, int ox, int oy, int sx, int sy, - sdl_overlay_font* font, const char* string) { + sdl_overlay_font* font, const char* string, + uint32_t color) { int x = ox; int y = oy; for (const char* c = string; *c; ++c) { @@ -140,6 +141,14 @@ void render_string(SDL_Renderer* rend, .h = char_h * sy, }; + SDL_SetTextureColorMod( + texture, + ((color >> 16) & 0xFFU), + ((color >> 8) & 0xFFU), + ((color >> 0) & 0xFFU) + ); + SDL_SetTextureAlphaMod(texture, + ((color >> 24) & 0xFFU)); SDL_RenderCopy(rend, texture, NULL, &rect); x += (cw - 1); @@ -158,7 +167,7 @@ int sdl_overlay_frame(Overlay* overlay, sdl_overlay_font* font, next = message->next; render_string(rend, overlay_start_x, y, sx, sy, - font, message->string); + font, message->string, 0xFFFFFFFFU); y += (char_h + 1); if (message->expiry > 0 && --(message->expiry) == 0) { diff --git a/src/sdl_overlay.h b/src/sdl_overlay.h index cf7909d..6c2365d 100644 --- a/src/sdl_overlay.h +++ b/src/sdl_overlay.h @@ -29,7 +29,8 @@ void measure_string(sdl_overlay_font* font, const char* string, void render_string(SDL_Renderer* rend, int ox, int oy, int sx, int sy, - sdl_overlay_font* font, const char* string); + sdl_overlay_font* font, const char* string, + uint32_t color); #endif // NESE_SDL_OVERLAY_ diff --git a/src/sdl_render.c b/src/sdl_render.c index ffe813a..d7ed959 100644 --- a/src/sdl_render.c +++ b/src/sdl_render.c @@ -589,12 +589,13 @@ static void sdl_draw_present(nes_Renderer* rend) { } static void sdl_draw_text(nes_Renderer* rend, - const char* str, int x, int y) { + const char* str, int x, int y, + uint32_t color) { sdl_render_data* data = (sdl_render_data*)rend->data; render_string(data->renderer, x, y, data->win_w / nes_ppu_render_w, data->win_h / nes_ppu_render_h, - &data->font, str); + &data->font, str, color); } static void sdl_text_size(nes_Renderer* rend,