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>
- #include <utils/renderer.hpp>
- extern const Uint8 _font8x8_default[1024];
- //the enum is mostly to name chars that don't
- //have proper char literals (or have diff ones)
- typedef enum font8x8_default_chars {
- //chr_null = 0x00,
- chr_human = 0x01,
- chr_box_v = 0x02, //box vertical
- chr_box_h = 0x03, //box horizontal
- chr_box_i = 0x04, //box intersection
- chr_potion = 0x05,
- chr_sword = 0x06,
- chr_armor = 0x07, //(aka '\a')
- chr_neg_x = 0x08, //move 1 char left (aka '\b')
- chr_heart = 0x09, //(aka '\t')
- //chr_newline = 0x0A, //'\n'
- chr_approx = 0x0B, //approximately equal to
- chr_pos_y = 0x0C, //move 1 char down (aka '\f')
- chr_neg_y = 0x0D, //move 1 char up (aka '\r')
- chr_moon = 0x0E,
- chr_sun = 0x0F,
- chr_grad_16 = 0x10, //gradient value 16/16
- chr_grad_15 = 0x11, //gradient value 15/16
- chr_grad_14 = 0x12, //gradient value 14/16
- chr_grad_13 = 0x13, //gradient value 13/16
- chr_grad_12 = 0x14, //gradient value 12/16
- chr_grad_11 = 0x15, //gradient value 11/16
- chr_grad_10 = 0x16, //gradient value 10/16
- chr_grad_9 = 0x17, //gradient value 9/16
- chr_grad_8 = 0x18, //gradient value 8/16
- chr_grad_7 = 0x19, //gradient value 7/16
- chr_grad_6 = 0x1A, //gradient value 6/16
- chr_grad_5 = 0x1B, //gradient value 5/16
- chr_grad_4 = 0x1C, //gradient value 4/16
- chr_grad_3 = 0x1D, //gradient value 3/16
- chr_grad_2 = 0x1E, //gradient value 2/16
- chr_grad_1 = 0x1F, //gradient value 1/16
- chr_grad_0 = 0x20, //gradient value 0/16 (aka ' ')
- //...
- chr_pos_x = 0x7F, //move 1 char right (aka transparent char)
- } font8x8_default_chars;
- 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
- union {
- SDL_Rect _lastChar = { 0,0, 8,8 }; //8x8 = 1x scale by default
- struct { SDL_Point _lastPosition, _scale; };
- };
- 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,
- SDL_Color background = { 0, 0, 0,255},
- SDL_Color text = {255,255,255,255},
- const Uint8* table = _font8x8_default);
- ~font8x8(){
- _freeSurfaceSafely();
- _freeTextureSafely();
- _valid = SDL_FALSE;
- }
- SDL_Color getBackgroundColor(){ return _palette[0]; }
- SDL_Color getTextColor(){ return _palette[1]; }
- void setPalette(SDL_Color background, SDL_Color text);
- SDL_Point getLastPosition(){ return _lastPosition; }
- SDL_Point getScale(){ return _scale; }
- void setScale(SDL_Point scale){ _scale = scale; }
- 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