Advertisement
Kitomas

gfx.cpp as of 2023-11-16

Nov 17th, 2023
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <game/gfx.hpp>
  2.  
  3.  
  4. //contains default palette
  5. #define _UTILS_GFX_CPP
  6. #include <utils/_gfx_tables.h>
  7. #undef _UTILS_GFX_CPP
  8.  
  9.  
  10.  
  11.  
  12. gfx_class::gfx_class(SDL_Renderer* renderer, const SDL_Color* palette){
  13.   //some domain checks
  14.   if(renderer == nullptr) throw "renderer = nullptr";
  15.   if(palette == nullptr) throw "palette = nullptr";
  16.   _renderer = renderer;
  17.   _palette = palette;
  18.  
  19.  
  20.   //create a font for every palette color combination
  21.   for(int color=0; color<256; ++color){
  22.     SDL_Color txt = palette[    color&15 ];
  23.     SDL_Color bg  = palette[16+(color>>4)];
  24.     _colors[color] = new font8x8(renderer, bg, txt);
  25.   }
  26.  
  27.  
  28.   _valid = SDL_TRUE;
  29.   clear(clr_black);
  30.  
  31. }
  32.  
  33.  
  34.  
  35.  
  36. void gfx_class::present(){
  37.   if(!_valid) throw "invalid gfx class";
  38.  
  39.   for(int yi=0; yi<45; ++yi){
  40.     for(int xi=0; xi<80; ++xi){
  41.       gfx_char chr = chars[yi][xi];
  42.       _colors[chr.color.both]->putChar(chr.chr, xi,yi);
  43.     }
  44.   }
  45.  
  46.   SDL_RenderPresent(_renderer);
  47. }
  48.  
  49.  
  50.  
  51. void gfx_class::clear(gfx_colors fillColor){
  52.   if(!_valid) throw "invalid gfx class";
  53.   gfx_char fillValue = { .chr = ' ' };
  54.   fillValue.color.bg = fillColor;
  55.  
  56.   for(int yi=0; yi<45; ++yi)
  57.     for(int xi=0; xi<80; ++xi)
  58.       chars[yi][xi].value = fillValue.value;
  59. }
  60.  
  61.  
  62.  
  63.  
  64. void gfx_class::set(gfx_char newChar, Uint32 column, Uint32 row){
  65.   if(!_valid) throw "invalid gfx class";
  66.   if(column<80 && row<45){
  67.     if(newChar.bgt){ //char has a transparent background
  68.       chars[row][column].chr = newChar.chr;
  69.       chars[row][column].color.txt = newChar.color.txt;
  70.     } else { //char has an opaque background
  71.       chars[row][column].value = newChar.value;
  72.     }
  73.   }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement