Advertisement
Kitomas

cool bitmap font thing header (font.hpp)

Nov 9th, 2023
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #ifndef _UTILS_FONT_HPP
  2. #define _UTILS_FONT_HPP
  3. //todo: make function to swap renderers
  4.  
  5. #include <SDL2/SDL.h>
  6.  
  7. #include <string>
  8.  
  9.  
  10. extern const Uint8 font8x8_default[1024];
  11.  
  12.  
  13. class font8x8 {
  14.   SDL_bool _valid = SDL_FALSE; //used for method call checks
  15.  
  16.   SDL_Renderer* _renderer    = nullptr;
  17.   SDL_Surface*  _fontSurface = nullptr;
  18.   SDL_Texture*  _fontTexture = nullptr;
  19.  
  20.   SDL_Color _palette[2] = {{0},{0}}; //bg and text palette colors respectively
  21.   SDL_Point _scale = {8,8}; //1x scale by default
  22.  
  23.  
  24.   void _freeSurfaceSafely(){
  25.     if(_fontSurface != nullptr){
  26.       SDL_FreeSurface(_fontSurface);
  27.       _fontSurface = nullptr;
  28.     }
  29.   }
  30.  
  31.   void _freeTextureSafely(){
  32.     if(_fontTexture != nullptr){
  33.       SDL_DestroyTexture(_fontTexture);
  34.       _fontTexture = nullptr;
  35.     }
  36.   }
  37.  
  38.  
  39. public:
  40.   SDL_bool isValid(){ return _valid; }
  41.  
  42.   font8x8(SDL_Renderer* renderer, const Uint8* table = font8x8_default);
  43.  
  44.   ~font8x8(){
  45.     _freeSurfaceSafely();
  46.     _freeTextureSafely();
  47.   }
  48.  
  49.  
  50.   SDL_Color getBackgroundColor(){ return _palette[0]; }
  51.   SDL_Color getTextColor(){ return _palette[1]; }
  52.   void setPalette(SDL_Color background, SDL_Color text);
  53.  
  54.   float getScaleX(){ return (float)_scale.x/8; }
  55.   float getScaleY(){ return (float)_scale.y/8; }
  56.   void setScale(SDL_FPoint scale){
  57.     _scale.x = scale.x*8 + 0.5f;
  58.     _scale.y = scale.y*8 + 0.5f;
  59.   }
  60.  
  61.  
  62.   void putCharRaw(char chr, int x, int y);
  63.   void putChar(char chr, int column, int row){ //align char position to scale
  64.     putCharRaw(chr, column*_scale.x, row*_scale.y);
  65.   }
  66.  
  67.   void printRaw(const std::string& txt, int x, int y);
  68.   void print(const std::string& txt, int column, int row){
  69.     printRaw(txt, column*_scale.x, row*_scale.y); //basically same thing as putChar
  70.   }
  71. };
  72.  
  73.  
  74. #endif /* _UTILS_FONT_HPP */
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement