diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-22 03:19:29 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-22 03:19:29 +0100 |
commit | 0fa4a8542bf9eda8bd9c28da199b3e90e04279b9 (patch) | |
tree | 8a91894a3b9eebdb8a7ef3c4b3e94d169efcf8ea | |
parent | 966ac722d72c8d89e621987090b19f2bde0cb58c (diff) |
wip
-rw-r--r-- | draw/anim.cpp | 10 | ||||
-rw-r--r-- | draw/anim.hpp | 1 | ||||
-rw-r--r-- | editor/app.cpp | 3 | ||||
-rw-r--r-- | editor/app.hpp | 3 | ||||
-rw-r--r-- | editor/dpi.cpp | 13 | ||||
-rw-r--r-- | floormat/settings.hpp | 2 | ||||
-rw-r--r-- | main/draw.cpp | 25 | ||||
-rw-r--r-- | main/main-impl.hpp | 14 | ||||
-rw-r--r-- | main/setup.cpp | 3 | ||||
-rw-r--r-- | shaders/tile.cpp | 2 | ||||
-rw-r--r-- | shaders/tile.hpp | 6 | ||||
-rw-r--r-- | src/anim-atlas.cpp | 19 | ||||
-rw-r--r-- | src/anim-atlas.hpp | 6 | ||||
-rw-r--r-- | src/camera-offset.cpp | 2 |
14 files changed, 83 insertions, 26 deletions
diff --git a/draw/anim.cpp b/draw/anim.cpp index d2f644a9..558dd197 100644 --- a/draw/anim.cpp +++ b/draw/anim.cpp @@ -22,16 +22,6 @@ std::array<UnsignedShort, 6> anim_mesh::make_index_array() }}; } -void anim_mesh::draw(tile_shader& shader, chunk& c) -{ - for (std::size_t i = 0; i < TILE_COUNT; i++) - { - const local_coords pos{i}; - if (auto [atlas, s] = c[pos].scenery(); atlas) - draw(shader, *atlas, s.r, s.frame, pos); - } -} - void anim_mesh::draw(tile_shader& shader, anim_atlas& atlas, rotation r, std::size_t frame, local_coords xy) { const auto center = Vector3(xy.x, xy.y, 0.f) * TILE_SIZE; diff --git a/draw/anim.hpp b/draw/anim.hpp index 324d8c3b..2b88e7af 100644 --- a/draw/anim.hpp +++ b/draw/anim.hpp @@ -22,7 +22,6 @@ struct chunk; struct anim_mesh final { anim_mesh(); - void draw(tile_shader& shader, chunk& c); void draw(tile_shader& shader, anim_atlas& atlas, rotation r, std::size_t frame, local_coords xy); private: diff --git a/editor/app.cpp b/editor/app.cpp index 85bbba54..75895ec2 100644 --- a/editor/app.cpp +++ b/editor/app.cpp @@ -120,6 +120,9 @@ int app::run_from_argv(const int argc, const char* const* const argv) int main(int argc, char** argv) { +#ifdef _WIN32 + floormat::app::set_dpi_aware(); +#endif return floormat::app::run_from_argv(argc, argv); } diff --git a/editor/app.hpp b/editor/app.hpp index 57487b01..a1b135e0 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -36,6 +36,9 @@ struct app final : floormat_app static int run_from_argv(int argc, const char* const* argv); static fm_settings parse_cmdline(int argc, const char* const* argv); ~app() override; +#ifdef _WIN32 + static void set_dpi_aware(); +#endif private: using key_set = enum_bitset<key, key_COUNT>; diff --git a/editor/dpi.cpp b/editor/dpi.cpp new file mode 100644 index 00000000..5893ac96 --- /dev/null +++ b/editor/dpi.cpp @@ -0,0 +1,13 @@ +#ifdef _WIN32 +#include "app.hpp" +#include <windows.h> + +namespace floormat { + +void app::set_dpi_aware() +{ + SetProcessDPIAware(); +} + +} // namespace floormat +#endif diff --git a/floormat/settings.hpp b/floormat/settings.hpp index 538d932d..bfc057ed 100644 --- a/floormat/settings.hpp +++ b/floormat/settings.hpp @@ -17,7 +17,7 @@ struct fm_settings fm_DECLARE_DEPRECATED_COPY_ASSIGNMENT(fm_settings); fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(fm_settings); - Magnum::Math::Vector2<int> resolution{1024, 768}; + Magnum::Math::Vector2<int> resolution{1536, 1152}; Corrade::Containers::String title{"Test"}; Corrade::Containers::String disabled_extensions; // TODO bool vsync = true; diff --git a/main/draw.cpp b/main/draw.cpp index 1b441650..4366fd9b 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -1,6 +1,7 @@ #include "main-impl.hpp" #include "floormat/app.hpp" #include "src/camera-offset.hpp" +#include "src/anim-atlas.hpp" #include <Magnum/GL/DefaultFramebuffer.h> #include <Magnum/GL/Renderer.h> #include <Magnum/Math/Color.h> @@ -56,8 +57,8 @@ global_coords main_impl::pixel_to_tile(Vector2d position) const noexcept { constexpr Vector2d pixel_size(TILE_SIZE2); constexpr Vector2d half{.5, .5}; - const Vector2d px = position - Vector2d{windowSize()}*.5 - _shader.camera_offset()*.5; - const Vector2d vec = tile_shader::unproject(px) / pixel_size + half; + const Vector2d px = position - Vector2d{windowSize()}*.5 - _shader.camera_offset(); + const Vector2d vec = tile_shader::unproject(px*.5) / pixel_size + half; const auto x = (std::int32_t)std::floor(vec[0]), y = (std::int32_t)std::floor(vec[1]); return { x, y }; } @@ -119,6 +120,7 @@ void main_impl::draw_anim() noexcept { const auto sz = windowSize(); const auto [minx, maxx, miny, maxy] = get_draw_bounds(); + _clickable_scenery.clear(); GL::Renderer::enable(GL::Renderer::Feature::DepthTest); @@ -129,7 +131,24 @@ void main_impl::draw_anim() noexcept auto& c = _world[pos]; const with_shifted_camera_offset o{_shader, pos}; if (check_chunk_visible(_shader.camera_offset(), sz)) - _anim_mesh.draw(_shader, c); + for (std::size_t i = 0; i < TILE_COUNT; i++) + { + const local_coords xy{i}; + if (auto [atlas, s] = c[xy].scenery(); atlas) + { + const auto& g = atlas->group(s.r); + const auto& f = atlas->frame(s.r, s.frame); + const auto& mask = atlas->bitmask(); + Vector2 offset; + constexpr Vector2 pixel88 = tile_shader::project(TILE_MAX_DIM*TILE_SIZE20*.5f); + const auto world_pos = TILE_SIZE20 * Vector3(xy.x, xy.y, 0) + Vector3(g.offset); + offset += (Vector2(_shader.camera_offset()) + Vector2(sz))*.5f; + //offset += _shader.project(world_pos); + //offset -= Vector2(f.ground); + Debug{} << "offset" << offset.x() << offset.y(); + _anim_mesh.draw(_shader, *atlas, s.r, s.frame, xy); + } + } } GL::Renderer::disable(GL::Renderer::Feature::DepthTest); diff --git a/main/main-impl.hpp b/main/main-impl.hpp index 93d0966e..63239a74 100644 --- a/main/main-impl.hpp +++ b/main/main-impl.hpp @@ -6,10 +6,10 @@ #include "draw/wall.hpp" #include "draw/anim.hpp" #include "shaders/tile.hpp" - +#include <vector> #include <Corrade/Containers/String.h> - #include <Magnum/Timeline.h> +#include <Magnum/Math/Range.h> #include <Magnum/GL/DebugOutput.h> #include <Magnum/GL/Framebuffer.h> #include <Magnum/GL/Renderbuffer.h> @@ -20,6 +20,13 @@ namespace floormat { struct floormat_app; +struct clickable final { + + float depth = 0; + chunk_coords chunk; + local_coords pos; +}; + struct main_impl final : Platform::Sdl2Application, floormat_main { explicit main_impl(floormat_app& app, fm_settings&& opts, int& fake_argc) noexcept; @@ -66,11 +73,12 @@ private: [[maybe_unused]] char _dummy = maybe_register_debug_callback(s.gpu_debug); floormat_app& app; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members) tile_shader _shader; + std::vector<clickable> _clickable_scenery; struct world _world{}; + Magnum::Timeline timeline; floor_mesh _floor_mesh; wall_mesh _wall_mesh; anim_mesh _anim_mesh; - Magnum::Timeline timeline; struct { float value = 0; float jitter = 0; diff --git a/main/setup.cpp b/main/setup.cpp index 1329374a..040c9bdd 100644 --- a/main/setup.cpp +++ b/main/setup.cpp @@ -21,6 +21,7 @@ main_impl::main_impl(floormat_app& app, fm_settings&& s, int& fake_argc) noexcep (void)setSwapInterval(0); set_fp_mask(); fm_assert(framebufferSize() == windowSize()); + _clickable_scenery.reserve(128); timeline.start(); } @@ -57,7 +58,7 @@ auto main_impl::make_conf(const fm_settings& s) -> Configuration } return Configuration{} .setTitle(s.title) - .setSize(s.resolution) + .setSize(s.resolution, Vector2(1, 1)) .setWindowFlags(make_window_flags(s)); } diff --git a/shaders/tile.cpp b/shaders/tile.cpp index bc5de830..7fa703fc 100644 --- a/shaders/tile.cpp +++ b/shaders/tile.cpp @@ -39,7 +39,7 @@ tile_shader::~tile_shader() = default; tile_shader& tile_shader::set_scale(const Vector2& scale) { if (scale != _scale) - setUniform(ScaleUniform, 1.f/(_scale = scale)); + setUniform(ScaleUniform, 2.f/(_scale = scale)); return *this; } diff --git a/shaders/tile.hpp b/shaders/tile.hpp index 4acc556b..5d763331 100644 --- a/shaders/tile.hpp +++ b/shaders/tile.hpp @@ -33,8 +33,7 @@ struct tile_shader : GL::AbstractShaderProgram template<typename T = float> static constexpr Math::Vector2<T> unproject(const Math::Vector2<T>& px); template<typename T, typename... Xs> - auto draw(T&& mesh, Xs&&... xs) -> - decltype(GL::AbstractShaderProgram::draw(std::forward<T>(mesh), std::forward<Xs>(xs)...)); + decltype(auto) draw(T&& mesh, Xs&&... xs); private: void _draw(); @@ -48,8 +47,7 @@ private: }; template<typename T, typename... Xs> -auto tile_shader::draw(T&& mesh, Xs&&... xs) -> - decltype(GL::AbstractShaderProgram::draw(std::forward<T>(mesh), std::forward<Xs>(xs)...)) +decltype(auto) tile_shader::draw(T&& mesh, Xs&&... xs) { _draw(); return GL::AbstractShaderProgram::draw(std::forward<T>(mesh), std::forward<Xs>(xs)...); diff --git a/src/anim-atlas.cpp b/src/anim-atlas.cpp index b566d19f..345ca63e 100644 --- a/src/anim-atlas.cpp +++ b/src/anim-atlas.cpp @@ -2,6 +2,7 @@ #include "compat/assert.hpp" #include "shaders/tile.hpp" #include "tile-defs.hpp" +#include <Corrade/Containers/BitArrayView.h> #include <Magnum/Math/Color.h> #include <Magnum/GL/TextureFormat.h> @@ -31,7 +32,7 @@ decltype(anim_atlas::_group_indices) anim_atlas::make_group_indices(const anim_d anim_atlas::anim_atlas() noexcept = default; anim_atlas::anim_atlas(StringView name, const ImageView2D& image, anim_def info) noexcept : - _name{name}, + _name{name}, _bitmask{make_bit_array(image)}, _info{std::move(info)}, _group_indices{make_group_indices(_info)} { _tex.setWrapping(GL::SamplerWrapping::ClampToEdge) @@ -99,4 +100,20 @@ auto anim_atlas::frame_quad(const Vector3& center, rotation r, std::size_t i) co }}; } +BitArray anim_atlas::make_bit_array(const ImageView2D& tex) +{ + fm_assert(tex.pixelSize() == 4); + const auto size = (std::size_t)tex.size().product(); + BitArray array{NoInit, size}; + const char* __restrict data = tex.data().data(); + for (std::size_t i = 0; i < size; i++) + array.set(i, data[i * 4 + 3] != 0); + return array; +} + +BitArrayView anim_atlas::bitmask() const +{ + return _bitmask; +} + } // namespace floormat diff --git a/src/anim-atlas.hpp b/src/anim-atlas.hpp index e3e7243d..ce0e6176 100644 --- a/src/anim-atlas.hpp +++ b/src/anim-atlas.hpp @@ -3,6 +3,8 @@ #include "scenery.hpp" #include "anim.hpp" #include <array> +#include <Corrade/Containers/BitArray.h> +#include <Corrade/Containers/BitArrayView.h> #include <Corrade/Containers/String.h> #include <Magnum/Math/Vector2.h> #include <Magnum/ImageView.h> @@ -31,11 +33,14 @@ struct anim_atlas final texcoords texcoords_for_frame(rotation r, std::size_t frame) const noexcept; quad frame_quad(const Vector3& center, rotation r, std::size_t frame) const noexcept; + BitArrayView bitmask() const; + fm_DECLARE_DELETED_COPY_ASSIGNMENT(anim_atlas); private: GL::Texture2D _tex; String _name; + BitArray _bitmask; anim_def _info; std::array<std::uint8_t, (std::size_t)rotation_COUNT> _group_indices = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, @@ -43,6 +48,7 @@ private: static decltype(_group_indices) make_group_indices(const anim_def& anim) noexcept; static std::uint8_t rotation_to_index(const anim_def& a, rotation r) noexcept; + static BitArray make_bit_array(const ImageView2D& tex); }; } // namespace floormat diff --git a/src/camera-offset.cpp b/src/camera-offset.cpp index b132cdab..335a9b65 100644 --- a/src/camera-offset.cpp +++ b/src/camera-offset.cpp @@ -11,7 +11,7 @@ with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, shor _offset{shader.camera_offset()} { constexpr auto chunk_size = TILE_MAX_DIM20d*dTILE_SIZE; - const auto offset = _offset + tile_shader::project(Vector3d(x, y, 0) * chunk_size); + const auto offset = _offset + tile_shader::project(Vector3d(x, y, 0) * chunk_size); _shader.set_camera_offset(offset); } |