
Former-commit-id: 00846e544ad84ed8c7d0d7fe9a62bebe9999cf2d Former-commit-id: 4688e987806e16e57798803f632e14d4e0f71b77
91 lines
1.6 KiB
C
Executable file
91 lines
1.6 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)
|
|
{
|
|
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 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);
|
|
}
|