From 3f4bcf3f5b1f30178e33624ef32d354528f95380 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 4 Sep 2023 13:26:37 +0200 Subject: [PATCH] feat: added loading font flowchart and description --- Thesis/Thesis.tex | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/Thesis/Thesis.tex b/Thesis/Thesis.tex index d83e648..8735b45 100644 --- a/Thesis/Thesis.tex +++ b/Thesis/Thesis.tex @@ -1712,6 +1712,86 @@ glm::vec3 vectorToVec3(const std::vector& vec) { \caption{Game object states} \end{figure} +\subsection{Text Renderer} +Text renderer class loads the library and single characters of font used to render text, applies text shaders, sets the text at the position requested by engine and removes text after it is not used \\ +We also use a shader for text, we will describe the class and shader in this section + +\paragraph{initialization} +When text render class is initialized, it loads the correct shader and sets it up to work + +\subsubsection{Loading font} +When initializing text render objects, we provide a ttf file from resources/fonts folder. Text render object calls free type library to load a font + +\paragraph{Initializing loading} +First we clear any characters that were loaded previously, initialize freeType library and check if it was correctly loaded + +\begin{lstlisting}[style=C++Style] +void TextRenderer::Load(const std::string &font, unsigned int fontSize) +{ + // first clear the previously loaded Characters + this->Characters.clear(); + // then initialize and load the FreeType library + FT_Library freeTypeLibrary; + // all functions return a value different than 0 whenever an error occurred + if (FT_Init_FreeType(&freeTypeLibrary)) { + std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl; + } + ... + \end{lstlisting} + +\newapge +\paragraph{Actual loading and configuration} +We load the font using freeType, after checking if it was loaded correctly we set size of a single character, disable byte-alignment in order for the text to be rendered correctly, preload first 128 [ASCII] characters and since we already loaded everything we wanted we destroy FreeType library + +\begin{lstlisting}[style=C++Style] +void TextRenderer::Load(const std::string &font, unsigned int fontSize) +{ + ... + FT_Face face = this -> loadFontAsFace(freeTypeLibrary, font); + // set size to load glyphs as + FT_Set_Pixel_Sizes(face, 0, fontSize); + // disable byte-alignment restriction + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + face = this -> preloadCharacters(face); + this -> destroyFreeType(face, freeTypeLibrary); +} +\end{lstlisting} + +\begin{figure}[htp] +\centering +\begin{tikzpicture}[node distance=1.5cm] +\useasboundingbox (-5,0) rectangle (5, -13cm); % Set a custom bounding box +\node (start) [startstop] {Load font}; +\node (pro1) [process, below of=start] {Clear previous characters}; +\draw [arrow] (start) -- (pro1); + +\node (pro2) [process, below of=pro1] {Initialize FreeType library}; +\draw [arrow] (pro1) -- (pro2); + +\node (pro3) [process, below of=pro2] {Load font}; +\draw [arrow] (pro2) -- (pro3); + +\node (pro4) [process, below of=pro3] {Set letter pixel size}; +\draw [arrow] (pro3) -- (pro4); + +\node (pro5) [process, below of=pro4] {Disable byte allignment}; +\draw [arrow] (pro4) -- (pro5); + +\node (pro6) [process, below of=pro5] {Preload ASCII characters}; +\draw [arrow] (pro5) -- (pro6); + +\node (pro7) [process, below of=pro6] {Destroy FreeType}; +\draw [arrow] (pro6) -- (pro7); + +\node (stop) [startstop, below of=pro7] {Finish loading}; +\draw [arrow] (pro7) -- (stop); + +\end{tikzpicture} +\caption{Loading font logic} +\label{fig:findingMatches} +\end{figure} + + \subsection{Dependency Management} There are several libraries: OpenGL, GLFW, stb\_image and freetype being integrated into engine, they are simply included at the top of any files that need them.