aboutsummaryrefslogtreecommitdiff
path: root/src/client/item
diff options
context:
space:
mode:
authorNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 21:57:46 +1100
committerNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 21:57:46 +1100
commite4483eca01b48b943cd0461e24a74ae1a3139ed4 (patch)
treeed58c3c246e3af1af337697695d780aa31f6ad9a /src/client/item
parent1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (diff)
Update to most recent version (old initial commit)
Diffstat (limited to 'src/client/item')
-rw-r--r--src/client/item/block.cc91
-rw-r--r--src/client/item/block.hh58
-rw-r--r--src/client/item/item.cc1
-rw-r--r--src/client/item/item.hh30
-rw-r--r--src/client/item/items.cc16
-rw-r--r--src/client/item/items.hh17
6 files changed, 213 insertions, 0 deletions
diff --git a/src/client/item/block.cc b/src/client/item/block.cc
new file mode 100644
index 0000000..08f072b
--- /dev/null
+++ b/src/client/item/block.cc
@@ -0,0 +1,91 @@
+#include "client/item/block.hh"
+
+namespace client {
+namespace item {
+
+void block::regenerate_glo() noexcept {
+ const auto make_vbo = [](const auto& data) -> GLuint {
+ GLuint vbo = 0;
+ glGenBuffers(1, &vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+ glBufferData(GL_ARRAY_BUFFER,
+ std::size(data) * sizeof(client::world::block::glvert),
+ std::data(data), GL_STATIC_DRAW);
+ return vbo;
+ };
+ const auto make_vao = []() -> GLuint {
+ GLuint vao = 0;
+ glGenVertexArrays(1, &vao);
+ glBindVertexArray(vao);
+ // posiiton
+ glEnableVertexAttribArray(0);
+ glVertexAttribPointer(0, sizeof(glm::vec3) / sizeof(float), GL_FLOAT,
+ GL_FALSE, sizeof(client::world::block::glvert),
+ nullptr);
+ // texture
+ glEnableVertexAttribArray(1);
+ glVertexAttribPointer(1, sizeof(glm::vec3) / sizeof(float), GL_FLOAT,
+ GL_FALSE, sizeof(client::world::block::glvert),
+ reinterpret_cast<void*>(sizeof(glm::vec3)));
+ return vao;
+ };
+ std::vector<client::world::block::glvert> data;
+
+ const float tex_yoff = static_cast<float>(this->type) - 1.0f;
+ for (const auto& face :
+ client::world::block::get_glfaces(this->shared::item::block::type)) {
+
+ std::ranges::transform(face, std::back_inserter(data), [&](auto vert) {
+ vert.texture.z += tex_yoff * 6.0f;
+ return vert;
+ });
+ }
+
+ const auto vbo = make_vbo(data);
+ const auto vao = make_vao();
+ this->glo.emplace(vbo, vao, std::size(data));
+}
+
+void block::draw(const glm::vec2& pos, const glm::vec2& size) noexcept {
+ const auto make_matrix = [&]() -> glm::mat4 {
+ const glm::vec2 window = client::render::get_window_size();
+
+ constexpr auto identity = glm::mat4{1.0f};
+ const auto proj = glm::ortho(0.0f, window.x, 0.0f, window.y);
+ const auto scal =
+ glm::scale(identity, glm::vec3{size.x * 0.5f, size.y * 0.5f, 0.0f});
+ const auto tran =
+ glm::translate(identity, glm::vec3{pos.x + size.x / 2.0f,
+ pos.y + size.y / 2.0f, 0.0f});
+
+ // hack for shurbs, which are already offset and look bad when rotated
+ const auto rotey = client::world::block::get_draw_type(this->type) ==
+ client::world::block::draw_type::block
+ ? glm::rotate(identity, glm::radians(45.0f),
+ glm::vec3(0.0f, 1.0f, 0.0f))
+ : identity;
+ const auto rotex = glm::rotate(identity, glm::radians(30.0f),
+ glm::vec3(1.0f, 0.0f, 0.0f));
+ return proj * tran * scal * rotex * rotey;
+ };
+
+ static const client::render::program program{"res/shaders/face.vs",
+ "res/shaders/face.fs"};
+ static const GLint u_matrix = glGetUniformLocation(program, "_u_matrix");
+
+ if (!this->glo.has_value()) {
+ this->regenerate_glo();
+ }
+
+ glDisable(GL_BLEND);
+ glDisable(GL_DEPTH_TEST);
+ glUseProgram(program);
+ glBindVertexArray(this->glo->vao);
+
+ glUniformMatrix4fv(u_matrix, 1, GL_FALSE, glm::value_ptr(make_matrix()));
+
+ glDrawArrays(GL_TRIANGLES, 0, this->glo->elements);
+}
+
+} // namespace item
+} // namespace client
diff --git a/src/client/item/block.hh b/src/client/item/block.hh
new file mode 100644
index 0000000..f259aad
--- /dev/null
+++ b/src/client/item/block.hh
@@ -0,0 +1,58 @@
+#ifndef CLIENT_ITEM_BLOCK_HH_
+#define CLIENT_ITEM_BLOCK_HH_
+
+#include <algorithm>
+#include <optional>
+
+#include "client/item/item.hh"
+#include "client/render/render.hh"
+#include "client/world/block.hh"
+#include "shared/item/block.hh"
+#include "shared/item/item.hh"
+
+namespace client {
+namespace item {
+
+class block : virtual public shared::item::block, public client::item::item {
+private:
+ struct gl_object {
+ GLuint vbo;
+ GLuint vao;
+ unsigned long elements;
+
+ gl_object(const GLuint& vbo, const GLuint& vao,
+ const unsigned long& elements) noexcept
+ : vbo(vbo), vao(vao), elements(elements) {}
+ gl_object(const gl_object&) = delete;
+ gl_object(gl_object&&) = delete;
+ ~gl_object() noexcept {
+ glDeleteBuffers(1, &vbo);
+ glDeleteVertexArrays(1, &vao);
+ }
+ };
+
+private:
+ std::optional<gl_object> glo = std::nullopt;
+ void regenerate_glo() noexcept;
+
+public:
+ template <typename... Args>
+ block(const enum shared::world::block::type& type, Args&&... args) noexcept
+ : shared::item::block(type, args...), client::item::item(
+ std::forward<Args>(args)...) {
+ }
+ template <typename... Args>
+ block(const shared::item::item::type_t& type, Args&&... args) noexcept
+ : shared::item::item(type, args...), shared::item::block(type, args...),
+ client::item::item(type, std::forward<Args>(args)...) {}
+ virtual ~block() noexcept {}
+
+public:
+ virtual void draw(const glm::vec2& pos,
+ const glm::vec2& size) noexcept override;
+};
+
+} // namespace item
+} // namespace client
+
+#endif
diff --git a/src/client/item/item.cc b/src/client/item/item.cc
new file mode 100644
index 0000000..64cc1f5
--- /dev/null
+++ b/src/client/item/item.cc
@@ -0,0 +1 @@
+#include "client/item/item.hh"
diff --git a/src/client/item/item.hh b/src/client/item/item.hh
new file mode 100644
index 0000000..ff80369
--- /dev/null
+++ b/src/client/item/item.hh
@@ -0,0 +1,30 @@
+#ifndef CLIENT_ITEM_ITEM_HH_
+#define CLIENT_ITEM_ITEM_HH_
+
+#include <algorithm>
+
+#include <glm/glm.hpp>
+
+#include "shared/item/item.hh"
+
+namespace client {
+namespace item {
+
+// client::item is a renderiable shared::item
+class item : virtual public shared::item::item {
+public:
+public:
+ template <typename... Args>
+ item(Args&&... args) noexcept
+ : shared::item::item(std::forward<Args>(args)...) {}
+ virtual ~item() noexcept {}
+
+public:
+ // 2d drawing of items
+ virtual void draw(const glm::vec2& pos, const glm::vec2& size) noexcept = 0;
+};
+
+} // namespace item
+} // namespace client
+
+#endif
diff --git a/src/client/item/items.cc b/src/client/item/items.cc
new file mode 100644
index 0000000..9ff25fe
--- /dev/null
+++ b/src/client/item/items.cc
@@ -0,0 +1,16 @@
+#include "client/item/items.hh"
+
+namespace client {
+namespace item {
+
+shared::item::item_t make_item(const shared::item::item::type_t& type,
+ const std::uint32_t& quantity) noexcept {
+ if (type >= shared::item::block::type_offset) {
+ return std::make_shared<client::item::block>(type, quantity);
+ }
+ // TODO non-block items
+ return std::make_shared<client::item::block>(type, quantity);
+}
+
+} // namespace item
+} // namespace client
diff --git a/src/client/item/items.hh b/src/client/item/items.hh
new file mode 100644
index 0000000..53cebfa
--- /dev/null
+++ b/src/client/item/items.hh
@@ -0,0 +1,17 @@
+#ifndef CLIENT_ITEM_ITEMS_HH_
+#define CLIENT_ITEM_ITEMS_HH_
+
+#include "client/item/block.hh"
+#include "shared/item/item.hh"
+#include "shared/item/items.hh"
+
+namespace client {
+namespace item {
+
+shared::item::item_t make_item(const shared::item::item::type_t& type,
+ const std::uint32_t& quantity) noexcept;
+
+} // namespace item
+} // namespace client
+
+#endif