summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/consts.h14
-rw-r--r--src/game.cpp1
-rw-r--r--src/game.h10
-rw-r--r--src/main.cpp69
-rw-r--r--src/renderer.cpp103
-rw-r--r--src/renderer.h11
6 files changed, 151 insertions, 57 deletions
diff --git a/src/consts.h b/src/consts.h new file mode 100644 index 0000000..141838b --- /dev/null +++ b/src/consts.h
@@ -0,0 +1,14 @@
1#ifndef CONSTS_H_152EBF56
2#define CONSTS_H_152EBF56
3
4constexpr int GAME_WIDTH = 1280;//640*2;
5constexpr int GAME_HEIGHT = 720;//480*2;
6constexpr int TILE_WIDTH = 8*2;
7constexpr int TILE_HEIGHT = TILE_WIDTH;
8constexpr int INIT_ZOOM = 5;
9constexpr int ZOOM_X_FACTOR = 16;
10constexpr int ZOOM_Y_FACTOR = 9;
11constexpr int RADIUS = 8;
12constexpr int NUM_TITLES = 1;
13
14#endif /* end of include guard: CONSTS_H_152EBF56 */
diff --git a/src/game.cpp b/src/game.cpp index 8de9b7b..a7c94db 100644 --- a/src/game.cpp +++ b/src/game.cpp
@@ -4,6 +4,7 @@
4#include <iostream> 4#include <iostream>
5#include "util.h" 5#include "util.h"
6#include "renderer.h" 6#include "renderer.h"
7#include "consts.h"
7 8
8Game::Game(std::mt19937& rng, Muxer& muxer) : 9Game::Game(std::mt19937& rng, Muxer& muxer) :
9 rng(rng), 10 rng(rng),
diff --git a/src/game.h b/src/game.h index 89b9eb2..446680a 100644 --- a/src/game.h +++ b/src/game.h
@@ -10,15 +10,7 @@
10#include "timer.h" 10#include "timer.h"
11#include "animation.h" 11#include "animation.h"
12#include "interpolation.h" 12#include "interpolation.h"
13 13#include "consts.h"
14const int GAME_WIDTH = 1280;//640*2;
15const int GAME_HEIGHT = 720;//480*2;
16const int TILE_WIDTH = 8*2;
17const int TILE_HEIGHT = TILE_WIDTH;
18const int INIT_ZOOM = 5;
19const int ZOOM_X_FACTOR = 16;
20const int ZOOM_Y_FACTOR = 9;
21const int RADIUS = 8;
22 14
23constexpr int TilesetIndex(int x, int y) { 15constexpr int TilesetIndex(int x, int y) {
24 return x + y * 24; 16 return x + y * 24;
diff --git a/src/main.cpp b/src/main.cpp index ec253ee..4ae11d6 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -5,6 +5,12 @@
5#include "game.h" 5#include "game.h"
6#include "renderer.h" 6#include "renderer.h"
7 7
8enum class TitleState {
9 FadeIn,
10 Sustain,
11 FadeOut
12};
13
8int main(int, char**) 14int main(int, char**)
9{ 15{
10 std::random_device randomEngine; 16 std::random_device randomEngine;
@@ -17,6 +23,14 @@ int main(int, char**)
17 23
18 Game game(rng, muxer); 24 Game game(rng, muxer);
19 25
26 constexpr int titleFadeLen = 3000;
27 bool doneTitles = false;
28 int titleNum = 0;
29 TitleState titleState = TitleState::FadeIn;
30 Interpolation titleFade;
31 Timer titleSustain(3000);
32 titleFade.start(titleFadeLen);
33
20 size_t lastTime = SDL_GetTicks(); 34 size_t lastTime = SDL_GetTicks();
21 while (!game.quit) 35 while (!game.quit)
22 { 36 {
@@ -24,9 +38,58 @@ int main(int, char**)
24 size_t frameTime = currentTime - lastTime; 38 size_t frameTime = currentTime - lastTime;
25 lastTime = currentTime; 39 lastTime = currentTime;
26 40
27 game.update(frameTime); 41 if (doneTitles) {
28 renderer.renderGame(game, true); 42 game.update(frameTime);
29 muxer.update(); 43 renderer.renderGame(game, true);
44 muxer.update();
45 } else {
46 SDL_Event e;
47
48 while (SDL_PollEvent(&e));
49
50 switch (titleState) {
51 case TitleState::FadeIn: {
52 titleFade.tick(frameTime);
53 if (titleFade.isComplete()) {
54 titleState = TitleState::Sustain;
55 titleSustain.reset();
56 }
57
58 renderer.renderTitle(titleNum, titleFade.getProgress(0, 1));
59
60 break;
61 }
62 case TitleState::Sustain: {
63 titleSustain.accumulate(frameTime);
64 const Uint8* state = SDL_GetKeyboardState(NULL);
65 if (state[SDL_SCANCODE_SPACE] || titleSustain.step()) {
66 titleState = TitleState::FadeOut;
67 titleFade.start(titleFadeLen);
68 }
69
70 renderer.renderTitle(titleNum, 1);
71
72 break;
73 }
74 case TitleState::FadeOut: {
75 titleFade.tick(frameTime);
76 renderer.renderTitle(titleNum, titleFade.getProgress(1, 0));
77
78 if (titleFade.isComplete()) {
79 titleNum++;
80
81 if (titleNum < NUM_TITLES) {
82 titleState = TitleState::FadeIn;
83 titleFade.start(titleFadeLen);
84 } else {
85 doneTitles = true;
86 }
87 }
88
89 break;
90 }
91 }
92 }
30 } 93 }
31 } catch (const sdl_error& ex) 94 } catch (const sdl_error& ex)
32 { 95 {
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
153void Renderer::renderGame( 114void Renderer::renderGame(
@@ -354,6 +315,47 @@ void Renderer::renderGame(
354 SDL_RenderPresent(ren_.get()); 315 SDL_RenderPresent(ren_.get());
355} 316}
356 317
318void 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
357std::tuple<int, int, int, int> Renderer::calculateZoomRect(const Game& game) 359std::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
381void 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}
diff --git a/src/renderer.h b/src/renderer.h index ca4e607..4d5484c 100644 --- a/src/renderer.h +++ b/src/renderer.h
@@ -5,6 +5,9 @@
5#include <SDL_image.h> 5#include <SDL_image.h>
6#include <stdexcept> 6#include <stdexcept>
7#include <memory> 7#include <memory>
8#include <array>
9#include <string_view>
10#include "consts.h"
8 11
9class Game; 12class Game;
10 13
@@ -117,10 +120,14 @@ public:
117 const Game& game, 120 const Game& game,
118 bool drawDark = true); 121 bool drawDark = true);
119 122
123 void renderTitle(int num, double fade);
124
120 static std::tuple<int, int, int, int> calculateZoomRect(const Game& game); 125 static std::tuple<int, int, int, int> calculateZoomRect(const Game& game);
121 126
122private: 127private:
123 128
129 void loadTextureFromFile(std::string_view path, texture_ptr& texture);
130
124 sdl_wrapper sdl_; 131 sdl_wrapper sdl_;
125 img_wrapper img_; 132 img_wrapper img_;
126 window_ptr win_; 133 window_ptr win_;
@@ -132,6 +139,10 @@ private:
132 texture_ptr playerSheet_; 139 texture_ptr playerSheet_;
133 texture_ptr tileset_; 140 texture_ptr tileset_;
134 texture_ptr lamp_; 141 texture_ptr lamp_;
142
143 std::array<texture_ptr, NUM_TITLES> titles_;
144 std::array<int, NUM_TITLES> titleWidths_;
145 std::array<int, NUM_TITLES> titleHeights_;
135}; 146};
136 147
137#endif /* end of include guard: RENDERER_H_6A58EC30 */ 148#endif /* end of include guard: RENDERER_H_6A58EC30 */