Skip to content

Instantly share code, notes, and snippets.

@Otteri
Created August 6, 2018 17:42
Show Gist options
  • Save Otteri/d48640655a3ada6f2c1812bafcb2c7ff to your computer and use it in GitHub Desktop.
Save Otteri/d48640655a3ada6f2c1812bafcb2c7ff to your computer and use it in GitHub Desktop.
#pragma once
#include <SDL_ttf.h>
#include <map>
#include <string>
#include <vector>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GOOD TO KNOW ABOUT THE CLASS. +--------------------------+ //
// | TextService | //
// How to use: |--------------------------| //
// 1. Define the text appearance by using the Properties struct. | + [Fonts] | //
// | + [Colors] | //
// 2. When all text properties are set, the text can be created by | + createText() | //
// using createText(). Text is automatically rendered on screen | + removeText() | //
// until it is removed with removeText() / RemoveAllTexts(). | + removeAllTexts() | //
// | + setTextColor() | //
// 3. If user wants to modify the text after creation, then functions | + setTextPosition() | //
// from TextService can be used (alternatively a new text can be | +--------------------+ | //
// created and old removed). | | TextProperties | | //
// | +--------------------+ | //
// In short: | | + text | | //
// TextService provides data structures and service functions for | | + color | | //
// manipulating and creating renderable texts on screen. Everything | | + font | | //
// necessary is under TextService namespace. There should be only one | | + size | | //
// TextService class at time, which keeps track of open fonts and | | + x | | //
// texts that are being passed to actual SDL renderer. TextProperties | | + y | | //
// is dynamically allocated class which holds the actual text data. | +--------------------+ | //
// +--------------------------+ //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TextService {
public:
enum class Colors
{
red = 1, // Note:
green = 2, // Enum allows colors to be called from TextService
blue = 3, // namespace instead of using instance to access some
yellow = 4, // color, which is the reason why enum is used here.
white = 5, // E.g struct values cannot be defined in header
gray = 6, // unless all values are static.
black = 7
};
enum class Fonts
{
ostrich = 1,
};
// 'User interface' for defining the text appearance.
struct Properties {
std::string text;
Colors color;
Fonts font;
int size;
int x;
int y;
};
class TextProperties {
public:
Properties properties;
TextProperties(SDL_Renderer* renderer, Properties properties); // The attribute names are intentionally left on place
TextProperties(SDL_Renderer* renderer, std::string text, TextService::Fonts font, int font_size, Colors color, SDL_Point point);
TextProperties(SDL_Renderer* renderer, std::string text, TextService::Fonts font, int font_size, Colors color, int x, int y);
~TextProperties();
friend class TextService; // Allow TextProperties to use helper functions from TextService.
private:
SDL_Rect rect; // These private attributes are needed for text creation.
TTF_Font* fontPtr; // Helper functions below set these automatically by using
SDL_Texture* texture; // user inputs from properties struct.
SDL_Renderer* renderer;
void setRect();
void setTexture(std::string text, TTF_Font* font, Colors color);
SDL_Texture* surfaceToTexture(SDL_Surface*);
SDL_Color getRGBA(TextService::Colors);
void checkValidity();
};
TextService(SDL_Renderer*);
~TextService();
void passTextsToRenderer();
TextService::TextProperties& createText(Properties);
TextService::TextProperties& createText(std::string text, TextService::Fonts font, int font_size, Colors color, int x, int y);
TextService::TextProperties& createText(std::string text, TextService::Fonts font, int font_size, Colors color, SDL_Point point);
void removeText(TextService::TextProperties& text);
void removeAllTexts();
// 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);
private:
TTF_Font* openFont(Fonts font, int);
std::map<std::pair<Fonts, int>, TTF_Font*> fonts;
std::vector<TextProperties*> renderQueue;
SDL_Renderer* renderer;
std::string FONTS_PATH = ("render/fonts/");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment