From 8aa3cd3550e92085b2b0192644236c0bae34391b Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Sun, 21 Jan 2018 19:41:29 +0100 Subject: [PATCH] Added font library (not entirely tested) --- breakout.c | 4 +++ font.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ font.h | 20 +++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 font.c create mode 100644 font.h diff --git a/breakout.c b/breakout.c index 2a98669..c2f2fdb 100644 --- a/breakout.c +++ b/breakout.c @@ -10,6 +10,8 @@ #include "breakout.h" #include "vector.h" +extern float XScale, YScale; + #define BALL_TexturePath "assets/images/ball.png" #define PADDLE_TexturePath "assets/images/paddle.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->Location) = VECTOR_Add((obj->Location), oldMomentum); + RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location)); if (!BALL_CollideWithPaddle(obj, paddle)) // Collide with Paddle 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) { case MouseControl: SDL_GetMouseState(&mouseX, NULL); + mouseX = (int)roundf((float)mouseX / XScale); if (abs(mouseX - paddleXMid) > (obj->Speed)) { if (mouseX > paddleXMid) rightKeyPressed = true; diff --git a/font.c b/font.c new file mode 100644 index 0000000..1cd22b1 --- /dev/null +++ b/font.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include + +#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); +} diff --git a/font.h b/font.h new file mode 100644 index 0000000..eaf2688 --- /dev/null +++ b/font.h @@ -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__