summary refs log tree commit diff stats
path: root/src/renderer.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-06-05 21:02:51 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-06-05 21:02:51 -0400
commit3fa9af58bc907048a8ccadc2bef7736ec554a09d (patch)
tree00bca7856629dcd4f9d2846433c37ab18d651d13 /src/renderer.cpp
parent5e94c9fb603dc4648fe6ab4c153ba4114b6bb486 (diff)
downloadether-3fa9af58bc907048a8ccadc2bef7736ec554a09d.tar.gz
ether-3fa9af58bc907048a8ccadc2bef7736ec554a09d.tar.bz2
ether-3fa9af58bc907048a8ccadc2bef7736ec554a09d.zip
refactored so that the renderer is its own class
Diffstat (limited to 'src/renderer.cpp')
-rw-r--r--src/renderer.cpp309
1 files changed, 309 insertions, 0 deletions
diff --git a/src/renderer.cpp b/src/renderer.cpp new file mode 100644 index 0000000..eddd11d --- /dev/null +++ b/src/renderer.cpp
@@ -0,0 +1,309 @@
1#include "renderer.h"
2#include "game.h"
3
4Renderer::Renderer()
5{
6 win_ = window_ptr(
7 SDL_CreateWindow(
8 "Ether",
9 100,
10 100,
11 GAME_WIDTH,
12 GAME_HEIGHT,
13 SDL_WINDOW_SHOWN));
14
15 if (!win_)
16 {
17 throw sdl_error();
18 }
19
20 ren_ = renderer_ptr(
21 SDL_CreateRenderer(
22 win_.get(),
23 -1,
24 SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC));
25
26 if (!ren_)
27 {
28 throw sdl_error();
29 }
30
31 texture_ptr origFade;
32 {
33 surface_ptr pfs(IMG_Load("../res/lighting.png"));
34 if (!pfs)
35 {
36 throw img_error();
37 }
38
39 origFade = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get()));
40 }
41
42 SDL_SetTextureBlendMode(origFade.get(), SDL_BLENDMODE_BLEND);
43
44 playerFade_ = texture_ptr(
45 SDL_CreateTexture(
46 ren_.get(),
47 SDL_PIXELFORMAT_RGBA4444,
48 SDL_TEXTUREACCESS_TARGET,
49 144,
50 144));
51
52 if (!playerFade_)
53 {
54 throw sdl_error();
55 }
56
57 SDL_SetRenderTarget(ren_.get(), playerFade_.get());
58 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
59 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
60 SDL_RenderClear(ren_.get());
61 SDL_RenderCopy(ren_.get(), origFade.get(), nullptr, nullptr);
62
63 lampFade_ = texture_ptr(
64 SDL_CreateTexture(
65 ren_.get(),
66 SDL_PIXELFORMAT_RGBA4444,
67 SDL_TEXTUREACCESS_TARGET,
68 144,
69 144));
70
71 if (!lampFade_)
72 {
73 throw sdl_error();
74 }
75
76 SDL_SetRenderTarget(ren_.get(), lampFade_.get());
77
78 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
79 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
80 SDL_RenderClear(ren_.get());
81 SDL_RenderCopy(ren_.get(), origFade.get(), nullptr, nullptr);
82
83 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD);
84 SDL_SetRenderDrawColor(ren_.get(), 255, 204, 58, 255);
85 SDL_RenderFillRect(ren_.get(), nullptr);
86
87 dustFade_ = texture_ptr(
88 SDL_CreateTexture(
89 ren_.get(),
90 SDL_PIXELFORMAT_RGBA4444,
91 SDL_TEXTUREACCESS_TARGET,
92 144,
93 144));
94
95 if (!dustFade_)
96 {
97 throw sdl_error();
98 }
99
100 SDL_SetRenderTarget(ren_.get(), dustFade_.get());
101
102 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
103 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
104 SDL_RenderClear(ren_.get());
105 SDL_RenderCopy(ren_.get(), origFade.get(), nullptr, nullptr);
106
107 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD);
108 SDL_SetRenderDrawColor(ren_.get(), 255, 150, 255, 255);
109 SDL_RenderFillRect(ren_.get(), nullptr);
110}
111
112void Renderer::render(
113 const Game& game,
114 bool drawDark)
115{
116 texture_ptr canvas(
117 SDL_CreateTexture(
118 ren_.get(),
119 SDL_PIXELFORMAT_RGBA8888,
120 SDL_TEXTUREACCESS_TARGET,
121 TILE_WIDTH * game.map.getWidth(),
122 TILE_HEIGHT * game.map.getHeight()));
123
124 if (!canvas)
125 {
126 throw sdl_error();
127 }
128
129 SDL_SetRenderTarget(ren_.get(), canvas.get());
130 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
131 SDL_SetRenderDrawColor(ren_.get(), rand() % 255, rand() % 255, rand() % 255, 255);
132 SDL_RenderClear(ren_.get());
133
134 for (int y = game.map.getTop(); y < game.map.getBottom(); y++)
135 {
136 for (int x = game.map.getLeft(); x < game.map.getRight(); x++)
137 {
138 bool draw = true;
139
140 if ((game.player_x == x && game.player_y == y) && game.renderPlayer)
141 {
142 SDL_SetRenderDrawColor(ren_.get(), 255, 255, 0, 255);
143 } else if (!game.map.at(x,y).lit)
144 {
145 if (drawDark)
146 {
147 SDL_SetRenderDrawColor(ren_.get(), 40, 40, 40, 255);
148 } else {
149 draw = false;
150 }
151 } else {
152 int alpha = 255;
153
154 switch (game.map.at(x,y).tile)
155 {
156 case Tile::Floor:
157 {
158 SDL_SetRenderDrawColor(ren_.get(), 210, 210, 210, alpha);
159 break;
160 }
161
162 case Tile::Wall:
163 {
164 SDL_SetRenderDrawColor(ren_.get(), 100, 100, 100, alpha);
165 break;
166 }
167
168 case Tile::Dust:
169 {
170 SDL_SetRenderDrawColor(ren_.get(), 128, 40, 255, alpha);
171 break;
172 }
173
174 case Tile::Lamp:
175 {
176 SDL_SetRenderDrawColor(ren_.get(), 0, 255, 255, alpha);
177 break;
178 }
179 }
180 }
181
182 if (draw)
183 {
184 SDL_Rect rect {
185 game.map.getTrueX(x) * TILE_WIDTH,
186 game.map.getTrueY(y) * TILE_HEIGHT,
187 TILE_WIDTH,
188 TILE_HEIGHT};
189
190 SDL_RenderFillRect(ren_.get(), &rect);
191 }
192 }
193 }
194
195 texture_ptr mask(
196 SDL_CreateTexture(
197 ren_.get(),
198 SDL_PIXELFORMAT_RGBA8888,
199 SDL_TEXTUREACCESS_TARGET,
200 TILE_WIDTH * game.map.getWidth(),
201 TILE_HEIGHT * game.map.getHeight()));
202
203 if (!mask)
204 {
205 throw sdl_error();
206 }
207
208 SDL_SetRenderTarget(ren_.get(), mask.get());
209 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
210 SDL_RenderClear(ren_.get());
211
212 for (int y = game.map.getTop(); y < game.map.getBottom(); y++)
213 {
214 for (int x = game.map.getLeft(); x < game.map.getRight(); x++)
215 {
216 if (game.map.at(x,y).lightType != Source::None)
217 {
218 texture_ptr sourceMask(
219 SDL_CreateTexture(
220 ren_.get(),
221 SDL_PIXELFORMAT_RGBA8888,
222 SDL_TEXTUREACCESS_TARGET,
223 TILE_WIDTH * game.map.getWidth(),
224 TILE_HEIGHT * game.map.getHeight()));
225
226 if (!sourceMask)
227 {
228 throw sdl_error();
229 }
230
231 SDL_SetRenderTarget(ren_.get(), sourceMask.get());
232 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
233 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
234 SDL_RenderClear(ren_.get());
235
236 int fadeX = game.map.getTrueX(x) - game.map.at(x,y).lightRadius;
237 int fadeY = game.map.getTrueY(y) - game.map.at(x,y).lightRadius;
238 int fadeRight = game.map.getTrueX(x) + game.map.at(x,y).lightRadius;
239 int fadeBottom = game.map.getTrueY(y) + game.map.at(x,y).lightRadius;
240
241 SDL_Rect fadeRect {
242 fadeX * TILE_WIDTH,
243 fadeY * TILE_HEIGHT,
244 (game.map.at(x,y).lightRadius * 2 + 1) * TILE_WIDTH,
245 (game.map.at(x,y).lightRadius * 2 + 1) * TILE_HEIGHT};
246
247 if (game.map.at(x,y).lightType == Source::Lamp)
248 {
249 SDL_SetTextureBlendMode(lampFade_.get(), SDL_BLENDMODE_NONE);
250 SDL_RenderCopy(ren_.get(), lampFade_.get(), nullptr, &fadeRect);
251 } else if (game.map.at(x,y).lightType == Source::Player) {
252 SDL_SetTextureBlendMode(playerFade_.get(), SDL_BLENDMODE_NONE);
253 SDL_RenderCopy(ren_.get(), playerFade_.get(), nullptr, &fadeRect);
254 } else if (game.map.at(x,y).lightType == Source::Dust) {
255 SDL_SetTextureBlendMode(dustFade_.get(), SDL_BLENDMODE_NONE);
256 SDL_RenderCopy(ren_.get(), dustFade_.get(), nullptr, &fadeRect);
257 }
258
259 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
260
261 for (int sy = fadeY; sy < fadeBottom; sy++)
262 {
263 for (int sx = fadeX; sx < fadeRight; sx++)
264 {
265 if (!game.map.at(x,y).litTiles.count({sx, sy}))
266 {
267 SDL_Rect rect {
268 game.map.getTrueX(sx) * TILE_WIDTH,
269 game.map.getTrueY(sy) * TILE_HEIGHT,
270 TILE_WIDTH,
271 TILE_HEIGHT};
272
273 SDL_RenderFillRect(ren_.get(), &rect);
274 }
275 }
276 }
277
278 SDL_SetRenderTarget(ren_.get(), mask.get());
279 SDL_SetTextureBlendMode(sourceMask.get(), SDL_BLENDMODE_ADD);
280 SDL_RenderCopy(ren_.get(), sourceMask.get(), nullptr, nullptr);
281 }
282 }
283 }
284
285 SDL_SetRenderTarget(ren_.get(), canvas.get());
286 SDL_SetTextureBlendMode(mask.get(), SDL_BLENDMODE_MOD);
287 SDL_RenderCopy(ren_.get(), mask.get(), nullptr, nullptr);
288
289 SDL_SetRenderTarget(ren_.get(), nullptr);
290
291 if (!game.zooming)
292 {
293 SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr);
294 } else {
295 // TODO: zooming back in to the player
296 SDL_Rect zoomRect {
297 ((game.maxZoom - game.curZoom) * TILE_WIDTH + game.zoomProgress)
298 * ZOOM_X_FACTOR / 2,
299 ((game.maxZoom - game.curZoom) * TILE_HEIGHT + game.zoomProgress)
300 * ZOOM_Y_FACTOR / 2,
301 (game.curZoom * TILE_WIDTH - game.zoomProgress) * ZOOM_X_FACTOR,
302 (game.curZoom * TILE_HEIGHT - game.zoomProgress) * ZOOM_Y_FACTOR
303 };
304
305 SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr);
306 }
307
308 SDL_RenderPresent(ren_.get());
309}