From e4cfb32534b2adebbacc1e4a36557874b31a4c53 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 27 May 2018 16:43:09 -0400 Subject: lighting fades away from source now --- src/main.cpp | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) (limited to 'src/main.cpp') 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; const int TILE_HEIGHT = 8*2; const int VIEW_WIDTH = GAME_WIDTH / TILE_WIDTH; const int VIEW_HEIGHT = GAME_HEIGHT / TILE_HEIGHT; +const int RADIUS = 8; struct Input { bool left = false; @@ -63,13 +64,15 @@ public: Map() : tiles(VIEW_WIDTH*VIEW_HEIGHT, Tile::Floor), - lighting(VIEW_WIDTH*VIEW_HEIGHT, false) + lighting(VIEW_WIDTH*VIEW_HEIGHT, false), + lightStrength(VIEW_WIDTH*VIEW_HEIGHT, 0.0) { } std::vector tiles; std::vector lighting; std::vector oldLighting; + std::vector lightStrength; int lightedSpots = 0; }; @@ -134,6 +137,15 @@ void render( { SDL_Rect rect{x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT}; SDL_RenderFillRect(ren, &rect); + + int alpha = (1.0 - map.lightStrength.at(x+y*VIEW_WIDTH)) * 255; + if (x == player_x && y == player_y) + { + alpha = 0; + } + + SDL_SetRenderDrawColor(ren, 40, 40, 40, alpha); + SDL_RenderFillRect(ren, &rect); } } } @@ -218,6 +230,7 @@ void recalculateLighting(Map& map, fov_settings_type* fov) { map.oldLighting = map.lighting; map.lighting = std::vector(VIEW_WIDTH*VIEW_HEIGHT, false); + map.lightStrength = std::vector(VIEW_WIDTH*VIEW_HEIGHT, 0.0); map.lightedSpots = 0; fov_settings_set_opacity_test_function( @@ -233,15 +246,24 @@ void recalculateLighting(Map& map, fov_settings_type* fov) fov_settings_set_apply_lighting_function( fov, - [] (void* map, int x, int y, int, int, void*) { + [] (void* map, int x, int y, int dx, int dy, void*) { if ((x >= 0) && (x < VIEW_WIDTH) && (y >= 0) && (y < VIEW_HEIGHT)) { Map& m = *static_cast(map); if (!m.lighting[x+VIEW_WIDTH*y]) { - m.lighting[x+VIEW_WIDTH*y] = true; m.lightedSpots++; } + + m.lighting[x+VIEW_WIDTH*y] = true; + + m.lightStrength[x+VIEW_WIDTH*y] = std::max( + m.lightStrength[x+VIEW_WIDTH*y], + std::pow( + std::max( + 0.0, + 1.0 - std::sqrt(dx * dx + dy * dy) / static_cast(RADIUS)), + 1.0/3.0)); } }); @@ -251,13 +273,14 @@ void recalculateLighting(Map& map, fov_settings_type* fov) { if ((player_x == x && player_y == y) || map.tiles[x+VIEW_WIDTH*y] == Tile::Dust || map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp) { - fov_circle(fov, static_cast(&map), nullptr, x, y, 8); + fov_circle(fov, static_cast(&map), nullptr, x, y, RADIUS); } if (map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp || map.tiles[x+VIEW_WIDTH*y] == Tile::Dust) { map.lighting[x+VIEW_WIDTH*y] = true; + map.lightStrength[x+VIEW_WIDTH*y] = 1.0; } } } @@ -327,6 +350,8 @@ int main(int, char**) throw sdl_error(); } + SDL_SetRenderDrawBlendMode(ren.get(), SDL_BLENDMODE_BLEND); + Map map; std::unique_ptr fov(new fov_settings_type()); @@ -388,10 +413,10 @@ int main(int, char**) tick( map, - player_x - 7, - player_y - 7, - player_x + 8, - player_y + 8); + player_x - (RADIUS - 1), + player_y - (RADIUS - 1), + player_x + RADIUS, + player_y + RADIUS); render(ren.get(), map, false); SDL_Delay(30); @@ -438,7 +463,7 @@ int main(int, char**) } }); - fov_circle(dusty.get(), static_cast(&map), static_cast(&lamps), px, py, 8+lamped*lamped); + fov_circle(dusty.get(), static_cast(&map), static_cast(&lamps), px, py, RADIUS+lamped*lamped); render(ren.get(), map, false); SDL_Delay(50); -- cgit 1.4.1