Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _UTILS_FONT_HPP
- #define _UTILS_FONT_HPP
- //todo: make function to swap renderers
- #include <SDL2/SDL.h>
- #include <string>
- extern const Uint8 font8x8_default[1024];
- class font8x8 {
- SDL_bool _valid = SDL_FALSE; //used for method call checks
- SDL_Renderer* _renderer = nullptr;
- SDL_Surface* _fontSurface = nullptr;
- SDL_Texture* _fontTexture = nullptr;
- SDL_Color _palette[2] = {{0},{0}}; //bg and text palette colors respectively
- SDL_Point _scale = {8,8}; //1x scale by default
- void _freeSurfaceSafely(){
- if(_fontSurface != nullptr){
- SDL_FreeSurface(_fontSurface);
- _fontSurface = nullptr;
- }
- }
- void _freeTextureSafely(){
- if(_fontTexture != nullptr){
- SDL_DestroyTexture(_fontTexture);
- _fontTexture = nullptr;
- }
- }
- public:
- SDL_bool isValid(){ return _valid; }
- font8x8(SDL_Renderer* renderer, const Uint8* table = font8x8_default);
- ~font8x8(){
- _freeSurfaceSafely();
- _freeTextureSafely();
- }
- SDL_Color getBackgroundColor(){ return _palette[0]; }
- SDL_Color getTextColor(){ return _palette[1]; }
- void setPalette(SDL_Color background, SDL_Color text);
- float getScaleX(){ return (float)_scale.x/8; }
- float getScaleY(){ return (float)_scale.y/8; }
- void setScale(SDL_FPoint scale){
- _scale.x = scale.x*8 + 0.5f;
- _scale.y = scale.y*8 + 0.5f;
- }
- void putCharRaw(char chr, int x, int y);
- void putChar(char chr, int column, int row){ //align char position to scale
- putCharRaw(chr, column*_scale.x, row*_scale.y);
- }
- void printRaw(const std::string& txt, int x, int y);
- void print(const std::string& txt, int column, int row){
- printRaw(txt, column*_scale.x, row*_scale.y); //basically same thing as putChar
- }
- };
- #endif /* _UTILS_FONT_HPP */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement