Advertisement
Kitomas

soft_rend.hpp

Dec 1st, 2023
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #ifndef _UTILS_SOFT_REND_HPP
  2. #define _UTILS_SOFT_REND_HPP
  3.  
  4.  
  5. #include "fx16_8.hpp"
  6.  
  7. #include <vector>
  8. #include <string>
  9.  
  10.  
  11.  
  12.  
  13. #define UNKNOWN_COLOR (0x7F000000)
  14. // = SDL_PIXELFORMAT_RGB888
  15. union soft_color {
  16.   Uint32 v;
  17.   struct {
  18.     Uint8 b;
  19.     Uint8 g;
  20.     Uint8 r;
  21.     Uint8 _;
  22.   };
  23.  
  24.   soft_color() : v(0) {}
  25.   soft_color(Uint32 _v) : v(_v) {}
  26. };
  27.  
  28.  
  29. struct soft_tri {
  30.   fx_vec3     norm;
  31.   fx_vec3      mid;
  32.   Uint32         a;
  33.   Uint32         b;
  34.   Uint32         c;
  35.   soft_color color;
  36.  
  37.   soft_tri(){}
  38.   soft_tri(Uint32 _a, Uint32 _b, Uint32 _c) : a(_a), b(_b), c(_c), color(0) {}
  39. };
  40.  
  41.  
  42.  
  43. class soft_rend {
  44.   bool _valid = false;
  45.  
  46.   SDL_Window*   _window = nullptr;
  47.   SDL_Surface* _winsurf = nullptr;
  48.   SDL_Surface*  _target = nullptr;
  49.  
  50.   SDL_Point _row_start[1080]; //includes scanline, and starting x
  51.   SDL_Point _row_end[1080]; //only includes ending x (.y is unused)
  52.   int _row_len = 0; //number of scanlines in draw queue
  53.  
  54.   //used for a simple form of screenspace occlusion culling
  55.   int  _edge_start[1080];
  56.   int  _edge_end[1080];
  57.   bool _edge_filled[1080];
  58.   bool _edges_completely_filled;
  59.  
  60.   SDL_Point _windowSize  = {-1,-1};
  61.   SDL_Point _logicalSize = {-1,-1};
  62.  
  63.   Uint32      _windowID = 0;
  64.   Uint32   _windowFlags = 0;
  65.   int   _fullscreenMode = 0;
  66.   int         _numLocks = 0;
  67.   soft_color _drawColor = { .v = 0xff000000 };
  68.  
  69.  
  70.   void _freeWindowSafely(){
  71.     if(_window != nullptr){
  72.       SDL_DestroyWindow(_window); //will free _winsurf automagically~
  73.       _window = nullptr;
  74.     }
  75.   }
  76.  
  77.   void _freeTargetSafely(){
  78.     if(_target != nullptr){
  79.       SDL_FreeSurface(_target);
  80.       _target = nullptr;
  81.     }
  82.   }
  83.  
  84.   void _interpolateTriangle(const soft_tri& tri, const std::vector<fx_vec3>& verts);
  85.  
  86.  
  87. public:
  88.   bool isValid(){ return _valid; }
  89.  
  90.   soft_rend(){}
  91.   soft_rend(SDL_Point windowSize,
  92.             Uint32 windowFlags = 0,
  93.             SDL_Point logicalSize = {256,144});
  94.  
  95.   ~soft_rend(){
  96.     _freeTargetSafely();
  97.     _freeWindowSafely();
  98.   }
  99.  
  100.  
  101.   SDL_Window* getWindow(){ return _window; }
  102.   SDL_Surface* getTarget(){ return _target; }
  103.  
  104.   SDL_Point getWindowSize(){ return _windowSize; }
  105.   SDL_Point getLogicalSize(){ return _logicalSize; }
  106.   SDL_Point getFullscreenSize();
  107.  
  108.   Uint32 getWindowID(){ return _windowID; }
  109.   Uint32 getWindowFlags(){ return _windowFlags; }
  110.   int getFullscreenMode(){ return _fullscreenMode; }
  111.  
  112.  
  113.   void setWindowPosition(SDL_Point newPos);
  114.   void setTitle(const std::string& newTitle);
  115.   void setFullscreenMode(int mode); //0,1,2 = windowed, fullscreen, windowed borderless
  116.   void setSize(SDL_Point windowSize = {-1,-1}, SDL_Point logicalSize = {-1,-1});
  117.   void setLock(bool lockState);
  118.   void setDrawColor(soft_color newColor={.v = 0}){ _drawColor = newColor; }
  119.  
  120.  
  121.   void present();
  122.   void clear(soft_color fillColor = { .v = UNKNOWN_COLOR });
  123.  
  124.  
  125.   void drawTriangle(const soft_tri& tri, const std::vector<fx_vec3>& verts);
  126.   void drawTriangles(const std::vector<soft_tri>& tris, const std::vector<fx_vec3>& verts);
  127. };
  128.  
  129.  
  130.  
  131.  
  132. #endif /* _UTILS_SOFT_REND_HPP */
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement