Skip to content

Commit

Permalink
b
Browse files Browse the repository at this point in the history
  • Loading branch information
sthalik committed Jan 15, 2024
1 parent a5acc70 commit 785293f
Show file tree
Hide file tree
Showing 24 changed files with 201 additions and 91 deletions.
4 changes: 2 additions & 2 deletions bench/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct bench_app final : private FM_APPLICATION
int argc;
char** argv;
};
bench_app::~bench_app() { loader_::destroy(); }
bench_app::~bench_app() { loader.destroy(); }

int argc_ = 0; // NOLINT

Expand Down Expand Up @@ -52,6 +52,6 @@ int main(int argc, char** argv)
{ auto app = bench_app{argc, argv};
status = app.exec();
}
loader_::destroy();
loader.destroy();
return status;
}
61 changes: 61 additions & 0 deletions compat/safe-ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once
#include "compat/assert.hpp"
#include "compat/defs.hpp"
#include <type_traits>
#include <Corrade/Tags.h>
#include <Corrade/Utility/Move.h>

namespace floormat {

template<typename T>
class safe_ptr final
{
T* ptr;

public:
template<typename... Ts>
requires requires (Ts&&... xs) {
new T{Utility::forward<Ts>(xs)...};
}
safe_ptr(InPlaceInitT, Ts&&... args) noexcept:
ptr{new T{Utility::forward<Ts>(args)...}}
{}

explicit safe_ptr(T*&& ptr) noexcept: ptr{ptr}
{
fm_assert(ptr != nullptr);
}

~safe_ptr() noexcept
{
if (ptr)
delete ptr;
ptr = (T*)-0xbadbabe;
}

explicit safe_ptr(safe_ptr&& other) noexcept: ptr{other.ptr}
{
other.ptr = nullptr;
}

safe_ptr& operator=(safe_ptr&& other) noexcept
{
fm_assert(this != &other);
if (ptr)
delete ptr;
ptr = other.ptr;
other.ptr = nullptr;
return *this;
}

fm_DECLARE_DELETED_COPY_ASSIGNMENT(safe_ptr);

//explicit operator bool() const noexcept { return ptr != nullptr; }

const T& operator*() const noexcept { return *ptr; }
T& operator*() noexcept { return *ptr; }
const T* operator->() const noexcept { return ptr; }
T* operator->() noexcept { return ptr; }
};

} // namespace floormat
12 changes: 12 additions & 0 deletions compat/shared-ptr-wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <memory>

namespace floormat {

template<typename T>
struct shared_ptr_wrapper final
{
std::shared_ptr<T> ptr;
};

} // namespace floormat
39 changes: 22 additions & 17 deletions editor/app.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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())
{
Expand All @@ -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_);
}
}
}
Expand All @@ -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_)
Expand Down Expand Up @@ -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;
}

Expand Down
17 changes: 9 additions & 8 deletions editor/app.hpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -37,6 +37,7 @@ class anim_atlas;
struct critter;
struct point;
class editor;
template<typename T> struct shared_ptr_wrapper;

struct cursor_state final
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -176,12 +177,12 @@ struct app final : floormat_app
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;
Expand Down
3 changes: 2 additions & 1 deletion editor/ctor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions editor/imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
Expand All @@ -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);
}

Expand Down
5 changes: 3 additions & 2 deletions editor/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "tests-private.hpp"
#include "compat/safe-ptr.hpp"
#include "app.hpp"
#include "floormat/events.hpp"
#include "imgui-raii.hpp"
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion editor/tests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

namespace floormat {

template<typename T> class safe_ptr;

struct tests_data;

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_();
Expand Down
3 changes: 2 additions & 1 deletion editor/tests/path-test.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion external/corrade
1 change: 1 addition & 0 deletions floormat/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Magnum::Platform { class Sdl2Application; }

namespace floormat {

template<typename T> class safe_ptr;
struct fm_settings;
struct floormat_app;
struct tile_shader;
Expand Down
2 changes: 1 addition & 1 deletion loader/impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ struct loader_impl final : loader_
{
explicit loader_impl();
~loader_impl() override;

// >-----> system >----->
String original_working_directory;

void set_application_working_directory();
StringView startup_directory() noexcept override;
static void system_init();
void destroy() override;
static bool chdir(StringView pathname);

// >-----> plugins >----->
Expand Down
33 changes: 27 additions & 6 deletions loader/loader.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
#include "impl.hpp"
#include "ground-info.hpp"
#include "wall-info.hpp"
#include "scenery.hpp"

namespace floormat {

using loader_detail::loader_impl;
namespace floormat::loader_detail {

void loader_::destroy()
void loader_impl::destroy()
{
loader.~loader_();
new (&loader) loader_impl();
wall_atlas_map.clear();
wall_atlas_array.clear();
invalid_wall_atlas = nullptr;
missing_wall_atlases.clear();

ground_atlas_map.clear();
ground_atlas_array.clear();
invalid_ground_atlas = nullptr;
missing_ground_atlases.clear();

anim_atlas_map.clear();
anim_atlases.clear();
sceneries_map.clear();
sceneries_array.clear();
vobj_atlas_map.clear();
vobjs.clear();
}

} // namespace floormat::loader_detail

namespace floormat {

using loader_detail::loader_impl;

loader_& loader_::default_loader() noexcept
{
static loader_impl loader_singleton{};
Expand Down
Loading

0 comments on commit 785293f

Please sign in to comment.