viz1090/sdl1090/font.c
Nathan c28bebc294 initial SDL version
Former-commit-id: eec0da8c0de0a6761c0f54fcf2dd3668181b260a
Former-commit-id: d9bbbde7a4209187620f4464b88f33f0b81cc73e
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);
}