#include "SDL2/SDL.h" #include "SDL2/SDL2_gfxPrimitives.h" #include #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 }; static SDL_Window *screen; static SDL_Renderer *renderer; 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 screen_width, int screen_height, int harfe_width, int harfe_height) { SDL_RWops *font_file; 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, screen_width, screen_height, SDL_WINDOW_OPENGL); if (!screen) { fprintf(stderr, "Can't set video mode.\n"); exit(1); } renderer = SDL_CreateRenderer(screen, -1, 0); SDL_RenderClear(renderer); TTF_Init(); 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; 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 display_clear() { SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); } void display_circle(int x, int y, int w) { 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); } 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); } static void display_rect_color_static(int x, int y, int width, int height, int color) { SDL_Rect r; r.x = x; 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() { SDL_RenderPresent(renderer); } static void display_text_static(char const *text, int x, int y, int color) { 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}; SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, sText); SDL_RenderCopy(renderer, texture, NULL, &rect); 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 const *text, int color) { int w, h, min_y, item_height; display_measure_text("#", &w, &h); item_height = 3 * h / 2; min_y = pos * item_height + 4; display_rect_color_static(min_x, min_y, max_x - min_x, item_height - 4, color); 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_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 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; }