viz1090/sdl1090/font.c
Nathan aff3e48bbc trying 90 degree lables
Former-commit-id: 416a4800a52e43f2583252650e6345f88fa7f988
Former-commit-id: 5efa8306924bca3a336db7412480ee40b3876dbc
2017-10-06 11:34:36 -05:00

127 lines
2.2 KiB
C
Executable file

#include "font.h"
#include "SDL/SDL_rotozoom.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)
{
if(!strlen(text)) {
return;
}
SDL_Surface *surface;
SDL_Rect dest;
surface = TTF_RenderUTF8_Blended(font, text, color);
if (surface == NULL)
{
printf("Couldn't create String %s: %s\n", text, SDL_GetError());
return;
}
/* Blit the entire surface to the screen */
dest.x = x;
dest.y = y;
dest.w = surface->w;
dest.h = surface->h;
SDL_BlitSurface(surface, NULL, game.screen, &dest);
/* Free the generated string image */
SDL_FreeSurface(surface);
}
void drawString90(char * text, int x, int y, TTF_Font *font, SDL_Color color)
{
if(!strlen(text)) {
return;
}
SDL_Surface *surface;
SDL_Rect dest;
surface = TTF_RenderUTF8_Blended(font, text, color);
if (surface == NULL)
{
printf("Couldn't create String %s: %s\n", text, SDL_GetError());
return;
}
/* Blit the entire surface to the screen */
dest.x = x;
dest.y = y;
dest.w = surface->w;
dest.h = surface->h;
SDL_Surface *temp = rotateSurface90Degrees(surface,1);
SDL_BlitSurface(temp, NULL, game.screen, &dest);
/* Free the generated string image */
SDL_FreeSurface(surface);
SDL_FreeSurface(temp);
}
void drawStringBG(char * text, int x, int y, TTF_Font *font, SDL_Color color, SDL_Color bgColor) {
if(!strlen(text)) {
return;
}
SDL_Surface *surface;
SDL_Rect dest;
surface = TTF_RenderUTF8_Shaded(font, text, color, bgColor);
if (surface == NULL)
{
printf("Couldn't create String %s: %s\n", text, SDL_GetError());
return;
}
/* Blit the entire surface to the screen */
dest.x = x;
dest.y = y;
dest.w = surface->w;
dest.h = surface->h;
SDL_BlitSurface(surface, NULL, game.screen, &dest);
/* Free the generated string image */
SDL_FreeSurface(surface);
}