Added font library (not entirely tested)

This commit is contained in:
Michael Chen 2018-01-21 19:41:29 +01:00
parent 9956b5a7ad
commit 8aa3cd3550
3 changed files with 96 additions and 0 deletions

View File

@ -10,6 +10,8 @@
#include "breakout.h" #include "breakout.h"
#include "vector.h" #include "vector.h"
extern float XScale, YScale;
#define BALL_TexturePath "assets/images/ball.png" #define BALL_TexturePath "assets/images/ball.png"
#define PADDLE_TexturePath "assets/images/paddle.png" #define PADDLE_TexturePath "assets/images/paddle.png"
#define BLOCK_TexturePath "assets/images/spritesheet.png" #define BLOCK_TexturePath "assets/images/spritesheet.png"
@ -296,6 +298,7 @@ void BALL_Update(Ball * obj, Scenery * scenery){
(obj->Rotation) += (obj->RotationValue); // No effect on physics (obj->Rotation) += (obj->RotationValue); // No effect on physics
(obj->Location) = VECTOR_Add((obj->Location), oldMomentum); (obj->Location) = VECTOR_Add((obj->Location), oldMomentum);
RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location));
if (!BALL_CollideWithPaddle(obj, paddle)) // Collide with Paddle if (!BALL_CollideWithPaddle(obj, paddle)) // Collide with Paddle
for (int i = 0; i < BlockCount; i++) { // Check Collide with each block for (int i = 0; i < BlockCount; i++) { // Check Collide with each block
@ -403,6 +406,7 @@ void PADDLE_Update(Paddle * obj, const Uint8 * keystate){
switch (obj->Mode) { switch (obj->Mode) {
case MouseControl: case MouseControl:
SDL_GetMouseState(&mouseX, NULL); SDL_GetMouseState(&mouseX, NULL);
mouseX = (int)roundf((float)mouseX / XScale);
if (abs(mouseX - paddleXMid) > (obj->Speed)) { if (abs(mouseX - paddleXMid) > (obj->Speed)) {
if (mouseX > paddleXMid) if (mouseX > paddleXMid)
rightKeyPressed = true; rightKeyPressed = true;

72
font.c Normal file
View File

@ -0,0 +1,72 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "font.h"
SDL_Color FONT_FontColor;
TTF_Font * FONT_FontFamily = NULL;
void FONT_Initialize(){
printf("Initializing Font...\n");
FONT_FontColor = (SDL_Color) {255, 255, 255 };
FONT_FontFamily = TTF_OpenFont(FONT_FontFile, 48);
if (!FONT_FontFamily) printf("Font could not initialize! Error: %s\n", TTF_GetError());
else printf("Font was successfully initialized!\n");
FONT_PrintFontStyle(FONT_FontFamily);
printf("Font initialized!\n");
}
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(){
printf("De-initializing Font...\n");
TTF_CloseFont(FONT_FontFamily);
FONT_FontFamily = NULL; // to be safe...
printf("Font de-initialized!\n");
}
void FONT_RenderText(SDL_Renderer * renderer, char * text, SDL_Rect * dstRect){
SDL_Rect srcRect;
srcRect.x = 0;
srcRect.y = 0;
SDL_Texture * texture = FONT_GenerateTexture(renderer, text, &srcRect);
SDL_RenderCopy(renderer, texture, &srcRect, dstRect);
SDL_DestroyTexture(texture);
}
SDL_Texture * FONT_GenerateTexture(SDL_Renderer * renderer, char * text, SDL_Rect * Message_rect){
SDL_Surface * tmpSurface = FONT_GenerateSurface(text, Message_rect);
SDL_Texture * resultTexture = SDL_CreateTextureFromSurface(renderer, tmpSurface);
SDL_FreeSurface(tmpSurface);
return resultTexture;
} /* FONT_GenerateSurface */
SDL_Surface * FONT_GenerateSurface(char * text, SDL_Rect * Message_rect){
TTF_SizeText(FONT_FontFamily, text, &(Message_rect->w), &(Message_rect->h));
return TTF_RenderText_Solid(FONT_FontFamily, text, FONT_FontColor);
}

20
font.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef __font_h__
#define __font_h__
#define FONT_FontFile "assets/fonts/monofur.ttf"
// Externs
extern SDL_Color FONT_FontColor;
extern TTF_Font * FONT_FontFamily;
// End Externs
// Prototypes
void FONT_Initialize();
void FONT_PrintFontStyle(TTF_Font * ffont);
void FONT_Deinitialize();
void FONT_RenderText(SDL_Renderer * renderer, char * text, SDL_Rect * dstRect);
SDL_Texture * FONT_GenerateTexture(SDL_Renderer * renderer, char * text, SDL_Rect * Message_rect);
SDL_Surface * FONT_GenerateSurface(char * text, SDL_Rect * Message_rect);
// End Prototypes
#endif // __font_h__