diff options
Diffstat (limited to 'src/renderer.cpp')
-rw-r--r-- | src/renderer.cpp | 103 |
1 files changed, 58 insertions, 45 deletions
diff --git a/src/renderer.cpp b/src/renderer.cpp index 90ce03f..9ef8bb9 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -31,17 +31,7 @@ Renderer::Renderer() | |||
31 | } | 31 | } |
32 | 32 | ||
33 | texture_ptr origFade; | 33 | texture_ptr origFade; |
34 | { | 34 | loadTextureFromFile("../res/lighting.png", origFade); |
35 | surface_ptr pfs(IMG_Load("../res/lighting.png")); | ||
36 | if (!pfs) | ||
37 | { | ||
38 | throw img_error(); | ||
39 | } | ||
40 | |||
41 | origFade = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); | ||
42 | } | ||
43 | |||
44 | SDL_SetTextureBlendMode(origFade.get(), SDL_BLENDMODE_BLEND); | ||
45 | 35 | ||
46 | playerFade_ = texture_ptr( | 36 | playerFade_ = texture_ptr( |
47 | SDL_CreateTexture( | 37 | SDL_CreateTexture( |
@@ -113,41 +103,12 @@ Renderer::Renderer() | |||
113 | SDL_SetRenderDrawColor(ren_.get(), 100, 100, 100, 255); | 103 | SDL_SetRenderDrawColor(ren_.get(), 100, 100, 100, 255); |
114 | SDL_RenderFillRect(ren_.get(), nullptr); | 104 | SDL_RenderFillRect(ren_.get(), nullptr); |
115 | 105 | ||
116 | { | 106 | loadTextureFromFile("../res/player.png", playerSheet_); |
117 | surface_ptr pfs(IMG_Load("../res/player.png")); | 107 | loadTextureFromFile("../res/runninbloods.png", tileset_); |
118 | if (!pfs) | 108 | loadTextureFromFile("../res/lamp.png", lamp_); |
119 | { | ||
120 | throw img_error(); | ||
121 | } | ||
122 | |||
123 | playerSheet_ = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); | ||
124 | } | ||
125 | 109 | ||
126 | SDL_SetTextureBlendMode(playerSheet_.get(), SDL_BLENDMODE_BLEND); | 110 | loadTextureFromFile("../res/title0.png", titles_[0]); |
127 | 111 | SDL_QueryTexture(titles_[0].get(), nullptr, nullptr, &titleWidths_[0], &titleHeights_[0]); | |
128 | { | ||
129 | surface_ptr pfs(IMG_Load("../res/runninbloods.png")); | ||
130 | if (!pfs) | ||
131 | { | ||
132 | throw img_error(); | ||
133 | } | ||
134 | |||
135 | tileset_ = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); | ||
136 | } | ||
137 | |||
138 | SDL_SetTextureBlendMode(tileset_.get(), SDL_BLENDMODE_BLEND); | ||
139 | |||
140 | { | ||
141 | surface_ptr pfs(IMG_Load("../res/lamp.png")); | ||
142 | if (!pfs) | ||
143 | { | ||
144 | throw img_error(); | ||
145 | } | ||
146 | |||
147 | lamp_ = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); | ||
148 | } | ||
149 | |||
150 | SDL_SetTextureBlendMode(lamp_.get(), SDL_BLENDMODE_BLEND); | ||
151 | } | 112 | } |
152 | 113 | ||
153 | void Renderer::renderGame( | 114 | void Renderer::renderGame( |
@@ -354,6 +315,47 @@ void Renderer::renderGame( | |||
354 | SDL_RenderPresent(ren_.get()); | 315 | SDL_RenderPresent(ren_.get()); |
355 | } | 316 | } |
356 | 317 | ||
318 | void Renderer::renderTitle(int num, double fade) { | ||
319 | texture_ptr canvas( | ||
320 | SDL_CreateTexture( | ||
321 | ren_.get(), | ||
322 | SDL_PIXELFORMAT_RGBA8888, | ||
323 | SDL_TEXTUREACCESS_TARGET, | ||
324 | GAME_WIDTH, | ||
325 | GAME_HEIGHT)); | ||
326 | |||
327 | if (!canvas) | ||
328 | { | ||
329 | throw sdl_error(); | ||
330 | } | ||
331 | |||
332 | SDL_SetRenderTarget(ren_.get(), canvas.get()); | ||
333 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); | ||
334 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); | ||
335 | SDL_RenderClear(ren_.get()); | ||
336 | |||
337 | if (fade > 0) { | ||
338 | SDL_Rect rect { | ||
339 | (GAME_WIDTH - titleWidths_[num]) / 2, | ||
340 | (GAME_HEIGHT - titleHeights_[num]) / 2, | ||
341 | titleWidths_[num], | ||
342 | titleHeights_[num] | ||
343 | }; | ||
344 | |||
345 | SDL_RenderCopy(ren_.get(), titles_[num].get(), nullptr, &rect); | ||
346 | |||
347 | if (fade < 1) { | ||
348 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); | ||
349 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, (1.0 - fade) * 255); | ||
350 | SDL_RenderFillRect(ren_.get(), nullptr); | ||
351 | } | ||
352 | } | ||
353 | |||
354 | SDL_SetRenderTarget(ren_.get(), nullptr); | ||
355 | SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); | ||
356 | SDL_RenderPresent(ren_.get()); | ||
357 | } | ||
358 | |||
357 | std::tuple<int, int, int, int> Renderer::calculateZoomRect(const Game& game) | 359 | std::tuple<int, int, int, int> Renderer::calculateZoomRect(const Game& game) |
358 | { | 360 | { |
359 | int x = game.map.getTrueX(game.curBoundX) * TILE_WIDTH; | 361 | int x = game.map.getTrueX(game.curBoundX) * TILE_WIDTH; |
@@ -375,3 +377,14 @@ std::tuple<int, int, int, int> Renderer::calculateZoomRect(const Game& game) | |||
375 | 377 | ||
376 | return {x, y, w, h}; | 378 | return {x, y, w, h}; |
377 | } | 379 | } |
380 | |||
381 | void Renderer::loadTextureFromFile(std::string_view path, texture_ptr& texture) { | ||
382 | surface_ptr pfs(IMG_Load(path.data())); | ||
383 | if (!pfs) | ||
384 | { | ||
385 | throw img_error(); | ||
386 | } | ||
387 | |||
388 | texture = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); | ||
389 | SDL_SetTextureBlendMode(texture.get(), SDL_BLENDMODE_BLEND); | ||
390 | } | ||