Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _UTILS_SOFT_REND_HPP
- #define _UTILS_SOFT_REND_HPP
- #include "fx16_8.hpp"
- #include <vector>
- #include <string>
- #define UNKNOWN_COLOR (0x7F000000)
- // = SDL_PIXELFORMAT_RGB888
- union soft_color {
- Uint32 v;
- struct {
- Uint8 b;
- Uint8 g;
- Uint8 r;
- Uint8 _;
- };
- soft_color() : v(0) {}
- soft_color(Uint32 _v) : v(_v) {}
- };
- struct soft_tri {
- fx_vec3 norm;
- fx_vec3 mid;
- Uint32 a;
- Uint32 b;
- Uint32 c;
- soft_color color;
- soft_tri(){}
- soft_tri(Uint32 _a, Uint32 _b, Uint32 _c) : a(_a), b(_b), c(_c), color(0) {}
- };
- class soft_rend {
- bool _valid = false;
- SDL_Window* _window = nullptr;
- SDL_Surface* _winsurf = nullptr;
- SDL_Surface* _target = nullptr;
- SDL_Point _row_start[1080]; //includes scanline, and starting x
- SDL_Point _row_end[1080]; //only includes ending x (.y is unused)
- int _row_len = 0; //number of scanlines in draw queue
- //used for a simple form of screenspace occlusion culling
- int _edge_start[1080];
- int _edge_end[1080];
- bool _edge_filled[1080];
- bool _edges_completely_filled;
- SDL_Point _windowSize = {-1,-1};
- SDL_Point _logicalSize = {-1,-1};
- Uint32 _windowID = 0;
- Uint32 _windowFlags = 0;
- int _fullscreenMode = 0;
- int _numLocks = 0;
- soft_color _drawColor = { .v = 0xff000000 };
- void _freeWindowSafely(){
- if(_window != nullptr){
- SDL_DestroyWindow(_window); //will free _winsurf automagically~
- _window = nullptr;
- }
- }
- void _freeTargetSafely(){
- if(_target != nullptr){
- SDL_FreeSurface(_target);
- _target = nullptr;
- }
- }
- void _interpolateTriangle(const soft_tri& tri, const std::vector<fx_vec3>& verts);
- public:
- bool isValid(){ return _valid; }
- soft_rend(){}
- soft_rend(SDL_Point windowSize,
- Uint32 windowFlags = 0,
- SDL_Point logicalSize = {256,144});
- ~soft_rend(){
- _freeTargetSafely();
- _freeWindowSafely();
- }
- SDL_Window* getWindow(){ return _window; }
- SDL_Surface* getTarget(){ return _target; }
- SDL_Point getWindowSize(){ return _windowSize; }
- SDL_Point getLogicalSize(){ return _logicalSize; }
- SDL_Point getFullscreenSize();
- Uint32 getWindowID(){ return _windowID; }
- Uint32 getWindowFlags(){ return _windowFlags; }
- int getFullscreenMode(){ return _fullscreenMode; }
- void setWindowPosition(SDL_Point newPos);
- void setTitle(const std::string& newTitle);
- void setFullscreenMode(int mode); //0,1,2 = windowed, fullscreen, windowed borderless
- void setSize(SDL_Point windowSize = {-1,-1}, SDL_Point logicalSize = {-1,-1});
- void setLock(bool lockState);
- void setDrawColor(soft_color newColor={.v = 0}){ _drawColor = newColor; }
- void present();
- void clear(soft_color fillColor = { .v = UNKNOWN_COLOR });
- void drawTriangle(const soft_tri& tri, const std::vector<fx_vec3>& verts);
- void drawTriangles(const std::vector<soft_tri>& tris, const std::vector<fx_vec3>& verts);
- };
- #endif /* _UTILS_SOFT_REND_HPP */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement