diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-24 09:32:13 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-24 09:32:13 +0200 |
commit | cecd124c0c043ef261256fb9e3f1108cde1265c1 (patch) | |
tree | 7fcba9f0904efa254d260feb1fc4eef444dd5339 | |
parent | 203701d221b5f982abf44379ce738f76e0db027a (diff) |
a
-rw-r--r-- | editor/app.cpp | 55 | ||||
-rw-r--r-- | editor/app.hpp | 7 | ||||
-rw-r--r-- | editor/update.cpp | 5 | ||||
-rw-r--r-- | main/draw.cpp | 2 | ||||
-rw-r--r-- | main/floormat-app.cpp | 2 | ||||
-rw-r--r-- | main/floormat-app.hpp | 4 | ||||
-rw-r--r-- | main/floormat-main-impl.cpp | 40 | ||||
-rw-r--r-- | main/floormat-main-impl.hpp | 15 | ||||
-rw-r--r-- | main/floormat-main.hpp | 4 | ||||
-rw-r--r-- | main/floormat.hpp | 4 | ||||
-rw-r--r-- | src/chunk.hpp | 2 | ||||
-rw-r--r-- | src/world.hpp | 1 |
12 files changed, 101 insertions, 40 deletions
diff --git a/editor/app.cpp b/editor/app.cpp index 324471cd..d6884edb 100644 --- a/editor/app.cpp +++ b/editor/app.cpp @@ -1,6 +1,9 @@ #include "app.hpp" +#include "compat/assert.hpp" #include "main/floormat-main.hpp" +#include "main/floormat.hpp" #include "src/loader.hpp" +#include <Corrade/Utility/Arguments.h> namespace floormat { @@ -19,4 +22,56 @@ app::~app() loader_::destroy(); } +int app::exec() +{ + return M->exec(); +} + +static const char* const true_values[] = { "1", "true", "yes", "y", "Y", "on", "ON", }; +static const char* const false_values[] = { "0", "false", "no", "n", "N", "off", "OFF", }; +static const char* const maybe_values[] = { "maybe", "m", "M", "default", }; + +template<typename T, typename U> +static inline bool find_arg(const T& list, const U& value) { + return std::find_if(std::cbegin(list), std::cend(list), + [&](const auto& x) { return x == value; }) != std::cend(list); +} + +static fm_tristate parse_tristate(StringView name, StringView str, fm_tristate def) +{ + if (find_arg(true_values, str)) + return fm_tristate::on; + else if (find_arg(false_values, str)) + return fm_tristate::off; + else if (find_arg(maybe_values, str)) + return fm_tristate::maybe; + + fm_warn("invalid '%s' argument '%s': should be true, false or default", name.data(), str.data()); + return def; +} + +static bool parse_bool(StringView name, StringView str, bool def) +{ + if (find_arg(true_values, str)) + return true; + else if (find_arg(false_values, str)) + return false; + fm_warn("invalid '%s' argument '%s': should be true or false", name.data(), str.data()); + return def; +} + +int app::run_from_argv(const int argc, const char* const* const argv) +{ + fm_options opts; + { + Corrade::Utility::Arguments args{}; + args.addOption("vsync", "default") + .addOption("gpu-validation", "true") + .parse(argc, argv); + opts.vsync = parse_tristate("--vsync", args.value<StringView>("vsync"), opts.vsync); + } + app application; + return application.exec(); +} + } // namespace floormat diff --git a/editor/app.hpp b/editor/app.hpp index f3c95d6f..ff5326ac 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -36,6 +36,7 @@ struct app final : floormat_app fm_DECLARE_DEPRECATED_MOVE_ASSIGNMENT(app); void update(float dt) override; + void maybe_init_chunk(const chunk_coords& pos, chunk& c) override; void draw_msaa() override; void draw() override; @@ -52,6 +53,10 @@ struct app final : floormat_app void on_mouse_leave() noexcept override; void on_mouse_enter() noexcept override; + int exec(); + + static int run_from_argv(int argv, const char* const* argc); + private: using tile_atlas_ = std::shared_ptr<tile_atlas>; @@ -68,7 +73,7 @@ private: void do_mouse_release(int button); void do_mouse_move(global_coords pos); - void do_camera(double dt); + void do_camera(float dt); void reset_camera_offset(); void recalc_cursor_tile(); void init_imgui(Vector2i size); diff --git a/editor/update.cpp b/editor/update.cpp index affcff57..d93c34e2 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -3,6 +3,7 @@ #include "src/tile-atlas.hpp" #include "main/floormat-events.hpp" #include "main/floormat-main.hpp" +#include <Magnum/Platform/Sdl2Application.h> namespace floormat { @@ -52,8 +53,8 @@ void app::update(float dt) { do_camera(dt); draw_ui(); - if (keys[key::quit]) - Platform::Sdl2Application::exit(0); + if (_keys[key::quit]) + M->quit(0); } } // namespace floormat diff --git a/main/draw.cpp b/main/draw.cpp index 71b2f249..dd6abc67 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -18,7 +18,7 @@ void floormat::drawEvent() _frame_time = _frame_time*(1-alpha) + alpha*dt; } - else + elseÅ› { swapBuffers(); timeline.nextFrame(); diff --git a/main/floormat-app.cpp b/main/floormat-app.cpp index c16bbf4b..2f69c0da 100644 --- a/main/floormat-app.cpp +++ b/main/floormat-app.cpp @@ -1,8 +1,10 @@ #include "floormat-app.hpp" +#include "floormat.hpp" namespace floormat { floormat_app::floormat_app() noexcept = default; floormat_app::~floormat_app() noexcept = default; +fm_settings::~fm_settings() noexcept = default; } // namespace floormat diff --git a/main/floormat-app.hpp b/main/floormat-app.hpp index e4bf2cae..c74ea461 100644 --- a/main/floormat-app.hpp +++ b/main/floormat-app.hpp @@ -13,6 +13,9 @@ struct text_input_event; struct text_editing_event; struct any_event; +struct chunk_coords; +struct chunk; + struct floormat_app { floormat_app() noexcept; @@ -22,6 +25,7 @@ struct floormat_app fm_DECLARE_DEPRECATED_MOVE_ASSIGNMENT(floormat_app); virtual void update(float dt) = 0; + virtual void maybe_init_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 6040beb8..7f2821d2 100644 --- a/main/floormat-main-impl.cpp +++ b/main/floormat-main-impl.cpp @@ -18,7 +18,7 @@ main_impl::~main_impl() noexcept = default; static const char* const fm_fake_argv[] = { "floormat", nullptr }; -auto main_impl::make_window_flags(const fm_options& s) -> Configuration::WindowFlags +auto main_impl::make_window_flags(const fm_settings& s) -> Configuration::WindowFlags { using flag = Configuration::WindowFlag; Configuration::WindowFlags flags{}; @@ -35,7 +35,7 @@ auto main_impl::make_window_flags(const fm_options& s) -> Configuration::WindowF return flags; } -auto main_impl::make_conf(const fm_options& s) -> Configuration +auto main_impl::make_conf(const fm_settings& s) -> Configuration { switch (s.log_level) { @@ -55,7 +55,7 @@ auto main_impl::make_conf(const fm_options& s) -> Configuration .setWindowFlags(make_window_flags(s)); } -auto main_impl::make_gl_conf(const fm_options& s) -> GLConfiguration +auto main_impl::make_gl_conf(const fm_settings& s) -> GLConfiguration { GLConfiguration::Flags flags{}; using f = GLConfiguration::Flag; @@ -82,7 +82,7 @@ void main_impl::recalc_viewport(Vector2i size) noexcept } // NOLINTNEXTLINE(performance-unnecessary-value-param) -main_impl::main_impl(floormat_app& app, fm_options s) noexcept : +main_impl::main_impl(floormat_app& app, fm_settings s) noexcept : Platform::Sdl2Application{Arguments{fake_argc, fm_fake_argv}, make_conf(s), make_gl_conf(s)}, app{app}, s{std::move(s)} @@ -143,10 +143,8 @@ void main_impl::draw_world() noexcept for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) { -#if 0 if (const chunk_coords c = {x, y}; !_world.contains(c)) - make_test_chunk(*_world[c]); -#endif + app.maybe_init_chunk(c, *_world[c]); const chunk_coords c{x, y}; const with_shifted_camera_offset o{_shader, c}; _floor_mesh.draw(_shader, *_world[c]); @@ -202,31 +200,17 @@ void main_impl::drawEvent() timeline.nextFrame(); } -void main_impl::quit(int status) -{ - Platform::Sdl2Application::exit(status); -} - -int main_impl::exec() -{ - return Sdl2Application::exec(); -} - -struct world& main_impl::world() noexcept -{ - return _world; -} - -SDL_Window* main_impl::window() noexcept -{ - return Sdl2Application::window(); -} - +void main_impl::quit(int status) { Platform::Sdl2Application::exit(status); } +int main_impl::exec() { return Sdl2Application::exec(); } +struct world& main_impl::world() noexcept { return _world; } +SDL_Window* main_impl::window() noexcept { return Sdl2Application::window(); } +fm_settings& main_impl::settings() noexcept { return s; } +const fm_settings& main_impl::settings() const noexcept { return s; } Vector2i main_impl::window_size() const noexcept { return windowSize(); } tile_shader& main_impl::shader() noexcept { return _shader; } const tile_shader& main_impl::shader() const noexcept { return _shader; } -floormat_main* floormat_main::create(floormat_app& app, const fm_options& options) +floormat_main* floormat_main::create(floormat_app& app, const fm_settings& options) { auto* ret = new main_impl(app, options); fm_assert(ret); diff --git a/main/floormat-main-impl.hpp b/main/floormat-main-impl.hpp index 2674bbae..c6a75129 100644 --- a/main/floormat-main-impl.hpp +++ b/main/floormat-main-impl.hpp @@ -17,7 +17,7 @@ struct floormat_app; struct main_impl final : Platform::Sdl2Application, floormat_main { - main_impl(floormat_app& app, fm_options opts) noexcept; + main_impl(floormat_app& app, fm_settings opts) noexcept; ~main_impl() noexcept override; int exec() override; @@ -31,6 +31,9 @@ struct main_impl final : Platform::Sdl2Application, floormat_main struct world& world() noexcept override; SDL_Window* window() noexcept override; + fm_settings& settings() noexcept; + const fm_settings& settings() const noexcept; + global_coords pixel_to_tile(Vector2d position) const noexcept override; [[maybe_unused]] void viewportEvent(ViewportEvent& event) override; @@ -52,13 +55,13 @@ private: floor_mesh _floor_mesh; wall_mesh _wall_mesh; Magnum::Timeline timeline; - fm_options s; + fm_settings s; int fake_argc = 1; struct draw_bounds final { std::int16_t minx, maxx, miny, maxy; }; #ifdef FM_MSAA - GL::Framebuffer _msaa_framebuffer{{{}, windowSize()}}; + GL::Framebuffer _msaa_framebuffer{{{}, window_size()}}; GL::Renderbuffer _msaa_renderbuffer{}; #endif @@ -72,9 +75,9 @@ private: static void _debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Type type, UnsignedInt id, GL::DebugOutput::Severity severity, const std::string& str, const void* self); - static Configuration make_conf(const fm_options& s); - static GLConfiguration make_gl_conf(const fm_options& s); - static Configuration::WindowFlags make_window_flags(const fm_options& s); + static Configuration make_conf(const fm_settings& s); + static GLConfiguration make_gl_conf(const fm_settings& s); + static Configuration::WindowFlags make_window_flags(const fm_settings& s); }; } // namespace floormat diff --git a/main/floormat-main.hpp b/main/floormat-main.hpp index b6503c08..3a4ed20e 100644 --- a/main/floormat-main.hpp +++ b/main/floormat-main.hpp @@ -28,13 +28,15 @@ struct floormat_main virtual const tile_shader& shader() const noexcept = 0; virtual void* register_debug_callback() noexcept = 0; constexpr float smoothed_dt() const noexcept { return _frame_time; } + virtual fm_settings& settings() noexcept = 0; + virtual const fm_settings& settings() const noexcept = 0; virtual global_coords pixel_to_tile(Vector2d position) const noexcept = 0; virtual world& world() noexcept = 0; virtual SDL_Window* window() noexcept = 0; - static floormat_main* create(floormat_app& app, const fm_options& options); + static floormat_main* create(floormat_app& app, const fm_settings& options); protected: float _frame_time = 0; diff --git a/main/floormat.hpp b/main/floormat.hpp index ce9e1b38..032c7865 100644 --- a/main/floormat.hpp +++ b/main/floormat.hpp @@ -9,8 +9,10 @@ enum class fm_gpu_debug : char { no_error = -1, on, robust, off }; enum class fm_tristate : char { maybe = -1, on, off }; enum class fm_log_level : unsigned char { quiet, normal, verbose, }; -struct fm_options final +struct fm_settings { + virtual ~fm_settings() noexcept; + Magnum::Math::Vector2<int> resolution{1024, 768}; Corrade::Containers::String title{"Test"}; Corrade::Containers::String disabled_extensions; // TODO diff --git a/src/chunk.hpp b/src/chunk.hpp index d342d9d9..13c9eeb7 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -32,7 +32,9 @@ struct chunk final chunk() = default; chunk(chunk&&) = default; chunk& operator=(chunk&&) = default; + fm_DECLARE_DELETED_COPY_ASSIGNMENT(chunk); + fm_DECLARE_DEPRECATED_MOVE_ASSIGNMENT(chunk); private: std::array<tile, TILE_COUNT> _tiles = {}; diff --git a/src/world.hpp b/src/world.hpp index 24a77d11..32c7206a 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -8,6 +8,7 @@ namespace floormat { +// todo remove shared_ptr struct chunk; struct world final |