summaryrefslogtreecommitdiffhomepage
path: root/editor
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-01-15 19:27:53 +0100
committerStanislaw Halik <sthalik@misaki.pl>2024-01-15 19:27:53 +0100
commit785293f4bf1beec65d23be0612e545e4c26ec366 (patch)
treef250c34d82977116498b8049c8055fc3981478ab /editor
parenta5acc700d6a3a9b050864cf78a0f9f2305babdff (diff)
b
Diffstat (limited to 'editor')
-rw-r--r--editor/app.cpp39
-rw-r--r--editor/app.hpp17
-rw-r--r--editor/ctor.cpp3
-rw-r--r--editor/imgui.cpp10
-rw-r--r--editor/tests.cpp5
-rw-r--r--editor/tests.hpp4
-rw-r--r--editor/tests/path-test.cpp3
7 files changed, 46 insertions, 35 deletions
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<critter> app::ensure_player_character(world& w)
+shared_ptr_wrapper<critter> app::ensure_player_character(world& w)
{
if (_character_id)
+ {
+ std::shared_ptr<critter> tmp;
if (auto C = w.find_object(_character_id); C && C->type() == object_type::critter)
- return std::static_pointer_cast<critter>(C);
+ {
+ auto ptr = std::static_pointer_cast<critter>(C);
+ return {ptr};
+ }
+ }
_character_id = 0;
auto id = (object_id)-1;
- std::shared_ptr<critter> ret;
+ shared_ptr_wrapper<critter> ret;
for (const auto& [coord, c] : w.chunks())
{
@@ -50,7 +57,7 @@ std::shared_ptr<critter> app::ensure_player_character(world& w)
if (C.playable)
{
id = std::min(id, C.id);
- ret = std::static_pointer_cast<critter>(e_);
+ ret.ptr = std::static_pointer_cast<critter>(e_);
}
}
}
@@ -63,11 +70,11 @@ std::shared_ptr<critter> app::ensure_player_character(world& w)
critter_proto cproto;
cproto.name = "Player"_s;
cproto.playable = true;
- ret = w.make_object<critter>(w.make_id(), global_coords{}, cproto);
- _character_id = ret->id;
+ ret.ptr = w.make_object<critter>(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<critter>{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<struct floormat_main> main;
- Pointer<struct app> 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 <memory>
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/StaticArray.h>
#include <Corrade/Containers/Pointer.h>
@@ -37,6 +37,7 @@ class anim_atlas;
struct critter;
struct point;
class editor;
+template<typename T> 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<Vector2i>& pixel);
- std::shared_ptr<critter> ensure_player_character(world& w);
+ shared_ptr_wrapper<critter> 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<floormat_main> M;
- Pointer<ImGuiIntegration::Context> _imgui;
- Pointer<floormat::wireframe::meshes> _wireframe;
- Pointer<tests_data_> _tests;
- Pointer<editor> _editor;
- Pointer<key_set> keys_;
+ floormat_main* M;
+ safe_ptr<ImGuiIntegration::Context> _imgui;
+ safe_ptr<floormat::wireframe::meshes> _wireframe;
+ safe_ptr<tests_data_> _tests;
+ safe_ptr<editor> _editor;
+ safe_ptr<key_set> keys_;
StaticArray<key_COUNT, int> key_modifiers{ValueInit};
Array<popup_target> 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<ImGuiIntegration::Context>{InPlaceInit, NoCreate};
+ _imgui = safe_ptr<ImGuiIntegration::Context>{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_> tests_data_::make()
+safe_ptr<tests_data_> tests_data_::make()
{
- return Pointer<tests_data>{InPlaceInit};
+ return safe_ptr<tests_data_>{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<typename T> 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<tests_data_> make();
+ [[nodiscard]] static safe_ptr<tests_data_> 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;