From 57bd5755394aeb965f3339e4bd523edbe6986be1 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Mon, 2 Apr 2018 01:38:07 +0200 Subject: Rework display framework --- display.c | 69 +++++++++++++++++++++++++++++++++++++-------------------------- display.h | 6 +++--- 2 files changed, 44 insertions(+), 31 deletions(-) diff --git a/display.c b/display.c index 4263e52..27b96e5 100644 --- a/display.c +++ b/display.c @@ -2,9 +2,12 @@ #include "SDL2/SDL2_gfxPrimitives.h" #include -#include "GenBkBasB.h" #include "display.h" +#define FONT_NAME(suffix) SourceCodePro_Semibold ## suffix +extern unsigned char FONT_NAME(_ttf[]); +extern unsigned int FONT_NAME(_ttf_len); + #define display_measure_text(text,w,h) TTF_SizeText(font,(text),(w),(h)) enum { FONT_SIZE = 28 }; @@ -39,7 +42,7 @@ display_init(int screen_width, int screen_height, int harfe_width, int harfe_hei SDL_RenderClear(renderer); TTF_Init(); - font_file = SDL_RWFromConstMem(GenBkBasB_ttf, GenBkBasB_ttf_len); + font_file = SDL_RWFromConstMem( FONT_NAME(_ttf), FONT_NAME(_ttf_len) ); font = TTF_OpenFontRW(font_file, 1, FONT_SIZE); g_scale_factor = (double)harfe_height / (double)screen_height; @@ -108,17 +111,22 @@ display_line_color(int x0, int y0, int x1, int y1, int color) SDL_RenderDrawLine(renderer, x0, y0, x1, y1); } -void -display_rect_color(int x, int y, int width, int height, int color) +static void +display_rect_color_static(int x, int y, int width, int height, int color) { SDL_Rect r; r.x = x; - r.y = g_screen_height - y; + r.y = y; r.w = width; r.h = height; SDL_SetRenderDrawColor(renderer, (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, 255); SDL_RenderFillRect(renderer, &r); } +void +display_rect_color(int x, int y, int width, int height, int color) +{ + display_rect_color_static(x, g_screen_height - y, width, height, color); +} void display_redraw() @@ -126,10 +134,9 @@ display_redraw() SDL_RenderPresent(renderer); } -void -display_text(char *text, int x, int y, int color) +static void +display_text_static(char const *text, int x, int y, int color) { - y = g_screen_height - y; SDL_Color s_color = { 255 & (color>>24), 255 & (color>>16), 255 & (color>>8) }; SDL_Surface *sText = TTF_RenderText_Solid(font, text, s_color); SDL_Rect rect = {x, y, sText->w, sText->h}; @@ -139,20 +146,41 @@ display_text(char *text, int x, int y, int color) SDL_DestroyTexture(texture); SDL_FreeSurface(sText); } +void +display_text(char const *text, int x, int y, int color) +{ + display_text_static(text, x, g_screen_height - y, color); +} void -display_textbox(int min_x, int pos, int max_x, int max_pos, char *text, int color) +display_textbox(int min_x, int pos, int max_x, int max_pos, char const *text, int color) { int w, h, min_y, item_height; display_measure_text("#", &w, &h); item_height = 3 * h / 2; - min_y = (g_screen_height - item_height * max_pos) / 2 + pos * item_height; + min_y = pos * item_height + 4; + + display_rect_color_static(min_x, min_y, max_x - min_x, item_height - 4, color); - //boxColor(screen, min_x, min_y, max_x, min_y + item_height, color); - //rectangleColor(screen, min_x, min_y, max_x, min_y + item_height, 0xffffffff); display_measure_text(text, &w, &h); - display_text(text, min_x + (max_x - min_x - w) / 2, min_y + (item_height - h) / 2, 0xffffffff); + // display_text(text, min_x + (max_x - min_x - w) / 2, min_y + (item_height - h) / 2, 0xffffffff); + display_text_static(text, min_x + 8, min_y + (item_height - h) / 2, 0xffffffff); +} + +int +display_test_menu_click(int y) +{ + int w, h, min_y, item_height; + + y -= 4; + if (y<0) + return -1; + + display_measure_text( "#", &w, &h ); + item_height = 3 * h / 2; + + return y / item_height; } void @@ -174,18 +202,3 @@ display_messagebox_yesno(char *title, char *info) { return buttonid == 1; } -int -display_test_menu_click(int y, int max_pos) -{ - int w, h, min_y, item_height; - -/* - display_measure_text( "#", &w, &h ); - item_height = 3 * h / 2; - min_y = ( g_screen_height - item_height * max_pos ) / 2; - if( y < min_y ) return -1; - if( y > min_y + item_height * max_pos ) return -1; - return ( y - min_y ) / item_height; -*/ - return 0; -} diff --git a/display.h b/display.h index c7003fd..da2f7fa 100644 --- a/display.h +++ b/display.h @@ -13,9 +13,9 @@ void display_circle(int x, int y, int w); void display_circle_color(int x, int y, int w, int color); void display_rect_color(int x, int y, int width, int height, int color); -void display_text(char *text, int x, int y, int color); -void display_textbox(int min_x, int pos, int max_x, int max_pos, char *text, int color); -int display_test_menu_click(int y, int max_pos); +void display_text(char const *text, int x, int y, int color); +void display_textbox(int min_x, int pos, int max_x, int max_pos, char const *text, int color); +int display_test_menu_click(int y); void display_messagebox(char *title, char *info); int display_messagebox_yesno(char *title, char *info); -- cgit v1.2.3