From 7277cfec9c286b1ec667888b1ea1f485f7a74304 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 23 May 2018 22:40:31 -0400 Subject: dropping a lamp includes a dash dropping a lamp now sprinkles dust in the same way that popping a lamp does. only dust sprinkled from dropping/popping a lamp will pop lamps -- dust from movement will not. pressing esc quits the game. SDL errors now output a message. --- src/main.cpp | 275 ++++++++++++++++++++--------------------------------------- 1 file changed, 93 insertions(+), 182 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 2fc610b..1b7d198 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include class sdl_error : public std::logic_error { public: @@ -44,10 +45,10 @@ enum class Tile { Lamp }; -const int GAME_WIDTH = 640; -const int GAME_HEIGHT = 480; -const int TILE_WIDTH = 8; -const int TILE_HEIGHT = 8; +const int GAME_WIDTH = 640*2; +const int GAME_HEIGHT = 480*2; +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; @@ -62,7 +63,7 @@ public: std::vector tiles; std::vector lighting; - std::deque> playerLocs; + int lightedSpots = 0; }; int player_x = VIEW_WIDTH / 2; @@ -123,14 +124,11 @@ void render( } } - if (draw) { SDL_Rect rect{x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT}; SDL_RenderFillRect(ren, &rect); } - - } } @@ -190,17 +188,8 @@ void movePlayer(int x, int y, Map& map) if (map.tiles[player_x+player_y*VIEW_WIDTH] == Tile::Floor) { map.tiles[player_x+player_y*VIEW_WIDTH] = Tile::Dust; - map.playerLocs.emplace_front(player_x, player_y); - - if (map.playerLocs.size() > 5) - { - map.playerLocs.pop_back(); - } } - - - player_x = x; player_y = y; } @@ -217,6 +206,7 @@ void setIfValid(Map& map, int x, int y, Tile val) void recalculateLighting(Map& map, fov_settings_type* fov) { map.lighting = std::vector(VIEW_WIDTH*VIEW_HEIGHT, false); + map.lightedSpots = 0; fov_settings_set_opacity_test_function( fov, @@ -234,7 +224,12 @@ void recalculateLighting(Map& map, fov_settings_type* fov) [] (void* map, int x, int y, int, int, void*) { if ((x >= 0) && (x < VIEW_WIDTH) && (y >= 0) && (y < VIEW_HEIGHT)) { - static_cast(map)->lighting[x+VIEW_WIDTH*y] = true; + Map& m = *static_cast(map); + if (!m.lighting[x+VIEW_WIDTH*y]) + { + m.lighting[x+VIEW_WIDTH*y] = true; + m.lightedSpots++; + } } }); @@ -255,6 +250,31 @@ void recalculateLighting(Map& map, fov_settings_type* fov) } } +void processKeys(Map& map) +{ + const Uint8* state = SDL_GetKeyboardState(NULL); + + if (state[SDL_SCANCODE_UP]) + { + movePlayer(player_x, player_y-1, map); + } + + if (state[SDL_SCANCODE_DOWN]) + { + movePlayer(player_x, player_y+1, map); + } + + if (state[SDL_SCANCODE_LEFT]) + { + movePlayer(player_x-1, player_y, map); + } + + if (state[SDL_SCANCODE_RIGHT]) + { + movePlayer(player_x+1, player_y, map); + } +} + int main(int, char**) { std::random_device randomEngine; @@ -286,14 +306,11 @@ int main(int, char**) throw sdl_error(); } - //std::vector tiles(VIEW_WIDTH*VIEW_HEIGHT, Tile::Floor); - //std::vector lighting(VIEW_WIDTH*VIEW_HEIGHT, false); Map map; std::unique_ptr fov(new fov_settings_type()); fov_settings_init(fov.get()); - for (int y = 0; y < VIEW_HEIGHT; y++) { for (int x = 0; x < VIEW_WIDTH; x++) @@ -313,7 +330,6 @@ int main(int, char**) SDL_Event e; while (!quit) { - //SDL_PumpEvents(); bool input = false; int presses = 0; while (SDL_PollEvent(&e)) @@ -327,21 +343,24 @@ int main(int, char**) switch (e.key.keysym.sym) { + case SDLK_ESCAPE: + { + quit = true; + break; + } + case SDLK_SPACE: { input = true; - setIfValid(map, player_x-1, player_y , Tile::Floor); - setIfValid(map, player_x+1, player_y , Tile::Floor); + std::deque> lamps; + lamps.emplace_back(player_x, player_y); + setIfValid(map, player_x , player_y , Tile::Lamp); - setIfValid(map, player_x , player_y-1, Tile::Floor); - setIfValid(map, player_x , player_y+1, Tile::Floor); - auto locs = map.playerLocs; - while (!locs.empty()) + for (int i = 0; i < 5; i++) { - movePlayer(std::get<0>(locs.front()), std::get<1>(locs.front()), map); - locs.pop_front(); + processKeys(map); tick( map, @@ -354,152 +373,11 @@ int main(int, char**) SDL_Delay(30); } - break; - } - } - } else if (e.type == SDL_KEYUP) - { - presses++; - } - } - - if (presses > 0) - { - for (int y = 0; y < VIEW_HEIGHT; y++) - { - for (int x = 0; x < VIEW_WIDTH; x++) - { - if (map.tiles[x+y*VIEW_WIDTH] == Tile::Dust) - { - map.tiles[x+y*VIEW_WIDTH] = Tile::Floor; - } - } - } - } - - const Uint8* state = SDL_GetKeyboardState(NULL); - - for (int i = 0; i < presses; i++) - { - //switch (e.key.keysym.sym) - { - //case SDLK_UP: - if (state[SDL_SCANCODE_UP]) - { - movePlayer(player_x, player_y-1, map); - input = true; - //break; - } - - //case SDLK_DOWN: - if (state[SDL_SCANCODE_DOWN]) - { - movePlayer(player_x, player_y+1, map); - input = true; - //break; - } - - //case SDLK_LEFT: - if (state[SDL_SCANCODE_LEFT]) - { - movePlayer(player_x-1, player_y, map); - input = true; - //break; - } - - //case SDLK_RIGHT: - if (state[SDL_SCANCODE_RIGHT]) - { - movePlayer(player_x+1, player_y, map); - input = true; - //break; - } - - - } - - if (input) - { - //render(ren.get(), tiles, false); - //SDL_Delay(1); - } - - //} - } - - bool checkForDust = true; - - while (checkForDust) - { - checkForDust = false; - - for (int y = 0; y < VIEW_HEIGHT; y++) - { - for (int x = 0; x < VIEW_WIDTH; x++) - { - if (map.tiles[x+y*VIEW_WIDTH] == Tile::Lamp) - { - int count = 0; - - incrementIfSet(map, count, x-1, y , VIEW_WIDTH, VIEW_HEIGHT, Tile::Dust); - incrementIfSet(map, count, x+1, y , VIEW_WIDTH, VIEW_HEIGHT, Tile::Dust); - incrementIfSet(map, count, x , y-1, VIEW_WIDTH, VIEW_HEIGHT, Tile::Dust); - incrementIfSet(map, count, x , y+1, VIEW_WIDTH, VIEW_HEIGHT, Tile::Dust); - - if (count > 0) + while (!lamps.empty()) { - checkForDust = true; - - map.tiles[x+y*VIEW_WIDTH] = Tile::Dust; - - /*for (int i = 0; i < 4; i++) - { - tick( - map, - x - 7, - y - 7, - x + 8, - y + 8); - - for (int l = 0; l < (i*2+1); l++) - { - int px = x - i + l; - int py = y - i + l; - - auto fillInDust = [&] (int sx, int sy) { - if (sx > 0 && sx < VIEW_WIDTH && - sy > 0 && sy < VIEW_HEIGHT && - map.tiles[sx+sy*VIEW_WIDTH] == Tile::Floor && - !(player_y == sy && player_x == sx)) - { - map.tiles[sx+sy*VIEW_WIDTH] = Tile::Dust; - } - }; - - fillInDust(px , y - i); - fillInDust(px , y + i); - fillInDust(x - i, py ); - fillInDust(x + i, py ); - } - - render(ren.get(), map, false); - SDL_Delay(30); - }*/ - - - /* - - for (int py = std::max(0, y - 7); py < std::min(VIEW_HEIGHT, y + 8); py++) - { - for (int px = std::max(0, x - 7); px < std::min(VIEW_WIDTH, x + 8); px++) - { - if ((map.tiles[px+py*VIEW_WIDTH] == Tile::Floor) && - !(player_y == py && player_x == px)) - { - map.tiles[px+py*VIEW_WIDTH] = Tile::Dust; - } - } - }*/ + int px, py; + std::tie(px, py) = lamps.front(); + lamps.pop_front(); std::unique_ptr dusty(new fov_settings_type); fov_settings_set_opacity_test_function( @@ -515,32 +393,65 @@ int main(int, char**) fov_settings_set_apply_lighting_function( dusty.get(), - [] (void* map, int x, int y, int, int, void*) { + [] (void* map, int x, int y, int, int, void* source) { + Map& m = *static_cast(map); + auto& lamps = *static_cast>*>(source); + if ((x >= 0) && (x < VIEW_WIDTH) && - (y >= 0) && (y < VIEW_HEIGHT) && - (static_cast(map)->tiles[x+VIEW_WIDTH*y] == Tile::Floor)) + (y >= 0) && (y < VIEW_HEIGHT)) { - static_cast(map)->tiles[x+VIEW_WIDTH*y] = Tile::Dust; + if (m.tiles[x+VIEW_WIDTH*y] == Tile::Floor) + { + m.tiles[x+VIEW_WIDTH*y] = Tile::Dust; + } else if (m.tiles[x+VIEW_WIDTH*y] == Tile::Lamp) + { + m.tiles[x+VIEW_WIDTH*y] = Tile::Dust; + lamps.emplace_back(x, y); + } } }); - fov_circle(dusty.get(), static_cast(&map), nullptr, x, y, 8); - + fov_circle(dusty.get(), static_cast(&map), static_cast(&lamps), px, py, 8); + render(ren.get(), map, false); SDL_Delay(50); } + + break; } } + } else if (e.type == SDL_KEYUP) + { + presses++; } } + if (presses > 0) + { + for (int y = 0; y < VIEW_HEIGHT; y++) + { + for (int x = 0; x < VIEW_WIDTH; x++) + { + if (map.tiles[x+y*VIEW_WIDTH] == Tile::Dust) + { + map.tiles[x+y*VIEW_WIDTH] = Tile::Floor; + } + } + } + } + + for (int i = 0; i < presses; i++) + { + processKeys(map); + } recalculateLighting(map, fov.get()); render(ren.get(), map, true); SDL_Delay(10); } - } catch (const sdl_error&) + } catch (const sdl_error& ex) { + std::cout << "SDL error (" << ex.what() << ")" << std::endl; } SDL_Quit(); -- cgit 1.4.1