From 785293f4bf1beec65d23be0612e545e4c26ec366 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 15 Jan 2024 19:27:53 +0100 Subject: b --- editor/app.cpp | 39 ++++++++++++++++++++++----------------- editor/app.hpp | 17 +++++++++-------- editor/ctor.cpp | 3 ++- editor/imgui.cpp | 10 +++++----- editor/tests.cpp | 5 +++-- editor/tests.hpp | 4 +++- editor/tests/path-test.cpp | 3 ++- 7 files changed, 46 insertions(+), 35 deletions(-) (limited to 'editor') diff --git a/editor/app.cpp b/editor/app.cpp index 44df5c5e..766d96c8 100644 --- a/editor/app.cpp +++ b/editor/app.cpp @@ -1,6 +1,7 @@ #include "app.hpp" #include "compat/assert.hpp" #include "compat/sysexits.hpp" +#include "compat/shared-ptr-wrapper.hpp" #include "editor.hpp" #include "src/anim-atlas.hpp" #include "src/critter.hpp" @@ -28,16 +29,22 @@ floormat_main& app::main() { return *M; } const cursor_state& app::cursor_state() { return cursor; } -std::shared_ptr app::ensure_player_character(world& w) +shared_ptr_wrapper app::ensure_player_character(world& w) { if (_character_id) + { + std::shared_ptr tmp; if (auto C = w.find_object(_character_id); C && C->type() == object_type::critter) - return std::static_pointer_cast(C); + { + auto ptr = std::static_pointer_cast(C); + return {ptr}; + } + } _character_id = 0; auto id = (object_id)-1; - std::shared_ptr ret; + shared_ptr_wrapper ret; for (const auto& [coord, c] : w.chunks()) { @@ -50,7 +57,7 @@ std::shared_ptr app::ensure_player_character(world& w) if (C.playable) { id = std::min(id, C.id); - ret = std::static_pointer_cast(e_); + ret.ptr = std::static_pointer_cast(e_); } } } @@ -63,11 +70,11 @@ std::shared_ptr app::ensure_player_character(world& w) critter_proto cproto; cproto.name = "Player"_s; cproto.playable = true; - ret = w.make_object(w.make_id(), global_coords{}, cproto); - _character_id = ret->id; + ret.ptr = w.make_object(w.make_id(), global_coords{}, cproto); + _character_id = ret.ptr->id; } - fm_debug_assert(ret); - return ret; + fm_debug_assert(ret.ptr); + return shared_ptr_wrapper{ret}; } void app::reset_world(class world&& w_) @@ -176,15 +183,13 @@ int app::run_from_argv(const int argc, const char* const* const argv) opts.argv = argv; opts.argc = argc; - Pointer main; - Pointer app_ptr{new app{Utility::move(opts)}}; - auto& app = *app_ptr; - { - ret = app.exec(); - main = Utility::move(app.M); - (void)main; - } - loader_::destroy(); + struct app* A = new app{Utility::move(opts)}; + floormat_main* M = A->M; + fm_assert(M != nullptr); + ret = A->exec(); + loader.destroy(); + delete A; + delete M; return ret; } diff --git a/editor/app.hpp b/editor/app.hpp index b869f2b4..fd24e495 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -1,13 +1,13 @@ #pragma once #include "compat/defs.hpp" #include "compat/enum-bitset-fwd.hpp" +#include "compat/safe-ptr.hpp" #include "floormat/app.hpp" #include "keys.hpp" #include "src/global-coords.hpp" #include "src/object-id.hpp" #include "editor-enums.hpp" #include "tests.hpp" -#include #include #include #include @@ -37,6 +37,7 @@ class anim_atlas; struct critter; struct point; class editor; +template struct shared_ptr_wrapper; struct cursor_state final { @@ -78,7 +79,7 @@ struct app final : floormat_app floormat_main& main(); const struct cursor_state& cursor_state(); clickable* find_clickable_scenery(const Optional& pixel); - std::shared_ptr ensure_player_character(world& w); + shared_ptr_wrapper ensure_player_character(world& w); private: app(fm_settings&& opts); @@ -176,12 +177,12 @@ private: void erase_inspector(size_t index, ptrdiff_t count = 1); void kill_inspectors(); - Pointer M; - Pointer _imgui; - Pointer _wireframe; - Pointer _tests; - Pointer _editor; - Pointer keys_; + floormat_main* M; + safe_ptr _imgui; + safe_ptr _wireframe; + safe_ptr _tests; + safe_ptr _editor; + safe_ptr keys_; StaticArray key_modifiers{ValueInit}; Array inspectors; object_id _character_id = 0; diff --git a/editor/ctor.cpp b/editor/ctor.cpp index ccda3060..9c1f64fc 100644 --- a/editor/ctor.cpp +++ b/editor/ctor.cpp @@ -10,10 +10,11 @@ namespace floormat { app::app(fm_settings&& opts) : M{floormat_main::create(*this, Utility::move(opts))}, + _imgui{InPlaceInit, NoCreate}, _wireframe{InPlaceInit}, _tests{tests_data_::make()}, _editor{InPlaceInit, this}, - keys_{InPlaceInit, 0} + keys_{InPlaceInit, 0u} { reset_world(); auto& w = M->world(); diff --git a/editor/imgui.cpp b/editor/imgui.cpp index 3465f27a..02e3abc1 100644 --- a/editor/imgui.cpp +++ b/editor/imgui.cpp @@ -21,9 +21,9 @@ bool popup_target::operator==(const popup_target&) const = default; void app::init_imgui(Vector2i size) { - if (!_imgui) [[unlikely]] + if (!_imgui->context()) [[unlikely]] { - _imgui = Pointer{InPlaceInit, NoCreate}; + _imgui = safe_ptr{InPlaceInit, NoCreate}; *_imgui = ImGuiIntegration::Context{Vector2{size}, size, size}; fm_assert(_imgui->context()); } @@ -392,7 +392,7 @@ void app::do_popup_menu() void app::kill_popups(bool hard) { - const bool imgui = _imgui != nullptr; + const bool imgui = _imgui->context() != nullptr; if (imgui) fm_assert(_imgui->context()); @@ -403,13 +403,13 @@ void app::kill_popups(bool hard) if (hard) tested_light_chunk = {}; - if (_imgui) + if (_imgui->context()) ImGui::CloseCurrentPopup(); if (hard) kill_inspectors(); - if (_imgui) + if (_imgui->context()) ImGui::FocusWindow(nullptr); } diff --git a/editor/tests.cpp b/editor/tests.cpp index ee108d01..41d39cd2 100644 --- a/editor/tests.cpp +++ b/editor/tests.cpp @@ -1,4 +1,5 @@ #include "tests-private.hpp" +#include "compat/safe-ptr.hpp" #include "app.hpp" #include "floormat/events.hpp" #include "imgui-raii.hpp" @@ -31,9 +32,9 @@ void tests_data::switch_to(size_t i) } } -Pointer tests_data_::make() +safe_ptr tests_data_::make() { - return Pointer{InPlaceInit}; + return safe_ptr{new tests_data}; } void app::tests_pre_update() diff --git a/editor/tests.hpp b/editor/tests.hpp index 06762127..b4ee5289 100644 --- a/editor/tests.hpp +++ b/editor/tests.hpp @@ -3,6 +3,8 @@ namespace floormat { +template class safe_ptr; + struct tests_data; struct tests_data_ @@ -10,7 +12,7 @@ struct tests_data_ fm_DECLARE_DELETED_COPY_ASSIGNMENT(tests_data_); virtual ~tests_data_() noexcept; - static Pointer make(); + [[nodiscard]] static safe_ptr make(); protected: tests_data_(); diff --git a/editor/tests/path-test.cpp b/editor/tests/path-test.cpp index 2bc40717..c21c13c9 100644 --- a/editor/tests/path-test.cpp +++ b/editor/tests/path-test.cpp @@ -1,5 +1,6 @@ #include "../tests-private.hpp" #include "../app.hpp" +#include "compat/shared-ptr-wrapper.hpp" #include "floormat/main.hpp" #include "src/path-search.hpp" #include "src/critter.hpp" @@ -26,7 +27,7 @@ bool path_test::handle_mouse_click(app& a, const mouse_button_event& e, bool is_ case mouse_button_left: { auto& M = a.main(); auto& w = M.world(); - auto C = a.ensure_player_character(w); + auto C = a.ensure_player_character(w).ptr; if (auto pt = a.cursor_state().point()) { constexpr auto chunk_size = iTILE_SIZE2 * TILE_MAX_DIM; -- cgit v1.2.3