mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 07:53:43 +00:00
add function to render text
This commit is contained in:
parent
d4374ef46d
commit
3fd7b383b9
@ -2,8 +2,27 @@
|
||||
#define CHICKENGAME_TEXTMANAGER_H
|
||||
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <SDL3/SDL_surface.h>
|
||||
#include <map>
|
||||
|
||||
enum DisplayOptions
|
||||
{
|
||||
SOLID,
|
||||
SHADED,
|
||||
BLENDED,
|
||||
LCD,
|
||||
};
|
||||
|
||||
struct Color
|
||||
{
|
||||
int r;
|
||||
int g;
|
||||
int b;
|
||||
int a;
|
||||
};
|
||||
|
||||
class GameInternal;
|
||||
|
||||
class TextManager
|
||||
{
|
||||
public:
|
||||
@ -12,6 +31,8 @@ public:
|
||||
for (auto& it : this->font_cache) {
|
||||
TTF_CloseFont(it.second);
|
||||
}
|
||||
|
||||
TTF_Quit();
|
||||
}
|
||||
|
||||
TextManager(TextManager const&) = delete;
|
||||
@ -19,8 +40,23 @@ public:
|
||||
|
||||
TTF_Font* loadFont(const char* filepath);
|
||||
|
||||
/*
|
||||
* font - to search for font
|
||||
* text - text to display
|
||||
* displayoptions - theres like 4 goddamn funcs, ill wrap them all in one
|
||||
* fg + bg - solid only takes fg, rest also bg colors
|
||||
* wraplength - manually set wraplength, set to 0 to wrap on newline chars, -1 to disable (again. 4 more funcs)
|
||||
* */
|
||||
// TODO: change the sdl surface, i just dont know what i gotta change it to lol
|
||||
SDL_Surface* RenderText(GameInternal* game, std::string font, std::string text, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth);
|
||||
|
||||
std::map<const char*, TTF_Font*> font_cache;
|
||||
|
||||
private:
|
||||
SDL_Surface* RenderSolid(TTF_Font* font, std::string text, Color fg, int wrapWidth);
|
||||
SDL_Surface* RenderShaded(TTF_Font* font, std::string text, Color fg, Color bg, int wrapWidth);
|
||||
SDL_Surface* RenderBlended(TTF_Font* font, std::string text, Color fg, int wrapWidth);
|
||||
SDL_Surface* RenderLCD(TTF_Font* font, std::string text, Color fg, Color bg, int wrapWidth);
|
||||
};
|
||||
|
||||
#endif //CHICKENGAME_TEXTMANAGER_H
|
||||
|
||||
@ -2,8 +2,14 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "GameInternal.h"
|
||||
#include "AssetManager.h"
|
||||
|
||||
TTF_Font* TextManager::loadFont(const char *filepath)
|
||||
{
|
||||
if(!TTF_Init())
|
||||
std::cerr << "Failed to initialize SDL_TTF'" << std::endl;
|
||||
|
||||
auto it = this->font_cache.find(filepath);
|
||||
|
||||
if (it != this->font_cache.end()) {
|
||||
@ -20,4 +26,60 @@ TTF_Font* TextManager::loadFont(const char *filepath)
|
||||
std::cout << "Loaded font at " << filepath << std::endl;
|
||||
|
||||
return font;
|
||||
}
|
||||
|
||||
SDL_Surface* TextManager::RenderText(GameInternal* game, std::string font, std::string text, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth)
|
||||
{
|
||||
TTF_Font* ttfFont = game->assets->getFont(font);
|
||||
|
||||
switch(displayOptions)
|
||||
{
|
||||
case SOLID:
|
||||
RenderSolid(font, text, fg, wrapWidth);
|
||||
return;
|
||||
case SHADED:
|
||||
RenderShaded(font, text, fg, bg, wrapWidth);
|
||||
return;
|
||||
case BLENDED:
|
||||
RenderBlended(font, text, fg, wrapWidth)
|
||||
return;
|
||||
case LCD:
|
||||
RenderLCD(font, text, fg, bg, wrapWidth);
|
||||
return;
|
||||
default:
|
||||
std::cerr << "Invalid display option!" << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Surface* TextManager::RenderSolid(TTF_Font* font, std::string text, Color fg, int wrapWidth)
|
||||
{
|
||||
if(wrapWidth == -1)
|
||||
return TTF_RenderText_Solid(font, text.c_str(), text.length(), fg);
|
||||
|
||||
return TTF_RenderText_Solid_Wrapped(font, text.c_str(), text.length(), fg, wrapWidth);
|
||||
}
|
||||
|
||||
SDL_Surface* TextManager::RenderShaded(TTF_Font* font, std::string text, Color fg, Color bg, int wrapWidth)
|
||||
{
|
||||
if(wrapWidth == -1)
|
||||
return TTF_RenderText_Shaded(font, text.c_str(), text.length(), fg, bg);
|
||||
|
||||
return TTF_RenderText_Shaded_Wrapped(font, text.c_str(), text.length(), fg, bg, wrapWidth);
|
||||
}
|
||||
|
||||
SDL_Surface* TextManager::RenderBlended(TTF_Font* font, std::string text, Color fg, int wrapWidth)
|
||||
{
|
||||
if(wrapWidth == -1)
|
||||
return TTF_RenderText_Blended(font, text.c_str(), text.length(), fg);
|
||||
|
||||
return TTF_RenderText_Blended_Wrapped(font, text.c_str(), text.length(), fg, wrapWidth);
|
||||
}
|
||||
|
||||
SDL_Surface* TextManager::RenderLCD(TTF_Font* font, std::string text, Color fg, Color bg, int wrapWidth)
|
||||
{
|
||||
if(wrapWidth == -1)
|
||||
return TTF_RenderText_LCD(font, text.c_str(), text.length(), fg, bg);
|
||||
|
||||
return TTF_RenderText_LCD_Wrapped(font, text.c_str(), text.length(), fg, bg, wrapWidth);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user