Advertisement
Kitomas

font.hpp as of 2023-11-16

Nov 17th, 2023
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 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. #include <utils/renderer.hpp>
  10.  
  11.  
  12. extern const Uint8 _font8x8_default[1024];
  13. //the enum is mostly to name chars that don't
  14.  //have proper char literals (or have diff ones)
  15. typedef enum font8x8_default_chars {
  16. //chr_null    = 0x00,
  17.   chr_human   = 0x01,
  18.   chr_box_v   = 0x02, //box vertical
  19.   chr_box_h   = 0x03, //box horizontal
  20.   chr_box_i   = 0x04, //box intersection
  21.   chr_potion  = 0x05,
  22.   chr_sword   = 0x06,
  23.   chr_armor   = 0x07, //(aka '\a')
  24.   chr_neg_x   = 0x08, //move 1 char left (aka '\b')
  25.   chr_heart   = 0x09, //(aka '\t')
  26. //chr_newline = 0x0A, //'\n'
  27.   chr_approx  = 0x0B, //approximately equal to
  28.   chr_pos_y   = 0x0C, //move 1 char down (aka '\f')
  29.   chr_neg_y   = 0x0D, //move 1 char up   (aka '\r')
  30.   chr_moon    = 0x0E,
  31.   chr_sun     = 0x0F,
  32.   chr_grad_16 = 0x10, //gradient value 16/16
  33.   chr_grad_15 = 0x11, //gradient value 15/16
  34.   chr_grad_14 = 0x12, //gradient value 14/16
  35.   chr_grad_13 = 0x13, //gradient value 13/16
  36.   chr_grad_12 = 0x14, //gradient value 12/16
  37.   chr_grad_11 = 0x15, //gradient value 11/16
  38.   chr_grad_10 = 0x16, //gradient value 10/16
  39.   chr_grad_9  = 0x17, //gradient value  9/16
  40.   chr_grad_8  = 0x18, //gradient value  8/16
  41.   chr_grad_7  = 0x19, //gradient value  7/16
  42.   chr_grad_6  = 0x1A, //gradient value  6/16
  43.   chr_grad_5  = 0x1B, //gradient value  5/16
  44.   chr_grad_4  = 0x1C, //gradient value  4/16
  45.   chr_grad_3  = 0x1D, //gradient value  3/16
  46.   chr_grad_2  = 0x1E, //gradient value  2/16
  47.   chr_grad_1  = 0x1F, //gradient value  1/16
  48.   chr_grad_0  = 0x20, //gradient value  0/16 (aka ' ')
  49. //...
  50.   chr_pos_x   = 0x7F, //move 1 char right (aka transparent char)
  51. } font8x8_default_chars;
  52.  
  53.  
  54. class font8x8 {
  55.   SDL_bool _valid = SDL_FALSE; //used for method call checks
  56.  
  57.   SDL_Renderer* _renderer    = nullptr;
  58.   SDL_Surface*  _fontSurface = nullptr;
  59.   SDL_Texture*  _fontTexture = nullptr;
  60.  
  61.   SDL_Color _palette[2] = { {0}, {0} }; //bg and text palette colors respectively
  62.   union {
  63.     SDL_Rect _lastChar = { 0,0, 8,8 }; //8x8 = 1x scale by default
  64.     struct { SDL_Point _lastPosition, _scale; };
  65.   };
  66.  
  67.  
  68.   void _freeSurfaceSafely(){
  69.     if(_fontSurface != nullptr){
  70.       SDL_FreeSurface(_fontSurface);
  71.       _fontSurface = nullptr;
  72.     }
  73.   }
  74.  
  75.   void _freeTextureSafely(){
  76.     if(_fontTexture != nullptr){
  77.       SDL_DestroyTexture(_fontTexture);
  78.       _fontTexture = nullptr;
  79.     }
  80.   }
  81.  
  82.  
  83. public:
  84.   SDL_bool isValid(){ return _valid; }
  85.  
  86.   font8x8(SDL_Renderer* renderer,
  87.           SDL_Color background = {  0,  0,  0,255},
  88.           SDL_Color text       = {255,255,255,255},
  89.           const Uint8* table = _font8x8_default);
  90.  
  91.   ~font8x8(){
  92.     _freeSurfaceSafely();
  93.     _freeTextureSafely();
  94.     _valid = SDL_FALSE;
  95.   }
  96.  
  97.  
  98.   SDL_Color getBackgroundColor(){ return _palette[0]; }
  99.   SDL_Color getTextColor(){ return _palette[1]; }
  100.   void setPalette(SDL_Color background, SDL_Color text);
  101.  
  102.   SDL_Point getLastPosition(){ return _lastPosition; }
  103.   SDL_Point getScale(){ return _scale; }
  104.   void setScale(SDL_Point scale){ _scale = scale; }
  105.  
  106.  
  107.   void putCharRaw(char chr, int x, int y);
  108.   void putChar(char chr, int column, int row){ //align char position to scale
  109.     putCharRaw(chr, column*_scale.x, row*_scale.y);
  110.   }
  111.  
  112.   void printRaw(const std::string& txt, int x, int y);
  113.   void print(const std::string& txt, int column, int row){
  114.     printRaw(txt, column*_scale.x, row*_scale.y); //basically same thing as putChar
  115.   }
  116. };
  117.  
  118.  
  119. #endif /* _UTILS_FONT_HPP */
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement