Skip to content

Commit

Permalink
convert renderttf functions to TTFRenderer methods for #199
Browse files Browse the repository at this point in the history
  • Loading branch information
no-lex committed Sep 23, 2022
1 parent 6f56d96 commit 7bbd2a4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
36 changes: 30 additions & 6 deletions src/engine/render/renderttf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@

#include "rendergl.h"
#include "rendertext.h"
#include "renderttf.h"
#include "shaderparam.h"
#include "texture.h"
#include "renderwindow.h"

TTFRenderer ttr;

//starts up SDL2_TTF
bool initttf()
bool TTFRenderer::initttf()
{
if(TTF_Init() < 0)
{
Expand All @@ -30,20 +33,21 @@ bool initttf()
}

//opens a font with the given path and size in points
//if fails, returns nullptr
TTF_Font* openfont(const char * path, int size)
//if fails, returns nullptr to internal value f
void TTFRenderer::openfont(const char * inpath, int size)
{
TTF_Font* f = TTF_OpenFont(path, size);
f = TTF_OpenFont(inpath, size);
TTF_SetFontStyle(f, TTF_STYLE_NORMAL);
TTF_SetFontOutline(f, 0);
TTF_SetFontKerning(f, 1);
TTF_SetFontHinting(f, TTF_HINTING_NORMAL);
return f;
fontcache[size] = f;
path = inpath;
}

//draws a string to the coordinates x, y in the current hud context at a scale factor `scale`
//with a (BGRA) SDL_Color value as passed to its third parameter
void renderttf(TTF_Font* f, const char* message, SDL_Color col, int x, int y, float scale)
void TTFRenderer::renderttf(const char* message, SDL_Color col, int x, int y, float scale)
{
glEnable(GL_BLEND);
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
Expand Down Expand Up @@ -75,3 +79,23 @@ void renderttf(TTF_Font* f, const char* message, SDL_Color col, int x, int y, fl
SDL_FreeSurface(text);
}

//sets the current working font renderer to one with the appropriate font size
//if the size does not exist already, creates a new one with the appropriate size
void TTFRenderer::fontsize(int pts)
{
auto itr = fontcache.find(pts);
if(itr == fontcache.end())
{
TTF_Font* newfont = TTF_OpenFont(path, pts);
TTF_SetFontStyle(newfont, TTF_STYLE_NORMAL);
TTF_SetFontOutline(newfont, 0);
TTF_SetFontKerning(newfont, 1);
TTF_SetFontHinting(newfont, TTF_HINTING_NORMAL);
fontcache[pts] = newfont;
f = newfont;
}
else
{
f = fontcache[pts];
}
}
20 changes: 17 additions & 3 deletions src/engine/render/renderttf.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
extern bool initttf();
extern TTF_Font* openfont(const char * path, int size);
extern void renderttf(TTF_Font* f, const char* message, SDL_Color col, int x, int y, float scale = 1.f);
struct _TTF_Font;
typedef struct _TTF_Font TTF_Font;

class TTFRenderer
{
public:
bool initttf();
void openfont(const char * path, int size);
void renderttf(const char* message, SDL_Color col, int x, int y, float scale = 1.f);
void fontsize(int pts = 12);
private:
TTF_Font* f; //the current working font
std::map<int, TTF_Font *> fontcache; //different sizes of the font are cached in a map which maps them to their size in pt
const char * path;
};

extern TTFRenderer ttr;

0 comments on commit 7bbd2a4

Please sign in to comment.