#include "SDL2/SDL.h" #include "SDL2/SDL2_gfxPrimitives.h" #include #include "GenBkBasB.h" #include "display.h" #define display_measure_text(text,w,h) TTF_SizeText(font,(text),(w),(h)) static SDL_Window *screen; static SDL_Renderer *renderer; static int g_width, g_height; static TTF_Font *font = NULL; void display_init(int width, int height) { SDL_RWops *font_file; g_width = width; g_height = 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); 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(GenBkBasB_ttf, GenBkBasB_ttf_len); font = TTF_OpenFontRW(font_file, 1, 28); } void display_clear() { SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); } 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); } void display_circle_color(int x, int y, int w, int color) { filledCircleColor(renderer, x, y, w, color); } void display_line(int x0, int y0, int x1, int y1) { 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) { SDL_SetRenderDrawColor(renderer, (color >> 24) & 255, (color >> 16) & 255, (color >> 8) & 255, 255); SDL_RenderDrawLine(renderer, x0, y0, x1, y1); } void display_redraw() { SDL_RenderPresent(renderer); } void display_text(char *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_textbox(int min_x, int pos, int max_x, int max_pos, char *text, int color) { int w, h, min_y, item_height; display_measure_text("#", &w, &h); item_height = 3 * h / 2; min_y = (g_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); display_measure_text(text, &w, &h); display_text(text, min_x + (max_x - min_x - w) / 2, min_y + (item_height - h) / 2, 0xffffffff); } 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_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; }