1 #ifndef _RENDERER_H
2 #define _RENDERER_H
3 
4 #include "../Singleton.h"
5 #include "../object.h"
6 #include "Surface.h"
7 #include "Font.h"
8 #include "Tileset.h"
9 #include "Sprites.h"
10 
11 #include <SDL.h>
12 
13 
14 namespace NXE
15 {
16 namespace Graphics
17 {
18 
19 const NXColor DK_BLUE(0,0,0x21);
20 const NXColor BLACK(0,0,0);
21 const NXColor WHITE(0xFF,0xFF,0xFF);
22 const NXColor GREEN1(0x0,0xFF,0x0);
23 const NXColor GREEN2(0x0,0x98,0x0);
24 const NXColor GREEN3(0x0,0x4E,0x0);
25 const NXColor GREEN4(0x0,0x19,0x0);
26 
27 typedef struct
28 {
29   char *name;
30   uint32_t width;
31   uint32_t height;
32   uint32_t base_width;
33   uint32_t base_height;
34   uint32_t scale;
35   bool widescreen;
36   bool enabled;
37 } gres_t;
38 
39 class Renderer
40 {
41   public:
42     static Renderer *getInstance();
43 
44     int screenWidth = 320;
45     int screenHeight = 240;
46     bool widescreen = false;
47     int scale = 1;
48 
49     bool init(int resolution);
50     void close();
51 
52     bool isWindowVisible();
53 
54     bool initVideo();
55     void setFullscreen(bool enable);
56 
57     bool setResolution(int factor, bool restoreOnFailure = true);
58     const gres_t *getResolutions(bool full_list = false);
59     int getResolutionCount();
60 
61     bool flushAll();
62 
63     void showLoadingScreen();
64 
65     void drawSurface(Surface *src, int x, int y);
66     void drawSurface(Surface *src, int dstx, int dsty, int srcx, int srcy, int wd, int ht);
67     void drawSurfaceMirrored(Surface *src, int dstx, int dsty, int srcx, int srcy, int wd, int ht);
68 
69     void blitPatternAcross(Surface *sfc, int x_dst, int y_dst, int y_src, int height);
70 
71     void clearScreen(NXColor color);
72     void clearScreen(uint8_t r, uint8_t g, uint8_t b);
73 
74     void drawLine(int x1, int y1, int x2, int y2, NXColor color);
75 
76     void drawRect(int x1, int y1, int x2, int y2, NXColor color);
77     void drawRect(int x1, int y1, int x2, int y2, uint8_t r, uint8_t g, uint8_t b);
78     void drawRect(NXRect *rect, uint8_t r, uint8_t g, uint8_t b);
79     void drawRect(NXRect *rect, NXColor color);
80 
81     void fillRect(int x1, int y1, int x2, int y2, NXColor color);
82     void fillRect(int x1, int y1, int x2, int y2, uint8_t r, uint8_t g, uint8_t b);
83     void fillRect(NXRect *rect, uint8_t r, uint8_t g, uint8_t b);
84     void fillRect(NXRect *rect, NXColor color);
85 
86     void drawPixel(int x, int y, NXColor color);
87     void drawPixel(int x, int y, uint8_t r, uint8_t g, uint8_t b);
88 
89     void setClip(int x, int y, int w, int h);
90     void setClip(NXRect *rect);
91     void clearClip();
92     bool isClipSet();
93     void clip(SDL_Rect &srcrect, SDL_Rect &dstrect);
94     void clipScaled(SDL_Rect &srcrect, SDL_Rect &dstrect);
95 
96     void saveScreenshot();
97 
98     void drawSpotLight(int x, int y, Object* o, int r = 255, int g = 255, int b = 255, int upscale = 6);
99 
100     void tintScreen();
101     void flip();
102     SDL_Renderer* renderer();
103     SDL_Window* window();
104     Font font;
105     Tileset tileset;
106     Sprites sprites;
107 
108   private:
109     SDL_Window *_window = nullptr;
110     SDL_Renderer *_renderer = nullptr;
111     int _current_res = -1;
112     bool _need_clip = false;
113     SDL_Rect _clip_rect;
114     SDL_Texture* _spot_light;
115 
116   protected:
117     friend class Singleton<Renderer>;
118 
119     Renderer();
120     ~Renderer();
121     Renderer(const Renderer &) = delete;
122     Renderer &operator=(const Renderer &) = delete;
123 };
124 
drawSurface(Surface * src,int dstx,int dsty)125 void inline Renderer::drawSurface(Surface *src, int dstx, int dsty)
126 {
127   drawSurface(src, dstx, dsty, 0, 0, src->width(), src->height());
128 }
129 
drawRect(int x1,int y1,int x2,int y2,NXColor color)130 void inline Renderer::drawRect(int x1, int y1, int x2, int y2, NXColor color)
131 {
132   drawRect(x1, y1, x2, y2, color.r, color.g, color.b);
133 }
134 
drawRect(NXRect * rect,uint8_t r,uint8_t g,uint8_t b)135 void inline Renderer::drawRect(NXRect *rect, uint8_t r, uint8_t g, uint8_t b)
136 {
137   drawRect(rect->x, rect->y, rect->x + (rect->w - 1), rect->y + (rect->h - 1), r, g, b);
138 }
139 
drawRect(NXRect * rect,NXColor color)140 void inline Renderer::drawRect(NXRect *rect, NXColor color)
141 {
142   drawRect(rect->x, rect->y, rect->x + (rect->w - 1), rect->y + (rect->h - 1), color.r, color.g, color.b);
143 }
144 
fillRect(int x1,int y1,int x2,int y2,NXColor color)145 void inline Renderer::fillRect(int x1, int y1, int x2, int y2, NXColor color)
146 {
147   fillRect(x1, y1, x2, y2, color.r, color.g, color.b);
148 }
149 
fillRect(NXRect * rect,uint8_t r,uint8_t g,uint8_t b)150 void inline Renderer::fillRect(NXRect *rect, uint8_t r, uint8_t g, uint8_t b)
151 {
152   fillRect(rect->x, rect->y, rect->x + (rect->w - 1), rect->y + (rect->h - 1), r, g, b);
153 }
154 
fillRect(NXRect * rect,NXColor color)155 void inline Renderer::fillRect(NXRect *rect, NXColor color)
156 {
157   fillRect(rect->x, rect->y, rect->x + (rect->w - 1), rect->y + (rect->h - 1), color.r, color.g, color.b);
158 }
159 
drawPixel(int x,int y,NXColor color)160 void inline Renderer::drawPixel(int x, int y, NXColor color)
161 {
162   drawPixel(x, y, color.r, color.g, color.b);
163 }
164 
clearScreen(NXColor color)165 void inline Renderer::clearScreen(NXColor color)
166 {
167   clearScreen(color.r, color.g, color.b);
168 }
169 
setClip(NXRect * rect)170 void inline Renderer::setClip(NXRect *rect)
171 {
172   setClip(rect->x, rect->y, rect->w, rect->h);
173 }
174 
175 
176 
177 }; // namespace Graphics
178 }; // namespace NXE
179 #endif
180