summary refs log tree commit diff stats
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp43
1 files changed, 34 insertions, 9 deletions
diff --git a/src/main.cpp b/src/main.cpp index 4674579..957789f 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -50,6 +50,7 @@ const int TILE_WIDTH = 8*2;
50const int TILE_HEIGHT = 8*2; 50const int TILE_HEIGHT = 8*2;
51const int VIEW_WIDTH = GAME_WIDTH / TILE_WIDTH; 51const int VIEW_WIDTH = GAME_WIDTH / TILE_WIDTH;
52const int VIEW_HEIGHT = GAME_HEIGHT / TILE_HEIGHT; 52const int VIEW_HEIGHT = GAME_HEIGHT / TILE_HEIGHT;
53const int RADIUS = 8;
53 54
54struct Input { 55struct Input {
55 bool left = false; 56 bool left = false;
@@ -63,13 +64,15 @@ public:
63 64
64 Map() : 65 Map() :
65 tiles(VIEW_WIDTH*VIEW_HEIGHT, Tile::Floor), 66 tiles(VIEW_WIDTH*VIEW_HEIGHT, Tile::Floor),
66 lighting(VIEW_WIDTH*VIEW_HEIGHT, false) 67 lighting(VIEW_WIDTH*VIEW_HEIGHT, false),
68 lightStrength(VIEW_WIDTH*VIEW_HEIGHT, 0.0)
67 { 69 {
68 } 70 }
69 71
70 std::vector<Tile> tiles; 72 std::vector<Tile> tiles;
71 std::vector<bool> lighting; 73 std::vector<bool> lighting;
72 std::vector<bool> oldLighting; 74 std::vector<bool> oldLighting;
75 std::vector<double> lightStrength;
73 int lightedSpots = 0; 76 int lightedSpots = 0;
74}; 77};
75 78
@@ -134,6 +137,15 @@ void render(
134 { 137 {
135 SDL_Rect rect{x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT}; 138 SDL_Rect rect{x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT};
136 SDL_RenderFillRect(ren, &rect); 139 SDL_RenderFillRect(ren, &rect);
140
141 int alpha = (1.0 - map.lightStrength.at(x+y*VIEW_WIDTH)) * 255;
142 if (x == player_x && y == player_y)
143 {
144 alpha = 0;
145 }
146
147 SDL_SetRenderDrawColor(ren, 40, 40, 40, alpha);
148 SDL_RenderFillRect(ren, &rect);
137 } 149 }
138 } 150 }
139 } 151 }
@@ -218,6 +230,7 @@ void recalculateLighting(Map& map, fov_settings_type* fov)
218{ 230{
219 map.oldLighting = map.lighting; 231 map.oldLighting = map.lighting;
220 map.lighting = std::vector<bool>(VIEW_WIDTH*VIEW_HEIGHT, false); 232 map.lighting = std::vector<bool>(VIEW_WIDTH*VIEW_HEIGHT, false);
233 map.lightStrength = std::vector<double>(VIEW_WIDTH*VIEW_HEIGHT, 0.0);
221 map.lightedSpots = 0; 234 map.lightedSpots = 0;
222 235
223 fov_settings_set_opacity_test_function( 236 fov_settings_set_opacity_test_function(
@@ -233,15 +246,24 @@ void recalculateLighting(Map& map, fov_settings_type* fov)
233 246
234 fov_settings_set_apply_lighting_function( 247 fov_settings_set_apply_lighting_function(
235 fov, 248 fov,
236 [] (void* map, int x, int y, int, int, void*) { 249 [] (void* map, int x, int y, int dx, int dy, void*) {
237 if ((x >= 0) && (x < VIEW_WIDTH) && (y >= 0) && (y < VIEW_HEIGHT)) 250 if ((x >= 0) && (x < VIEW_WIDTH) && (y >= 0) && (y < VIEW_HEIGHT))
238 { 251 {
239 Map& m = *static_cast<Map*>(map); 252 Map& m = *static_cast<Map*>(map);
240 if (!m.lighting[x+VIEW_WIDTH*y]) 253 if (!m.lighting[x+VIEW_WIDTH*y])
241 { 254 {
242 m.lighting[x+VIEW_WIDTH*y] = true;
243 m.lightedSpots++; 255 m.lightedSpots++;
244 } 256 }
257
258 m.lighting[x+VIEW_WIDTH*y] = true;
259
260 m.lightStrength[x+VIEW_WIDTH*y] = std::max(
261 m.lightStrength[x+VIEW_WIDTH*y],
262 std::pow(
263 std::max(
264 0.0,
265 1.0 - std::sqrt(dx * dx + dy * dy) / static_cast<double>(RADIUS)),
266 1.0/3.0));
245 } 267 }
246 }); 268 });
247 269
@@ -251,13 +273,14 @@ void recalculateLighting(Map& map, fov_settings_type* fov)
251 { 273 {
252 if ((player_x == x && player_y == y) || map.tiles[x+VIEW_WIDTH*y] == Tile::Dust || map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp) 274 if ((player_x == x && player_y == y) || map.tiles[x+VIEW_WIDTH*y] == Tile::Dust || map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp)
253 { 275 {
254 fov_circle(fov, static_cast<void*>(&map), nullptr, x, y, 8); 276 fov_circle(fov, static_cast<void*>(&map), nullptr, x, y, RADIUS);
255 } 277 }
256 278
257 if (map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp || 279 if (map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp ||
258 map.tiles[x+VIEW_WIDTH*y] == Tile::Dust) 280 map.tiles[x+VIEW_WIDTH*y] == Tile::Dust)
259 { 281 {
260 map.lighting[x+VIEW_WIDTH*y] = true; 282 map.lighting[x+VIEW_WIDTH*y] = true;
283 map.lightStrength[x+VIEW_WIDTH*y] = 1.0;
261 } 284 }
262 } 285 }
263 } 286 }
@@ -327,6 +350,8 @@ int main(int, char**)
327 throw sdl_error(); 350 throw sdl_error();
328 } 351 }
329 352
353 SDL_SetRenderDrawBlendMode(ren.get(), SDL_BLENDMODE_BLEND);
354
330 Map map; 355 Map map;
331 356
332 std::unique_ptr<fov_settings_type> fov(new fov_settings_type()); 357 std::unique_ptr<fov_settings_type> fov(new fov_settings_type());
@@ -388,10 +413,10 @@ int main(int, char**)
388 413
389 tick( 414 tick(
390 map, 415 map,
391 player_x - 7, 416 player_x - (RADIUS - 1),
392 player_y - 7, 417 player_y - (RADIUS - 1),
393 player_x + 8, 418 player_x + RADIUS,
394 player_y + 8); 419 player_y + RADIUS);
395 420
396 render(ren.get(), map, false); 421 render(ren.get(), map, false);
397 SDL_Delay(30); 422 SDL_Delay(30);
@@ -438,7 +463,7 @@ int main(int, char**)
438 } 463 }
439 }); 464 });
440 465
441 fov_circle(dusty.get(), static_cast<void*>(&map), static_cast<void*>(&lamps), px, py, 8+lamped*lamped); 466 fov_circle(dusty.get(), static_cast<void*>(&map), static_cast<void*>(&lamps), px, py, RADIUS+lamped*lamped);
442 467
443 render(ren.get(), map, false); 468 render(ren.get(), map, false);
444 SDL_Delay(50); 469 SDL_Delay(50);