breakout/font.c
2018-01-21 21:10:27 +01:00

119 lines
4.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "font.h"
#define FONT_FontFile1 "assets/fonts/monofur.ttf"
#define FONT_FontFile2 "assets/fonts/ka1.ttf"
#define FONT_FontFile3 "assets/fonts/minecraft.ttf"
#define FONT_FontFile4 "assets/fonts/ae-bi.ttf"
extern int width, height;
int FONT_FontCount;
SDL_Color FONT_FontColor;
TTF_Font ** FONT_FontFamily;
bool FONT_IsInit = false;
void FONT_Initialize(){
if (!FONT_IsInit) {
printf("Initializing Font...\n");
FONT_FontColor = (SDL_Color) {255, 255, 255 };
FONT_FontCount = 4;
FONT_FontFamily = (TTF_Font **)malloc(FONT_FontCount * sizeof(TTF_Font *));
FONT_FontFamily[0] = TTF_OpenFont(FONT_FontFile1, 48);
if (!FONT_FontFamily) printf("Font 1 could not initialize! Error: %s\n", TTF_GetError());
else printf("Font 1 was successfully initialized!\n");
FONT_PrintFontStyle(FONT_FontFamily[0]);
FONT_FontFamily[1] = TTF_OpenFont(FONT_FontFile2, 48);
if (!FONT_FontFamily) printf("Font 2 could not initialize! Error: %s\n", TTF_GetError());
else printf("Font 2 was successfully initialized!\n");
FONT_PrintFontStyle(FONT_FontFamily[1]);
FONT_FontFamily[2] = TTF_OpenFont(FONT_FontFile3, 48);
if (!FONT_FontFamily) printf("Font 3 could not initialize! Error: %s\n", TTF_GetError());
else printf("Font 3 was successfully initialized!\n");
FONT_PrintFontStyle(FONT_FontFamily[2]);
FONT_FontFamily[3] = TTF_OpenFont(FONT_FontFile4, 48);
if (!FONT_FontFamily) printf("Font 4 could not initialize! Error: %s\n", TTF_GetError());
else printf("Font 4 was successfully initialized!\n");
FONT_PrintFontStyle(FONT_FontFamily[3]);
printf("Font initialized!\n");
FONT_IsInit = true;
} else printf("Font already initialized!\n");
} /* FONT_Initialize */
void FONT_PrintFontStyle(TTF_Font * ffont){
int style;
style = TTF_GetFontStyle(ffont);
printf("The font style is:");
if (style == TTF_STYLE_NORMAL)
printf(" normal");
else {
if (style & TTF_STYLE_BOLD)
printf(" bold");
if (style & TTF_STYLE_ITALIC)
printf(" italic");
if (style & TTF_STYLE_UNDERLINE)
printf(" underline");
if (style & TTF_STYLE_STRIKETHROUGH)
printf(" strikethrough");
}
printf("\n");
}
void FONT_Deinitialize(){
if (FONT_IsInit) {
printf("De-initializing Font...\n");
for (int i = 0; i < FONT_FontCount; i++) {
TTF_CloseFont(FONT_FontFamily[i]);
}
free(FONT_FontFamily);
printf("Font de-initialized!\n");
FONT_IsInit = false;
} else printf("Font already de-initialized!\n");
}
void FONT_RenderText(SDL_Renderer * renderer, char * text, SDL_Rect * dstRect, int index){
SDL_Rect srcRect;
srcRect.x = 0;
srcRect.y = 0;
SDL_Texture * texture = FONT_GenerateTexture(renderer, text, &srcRect, index);
SDL_RenderCopy(renderer, texture, &srcRect, dstRect);
SDL_DestroyTexture(texture);
}
void FONT_RenderTextCentered(SDL_Renderer * renderer, char * text, float scale, int index){
SDL_Rect srcRect, dstRect;
srcRect.x = 0;
srcRect.y = 0;
SDL_Texture * texture = FONT_GenerateTexture(renderer, text, &srcRect, index);
dstRect.w = (int)roundf((float)(srcRect.w) * scale);
dstRect.h = (int)roundf((float)(srcRect.h) * scale);
dstRect.x = (width - dstRect.w) / 2;
dstRect.y = (height - dstRect.h) / 2;
SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
SDL_DestroyTexture(texture);
}
SDL_Texture * FONT_GenerateTexture(SDL_Renderer * renderer, char * text, SDL_Rect * Message_rect, int index){
SDL_Surface * tmpSurface = FONT_GenerateSurface(text, Message_rect, index);
SDL_Texture * resultTexture = SDL_CreateTextureFromSurface(renderer, tmpSurface);
SDL_FreeSurface(tmpSurface);
return resultTexture;
} /* FONT_GenerateSurface */
SDL_Surface * FONT_GenerateSurface(char * text, SDL_Rect * Message_rect, int index){
TTF_SizeText(FONT_FontFamily[index], text, &(Message_rect->w), &(Message_rect->h));
return TTF_RenderText_Solid(FONT_FontFamily[index], text, FONT_FontColor);
}