#include #include #include #include #include #include #include #include #include "util.h" class sdl_error : public std::logic_error { public: sdl_error() : std::logic_error(SDL_GetError()) { } }; class window_deleter { public: void operator()(SDL_Window* ptr) { SDL_DestroyWindow(ptr); } }; using window_ptr = std::unique_ptr; class renderer_deleter { public: void operator()(SDL_Renderer* ptr) { SDL_DestroyRenderer(ptr); } }; using renderer_ptr = std::unique_ptr; enum class Tile { Floor, Wall, Dust, Lamp }; 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; const int RADIUS = 8; struct Input { bool left = false; bool right = false; bool up = false; bool down = false; }; struct Kickup { int x; int y; size_t cur; size_t radius; size_t chain; }; class Map { public: Map() : tiles(VIEW_WIDTH*VIEW_HEIGHT, Tile::Floor), 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; std::list kickups; int lightedSpots = 0; bool dirtyLighting = true; }; int player_x = VIEW_WIDTH / 2; int player_y = VIEW_HEIGHT / 2; void render( SDL_Renderer* ren, const Map& map, bool drawDark = true) { SDL_SetRenderDrawColor(ren, rand() % 255, rand() % 255, rand() % 255, 255); SDL_RenderClear(ren); for (int y = 0; y < VIEW_HEIGHT; y++) { for (int x = 0; x < VIEW_WIDTH; x++) { bool draw = true; if (player_x == x && player_y == y) { SDL_SetRenderDrawColor(ren, 255, 255, 0, 255); } else if (!map.lighting.at(x+VIEW_WIDTH*y)) { if (drawDark) { SDL_SetRenderDrawColor(ren, 40, 40, 40, 255); } else { draw = false; } } else { switch (map.tiles.at(x+y*VIEW_WIDTH)) { case Tile::Floor: { SDL_SetRenderDrawColor(ren, 210, 210, 210, 255); break; } case Tile::Wall: { SDL_SetRenderDrawColor(ren, 100, 100, 100, 255); break; } case Tile::Dust: { SDL_SetRenderDrawColor(ren, 128, 40, 255, 255); break; } case Tile::Lamp: { SDL_SetRenderDrawColor(ren, 0, 255, 255, 255); break; } } } if (draw) { 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; SDL_SetRenderDrawColor(ren, 40, 40, 40, alpha); SDL_RenderFillRect(ren, &rect); } } } SDL_RenderPresent(ren); } void incrementIfSet(Map& map, int& count, int x, int y, int w, int h, Tile val = Tile::Wall) { if ((x >= 0) && (x < w) && (y >= 0) && (y < h) && (map.tiles[x+w*y] == val)) { count++; } } void tick(Map& map, int x1 = 0, int y1 = 0, int x2 = VIEW_WIDTH, int y2 = VIEW_HEIGHT, bool onlyDark = false) { std::vector temp(map.tiles); for (int y = std::max(y1, 0); y < std::min(y2, VIEW_HEIGHT); y++) { for (int x = std::max(x1, 0); x < std::min(x2, VIEW_WIDTH); x++) { if (onlyDark && map.lighting[x+y*VIEW_WIDTH]) { continue; } if (map.tiles[x+y*VIEW_WIDTH] == Tile::Lamp) { continue; } int count = 0; incrementIfSet(map, count, x-1, y-1, VIEW_WIDTH, VIEW_HEIGHT); incrementIfSet(map, count, x-1, y , VIEW_WIDTH, VIEW_HEIGHT); incrementIfSet(map, count, x-1, y+1, VIEW_WIDTH, VIEW_HEIGHT); incrementIfSet(map, count, x , y-1, VIEW_WIDTH, VIEW_HEIGHT); incrementIfSet(map, count, x , y , VIEW_WIDTH, VIEW_HEIGHT); incrementIfSet(map, count, x , y+1, VIEW_WIDTH, VIEW_HEIGHT); incrementIfSet(map, count, x+1, y-1, VIEW_WIDTH, VIEW_HEIGHT); incrementIfSet(map, count, x+1, y , VIEW_WIDTH, VIEW_HEIGHT); incrementIfSet(map, count, x+1, y+1, VIEW_WIDTH, VIEW_HEIGHT); if (count >= 5) { temp[x+VIEW_WIDTH*y] = Tile::Wall; } else { temp[x+VIEW_WIDTH*y] = Tile::Floor; } } } map.tiles = temp; } void movePlayer(int x, int y, Map& map) { if ((x >= 0) && (x < VIEW_WIDTH) && (y >= 0) && (y < VIEW_HEIGHT) && map.tiles[x+VIEW_WIDTH*y] == Tile::Floor) { if (map.tiles[player_x+player_y*VIEW_WIDTH] == Tile::Floor) { map.tiles[player_x+player_y*VIEW_WIDTH] = Tile::Dust; } player_x = x; player_y = y; map.dirtyLighting = true; } } void setIfValid(Map& map, int x, int y, Tile val) { if ((x >= 0) && (x < VIEW_WIDTH) && (y >= 0) && (y < VIEW_HEIGHT)) { map.tiles[x+VIEW_WIDTH*y] = val; } } 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( fov, [] (void* map, int x, int y) { return x >= 0 && x < VIEW_WIDTH && y >= 0 && y < VIEW_HEIGHT && static_cast(map)->tiles.at(x+VIEW_WIDTH*y) == Tile::Wall; }); fov_settings_set_apply_lighting_function( fov, [] (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.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)); } }); for (int y = 0; y < VIEW_HEIGHT; y++) { for (int x = 0; x < VIEW_WIDTH; x++) { 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, RADIUS); map.lighting[x+VIEW_WIDTH*y] = true; map.lightStrength[x+VIEW_WIDTH*y] = 1.0; } } } map.dirtyLighting = false; } void processKeys(Map& map, const Input& keystate) { int px = player_x; int py = player_y; if (keystate.up) { py--; } if (keystate.down) { py++; } if (keystate.left) { px--; } if (keystate.right) { px++; } if (!(player_x == px && player_y == py)) { movePlayer(px, py, map); } } void kickUpDust(Map& map, int x, int y, size_t chain) { std::cout << "kickup " << x << " " << y << " " << chain << std::endl; Kickup dk; dk.x = x; dk.y = y; dk.chain = chain; dk.cur = 0; dk.radius = RADIUS + (chain + 1) * (chain + 1); map.kickups.push_back(dk); } void processKickup(Map& map) { for (Kickup& kickup : map.kickups) { kickup.cur++; if (map.tiles[kickup.x+VIEW_WIDTH*kickup.y] == Tile::Floor) { map.tiles[kickup.x+VIEW_WIDTH*kickup.y] = Tile::Dust; } std::unique_ptr dusty(new fov_settings_type); fov_settings_set_opacity_test_function( dusty.get(), [] (void* map, int x, int y) { return x >= 0 && x < VIEW_WIDTH && y >= 0 && y < VIEW_HEIGHT && static_cast(map)->tiles.at(x+VIEW_WIDTH*y) == Tile::Wall; }); fov_settings_set_apply_lighting_function( dusty.get(), [] (void* data, int x, int y, int, int, void* source) { Map& map = *static_cast(data); Kickup& kickup = *static_cast(source); if ((x >= 0) && (x < VIEW_WIDTH) && (y >= 0) && (y < VIEW_HEIGHT)) { if (map.tiles[x+VIEW_WIDTH*y] == Tile::Floor) { map.tiles[x+VIEW_WIDTH*y] = Tile::Dust; map.dirtyLighting = true; } else if (map.tiles[x+VIEW_WIDTH*y] == Tile::Lamp) { map.tiles[x+VIEW_WIDTH*y] = Tile::Dust; map.dirtyLighting = true; kickUpDust(map, x, y, kickup.chain + 1); } } }); fov_circle( dusty.get(), static_cast(&map), static_cast(&kickup), kickup.x, kickup.y, kickup.cur); } erase_if( map.kickups, [] (const Kickup& kickup) { return kickup.cur == kickup.radius; }); } int main(int, char**) { std::random_device randomEngine; std::mt19937 rng(randomEngine()); if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw sdl_error(); } try { window_ptr win( SDL_CreateWindow("Ether", 100, 100, GAME_WIDTH, GAME_HEIGHT, SDL_WINDOW_SHOWN)); if (!win) { throw sdl_error(); } renderer_ptr ren( SDL_CreateRenderer( win.get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)); if (!ren) { throw sdl_error(); } SDL_SetRenderDrawBlendMode(ren.get(), SDL_BLENDMODE_BLEND); 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++) { if (std::bernoulli_distribution(0.5)(rng)) { map.tiles[x+y*VIEW_WIDTH] = Tile::Wall; } } } tick(map); tick(map); tick(map); bool quit = false; Input keystate; SDL_Event e; size_t dustDt = 30; size_t dustAcc = 0; size_t inputDt = 50; size_t inputAcc = 0; size_t lastTime = SDL_GetTicks(); while (!quit) { size_t currentTime = SDL_GetTicks(); size_t frameTime = currentTime - lastTime; lastTime = currentTime; //bool input = false; //int presses = 0; bool pressedSpace = false; while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) { quit = true; } else if (e.type == SDL_KEYDOWN) { //presses++; switch (e.key.keysym.sym) { case SDLK_ESCAPE: { quit = true; break; } case SDLK_SPACE: { pressedSpace = true; setIfValid(map, player_x , player_y , Tile::Lamp); for (int i = 0; i < 5; i++) { processKeys(map, keystate); tick( map, player_x - (RADIUS - 1), player_y - (RADIUS - 1), player_x + RADIUS, player_y + RADIUS); //render(ren.get(), map, false); //SDL_Delay(30); } map.dirtyLighting = true; kickUpDust(map, player_x, player_y, 0); break; } } } } const Uint8* state = SDL_GetKeyboardState(NULL); keystate.left = state[SDL_SCANCODE_LEFT]; keystate.right = state[SDL_SCANCODE_RIGHT]; keystate.up = state[SDL_SCANCODE_UP]; keystate.down = state[SDL_SCANCODE_DOWN]; bool input = keystate.left || keystate.right || keystate.up || keystate.down || pressedSpace; dustAcc += frameTime; inputAcc += frameTime; while (dustAcc >= dustDt) { 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; map.dirtyLighting = true; } } } processKickup(map); dustAcc -= dustDt; } while (inputAcc >= inputDt) { processKeys(map, keystate); inputAcc -= inputDt; } if (map.dirtyLighting) { recalculateLighting(map, fov.get()); for (int y = 0; y < VIEW_HEIGHT; y++) { for (int x = 0; x < VIEW_WIDTH; x++) { if (!map.lighting[x+y*VIEW_WIDTH] && map.oldLighting[x+y*VIEW_WIDTH]) { if (std::bernoulli_distribution(0.5)(rng)) { map.tiles[x+y*VIEW_WIDTH] = Tile::Wall; } else { map.tiles[x+y*VIEW_WIDTH] = Tile::Floor; } } } } tick(map, 0, 0, VIEW_WIDTH, VIEW_HEIGHT, true); tick(map, 0, 0, VIEW_WIDTH, VIEW_HEIGHT, true); tick(map, 0, 0, VIEW_WIDTH, VIEW_HEIGHT, true); } render(ren.get(), map, true); //SDL_Delay(50); } } catch (const sdl_error& ex) { std::cout << "SDL error (" << ex.what() << ")" << std::endl; } SDL_Quit(); return 0; }