2424#include " preference.h"
2525#include " sprite.h"
2626#include " screen.h"
27+ #include " utils.h"
2728
2829/* ** Variables globales ***/
2930/* *************************/
3031extern Sprite *Sprites;
32+ extern SDL_Renderer *sdlRenderer;
33+
34+ void Screen::Init ()
35+ {
36+ char fontPath[512 ];
37+ strcpy (fontPath, " Andika-R.ttf" );
38+ Utils::GetPath (fontPath);
39+
40+ m_font = TTF_OpenFont (fontPath, 28 );
41+ }
42+
43+ void Screen::Delete ()
44+ {
45+ for (auto &texture: cachedString) {
46+ SDL_DestroyTexture (texture.second );
47+ }
48+ TTF_CloseFont (m_font);
49+ }
3150
3251/* ** Affiche un Sprite ***/
3352/* ************************/
@@ -42,6 +61,29 @@ void Screen::PrintCable(int dx, int dy, int fx, int fy)
4261 Sprites[rope].PrintRope (dx, dy, fx, fy);
4362}
4463
64+ void Screen::PrintText (const std::string &Text, int x, int y)
65+ {
66+ SDL_Texture *texture = nullptr ;
67+ if (cachedString.count (Text) > 0 ) {
68+ texture = cachedString[Text];
69+ }
70+ else {
71+ SDL_Color fgColor = { 255 , 255 , 255 , 255 };
72+ SDL_Surface *surf = TTF_RenderText_Blended (m_font, Text.c_str (), fgColor);
73+ texture = SDL_CreateTextureFromSurface (sdlRenderer, surf);
74+ SDL_FreeSurface (surf);
75+ cachedString.insert ({ Text, texture });
76+ }
77+
78+ SDL_Rect Position;
79+
80+ SDL_QueryTexture (texture, NULL , NULL , &Position.w , &Position.h );
81+ Position.x = x - Position.w ;
82+ Position.y = y - Position.h ;
83+
84+ SDL_RenderCopy (sdlRenderer, texture, nullptr , &Position);
85+ }
86+
4587/* ** Affiche un text ***/
4688/* **********************/
4789void Screen::PrintText (e_Sprite Text, int x, int y)
0 commit comments