From f2683a4b707cd714b7f540ebf6482563df83d51e Mon Sep 17 00:00:00 2001 From: erdgeist Date: Sat, 8 Apr 2017 14:21:36 +0200 Subject: Near complete rewrite. --- display.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 10 deletions(-) (limited to 'display.c') diff --git a/display.c b/display.c index 42235a2..4263e52 100644 --- a/display.c +++ b/display.c @@ -6,25 +6,30 @@ #include "display.h" #define display_measure_text(text,w,h) TTF_SizeText(font,(text),(w),(h)) +enum { FONT_SIZE = 28 }; static SDL_Window *screen; static SDL_Renderer *renderer; -static int g_width, g_height; +static int g_screen_width, g_screen_height; +static int g_harfe_width, g_harfe_height; +double g_scale_factor; static TTF_Font *font = NULL; void -display_init(int width, int height) +display_init(int screen_width, int screen_height, int harfe_width, int harfe_height) { SDL_RWops *font_file; - g_width = width; - g_height = height; + g_screen_width = screen_width; + g_screen_height = screen_height; + g_harfe_width = harfe_width; + g_harfe_height = harfe_height; if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { fprintf(stderr, "Can't initialize SDL.\n"); exit(1); } - screen = SDL_CreateWindow("Laserharfe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL); + screen = SDL_CreateWindow("Laserharfe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height, SDL_WINDOW_OPENGL); if (!screen) { fprintf(stderr, "Can't set video mode.\n"); exit(1); @@ -35,7 +40,32 @@ display_init(int width, int height) TTF_Init(); font_file = SDL_RWFromConstMem(GenBkBasB_ttf, GenBkBasB_ttf_len); - font = TTF_OpenFontRW(font_file, 1, 28); + font = TTF_OpenFontRW(font_file, 1, FONT_SIZE); + + g_scale_factor = (double)harfe_height / (double)screen_height; + if( (double)harfe_width / (double)screen_width < g_scale_factor ) + g_scale_factor = (double)harfe_width / (double)screen_width; + printf( "Using scale factor of %lf\n", g_scale_factor ); +} + +void +display_getdimensions(int *width, int *height, int *fontsize) +{ + *width = g_screen_width; + *height = g_screen_height; + *fontsize = FONT_SIZE; +} + +int +display_scale_harfe_to_screen(int coord) +{ + return (int)((double)coord / g_scale_factor); +} + +int +display_scale_screen_to_harfe(int coord) +{ + return (int)((double)coord * g_scale_factor); } void @@ -48,19 +78,23 @@ display_clear() void display_circle(int x, int y, int w) { - if (x >= 0 && x < g_width && y >= 0 && y < g_height) - display_circle_color(x, y, w, 0xffffffff); + y = g_screen_height - y; + if (x >= 0 && x <= g_screen_width && y >= 0 && y <= g_screen_height) + filledCircleColor(renderer, x, y, w, 0xffffffff); } void display_circle_color(int x, int y, int w, int color) { + y = g_screen_height - y; filledCircleColor(renderer, x, y, w, color); } void display_line(int x0, int y0, int x1, int y1) { + y1 = g_screen_height - y1; + y0 = g_screen_height - y0; SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_RenderDrawLine(renderer, x0, y0, x1, y1); } @@ -68,10 +102,24 @@ display_line(int x0, int y0, int x1, int y1) void display_line_color(int x0, int y0, int x1, int y1, int color) { + y1 = g_screen_height - y1; + y0 = g_screen_height - y0; SDL_SetRenderDrawColor(renderer, (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, 255); SDL_RenderDrawLine(renderer, x0, y0, x1, y1); } +void +display_rect_color(int x, int y, int width, int height, int color) +{ + SDL_Rect r; + r.x = x; + r.y = g_screen_height - 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_redraw() { @@ -81,6 +129,7 @@ display_redraw() void display_text(char *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}; @@ -98,7 +147,7 @@ display_textbox(int min_x, int pos, int max_x, int max_pos, char *text, int colo display_measure_text("#", &w, &h); item_height = 3 * h / 2; - min_y = (g_height - item_height * max_pos) / 2 + pos * item_height; + min_y = (g_screen_height - item_height * max_pos) / 2 + pos * item_height; //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); @@ -106,6 +155,25 @@ display_textbox(int min_x, int pos, int max_x, int max_pos, char *text, int colo display_text(text, min_x + (max_x - min_x - w) / 2, min_y + (item_height - h) / 2, 0xffffffff); } +void +display_messagebox(char *title, char *info) { + SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_INFORMATION, title, info, screen); +} + +int +display_messagebox_yesno(char *title, char *info) { + const SDL_MessageBoxButtonData buttons[] = { + { /* .flags, .buttonid, .text */ 0, 0, "Cancel" }, + { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "OK" }, + }; + const SDL_MessageBoxData messageboxdata = { + SDL_MESSAGEBOX_INFORMATION, screen, title, info, SDL_arraysize(buttons), buttons, NULL + }; + int buttonid; + SDL_ShowMessageBox(&messageboxdata, &buttonid); + return buttonid == 1; +} + int display_test_menu_click(int y, int max_pos) { @@ -114,7 +182,7 @@ display_test_menu_click(int y, int max_pos) /* display_measure_text( "#", &w, &h ); item_height = 3 * h / 2; - min_y = ( g_height - item_height * max_pos ) / 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; -- cgit v1.2.3