diff options
-rw-r--r-- | editor/app.cpp | 12 | ||||
-rw-r--r-- | editor/app.hpp | 3 | ||||
-rw-r--r-- | editor/draw.cpp | 19 | ||||
-rw-r--r-- | editor/save.cpp | 5 | ||||
-rw-r--r-- | editor/update.cpp | 8 | ||||
-rw-r--r-- | floormat/main.hpp | 2 | ||||
-rw-r--r-- | main/main-impl.cpp | 6 | ||||
-rw-r--r-- | main/main-impl.hpp | 2 | ||||
-rw-r--r-- | main/setup.cpp | 12 | ||||
-rw-r--r-- | src/world.cpp | 6 | ||||
-rw-r--r-- | src/world.hpp | 1 |
11 files changed, 44 insertions, 32 deletions
diff --git a/editor/app.cpp b/editor/app.cpp index aa8608c5..9023b5ac 100644 --- a/editor/app.cpp +++ b/editor/app.cpp @@ -33,17 +33,17 @@ app::app(fm_settings&& opts) : maybe_initialize_chunk_(coord, w[coord]); reset_camera_offset(); inspectors.reserve(16); - _character = w.make_entity<character>(global_coords{}); + _character_id = w.make_entity<character>(global_coords{})->id; } -app::~app() -{ - void(); -} +app::~app() = default; int app::exec() { - return M->exec(); + int ret = M->exec(); + if (M) + M->reset_world(); + return ret; } static const char* const true_values[] = { "1", "true", "yes", "y", "Y", "on", "ON", "enable", "enabled", }; diff --git a/editor/app.hpp b/editor/app.hpp index 9e6b1312..abfe2418 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -77,7 +77,6 @@ private: void update_character(float dt); void draw() override; - void draw_character(); void on_mouse_move(const mouse_move_event& event) noexcept override; void on_mouse_up_down(const mouse_button_event& event, bool is_down) noexcept override; @@ -149,7 +148,7 @@ private: key_set keys; std::array<int, key_set::COUNT> key_modifiers = {}; std::vector<popup_target> inspectors; - std::shared_ptr<character> _character; + std::uint64_t _character_id = 0; cursor_state cursor; popup_target _popup_target; diff --git a/editor/draw.cpp b/editor/draw.cpp index dd286edf..5cd6a124 100644 --- a/editor/draw.cpp +++ b/editor/draw.cpp @@ -142,25 +142,6 @@ void app::draw_collision_boxes() shader.set_tint({1, 1, 1, 1}); } -void app::draw_character() -{ - GL::Renderer::enable(GL::Renderer::Feature::DepthTest); - GL::Renderer::setDepthMask(false); - - auto& shader = M->shader(); - auto& mesh = M->meshes().anim; - const auto sz = M->window_size(); - auto& c = *_character; - const auto [minx, maxx, miny, maxy] = M->get_draw_bounds(); - - const with_shifted_camera_offset o{shader, c.coord.chunk(), {minx, miny}, {maxx, maxy}}; - if (floormat_main::check_chunk_visible(shader.camera_offset(), sz)) - mesh.draw(shader, *c.atlas, c.r, c.frame, c.coord.local(), Vector2b(c.offset), tile_shader::character_depth_offset); - - GL::Renderer::setDepthMask(true); - GL::Renderer::disable(GL::Renderer::Feature::DepthTest); -} - void app::draw() { //draw_character(); diff --git a/editor/save.cpp b/editor/save.cpp index 8c33b435..8e38b2da 100644 --- a/editor/save.cpp +++ b/editor/save.cpp @@ -45,14 +45,13 @@ void app::do_quickload() return; } fputs("quickload... ", stderr); fflush(stderr); - M->world() = world::deserialize(quicksave_file); + M->reset_world(world::deserialize(quicksave_file)); fputs("done\n", stderr); fflush(stderr); } void app::do_new_file() { - auto& w = M->world(); - w = world{}; + auto& w = M->reset_world(); maybe_initialize_chunk_(chunk_coords{}, w[chunk_coords{}]); } diff --git a/editor/update.cpp b/editor/update.cpp index dd985b88..07d703e0 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -201,7 +201,13 @@ void app::update_world(float dt) void app::update_character([[maybe_unused]] float dt) { - _character->set_keys(keys[key_left], keys[key_right], keys[key_up], keys[key_down]); + auto& w = M->world(); + auto cptr = w.find_entity(_character_id); + if (cptr) + { + fm_debug_assert(cptr->type == entity_type::character); + static_cast<character&>(*cptr).set_keys(keys[key_left], keys[key_right], keys[key_up], keys[key_down]); + } } void app::set_cursor() diff --git a/floormat/main.hpp b/floormat/main.hpp index 9cc8f9a6..92882bae 100644 --- a/floormat/main.hpp +++ b/floormat/main.hpp @@ -65,6 +65,8 @@ struct floormat_main virtual struct meshes meshes() noexcept = 0; virtual struct world& world() noexcept = 0; + virtual struct world& reset_world() noexcept = 0; + virtual struct world& reset_world(struct world) noexcept = 0; virtual SDL_Window* window() noexcept = 0; Vector2 dpi_scale() const noexcept { return _dpi_scale; } static int get_mods() noexcept; diff --git a/main/main-impl.cpp b/main/main-impl.cpp index ba2fa3ec..a971edcf 100644 --- a/main/main-impl.cpp +++ b/main/main-impl.cpp @@ -8,7 +8,11 @@ namespace floormat { floormat_main::floormat_main() noexcept = default; floormat_main::~floormat_main() noexcept = default; -main_impl::~main_impl() noexcept = default; + +main_impl::~main_impl() noexcept +{ + reset_world(); +} void main_impl::quit(int status) { Platform::Sdl2Application::exit(status); } struct world& main_impl::world() noexcept { return _world; } diff --git a/main/main-impl.hpp b/main/main-impl.hpp index ca3e9ad3..ddb16e81 100644 --- a/main/main-impl.hpp +++ b/main/main-impl.hpp @@ -33,6 +33,8 @@ struct main_impl final : Platform::Sdl2Application, floormat_main const tile_shader& shader() const noexcept override; struct world& world() noexcept override; + struct world& reset_world() noexcept override; + struct world& reset_world(struct world) noexcept override; SDL_Window* window() noexcept override; fm_settings& settings() noexcept override; diff --git a/main/setup.cpp b/main/setup.cpp index 8e0e36b9..1f0adcb4 100644 --- a/main/setup.cpp +++ b/main/setup.cpp @@ -98,4 +98,16 @@ auto main_impl::meshes() noexcept -> struct meshes return { _floor_mesh, _wall_mesh, _anim_mesh, }; }; +struct world& main_impl::reset_world() noexcept +{ + return reset_world(floormat::world{}); +} + +struct world& main_impl::reset_world(struct world w) noexcept +{ + _clickable_scenery.clear(); + _world = std::move(w); + return _world; +} + } // namespace floormat diff --git a/src/world.cpp b/src/world.cpp index 37b78616..f5c378cd 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -105,4 +105,10 @@ void world::do_kill_entity(std::uint64_t id) fm_debug_assert(cnt > 0); } +std::shared_ptr<entity> world::find_entity(std::uint64_t id) +{ + auto it = _entities.find(id); + return it == _entities.end() ? nullptr : it->second.lock(); +} + } // namespace floormat diff --git a/src/world.hpp b/src/world.hpp index dfd7a71a..039f3387 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -77,6 +77,7 @@ public: return ret; } + std::shared_ptr<entity> find_entity(std::uint64_t id); bool is_teardown() const { return _teardown; } fm_DECLARE_DEPRECATED_COPY_ASSIGNMENT(world); |