Skip to content

Instantly share code, notes, and snippets.

@Otteri
Created August 4, 2018 19:51
Show Gist options
  • Save Otteri/afab3c489b5708a0bdfab414dd358bcd to your computer and use it in GitHub Desktop.
Save Otteri/afab3c489b5708a0bdfab414dd358bcd to your computer and use it in GitHub Desktop.
// Note: static renderer must be defined when creating the service class.
class TextService {
public:
// Colors has to be defined as an enum, because otherwise
// they cant be accessed outside class. ('Has to be specific for some object').
enum class Colors { red = 1, green = 2, blue = 3, yellow = 4, white = 5, gray = 6, black = 7 };
class TextProperties; // forward declaration;
enum class FontNames { ostrich = 1, blabalb = 2 };
private:
static TTF_Font* openFont(FontNames font, int);
static std::map<std::pair<FontNames, int>, TTF_Font*> fonts;
std::vector<TextProperties*> renderQueue;
public:
class TextProperties { // Nested because this class isn't usable anywhere else.
private:
TTF_Font* fontPtr; // Font enums are only for user, as well as
SDL_Rect rect; // x, y cordinates (or SDL_Points),
SDL_Texture* texture; // These are actually needed for text creation.
SDL_Texture* surfaceToTexture(SDL_Surface*);
void checkValidity();
SDL_Rect getRect();
SDL_Texture* getTexture();
void setRect();
void setTexture(std::string text, TTF_Font* font, Colors color);
void setTextPosition(int x, int y);
void setTextPosition(SDL_Point point);
public:
friend class TextService; // Allow TextProperties to use helper functions from TextService.
int x;
int y;
int size;
FontNames font;
Colors color;
std::string text;
// The attribute names are intentionally left on place,
// because they help to percieve the differences between overloads.
TextProperties();
TextProperties(std::string text, TextService::FontNames font, int font_size, Colors color, SDL_Point point);
TextProperties(std::string text, TextService::FontNames font, int font_size, Colors color, int x, int y);
~TextProperties();
};
TextService();
~TextService();
void render(); // Put this into game main loop and call once.
void createText(TextService::TextProperties& text);
void removeText(TextService::TextProperties& text);
// Text modification functions for user
void setTextColor(TextService::TextProperties& text, Colors color);
void setTextPosition(TextService::TextProperties& text, int x, int y);
void setTextPosition(TextService::TextProperties& text, SDL_Point point);
FontNames Fonts; // Available text fonts
static SDL_Renderer* renderer; // Static attribute must be public
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment