diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/game.h | 29 | ||||
-rw-r--r-- | src/main.cpp | 8 | ||||
-rw-r--r-- | src/renderer.cpp | 186 | ||||
-rw-r--r-- | src/renderer.h | 8 | ||||
-rw-r--r-- | src/sprite.cpp | 6 | ||||
-rw-r--r-- | src/sprite.h | 33 |
6 files changed, 105 insertions, 165 deletions
diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..46f1ab2 --- /dev/null +++ b/src/game.h | |||
@@ -0,0 +1,29 @@ | |||
1 | #ifndef GAME_H_E6F1396E | ||
2 | #define GAME_H_E6F1396E | ||
3 | |||
4 | #include <vector> | ||
5 | #include "sprite.h" | ||
6 | |||
7 | class Game { | ||
8 | public: | ||
9 | |||
10 | int addSprite(Sprite sprite) { | ||
11 | int id = sprites_.size(); | ||
12 | sprites_.push_back(std::move(sprite)); | ||
13 | return id; | ||
14 | } | ||
15 | |||
16 | Sprite& getSprite(int id) { | ||
17 | return sprites_.at(id); | ||
18 | } | ||
19 | |||
20 | std::vector<Sprite>& getSprites() { | ||
21 | return sprites_; | ||
22 | } | ||
23 | |||
24 | private: | ||
25 | |||
26 | std::vector<Sprite> sprites_; | ||
27 | }; | ||
28 | |||
29 | #endif /* end of include guard: GAME_H_E6F1396E */ | ||
diff --git a/src/main.cpp b/src/main.cpp index 2da7d9a..e91f8f0 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -1,7 +1,13 @@ | |||
1 | #include <iostream> | 1 | #include <iostream> |
2 | #include "renderer.h" | 2 | #include "renderer.h" |
3 | #include "game.h" | ||
3 | 4 | ||
4 | void loop(Renderer& renderer) { | 5 | void loop(Renderer& renderer) { |
6 | Game game; | ||
7 | game.addSprite(Sprite("../res/lucas.png", renderer)); | ||
8 | |||
9 | renderer.render(game); | ||
10 | |||
5 | for (;;) { | 11 | for (;;) { |
6 | SDL_Event e; | 12 | SDL_Event e; |
7 | while (SDL_PollEvent(&e)) | 13 | while (SDL_PollEvent(&e)) |
@@ -11,6 +17,8 @@ void loop(Renderer& renderer) { | |||
11 | return; | 17 | return; |
12 | } | 18 | } |
13 | } | 19 | } |
20 | |||
21 | renderer.render(game); | ||
14 | } | 22 | } |
15 | } | 23 | } |
16 | 24 | ||
diff --git a/src/renderer.cpp b/src/renderer.cpp index e29d8cd..3bd0beb 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -1,8 +1,8 @@ | |||
1 | #include "renderer.h" | 1 | #include "renderer.h" |
2 | #include "consts.h" | 2 | #include "consts.h" |
3 | #include "game.h" | ||
3 | 4 | ||
4 | Renderer::Renderer() | 5 | Renderer::Renderer() { |
5 | { | ||
6 | win_ = window_ptr( | 6 | win_ = window_ptr( |
7 | SDL_CreateWindow( | 7 | SDL_CreateWindow( |
8 | "Tanetane", | 8 | "Tanetane", |
@@ -29,15 +29,14 @@ Renderer::Renderer() | |||
29 | } | 29 | } |
30 | } | 30 | } |
31 | 31 | ||
32 | /*void Renderer::render() | 32 | void Renderer::render(Game& game) { |
33 | { | ||
34 | texture_ptr canvas( | 33 | texture_ptr canvas( |
35 | SDL_CreateTexture( | 34 | SDL_CreateTexture( |
36 | ren_.get(), | 35 | ren_.get(), |
37 | SDL_PIXELFORMAT_RGBA8888, | 36 | SDL_PIXELFORMAT_RGBA8888, |
38 | SDL_TEXTUREACCESS_TARGET, | 37 | SDL_TEXTUREACCESS_TARGET, |
39 | TILE_WIDTH * game.map.getWidth(), | 38 | GAME_WIDTH, |
40 | TILE_HEIGHT * game.map.getHeight())); | 39 | GAME_HEIGHT)); |
41 | 40 | ||
42 | if (!canvas) | 41 | if (!canvas) |
43 | { | 42 | { |
@@ -46,171 +45,30 @@ Renderer::Renderer() | |||
46 | 45 | ||
47 | SDL_SetRenderTarget(ren_.get(), canvas.get()); | 46 | SDL_SetRenderTarget(ren_.get(), canvas.get()); |
48 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); | 47 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); |
49 | SDL_SetRenderDrawColor(ren_.get(), rand() % 255, rand() % 255, rand() % 255, 255); | 48 | SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 255); |
50 | SDL_RenderClear(ren_.get()); | 49 | SDL_RenderClear(ren_.get()); |
51 | 50 | ||
52 | for (int y = game.map.getTop(); y < game.map.getBottom(); y++) | 51 | for (Sprite& sprite : game.getSprites()) { |
53 | { | 52 | SDL_Rect dest { sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight() }; |
54 | for (int x = game.map.getLeft(); x < game.map.getRight(); x++) | 53 | SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), nullptr, &dest); |
55 | { | ||
56 | bool draw = true; | ||
57 | |||
58 | if ((game.player_x == x && game.player_y == y) && game.renderPlayer) | ||
59 | { | ||
60 | SDL_SetRenderDrawColor(ren_.get(), 255, 255, 0, 255); | ||
61 | } else if (!game.map.at(x,y).lit) | ||
62 | { | ||
63 | if (drawDark) | ||
64 | { | ||
65 | SDL_SetRenderDrawColor(ren_.get(), 40, 40, 40, 255); | ||
66 | } else { | ||
67 | draw = false; | ||
68 | } | ||
69 | } else { | ||
70 | int alpha = 255; | ||
71 | |||
72 | switch (game.map.at(x,y).tile) | ||
73 | { | ||
74 | case Tile::Floor: | ||
75 | { | ||
76 | SDL_SetRenderDrawColor(ren_.get(), 210, 210, 210, alpha); | ||
77 | break; | ||
78 | } | ||
79 | |||
80 | case Tile::Wall: | ||
81 | { | ||
82 | SDL_SetRenderDrawColor(ren_.get(), 100, 100, 100, alpha); | ||
83 | break; | ||
84 | } | ||
85 | |||
86 | case Tile::Dust: | ||
87 | { | ||
88 | SDL_SetRenderDrawColor(ren_.get(), 128, 40, 255, alpha); | ||
89 | break; | ||
90 | } | ||
91 | |||
92 | case Tile::Lamp: | ||
93 | { | ||
94 | SDL_SetRenderDrawColor(ren_.get(), 0, 255, 255, alpha); | ||
95 | break; | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | |||
100 | if (draw) | ||
101 | { | ||
102 | SDL_Rect rect { | ||
103 | game.map.getTrueX(x) * TILE_WIDTH, | ||
104 | game.map.getTrueY(y) * TILE_HEIGHT, | ||
105 | TILE_WIDTH, | ||
106 | TILE_HEIGHT}; | ||
107 | |||
108 | SDL_RenderFillRect(ren_.get(), &rect); | ||
109 | } | ||
110 | } | ||
111 | } | 54 | } |
112 | 55 | ||
113 | texture_ptr mask( | 56 | SDL_SetRenderTarget(ren_.get(), nullptr); |
114 | SDL_CreateTexture( | 57 | SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); |
115 | ren_.get(), | 58 | SDL_RenderPresent(ren_.get()); |
116 | SDL_PIXELFORMAT_RGBA8888, | 59 | } |
117 | SDL_TEXTUREACCESS_TARGET, | ||
118 | TILE_WIDTH * game.map.getWidth(), | ||
119 | TILE_HEIGHT * game.map.getHeight())); | ||
120 | |||
121 | if (!mask) | ||
122 | { | ||
123 | throw sdl_error(); | ||
124 | } | ||
125 | |||
126 | SDL_SetRenderTarget(ren_.get(), mask.get()); | ||
127 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); | ||
128 | SDL_RenderClear(ren_.get()); | ||
129 | 60 | ||
130 | for (int y = game.map.getTop(); y < game.map.getBottom(); y++) | 61 | int Renderer::loadImageFromFile(std::string_view filename) { |
62 | surface_ptr pfs(IMG_Load(filename.data())); | ||
63 | if (!pfs) | ||
131 | { | 64 | { |
132 | for (int x = game.map.getLeft(); x < game.map.getRight(); x++) | 65 | throw img_error(); |
133 | { | ||
134 | if (game.map.at(x,y).lightType != Source::None) | ||
135 | { | ||
136 | texture_ptr sourceMask( | ||
137 | SDL_CreateTexture( | ||
138 | ren_.get(), | ||
139 | SDL_PIXELFORMAT_RGBA8888, | ||
140 | SDL_TEXTUREACCESS_TARGET, | ||
141 | TILE_WIDTH * game.map.getWidth(), | ||
142 | TILE_HEIGHT * game.map.getHeight())); | ||
143 | |||
144 | if (!sourceMask) | ||
145 | { | ||
146 | throw sdl_error(); | ||
147 | } | ||
148 | |||
149 | SDL_SetRenderTarget(ren_.get(), sourceMask.get()); | ||
150 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); | ||
151 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); | ||
152 | SDL_RenderClear(ren_.get()); | ||
153 | |||
154 | int fadeX = game.map.getTrueX(x) - game.map.at(x,y).lightRadius; | ||
155 | int fadeY = game.map.getTrueY(y) - game.map.at(x,y).lightRadius; | ||
156 | int fadeRight = game.map.getTrueX(x) + game.map.at(x,y).lightRadius; | ||
157 | int fadeBottom = game.map.getTrueY(y) + game.map.at(x,y).lightRadius; | ||
158 | |||
159 | SDL_Rect fadeRect { | ||
160 | fadeX * TILE_WIDTH, | ||
161 | fadeY * TILE_HEIGHT, | ||
162 | (game.map.at(x,y).lightRadius * 2 + 1) * TILE_WIDTH, | ||
163 | (game.map.at(x,y).lightRadius * 2 + 1) * TILE_HEIGHT}; | ||
164 | |||
165 | if (game.map.at(x,y).lightType == Source::Lamp) | ||
166 | { | ||
167 | SDL_SetTextureBlendMode(lampFade_.get(), SDL_BLENDMODE_NONE); | ||
168 | SDL_RenderCopy(ren_.get(), lampFade_.get(), nullptr, &fadeRect); | ||
169 | } else if (game.map.at(x,y).lightType == Source::Player) { | ||
170 | SDL_SetTextureBlendMode(playerFade_.get(), SDL_BLENDMODE_NONE); | ||
171 | SDL_RenderCopy(ren_.get(), playerFade_.get(), nullptr, &fadeRect); | ||
172 | } else if (game.map.at(x,y).lightType == Source::Dust) { | ||
173 | SDL_SetTextureBlendMode(dustFade_.get(), SDL_BLENDMODE_NONE); | ||
174 | SDL_RenderCopy(ren_.get(), dustFade_.get(), nullptr, &fadeRect); | ||
175 | } | ||
176 | |||
177 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); | ||
178 | |||
179 | for (int sy = fadeY; sy < fadeBottom; sy++) | ||
180 | { | ||
181 | for (int sx = fadeX; sx < fadeRight; sx++) | ||
182 | { | ||
183 | if (!game.map.at(x,y).litTiles.count({sx, sy})) | ||
184 | { | ||
185 | SDL_Rect rect { | ||
186 | game.map.getTrueX(sx) * TILE_WIDTH, | ||
187 | game.map.getTrueY(sy) * TILE_HEIGHT, | ||
188 | TILE_WIDTH, | ||
189 | TILE_HEIGHT}; | ||
190 | |||
191 | SDL_RenderFillRect(ren_.get(), &rect); | ||
192 | } | ||
193 | } | ||
194 | } | ||
195 | |||
196 | SDL_SetRenderTarget(ren_.get(), mask.get()); | ||
197 | SDL_SetTextureBlendMode(sourceMask.get(), SDL_BLENDMODE_ADD); | ||
198 | SDL_RenderCopy(ren_.get(), sourceMask.get(), nullptr, nullptr); | ||
199 | } | ||
200 | } | ||
201 | } | 66 | } |
202 | 67 | ||
203 | SDL_SetRenderTarget(ren_.get(), canvas.get()); | 68 | texture_ptr tex = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); |
204 | SDL_SetTextureBlendMode(mask.get(), SDL_BLENDMODE_MOD); | ||
205 | SDL_RenderCopy(ren_.get(), mask.get(), nullptr, nullptr); | ||
206 | 69 | ||
207 | SDL_SetRenderTarget(ren_.get(), nullptr); | 70 | int texId = textures_.size(); |
71 | textures_.push_back(std::move(tex)); | ||
208 | 72 | ||
209 | SDL_Rect zoomRect; | 73 | return texId; |
210 | 74 | } | |
211 | std::tie(zoomRect.x, zoomRect.y, zoomRect.w, zoomRect.h) = | ||
212 | calculateZoomRect(game); | ||
213 | |||
214 | SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr); | ||
215 | SDL_RenderPresent(ren_.get()); | ||
216 | }*/ | ||
diff --git a/src/renderer.h b/src/renderer.h index 78856ed..abedb62 100644 --- a/src/renderer.h +++ b/src/renderer.h | |||
@@ -5,6 +5,8 @@ | |||
5 | #include <SDL_image.h> | 5 | #include <SDL_image.h> |
6 | #include <stdexcept> | 6 | #include <stdexcept> |
7 | #include <memory> | 7 | #include <memory> |
8 | #include <string_view> | ||
9 | #include <vector> | ||
8 | 10 | ||
9 | class Game; | 11 | class Game; |
10 | 12 | ||
@@ -113,7 +115,9 @@ public: | |||
113 | 115 | ||
114 | Renderer(); | 116 | Renderer(); |
115 | 117 | ||
116 | //void render(); | 118 | void render(Game& game); |
119 | |||
120 | int loadImageFromFile(std::string_view filename); | ||
117 | 121 | ||
118 | private: | 122 | private: |
119 | 123 | ||
@@ -121,6 +125,8 @@ private: | |||
121 | img_wrapper img_; | 125 | img_wrapper img_; |
122 | window_ptr win_; | 126 | window_ptr win_; |
123 | renderer_ptr ren_; | 127 | renderer_ptr ren_; |
128 | |||
129 | std::vector<texture_ptr> textures_; | ||
124 | }; | 130 | }; |
125 | 131 | ||
126 | #endif /* end of include guard: RENDERER_H_6A58EC30 */ | 132 | #endif /* end of include guard: RENDERER_H_6A58EC30 */ |
diff --git a/src/sprite.cpp b/src/sprite.cpp new file mode 100644 index 0000000..0059f70 --- /dev/null +++ b/src/sprite.cpp | |||
@@ -0,0 +1,6 @@ | |||
1 | #include "sprite.h" | ||
2 | #include <SDL_image.h> | ||
3 | |||
4 | Sprite::Sprite(std::string_view filename, Renderer& renderer) { | ||
5 | textureId_ = renderer.loadImageFromFile(filename); | ||
6 | } \ No newline at end of file | ||
diff --git a/src/sprite.h b/src/sprite.h new file mode 100644 index 0000000..3af9341 --- /dev/null +++ b/src/sprite.h | |||
@@ -0,0 +1,33 @@ | |||
1 | #ifndef SPRITE_H_70503825 | ||
2 | #define SPRITE_H_70503825 | ||
3 | |||
4 | #include <string_view> | ||
5 | #include "renderer.h" | ||
6 | |||
7 | class Sprite { | ||
8 | public: | ||
9 | |||
10 | Sprite(std::string_view filename, Renderer& renderer); | ||
11 | |||
12 | int getTextureId() const { | ||
13 | return textureId_; | ||
14 | } | ||
15 | |||
16 | int getX() { return x_; } | ||
17 | |||
18 | int getY() { return y_; } | ||
19 | |||
20 | int getWidth() { return width_; } | ||
21 | |||
22 | int getHeight() { return height_; } | ||
23 | |||
24 | private: | ||
25 | |||
26 | int textureId_; | ||
27 | int x_ = 0; | ||
28 | int y_ = 0; | ||
29 | int width_ = 17*4; | ||
30 | int height_ = 27*4; | ||
31 | }; | ||
32 | |||
33 | #endif /* end of include guard: SPRITE_H_70503825 */ | ||