diff options
-rw-r--r-- | main/app.cpp | 1 | ||||
-rw-r--r-- | main/app.hpp | 2 | ||||
-rw-r--r-- | main/camera.cpp | 5 | ||||
-rw-r--r-- | main/draw.cpp | 21 | ||||
-rw-r--r-- | main/gui.cpp | 9 | ||||
-rw-r--r-- | main/update.cpp | 2 | ||||
-rw-r--r-- | src/camera-offset.cpp | 22 | ||||
-rw-r--r-- | src/camera-offset.hpp | 10 |
8 files changed, 26 insertions, 46 deletions
diff --git a/main/app.cpp b/main/app.cpp index 569cc3b9..d432c716 100644 --- a/main/app.cpp +++ b/main/app.cpp @@ -28,7 +28,6 @@ app::app(const Arguments& arguments): update_window_scale(windowSize()); setMinimalLoopPeriod(5); _imgui = ImGuiIntegration::Context(Vector2{windowSize()}, windowSize(), framebufferSize()); - setup_menu(); SDL_MaximizeWindow(window()); { auto c = _world[chunk_coords{0, 0}]; diff --git a/main/app.hpp b/main/app.hpp index 54e2ee4d..395fa811 100644 --- a/main/app.hpp +++ b/main/app.hpp @@ -61,7 +61,6 @@ struct app final : Platform::Application void do_menu(); void draw_menu_(tile_type& type, float main_menu_height); void draw_fps(float main_menu_height); - void setup_menu(); void display_menu(); void debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Type type, UnsignedInt id, @@ -92,7 +91,6 @@ struct app final : Platform::Application ImGuiIntegration::Context _imgui{NoCreate}; world _world; - Vector2 camera_offset; enum_bitset<key> keys; Magnum::Timeline timeline; editor _editor; diff --git a/main/camera.cpp b/main/camera.cpp index dbead9a3..e9f9e1a2 100644 --- a/main/camera.cpp +++ b/main/camera.cpp @@ -5,7 +5,8 @@ namespace floormat { void app::do_camera(float dt) { - constexpr float pixels_per_second = 384; + auto camera_offset = _shader.camera_offset(); + constexpr float pixels_per_second = 384; if (keys[key::camera_up]) camera_offset += Vector2(0, 1) * dt * pixels_per_second; else if (keys[key::camera_down]) @@ -30,7 +31,7 @@ void app::do_camera(float dt) void app::reset_camera_offset() { - camera_offset = tile_shader::project({TILE_MAX_DIM/2.f*TILE_SIZE[0], TILE_MAX_DIM/2.f*TILE_SIZE[1], 0}); + _shader.set_camera_offset(tile_shader::project({TILE_MAX_DIM/2.f*TILE_SIZE[0]/2.f, TILE_MAX_DIM/2.f*TILE_SIZE[1]/2.f, 0})); recalc_cursor_tile(); } diff --git a/main/draw.cpp b/main/draw.cpp index a7ee2ec3..e2bcdd31 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -1,22 +1,18 @@ #include "app.hpp" #include "tile-defs.hpp" #include "camera-offset.hpp" -#include <Magnum/Math/Vector3.h> #include <Magnum/GL/DefaultFramebuffer.h> #include <Magnum/GL/Renderer.h> -#include <Magnum/Trade/AbstractImporter.h> namespace floormat { void app::drawEvent() { - GL::defaultFramebuffer.clear(GL::FramebufferClear::Color); - GL::Renderer::setDepthFunction(GL::Renderer::DepthFunction::Never); - { const float dt = std::min(1.f/10, timeline.previousFrameDuration()); update(dt); } + GL::defaultFramebuffer.clear(GL::FramebufferClear::Color); _shader.set_tint({1, 1, 1, 1}); { const with_shifted_camera_offset o{_shader, BASE_X, BASE_Y}; @@ -35,7 +31,7 @@ std::array<std::int16_t, 4> app::get_draw_bounds() const noexcept { std::int16_t minx = BASE_X, maxx = BASE_X, miny = BASE_Y, maxy = BASE_Y; { - const auto fn = [&](std::int32_t x, std::int32_t y) { + auto fn = [&](std::int32_t x, std::int32_t y) { const auto pos = pixel_to_tile({float(x + BASE_X), float(y + BASE_Y)}).chunk(); minx = std::min(minx, pos.x); maxx = std::max(maxx, pos.x); @@ -49,7 +45,6 @@ std::array<std::int16_t, 4> app::get_draw_bounds() const noexcept fn(0, y); fn(x, y); } - fflush(stdout); return {minx, maxx, miny, maxy}; } @@ -57,21 +52,13 @@ void app::draw_world() { auto [minx, maxx, miny, maxy] = get_draw_bounds(); - _shader.set_tint({1, 1, 1, 1}); - for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) - { - auto c = _world[chunk_coords{x, y}]; - _floor_mesh.draw(_shader, *c); - } + _floor_mesh.draw(_shader, *_world[chunk_coords{x, y}]); for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) - { - auto c = _world[chunk_coords{x, y}]; - _wall_mesh.draw(_shader, *c); - } + _wall_mesh.draw(_shader, *_world[chunk_coords{x, y}]); } void app::draw_wireframe_quad(global_coords pos) diff --git a/main/gui.cpp b/main/gui.cpp index e665c27b..22bb95fd 100644 --- a/main/gui.cpp +++ b/main/gui.cpp @@ -9,20 +9,17 @@ namespace floormat { using namespace floormat::imgui; -void app::setup_menu() +void app::display_menu() { GL::Renderer::setBlendEquation(GL::Renderer::BlendEquation::Add, GL::Renderer::BlendEquation::Add); GL::Renderer::setBlendFunction(GL::Renderer::BlendFunction::SourceAlpha, GL::Renderer::BlendFunction::OneMinusSourceAlpha); -} - -void app::display_menu() -{ GL::Renderer::enable(GL::Renderer::Feature::Blending); + GL::Renderer::enable(GL::Renderer::Feature::ScissorTest); GL::Renderer::disable(GL::Renderer::Feature::FaceCulling); GL::Renderer::disable(GL::Renderer::Feature::DepthTest); + _imgui.drawFrame(); - GL::Renderer::enable(GL::Renderer::Feature::ScissorTest); } void app::do_menu() diff --git a/main/update.cpp b/main/update.cpp index eeeede2b..facabc37 100644 --- a/main/update.cpp +++ b/main/update.cpp @@ -31,7 +31,7 @@ void app::update(float dt) global_coords app::pixel_to_tile(Vector2 position) const { - const Vector2 px = position - Vector2{windowSize()}*.5f - camera_offset; + const Vector2 px = position - Vector2{windowSize()}*.5f - _shader.camera_offset(); const Vector2 vec = tile_shader::unproject(px) / Vector2{TILE_SIZE[0]*.5f, TILE_SIZE[1]*.5f} + Vector2{.5f, .5f}; const auto x = (std::int32_t)std::floor(vec[0]), y = (std::int32_t)std::floor(vec[1]); return { x, y }; diff --git a/src/camera-offset.cpp b/src/camera-offset.cpp index 47cb690d..f4c3271e 100644 --- a/src/camera-offset.cpp +++ b/src/camera-offset.cpp @@ -1,27 +1,25 @@ #include "camera-offset.hpp" +#include "tile-defs.hpp" #include "shaders/tile-shader.hpp" +#include <Magnum/Math/Vector2.h> namespace floormat { -with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, std::int32_t x, std::int32_t y) : - with_shifted_camera_offset{shader, chunk_coords{std::int16_t(x), std::int16_t(y)}} -{ - ASSERT(std::abs(x) < (1 << 15) && std::abs(y) < (1 << 15)); -} +static_assert(sizeof(short) == 2); -with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, const chunk_coords c) : - s{shader}, - orig_offset(shader.camera_offset()) +with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, short x, short y) : + _shader{shader}, + _offset{shader.camera_offset()} { - const auto offset = tile_shader::project({float(c.x)*TILE_MAX_DIM*TILE_SIZE[0], - float(c.y)*TILE_MAX_DIM*TILE_SIZE[1], + const auto offset = tile_shader::project({float(x)*TILE_MAX_DIM*TILE_SIZE[0], + float(y)*TILE_MAX_DIM*TILE_SIZE[1], 0}); - s.set_camera_offset(orig_offset + offset); + _shader.set_camera_offset(_offset + Vector2(x, y)); } with_shifted_camera_offset::~with_shifted_camera_offset() { - s.set_camera_offset(orig_offset); + _shader.set_camera_offset(_offset); } } // namespace floormat diff --git a/src/camera-offset.hpp b/src/camera-offset.hpp index da07cec0..927d5693 100644 --- a/src/camera-offset.hpp +++ b/src/camera-offset.hpp @@ -1,5 +1,6 @@ #pragma once -#include "src/global-coords.hpp" +#include <Magnum/Magnum.h> +#include <Magnum/Math/Vector2.h> namespace floormat { @@ -7,12 +8,11 @@ struct tile_shader; struct with_shifted_camera_offset final { - explicit with_shifted_camera_offset(tile_shader& shader, std::int32_t, std::int32_t); - explicit with_shifted_camera_offset(tile_shader& shader, chunk_coords c); + explicit with_shifted_camera_offset(tile_shader& shader, short x, short y); ~with_shifted_camera_offset(); private: - tile_shader& s; // NOLINT - Vector2 orig_offset; + tile_shader& _shader; // NOLINT + Vector2 _offset; }; } // namespace floormat |