#include #include #include #include #include SDL_Surface * ui_label_outline( TTF_Font *font, const SDL_Color *fg_color, const SDL_Color *bg_color, const int outline, const char text[]) { const int original_outline = TTF_GetFontOutline(font); TTF_SetFontOutline(font, outline); SDL_Surface *bg_surface = TTF_RenderText_Blended(font, text, *bg_color); TTF_SetFontOutline(font, 0); SDL_Surface *fg_surface = TTF_RenderText_Blended(font, text, *fg_color); SDL_Rect rect = {outline, outline, fg_surface->w, fg_surface->h}; SDL_SetSurfaceBlendMode(fg_surface, SDL_BLENDMODE_BLEND); SDL_BlitSurface(fg_surface, NULL, bg_surface, &rect); SDL_FreeSurface(fg_surface); TTF_SetFontOutline(font, original_outline); return bg_surface; } void init_all() { if( SDL_Init(SDL_INIT_EVERYTHING) < 0 ) { printf("SDL: init %s\n", SDL_GetError()); exit(2); } atexit(SDL_Quit); if( TTF_Init() < 0 ) { printf("SDLTTF: init %s\n", TTF_GetError()); exit(2); } atexit(TTF_Quit); } bool handle_input(SDL_Event * event) { while (SDL_PollEvent(event)) if (( event->type == SDL_QUIT ) || ( event->type == SDL_KEYDOWN && event->key.keysym.sym == SDLK_q )) return false; return true; } int main(int argc, char *argv[]) { const unsigned int SCREEN_WIDTH = 800; const unsigned int SCREEN_HEIGHT = 600; const char fontname[] = "PressStart2P.ttf"; const uint8_t fontsize = 16; const uint8_t outline = 3; time_t rawtime; struct tm *timeinfo; int w, h; bool running = true; init_all(); SDL_Window * window = SDL_CreateWindow("Outline", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); TTF_Font *monofont = TTF_OpenFont(fontname, fontsize); if(!monofont) { printf("SDL: OpenFont %s\n", TTF_GetError()); exit(2); } SDL_Color Yellow = {255, 255, 0}; SDL_Color Black = {0, 0, 0}; SDL_Event event; SDL_SetRenderDrawColor( renderer, 255, 255, 255, SDL_ALPHA_OPAQUE ); SDL_Texture * label_texture = NULL; SDL_Surface * label = NULL; SDL_Rect rect; char timestr[25]; while(running) { SDL_RenderClear( renderer ); running = handle_input(&event); time(&rawtime); timeinfo = localtime(&rawtime); snprintf(timestr, 25, "%s", asctime(timeinfo)); label = ui_label_outline(monofont, &Yellow, &Black, 3, timestr); label_texture = SDL_CreateTextureFromSurface(renderer, label); SDL_QueryTexture(label_texture, NULL, NULL, &w, &h); rect = (SDL_Rect){ (SCREEN_WIDTH-w)/2, (SCREEN_HEIGHT-h)/2, w, h }; SDL_RenderCopy(renderer, label_texture, NULL, &rect); SDL_RenderPresent(renderer); // SDL_FreeSurface(label); label = NULL; // SDL_DestroyTexture(label_texture); label_texture = NULL; SDL_Delay(100); } TTF_CloseFont(monofont); SDL_DestroyRenderer(renderer); renderer = NULL; SDL_DestroyWindow(window); window = NULL; return 0; }