viz1090/sdl1090/font.c
Nathan d450dc59ce initial SDL version
Former-commit-id: 3714e6bfc11cd1ccb095602b2f93d64b4d107346
Former-commit-id: cfe3cc42c26367d1875c31c581ef1dbf4e2d89e5
2017-09-13 22:21:36 -05:00

60 lines
1.4 KiB
C
Executable file

#include "font.h"
TTF_Font *loadFont(char *name, int size)
{
/* Use SDL_TTF to load the font at the specified size */
TTF_Font *font = TTF_OpenFont(name, size);
if (font == NULL)
{
printf("Failed to open Font %s: %s\n", name, TTF_GetError());
exit(1);
}
return font;
}
void closeFont(TTF_Font *font)
{
/* Close the font once we're done with it */
if (font != NULL)
{
TTF_CloseFont(font);
}
}
void drawString(char * text, int x, int y, TTF_Font *font, SDL_Color color)
{
SDL_Rect dest;
SDL_Surface *surface;
// surface = TTF_RenderUTF8_Shaded(font, text, foregroundColor, backgroundColor);
surface = TTF_RenderText_Solid(font, text, color);
if (surface == NULL)
{
printf("Couldn't create String: %s\n", SDL_GetError());
return;
}
SDL_Texture* Message = SDL_CreateTextureFromSurface(game.renderer, surface); //now you can convert it into a texture
SDL_Rect Message_rect; //create a rect
Message_rect.x = x; //controls the rect's x coordinate
Message_rect.y = y; // controls the rect's y coordinte
Message_rect.w = surface->w; // controls the width of the rect
Message_rect.h = surface->h; // controls the height of the rect
SDL_RenderCopy(game.renderer, Message, NULL, &Message_rect); //you put the renderer's name first, the Message, the crop size(you can ignore this if you don't want to dabble with cropping), and the rect which is the size and coordinate of your texture
SDL_FreeSurface(surface);
}