summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-01-30 04:46:44 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-01-30 04:46:44 -0500
commitd92c7970df0d2e0a0c3f84037640336102c084a6 (patch)
tree162f27ff353ca1ae7a5df78a651c93e2e5865aac /src
parent2ff08dfdbb94d059a2c92da56c0266d38854df8a (diff)
downloadtanetane-d92c7970df0d2e0a0c3f84037640336102c084a6.tar.gz
tanetane-d92c7970df0d2e0a0c3f84037640336102c084a6.tar.bz2
tanetane-d92c7970df0d2e0a0c3f84037640336102c084a6.zip
Imported renderer stuff from Ether
Diffstat (limited to 'src')
-rw-r--r--src/consts.h7
-rw-r--r--src/main.cpp25
-rw-r--r--src/renderer.cpp216
-rw-r--r--src/renderer.h126
4 files changed, 373 insertions, 1 deletions
diff --git a/src/consts.h b/src/consts.h new file mode 100644 index 0000000..2088f45 --- /dev/null +++ b/src/consts.h
@@ -0,0 +1,7 @@
1#ifndef CONSTS_H_9561E49C
2#define CONSTS_H_9561E49C
3
4const int GAME_WIDTH = 640;
5const int GAME_HEIGHT = 480;
6
7#endif /* end of include guard: CONSTS_H_9561E49C */
diff --git a/src/main.cpp b/src/main.cpp index 22bdf92..2da7d9a 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -1,6 +1,29 @@
1#include <iostream> 1#include <iostream>
2#include "renderer.h"
3
4void loop(Renderer& renderer) {
5 for (;;) {
6 SDL_Event e;
7 while (SDL_PollEvent(&e))
8 {
9 if (e.type == SDL_QUIT)
10 {
11 return;
12 }
13 }
14 }
15}
2 16
3int main(int, char**) { 17int main(int, char**) {
4 std::cout << "hi" << std::endl; 18 try
19 {
20 Renderer renderer;
21
22 loop(renderer);
23 } catch (const sdl_error& ex)
24 {
25 std::cout << "SDL error (" << ex.what() << ")" << std::endl;
26 }
27
5 return 0; 28 return 0;
6} 29}
diff --git a/src/renderer.cpp b/src/renderer.cpp new file mode 100644 index 0000000..e29d8cd --- /dev/null +++ b/src/renderer.cpp
@@ -0,0 +1,216 @@
1#include "renderer.h"
2#include "consts.h"
3
4Renderer::Renderer()
5{
6 win_ = window_ptr(
7 SDL_CreateWindow(
8 "Tanetane",
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
32/*void Renderer::render()
33{
34 texture_ptr canvas(
35 SDL_CreateTexture(
36 ren_.get(),
37 SDL_PIXELFORMAT_RGBA8888,
38 SDL_TEXTUREACCESS_TARGET,
39 TILE_WIDTH * game.map.getWidth(),
40 TILE_HEIGHT * game.map.getHeight()));
41
42 if (!canvas)
43 {
44 throw sdl_error();
45 }
46
47 SDL_SetRenderTarget(ren_.get(), canvas.get());
48 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
49 SDL_SetRenderDrawColor(ren_.get(), rand() % 255, rand() % 255, rand() % 255, 255);
50 SDL_RenderClear(ren_.get());
51
52 for (int y = game.map.getTop(); y < game.map.getBottom(); y++)
53 {
54 for (int x = game.map.getLeft(); x < game.map.getRight(); x++)
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 }
112
113 texture_ptr mask(
114 SDL_CreateTexture(
115 ren_.get(),
116 SDL_PIXELFORMAT_RGBA8888,
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
130 for (int y = game.map.getTop(); y < game.map.getBottom(); y++)
131 {
132 for (int x = game.map.getLeft(); x < game.map.getRight(); x++)
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 }
202
203 SDL_SetRenderTarget(ren_.get(), canvas.get());
204 SDL_SetTextureBlendMode(mask.get(), SDL_BLENDMODE_MOD);
205 SDL_RenderCopy(ren_.get(), mask.get(), nullptr, nullptr);
206
207 SDL_SetRenderTarget(ren_.get(), nullptr);
208
209 SDL_Rect zoomRect;
210
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 new file mode 100644 index 0000000..78856ed --- /dev/null +++ b/src/renderer.h
@@ -0,0 +1,126 @@
1#ifndef RENDERER_H_6A58EC30
2#define RENDERER_H_6A58EC30
3
4#include <SDL.h>
5#include <SDL_image.h>
6#include <stdexcept>
7#include <memory>
8
9class Game;
10
11class sdl_error : public std::logic_error {
12public:
13
14 sdl_error() : std::logic_error(SDL_GetError())
15 {
16 }
17};
18
19class img_error : public std::logic_error {
20public:
21
22 img_error() : std::logic_error(IMG_GetError())
23 {
24 }
25};
26
27class sdl_wrapper {
28public:
29
30 sdl_wrapper()
31 {
32 if (SDL_Init(SDL_INIT_VIDEO) != 0)
33 {
34 sdl_error ex;
35 SDL_Quit();
36
37 throw ex;
38 }
39 }
40
41 ~sdl_wrapper()
42 {
43 SDL_Quit();
44 }
45};
46
47class img_wrapper {
48public:
49
50 img_wrapper()
51 {
52 if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG)
53 {
54 img_error ex;
55 IMG_Quit();
56
57 throw ex;
58 }
59 }
60
61 ~img_wrapper()
62 {
63 IMG_Quit();
64 }
65};
66
67class window_deleter {
68public:
69
70 void operator()(SDL_Window* ptr)
71 {
72 SDL_DestroyWindow(ptr);
73 }
74};
75
76using window_ptr = std::unique_ptr<SDL_Window, window_deleter>;
77
78class renderer_deleter {
79public:
80
81 void operator()(SDL_Renderer* ptr)
82 {
83 SDL_DestroyRenderer(ptr);
84 }
85};
86
87using renderer_ptr = std::unique_ptr<SDL_Renderer, renderer_deleter>;
88
89class surface_deleter {
90public:
91
92 void operator()(SDL_Surface* ptr)
93 {
94 SDL_FreeSurface(ptr);
95 }
96};
97
98using surface_ptr = std::unique_ptr<SDL_Surface, surface_deleter>;
99
100class texture_deleter {
101public:
102
103 void operator()(SDL_Texture* ptr)
104 {
105 SDL_DestroyTexture(ptr);
106 }
107};
108
109using texture_ptr = std::unique_ptr<SDL_Texture, texture_deleter>;
110
111class Renderer {
112public:
113
114 Renderer();
115
116 //void render();
117
118private:
119
120 sdl_wrapper sdl_;
121 img_wrapper img_;
122 window_ptr win_;
123 renderer_ptr ren_;
124};
125
126#endif /* end of include guard: RENDERER_H_6A58EC30 */