summaryrefslogtreecommitdiffhomepage
path: root/draw
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-11-07 10:29:56 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-11-07 10:29:56 +0100
commit672f67c61f275dea196d4c3fbc5ffe7405dc43d3 (patch)
tree22559814268d289245d26f363708678b02c396c9 /draw
parent411164f34aeea4987dae101cf92beb5dfa8c0cd9 (diff)
wip
Diffstat (limited to 'draw')
-rw-r--r--draw/anim.cpp25
-rw-r--r--draw/anim.hpp36
-rw-r--r--draw/wall.cpp3
-rw-r--r--draw/wall.hpp5
4 files changed, 68 insertions, 1 deletions
diff --git a/draw/anim.cpp b/draw/anim.cpp
new file mode 100644
index 00000000..0b69e4dd
--- /dev/null
+++ b/draw/anim.cpp
@@ -0,0 +1,25 @@
+#include "anim.hpp"
+#include "anim-atlas.hpp"
+#include "shaders/tile.hpp"
+
+namespace floormat {
+
+anim_mesh::anim_mesh() = default;
+
+std::array<UnsignedShort, 6> anim_mesh::make_index_array()
+{
+ using u16 = std::uint16_t;
+ return {{
+ (u16)0, (u16)1, (u16)2,
+ (u16)2, (u16)1, (u16)3,
+ }};
+}
+
+void anim_mesh::draw(local_coords xy, const anim_atlas& atlas, const anim_frame& frame)
+{
+ const auto center_ = Vector3(xy.x, xy.y, 0.f) * TILE_SIZE;
+ const auto pos = atlas.frame_quad(center_, frame);
+ _positions_buffer.setSubData(0, pos);
+}
+
+} // namespace floormat
diff --git a/draw/anim.hpp b/draw/anim.hpp
new file mode 100644
index 00000000..60a4e839
--- /dev/null
+++ b/draw/anim.hpp
@@ -0,0 +1,36 @@
+#pragma once
+
+#include "local-coords.hpp"
+#include <array>
+#include <Corrade/Containers/ArrayViewStl.h>
+#include <Magnum/Magnum.h>
+#include <Magnum/Math/Vector2.h>
+#include <Magnum/Math/Vector3.h>
+#include <Magnum/GL/Mesh.h>
+#include <Magnum/GL/Buffer.h>
+
+namespace floormat::Serialize { struct anim_atlas; struct anim_frame; }
+
+namespace floormat {
+
+struct anim_atlas;
+using anim_frame = Serialize::anim_frame;
+
+struct anim_mesh final
+{
+ anim_mesh();
+ void draw(local_coords pos, const anim_atlas& atlas, const anim_frame& frame);
+
+private:
+ struct vertex_data final { Vector2 texcoords; };
+ using quad_data = std::array<vertex_data, 4>;
+
+ static std::array<UnsignedShort, 6> make_index_array();
+
+
+ GL::Mesh _mesh;
+ GL::Buffer _vertex_buffer{quad_data{}, Magnum::GL::BufferUsage::DynamicDraw},
+ _index_buffer{make_index_array()}, _positions_buffer{quad_data{}};
+};
+
+} // namespace floormat
diff --git a/draw/wall.cpp b/draw/wall.cpp
index 18462e2c..42d62e49 100644
--- a/draw/wall.cpp
+++ b/draw/wall.cpp
@@ -3,6 +3,7 @@
#include "shaders/tile.hpp"
#include "chunk.hpp"
#include "tile-image.hpp"
+#include "anim-atlas.hpp"
#include <Magnum/GL/Texture.h>
#include <Magnum/GL/MeshView.h>
@@ -63,6 +64,8 @@ void wall_mesh::draw(tile_shader& shader, chunk& c)
tex->bind(0);
last_texture = tex;
shader.draw(mesh);
+ if (auto a = c[i].scenery())
+ _anim_mesh.draw(local_coords{i}, *a.atlas, a.atlas->frame(a.frame.r, a.frame.frame));
}
}
diff --git a/draw/wall.hpp b/draw/wall.hpp
index ad1eafad..81c6b60e 100644
--- a/draw/wall.hpp
+++ b/draw/wall.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "tile-defs.hpp"
+#include "anim.hpp"
#include <array>
#include <Corrade/Containers/ArrayViewStl.h>
#include <Magnum/Math/Vector2.h>
@@ -31,13 +32,15 @@ private:
using vertex_array = std::array<quad, COUNT>;
using texture_array = std::array<GL::Texture2D*, COUNT>;
+ anim_mesh _anim_mesh;
+
static void maybe_add_tile(vertex_array& data, texture_array& textures, tile_ref x, std::size_t pos);
static void add_wall(vertex_array& data, texture_array& textures, const tile_image_ref& img, std::size_t pos);
GL::Mesh _mesh;
GL::Buffer _vertex_buffer{vertex_array{}, Magnum::GL::BufferUsage::DynamicDraw},
_index_buffer{make_index_array()},
- _positions_buffer{make_position_array()};
+ _positions_buffer{make_position_array()};
static std::array<std::array<UnsignedShort, 6>, COUNT> make_index_array();
static std::array<std::array<Vector3, 4>, COUNT> make_position_array();
};