From 5385be91cf40b9e610f3c7c9488023b925b563e1 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 24 Oct 2022 12:05:40 +0200 Subject: a --- CMakeLists.txt | 10 +-- editor/camera.cpp | 1 + main/debug.cpp | 11 ++-- main/draw.cpp | 117 ----------------------------------- main/events.cpp | 111 --------------------------------- main/floormat-main-impl.cpp | 1 + main/floormat-main-impl.hpp | 12 ++-- main/main.cpp | 115 ---------------------------------- main/main.hpp | 146 -------------------------------------------- main/precomp.hpp | 16 +---- 10 files changed, 24 insertions(+), 516 deletions(-) delete mode 100644 main/draw.cpp delete mode 100644 main/events.cpp delete mode 100644 main/main.cpp delete mode 100644 main/main.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e510ceb5..4c8f3a92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -109,10 +109,12 @@ endif() add_subdirectory(external) -if(MSVC) - add_definitions(/FI"compat/prelude.hpp") -else() - add_definitions(-include compat/prelude.hpp) +if(NOT FLOORMAT_PRECOMPILED-HEADERS) + if(MSVC) + add_definitions(/FI"compat/prelude.hpp") + else() + add_definitions(-include compat/prelude.hpp) + endif() endif() add_definitions( diff --git a/editor/camera.cpp b/editor/camera.cpp index ecbeafc1..b19acdd5 100644 --- a/editor/camera.cpp +++ b/editor/camera.cpp @@ -37,6 +37,7 @@ void app::do_camera(float dt) //camera_offset[0] = std::clamp(camera_offset[0], -max_camera_offset[0], max_camera_offset[0]); //camera_offset[1] = std::clamp(camera_offset[1], -max_camera_offset[1], max_camera_offset[1]); + Debug{} << "camera" << camera_offset; shader.set_camera_offset(camera_offset); } else diff --git a/main/debug.cpp b/main/debug.cpp index 14cdad00..04c76343 100644 --- a/main/debug.cpp +++ b/main/debug.cpp @@ -6,8 +6,7 @@ namespace floormat { using Severity = GL::DebugOutput::Severity; // NOLINTNEXTLINE(readability-convert-member-functions-to-static) -void main_impl::debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Type type, UnsignedInt id, - Severity severity, const std::string& str) const +void main_impl::debug_callback(unsigned src, unsigned type, unsigned id, unsigned severity, const std::string& str) const { static thread_local auto clock = std::chrono::steady_clock{}; static const auto t0 = clock.now(); @@ -29,7 +28,7 @@ void main_impl::debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Typ const auto t = std::chrono::duration_cast(clock.now() - t0).count(); printf("[%10.03f] ", t); - switch (severity) + switch (Severity{severity}) { using enum GL::DebugOutput::Severity; case Notification: std::fputs("fm_debug ", stdout); break; @@ -52,10 +51,10 @@ void main_impl::debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Typ std::fputs("", stdout); // put breakpoint here } -void main_impl::_debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Type type, UnsignedInt id, - GL::DebugOutput::Severity severity, const std::string& str, const void* self) +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_cast(self)->debug_callback(src, type, id, severity, str); + static_cast(self)->debug_callback((unsigned)src, (unsigned)type, (unsigned)id, (unsigned)severity, str); } void main_impl::register_debug_callback() diff --git a/main/draw.cpp b/main/draw.cpp deleted file mode 100644 index dd6abc67..00000000 --- a/main/draw.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include "main.hpp" -#include "tile-defs.hpp" -#include "camera-offset.hpp" -#include -#include - -//#define FM_SKIP_MSAA - -namespace floormat { - -#if 0 -void floormat::drawEvent() -{ - if (const float dt = timeline.previousFrameDuration(); dt > 0) - { - constexpr float RC = 0.1f; - const float alpha = dt/(dt + RC); - - _frame_time = _frame_time*(1-alpha) + alpha*dt; - } - elseÅ› - { - swapBuffers(); - timeline.nextFrame(); - } - - { - const auto dt = std::clamp((double)timeline.previousFrameDuration(), 1e-6, 1e-1); - update(dt); - } - - _shader.set_tint({1, 1, 1, 1}); - - { - GL::defaultFramebuffer.clear(GL::FramebufferClear::Color); -#if defined FM_MSAA && !defined FM_SKIP_MSAA - _msaa_framebuffer.clear(GL::FramebufferClear::Color); - _msaa_framebuffer.bind(); -#endif - draw_msaa(); -#if defined FM_MSAA && !defined FM_SKIP_MSAA - GL::defaultFramebuffer.bind(); - GL::Framebuffer::blit(_msaa_framebuffer, GL::defaultFramebuffer, {{}, windowSize()}, GL::FramebufferBlit::Color); -#endif - } - - render_menu(); - - swapBuffers(); - redraw(); - timeline.nextFrame(); -} - -void floormat::draw_msaa() -{ - const with_shifted_camera_offset o{_shader, BASE_X, BASE_Y}; - draw_world(); - draw_cursor_tile(); -} - -void floormat::draw_world() -{ - auto foo = get_draw_bounds(); - auto [minx, maxx, miny, maxy] = foo; - - for (std::int16_t y = miny; y <= maxy; y++) - for (std::int16_t x = minx; x <= maxx; x++) - { -#if 1 - if (const chunk_coords c = {x, y}; !_world.contains(c)) - make_test_chunk(*_world[c]); -#endif - const chunk_coords c{x, y}; - const with_shifted_camera_offset o{_shader, c}; - _floor_mesh.draw(_shader, *_world[c]); - } - - for (std::int16_t y = miny; y <= maxy; y++) - for (std::int16_t x = minx; x <= maxx; x++) - { - const chunk_coords c{x, y}; - const with_shifted_camera_offset o{_shader, c}; - _wall_mesh.draw(_shader, *_world[c]); - } -} - -void floormat::draw_wireframe_quad(global_coords pos) -{ - constexpr float LINE_WIDTH = 2; - const auto pt = pos.to_signed(); - - //if (const auto& [c, tile] = _world[pos]; tile.ground_image) - { - const Vector3 center{pt[0]*TILE_SIZE[0], pt[1]*TILE_SIZE[1], 0}; - _shader.set_tint({1, 0, 0, 1}); - _wireframe_quad.draw(_shader, {center, {TILE_SIZE[0], TILE_SIZE[1]}, LINE_WIDTH}); - } -} - -void floormat::draw_wireframe_box(local_coords pt) -{ - constexpr float LINE_WIDTH = 1.5; - - constexpr auto X = TILE_SIZE[0], Y = TILE_SIZE[1], Z = TILE_SIZE[2]; - constexpr Vector3 size{X, Y, Z}; - const Vector3 center1{X*pt.x, Y*pt.y, 0}; - _shader.set_tint({0, 1, 0, 1}); - _wireframe_box.draw(_shader, {center1, size, LINE_WIDTH}); -} - -void floormat::draw_cursor_tile() -{ - if (_cursor_tile && !_cursor_in_imgui) - draw_wireframe_quad(*_cursor_tile); -} -#endif -} // namespace floormat diff --git a/main/events.cpp b/main/events.cpp deleted file mode 100644 index ef1d4ead..00000000 --- a/main/events.cpp +++ /dev/null @@ -1,111 +0,0 @@ -#include "floormat-main.hpp" -#include -#include -#include - -namespace floormat { - -#if 0 -void main_impl::viewportEvent(Platform::Sdl2Application::ViewportEvent& event) -{ - fm_assert(event.framebufferSize() == event.windowSize()); - recalc_viewport(event.windowSize()); -} - -void main_impl::mousePressEvent(Platform::Sdl2Application::MouseEvent& event) -{ - if (_imgui.handleMousePressEvent(event)) - return event.setAccepted(); - else if (_cursor_tile) - { - const auto& tile = *_cursor_tile; - do_mouse_click(tile, (int)event.button()); - } -} - -void main_impl::mouseReleaseEvent(Platform::Sdl2Application::MouseEvent& event) -{ - if (_imgui.handleMouseReleaseEvent(event)) - return event.setAccepted(); - do_mouse_release((int)event.button()); -} - -void main_impl::mouseMoveEvent(Platform::Sdl2Application::MouseMoveEvent& event) -{ - _cursor_in_imgui = _imgui.handleMouseMoveEvent(event); - if (_cursor_in_imgui) - _cursor_pixel = std::nullopt; - else - _cursor_pixel = event.position(); - recalc_cursor_tile(); - if (_cursor_tile) - do_mouse_move(*_cursor_tile); -} - -void main_impl::mouseScrollEvent(Platform::Sdl2Application::MouseScrollEvent& event) -{ - if (_imgui.handleMouseScrollEvent(event)) - return event.setAccepted(); -} - -void main_impl::textInputEvent(Platform::Sdl2Application::TextInputEvent& event) -{ - if (_imgui.handleTextInputEvent(event)) - { - keys = {}; - event.setAccepted(); - } -} - -void main_impl::keyPressEvent(Platform::Sdl2Application::KeyEvent& event) -{ - if (_imgui.handleKeyPressEvent(event)) - { - keys = {}; - return event.setAccepted(); - } - do_key(event.key(), event.modifiers(), true, event.isRepeated()); -} - -void main_impl::keyReleaseEvent(Platform::Sdl2Application::KeyEvent& event) -{ - if (_imgui.handleKeyReleaseEvent(event)) - { - keys = {}; - return event.setAccepted(); - } - do_key(event.key(), event.modifiers(), false, false); -} - -void main_impl::anyEvent(SDL_Event& event) -{ - if (event.type == SDL_WINDOWEVENT) - switch (event.window.event) - { - case SDL_WINDOWEVENT_FOCUS_LOST: - return event_focus_out(); - case SDL_WINDOWEVENT_FOCUS_GAINED: - return event_focus_in(); - case SDL_WINDOWEVENT_LEAVE: - return event_mouse_leave(); - case SDL_WINDOWEVENT_ENTER: - return event_mouse_enter(); - default: - std::fputs("", stdout); break; // put breakpoint here - } -} - -void main_impl::event_focus_out() // TODO move to app -{ - _cursor_pixel = std::nullopt; - recalc_cursor_tile(); -} - -void main_impl::event_mouse_leave() // TODO move to app -{ - _cursor_pixel = std::nullopt; - recalc_cursor_tile(); -} -#endif - -} // namespace floormat diff --git a/main/floormat-main-impl.cpp b/main/floormat-main-impl.cpp index 3cec55ac..bb92c1d9 100644 --- a/main/floormat-main-impl.cpp +++ b/main/floormat-main-impl.cpp @@ -6,6 +6,7 @@ #include "src/chunk.hpp" #include "src/world.hpp" #include "src/camera-offset.hpp" +#include #include //#define FM_SKIP_MSAA diff --git a/main/floormat-main-impl.hpp b/main/floormat-main-impl.hpp index 4c3a9076..4ca375bf 100644 --- a/main/floormat-main-impl.hpp +++ b/main/floormat-main-impl.hpp @@ -5,7 +5,13 @@ #include "draw/floor-mesh.hpp" #include "draw/wall-mesh.hpp" #include "shaders/tile-shader.hpp" + #include + +#include +#include +#include +#include #include #include @@ -52,6 +58,8 @@ struct main_impl final : Platform::Sdl2Application, floormat_main void start_text_input() noexcept override; void stop_text_input() noexcept override; + void debug_callback(unsigned src, unsigned type, unsigned id, unsigned severity, const std::string& str) const; + private: fm_settings s; char _dummy = maybe_register_debug_callback(s.gpu_debug); @@ -74,10 +82,6 @@ private: draw_bounds get_draw_bounds() const noexcept; - void debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Type type, UnsignedInt id, - GL::DebugOutput::Severity severity, const std::string& str) const; - 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); char maybe_register_debug_callback(fm_gpu_debug flag); void register_debug_callback(); diff --git a/main/main.cpp b/main/main.cpp deleted file mode 100644 index 37325809..00000000 --- a/main/main.cpp +++ /dev/null @@ -1,115 +0,0 @@ -#include -#include "compat/sysexits.hpp" -#include "main.hpp" -#include "compat/fpu.hpp" -#include -#include -#include - -#if 0 - -#ifdef FM_MSAA -#include -#endif - -namespace floormat { - -#if 0 -int floormat::run_from_argv(int argc, char** argv) -{ - Corrade::Utility::Arguments args{}; - app_settings opts; - args.addSkippedPrefix("magnum") - .addOption("vsync", opts.vsync ? "1" : "0") - .parse(argc, argv); - opts.vsync = args.value("vsync"); - floormat x{{argc, argv}, std::move(opts)}; // NOLINT(performance-move-const-arg) - return x.exec(); -} - -void floormat::usage(const Utility::Arguments& args) -{ - Error{Error::Flag::NoNewlineAtTheEnd} << args.usage(); - std::exit(EX_USAGE); // NOLINT(concurrency-mt-unsafe) -} - -floormat::floormat(const Arguments& arguments, app_settings opts): - Platform::Sdl2Application{ - arguments, - Configuration{} - .setTitle("Test") - .setSize({1024, 768}, dpi_policy::Physical) - .setWindowFlags(Configuration::WindowFlag::Resizable), - GLConfiguration{} - }, - _settings{opts} -{ - SDL_MaximizeWindow(window()); - - if (opts.vsync) - { - (void)setSwapInterval(1); - if (const auto list = GL::Context::current().extensionStrings(); - std::find(list.cbegin(), list.cend(), "EXT_swap_control_tear") != list.cbegin()) - (void)setSwapInterval(-1); - } - else - setSwapInterval(0); - - set_fp_mask(); - reset_camera_offset(); - - fm_assert(framebufferSize() == windowSize()); - recalc_viewport(windowSize()); - - setMinimalLoopPeriod(5); - { - auto c = _world[chunk_coords{0, 0}]; - make_test_chunk(*c); - } - timeline.start(); -} - -void floormat::recalc_viewport(Vector2i size) -{ - _shader.set_scale(Vector2(size)); - init_imgui(size); - _cursor_pixel = std::nullopt; - recalc_cursor_tile(); - - GL::defaultFramebuffer.setViewport({{}, size }); -#ifdef FM_MSAA - _msaa_framebuffer.detach(GL::Framebuffer::ColorAttachment{0}); - _msaa_renderbuffer = Magnum::GL::Renderbuffer{}; - _msaa_renderbuffer.setStorageMultisample(msaa_samples, GL::RenderbufferFormat::RGBA8, size); - _msaa_framebuffer.setViewport({{}, size }); - _msaa_framebuffer.attachRenderbuffer(GL::Framebuffer::ColorAttachment{0}, _msaa_renderbuffer); -#endif -} -#endif - -} // namespace floormat - -int main(int argc, char** argv) -{ - return floormat::floormat::run_from_argv(argc, argv); -} - -#ifdef _MSC_VER -#include // for __arg{c,v} -#ifdef __clang__ -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wmain" -#endif -extern "C" int __stdcall WinMain(void*, void*, void*, int); - -extern "C" int __stdcall WinMain(void*, void*, void*, int) -{ - return main(__argc, __argv); -} -#ifdef __clang__ -# pragma clang diagnostic pop -#endif -#endif - -#endif diff --git a/main/main.hpp b/main/main.hpp deleted file mode 100644 index aa7671e6..00000000 --- a/main/main.hpp +++ /dev/null @@ -1,146 +0,0 @@ -#pragma once - -#if 0 -#include "floormat.hpp" - -#include "tile-atlas.hpp" -#include "src/chunk.hpp" -#include "shaders/tile-shader.hpp" -#include "src/loader.hpp" -#include "draw/floor-mesh.hpp" -#include "draw/wall-mesh.hpp" -#include "draw/wireframe-mesh.hpp" -#include "draw/wireframe-quad.hpp" -#include "draw/wireframe-box.hpp" -#include "compat/enum-bitset.hpp" -#include "src/world.hpp" -#include -#include -#include -#include -#include - -#define FM_MSAA - -#ifdef FM_MSAA -#include -#include -#endif - -namespace floormat { - -struct floormat final : private Platform::Sdl2Application -{ - static int run_from_argv(int argc, char** argv); - virtual ~floormat(); - -private: - struct app_settings; - - [[maybe_unused]] [[noreturn]] static void usage(const Utility::Arguments& args); - explicit floormat(const Arguments& arguments, app_settings opts); - - using dpi_policy = Platform::Implementation::Sdl2DpiScalingPolicy; - using tile_atlas_ = std::shared_ptr; - - void update(double dt); - - void do_key(KeyEvent::Key k, KeyEvent::Modifiers m, bool pressed, bool repeated); - void do_mouse_click(global_coords pos, int button); - void do_mouse_release(int button); - void do_mouse_move(global_coords pos); - - void do_camera(double dt); - void reset_camera_offset(); - void recalc_cursor_tile(); - void recalc_viewport(Vector2i size); - void init_imgui(Vector2i size); - - [[maybe_unused]] void viewportEvent(ViewportEvent& event) override; - [[maybe_unused]] void mousePressEvent(MouseEvent& event) override; - [[maybe_unused]] void mouseReleaseEvent(MouseEvent& event) override; - [[maybe_unused]] void mouseMoveEvent(MouseMoveEvent& event) override; - [[maybe_unused]] void mouseScrollEvent(MouseScrollEvent& event) override; - [[maybe_unused]] void textInputEvent(TextInputEvent& event) override; - [[maybe_unused]] void keyPressEvent(KeyEvent& event) override; - [[maybe_unused]] void keyReleaseEvent(KeyEvent& event) override; - [[maybe_unused]] void anyEvent(SDL_Event& event) override; - - void event_focus_out(); - void event_focus_in(); - void event_mouse_enter(); - void event_mouse_leave(); - - std::array get_draw_bounds() const noexcept; - void drawEvent() override; - void draw_msaa(); - void draw_world(); - void draw_cursor_tile(); - void draw_wireframe_quad(global_coords pt); - void draw_wireframe_box(local_coords pt); - - void draw_ui(); - float draw_main_menu(); - void draw_editor_pane(tile_type& type, float main_menu_height); - void draw_fps(); - void draw_cursor_coord(); - void render_menu(); - - void debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Type type, UnsignedInt id, - GL::DebugOutput::Severity severity, const std::string& str) const; - 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); - void* register_debug_callback(); - - global_coords pixel_to_tile(Vector2d position) const; - - enum class key : int { - camera_up, camera_left, camera_right, camera_down, camera_reset, - rotate_tile, quicksave, quickload, - quit, - MAX = quit, COUNT - }; - void make_test_chunk(chunk& c); - - [[maybe_unused]] void* _dummy = register_debug_callback(); - -#ifdef FM_MSAA - GL::Framebuffer _msaa_framebuffer{{{}, windowSize()}}; - GL::Renderbuffer _msaa_renderbuffer{}; -#endif - - tile_shader _shader; - tile_atlas_ floor1 = loader.tile_atlas("floor-tiles", {44, 4}); - tile_atlas_ floor2 = loader.tile_atlas("metal1", {2, 2}); - tile_atlas_ wall1 = loader.tile_atlas("wood2", {1, 1}); - tile_atlas_ wall2 = loader.tile_atlas("wood1", {1, 1}); - - floor_mesh _floor_mesh; - wall_mesh _wall_mesh; - wireframe_mesh _wireframe_quad; - wireframe_mesh _wireframe_box; - - ImGuiIntegration::Context _imgui{NoCreate}; - - world _world; - enum_bitset keys; - Magnum::Timeline timeline; - editor _editor; - std::optional _cursor_pixel; - std::optional _cursor_tile; - float _frame_time = 0; - - struct app_settings { - bool vsync = true; - }; - - app_settings _settings; - - static constexpr std::int16_t BASE_X = 0, BASE_Y = 0; -#ifdef FM_MSAA - static constexpr int msaa_samples = 16; -#endif -}; - -} // namespace floormat -#endif diff --git a/main/precomp.hpp b/main/precomp.hpp index 0727f83d..bd769578 100644 --- a/main/precomp.hpp +++ b/main/precomp.hpp @@ -1,20 +1,10 @@ #pragma once + #include "src/precomp.hpp" #include #include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include +#include +#include #include -- cgit v1.2.3