diff options
-rw-r--r-- | editor/app.cpp | 49 | ||||
-rw-r--r-- | editor/app.hpp | 7 | ||||
-rw-r--r-- | editor/camera.cpp | 1 | ||||
-rw-r--r-- | editor/update.cpp | 9 | ||||
-rw-r--r-- | main/debug.cpp | 2 | ||||
-rw-r--r-- | main/floormat-app.hpp | 2 | ||||
-rw-r--r-- | main/floormat-main-impl.cpp | 6 | ||||
-rw-r--r-- | main/floormat.hpp | 2 | ||||
-rw-r--r-- | src/chunk.cpp | 4 | ||||
-rw-r--r-- | src/chunk.hpp | 31 | ||||
-rw-r--r-- | src/world.cpp | 35 | ||||
-rw-r--r-- | src/world.hpp | 23 |
12 files changed, 89 insertions, 82 deletions
diff --git a/editor/app.cpp b/editor/app.cpp index da3678d7..1906e3f5 100644 --- a/editor/app.cpp +++ b/editor/app.cpp @@ -3,17 +3,21 @@ #include "main/floormat-main.hpp" #include "main/floormat.hpp" #include "src/loader.hpp" +#include "world.hpp" #include <Corrade/Utility/Arguments.h> namespace floormat { -app::app() : - M{floormat_main::create(*this, {})}, +app::app(fm_settings&& opts) : + M{floormat_main::create(*this, std::move(opts))}, _floor1{loader.tile_atlas("floor-tiles", {44, 4})}, _floor2{loader.tile_atlas("metal1", {2, 2})}, _wall1{loader.tile_atlas("wood2", {1, 1})}, _wall2{loader.tile_atlas("wood1", {1, 1})} { + world& w = M->world(); + chunk_coords coord{0 ,0}; + maybe_initialize_chunk_(coord, w[coord]); } app::~app() // NOLINT(modernize-use-equals-default) @@ -61,33 +65,32 @@ static bool parse_bool(StringView name, StringView str, bool def) int app::run_from_argv(const int argc, const char* const* const argv) { fm_settings opts; + Corrade::Utility::Arguments args{}; + args.addOption("vsync", "m") + .addOption("gpu-validation", "m") + .addOption("msaa", "1") + .parse(argc, argv); + opts.vsync = parse_tristate("--vsync", args.value<StringView>("vsync"), opts.vsync); + opts.msaa = parse_bool("--msaa", args.value<StringView>("msaa"), opts.msaa); { - Corrade::Utility::Arguments args{}; - args.addOption("vsync", "m") - .addOption("gpu-validation", "m") - .addOption("msaa", "1") - .parse(argc, argv); - opts.vsync = parse_tristate("--vsync", args.value<StringView>("vsync"), opts.vsync); - opts.msaa = parse_bool("--msaa", args.value<StringView>("msaa"), opts.msaa); - { - auto str = args.value<StringView>("gpu-validation"); - if (str == "no-error" || str == "NO-ERROR") - opts.gpu_debug = fm_gpu_debug::no_error; - else if (str == "robust" || str == "robust") - opts.gpu_debug = fm_gpu_debug::robust; - else switch (parse_tristate("--gpu-validation", args.value<StringView>("gpu-validation"), fm_tristate::maybe)) - { - default: - case fm_tristate::on: opts.gpu_debug = fm_gpu_debug::on; break; - case fm_tristate::off: opts.gpu_debug = fm_gpu_debug::off; break; - } - } + auto str = args.value<StringView>("gpu-validation"); + if (str == "no-error" || str == "NO-ERROR") + opts.gpu_debug = fm_gpu_debug::no_error; + else if (str == "robust" || str == "robust") + opts.gpu_debug = fm_gpu_debug::robust; + else switch (parse_tristate("--gpu-validation", args.value<StringView>("gpu-validation"), fm_tristate::maybe)) + { + default: + case fm_tristate::on: opts.gpu_debug = fm_gpu_debug::on; break; + case fm_tristate::off: opts.gpu_debug = fm_gpu_debug::off; break; + } } + opts.vsync = parse_tristate("--vsync", args.value<StringView>("vsync"), opts.vsync); int ret; Pointer<floormat_main> ptr; { - app application; + app application{std::move(opts)}; ret = application.exec(); ptr = std::move(application.M); } diff --git a/editor/app.hpp b/editor/app.hpp index b2fcf480..663b4923 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -20,6 +20,7 @@ struct chunk; struct floormat_main; struct tile_atlas; struct tile_editor; +struct fm_settings; struct cursor_state final { std::optional<Vector2i> pixel; @@ -29,14 +30,14 @@ struct cursor_state final { struct app final : floormat_app { - app(); + app(fm_settings&& opts); ~app() override; fm_DECLARE_DELETED_COPY_ASSIGNMENT(app); fm_DECLARE_DEPRECATED_MOVE_ASSIGNMENT(app); void update(float dt) override; - void maybe_init_chunk(const chunk_coords& pos, chunk& c) override; + void maybe_initialize_chunk(const chunk_coords& pos, chunk& c) override; void draw_msaa() override; void draw() override; @@ -67,6 +68,8 @@ private: MAX = quit, COUNT }; + void maybe_initialize_chunk_(const chunk_coords& pos, chunk& c); + void do_mouse_click(global_coords pos, int button); void do_mouse_release(int button); void do_mouse_move(global_coords pos); diff --git a/editor/camera.cpp b/editor/camera.cpp index 7e9d4870..529f55e9 100644 --- a/editor/camera.cpp +++ b/editor/camera.cpp @@ -37,7 +37,6 @@ void app::do_camera(float dt) camera_offset[0] = std::clamp(camera_offset[0], -max_camera_offset[0], max_camera_offset[0]); camera_offset[1] = std::clamp(camera_offset[1], -max_camera_offset[1], max_camera_offset[1]); - Debug{} << "camera" << camera_offset; shader.set_camera_offset(camera_offset); } else diff --git a/editor/update.cpp b/editor/update.cpp index bed45c94..734ed7b5 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -7,9 +7,9 @@ namespace floormat { //#define FM_NO_BINDINGS -void app::maybe_init_chunk(const chunk_coords& pos, chunk& c) +void app::maybe_initialize_chunk_(const chunk_coords& pos, chunk& c) { - (void)pos; + (void)pos; (void)c; constexpr auto N = TILE_MAX_DIM; for (auto [x, k, pt] : c) { @@ -30,6 +30,11 @@ void app::maybe_init_chunk(const chunk_coords& pos, chunk& c) c[{K+1, K }].wall_west = { _wall2, 0 }; } +void app::maybe_initialize_chunk(const chunk_coords& pos, chunk& c) +{ + //maybe_initialize_chunk(pos, c); +} + void app::do_mouse_click(const global_coords pos, int button) { if (button == mouse_button_left) diff --git a/main/debug.cpp b/main/debug.cpp index 04c76343..f46c63d5 100644 --- a/main/debug.cpp +++ b/main/debug.cpp @@ -61,7 +61,7 @@ void main_impl::register_debug_callback() { GL::DebugOutput::setCallback(_debug_callback, this); -#if 0 +#if 1 /* Disable rather spammy "Buffer detailed info" debug messages on NVidia drivers */ GL::DebugOutput::setEnabled(GL::DebugOutput::Source::Api, GL::DebugOutput::Type::Other, {131185}, false); #endif diff --git a/main/floormat-app.hpp b/main/floormat-app.hpp index 3f382e32..60ecc52c 100644 --- a/main/floormat-app.hpp +++ b/main/floormat-app.hpp @@ -26,7 +26,7 @@ struct floormat_app [[deprecated]] floormat_app& operator=(floormat_app&&) = default; virtual void update(float dt) = 0; - virtual void maybe_init_chunk(const chunk_coords& pos, chunk& c) = 0; + virtual void maybe_initialize_chunk(const chunk_coords& pos, chunk& c) = 0; virtual void draw_msaa(); virtual void draw() = 0; diff --git a/main/floormat-main-impl.cpp b/main/floormat-main-impl.cpp index 0dfd3e01..dc9bc0b0 100644 --- a/main/floormat-main-impl.cpp +++ b/main/floormat-main-impl.cpp @@ -143,10 +143,10 @@ void main_impl::draw_world() noexcept for (std::int16_t x = minx; x <= maxx; x++) { if (const chunk_coords c = {x, y}; !_world.contains(c)) - app.maybe_init_chunk(c, *_world[c]); + app.maybe_initialize_chunk(c, _world[c]); const chunk_coords c{x, y}; const with_shifted_camera_offset o{_shader, c}; - _floor_mesh.draw(_shader, *_world[c]); + _floor_mesh.draw(_shader, _world[c]); } for (std::int16_t y = miny; y <= maxy; y++) @@ -154,7 +154,7 @@ void main_impl::draw_world() noexcept { const chunk_coords c{x, y}; const with_shifted_camera_offset o{_shader, c}; - _wall_mesh.draw(_shader, *_world[c]); + _wall_mesh.draw(_shader, _world[c]); } } diff --git a/main/floormat.hpp b/main/floormat.hpp index dffb551f..3bf4b492 100644 --- a/main/floormat.hpp +++ b/main/floormat.hpp @@ -20,7 +20,7 @@ struct fm_settings Magnum::Math::Vector2<int> resolution{1024, 768}; Corrade::Containers::String title{"Test"}; Corrade::Containers::String disabled_extensions; // TODO - std::uint8_t msaa_samples = 4; + std::uint8_t msaa_samples = 16; fm_tristate vsync = fm_tristate::maybe; fm_gpu_debug gpu_debug = fm_gpu_debug::on; fm_log_level log_level = fm_log_level::normal; diff --git a/src/chunk.cpp b/src/chunk.cpp index d8c2d011..92817765 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -2,9 +2,9 @@ namespace floormat { -bool chunk::empty() const +bool chunk::empty(bool force) const noexcept { - if (!_maybe_empty) + if (!force && !_maybe_empty) return false; for (const tile& x : _tiles) diff --git a/src/chunk.hpp b/src/chunk.hpp index f0ed1426..3bf66920 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -10,30 +10,29 @@ template<typename T> class basic_tile_iterator; struct chunk final { - tile& operator[](local_coords xy) { return _tiles[xy.to_index()]; } - const tile& operator[](local_coords xy) const { return _tiles[xy.to_index()]; } - tile& operator[](std::size_t i) { return _tiles[i]; } - const tile& operator[](std::size_t i) const { return _tiles[i]; } - const auto& tiles() const { return _tiles; } - auto& tiles() { return _tiles; } + tile& operator[](local_coords xy) noexcept { return _tiles[xy.to_index()]; } + const tile& operator[](local_coords xy) const noexcept { return _tiles[xy.to_index()]; } + tile& operator[](std::size_t i) noexcept { return _tiles[i]; } + const tile& operator[](std::size_t i) const noexcept { return _tiles[i]; } + const auto& tiles() const noexcept { return _tiles; } + auto& tiles() noexcept { return _tiles; } using iterator = basic_tile_iterator<tile>; using const_iterator = basic_tile_iterator<const tile>; - iterator begin() { return iterator{_tiles.data(), 0}; } - iterator end() { return iterator{_tiles.data(), _tiles.size()}; } - const_iterator cbegin() const { return const_iterator{_tiles.data(), 0}; } - const_iterator cend() const { return const_iterator{_tiles.data(), _tiles.size()}; } - const_iterator begin() const { return cbegin(); } - const_iterator end() const { return cend(); } + iterator begin() noexcept { return iterator{_tiles.data(), 0}; } + iterator end() noexcept { return iterator{_tiles.data(), _tiles.size()}; } + const_iterator cbegin() const noexcept { return const_iterator{_tiles.data(), 0}; } + const_iterator cend() const noexcept { return const_iterator{_tiles.data(), _tiles.size()}; } + const_iterator begin() const noexcept { return cbegin(); } + const_iterator end() const noexcept { return cend(); } - bool empty() const; + bool empty(bool force = false) const noexcept; - chunk() = default; - chunk(chunk&&) = default; - chunk& operator=(chunk&&) = default; + chunk() noexcept = default; fm_DECLARE_DELETED_COPY_ASSIGNMENT(chunk); + fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(chunk); private: std::array<tile, TILE_COUNT> _tiles = {}; diff --git a/src/world.cpp b/src/world.cpp index 4e47f912..dcb1b4a3 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -3,37 +3,32 @@ namespace floormat { -struct chunk_pointer_maker final -{ - inline operator std::shared_ptr<chunk>() const { return std::make_shared<chunk>(); } -}; - world::world() { _chunks.max_load_factor(max_load_factor); } -std::shared_ptr<chunk> world::operator[](chunk_coords c) noexcept +chunk& world::operator[](chunk_coords coord) noexcept { maybe_collect(); - if (_last_chunk) + if (auto& [c, coord2] = _last_chunk; c && coord == coord2) { - auto& [ret, pos] = *_last_chunk; - if (pos == c) - return ret; + return *c; } - auto [it, inserted] = _chunks.try_emplace(c, chunk_pointer_maker{}); - auto ret = it->second; - _last_chunk = { ret, c }; + auto [it, inserted] = _chunks.try_emplace(coord); + auto& ret = it->second; + auto& [_c, _coord] = _last_chunk; + _c = &ret; + _coord = coord; return ret; } -std::tuple<std::shared_ptr<chunk>, tile&> world::operator[](global_coords pt) noexcept +std::tuple<chunk&, tile&> world::operator[](global_coords pt) noexcept { - auto c = operator[](pt.chunk()); - return { c, (*c)[pt.local()] }; + auto& c = operator[](pt.chunk()); + return { c, c[pt.local()] }; } bool world::contains(chunk_coords c) const noexcept @@ -44,9 +39,10 @@ bool world::contains(chunk_coords c) const noexcept void world::clear() { _last_collection = 0; - _last_chunk = std::nullopt; _chunks.clear(); _chunks.rehash(initial_capacity); + auto& [c, _] = _last_chunk; + c = nullptr; } void world::maybe_collect() @@ -60,14 +56,15 @@ void world::collect() for (auto it = _chunks.begin(); it != _chunks.end(); (void)0) { const auto& [_, c] = *it; - if (c->empty()) + if (c.empty()) it = _chunks.erase(it); else ++it; } _last_collection = _chunks.size(); - _last_chunk = std::nullopt; + auto& [c, _] = _last_chunk; + c = nullptr; } } // namespace floormat diff --git a/src/world.hpp b/src/world.hpp index 32c7206a..1b05a824 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -2,13 +2,13 @@ #include "compat/int-hash.hpp" #include "global-coords.hpp" #include "tile.hpp" +#include "chunk.hpp" #include <unordered_map> #include <memory> #include <optional> namespace floormat { -// todo remove shared_ptr struct chunk; struct world final @@ -22,18 +22,18 @@ private: return int_hash((std::size_t)c.y << 16 | (std::size_t)c.x); }; - std::unordered_map<chunk_coords, std::shared_ptr<chunk>, decltype(hasher)> _chunks{initial_capacity, hasher}; - mutable std::optional<std::tuple<std::shared_ptr<chunk>, chunk_coords>> _last_chunk; + std::unordered_map<chunk_coords, chunk, decltype(hasher)> _chunks{initial_capacity, hasher}; + mutable std::tuple<chunk*, chunk_coords>_last_chunk; std::size_t _last_collection = 0; public: explicit world(); template<typename Hash, typename Alloc, typename Pred> - explicit world(std::unordered_map<chunk_coords, std::shared_ptr<chunk>, Hash, Alloc, Pred>&& chunks); + explicit world(std::unordered_map<chunk_coords, chunk, Hash, Alloc, Pred>&& chunks); - std::shared_ptr<chunk> operator[](chunk_coords c) noexcept; - std::tuple<std::shared_ptr<chunk>, tile&> operator[](global_coords pt) noexcept; + chunk& operator[](chunk_coords c) noexcept; + std::tuple<chunk&, tile&> operator[](global_coords pt) noexcept; bool contains(chunk_coords c) const noexcept; void clear(); void collect(); @@ -45,10 +45,11 @@ public: }; template<typename Hash, typename Alloc, typename Pred> -world::world(std::unordered_map<chunk_coords, std::shared_ptr<chunk>, Hash, Alloc, Pred>&& chunks) : - _chunks{chunks.begin(), chunks.end(), - std::max(initial_capacity, std::size_t(1/max_load_factor * 2 * chunks.size())), - hasher} -{} +world::world(std::unordered_map<chunk_coords, chunk, Hash, Alloc, Pred>&& chunks) : + _chunks{std::max(initial_capacity, std::size_t(1/max_load_factor * 2 * chunks.size())), hasher} +{ + for (auto&& [coord, c] : chunks) + operator[](coord) = std::move(c); +} } // namespace floormat |