From 16c1b6481e0c2d9dc3facc0c3be59874b330372c Mon Sep 17 00:00:00 2001 From: omigamedev Date: Sun, 29 Jan 2017 17:42:23 +0000 Subject: [PATCH] started implementing dynamic widget allocation by xml tag --- data/layout.xml | 2 +- engine.xcodeproj/project.pbxproj | 6 ++++++ engine/app.cpp | 28 +++++++++++--------------- engine/layout.cpp | 34 ++++++++++++++++++++++++-------- engine/layout.h | 23 +++++++++++---------- engine/shader.cpp | 12 +++++------ engine/shader.hpp | 19 +++++++++++++++--- engine/shape.hpp | 5 +++-- engine/util.cpp | 3 +++ engine/util.hpp | 8 ++++++++ 10 files changed, 94 insertions(+), 46 deletions(-) create mode 100644 engine/util.cpp create mode 100644 engine/util.hpp diff --git a/data/layout.xml b/data/layout.xml index c3523d6..cc3cc77 100644 --- a/data/layout.xml +++ b/data/layout.xml @@ -34,7 +34,7 @@ - + diff --git a/engine.xcodeproj/project.pbxproj b/engine.xcodeproj/project.pbxproj index 21dae9c..f987223 100644 --- a/engine.xcodeproj/project.pbxproj +++ b/engine.xcodeproj/project.pbxproj @@ -22,6 +22,7 @@ AD58E0761E3421F2006ACC15 /* YGNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = AD58E0741E3421F2006ACC15 /* YGNodeList.c */; }; AD58E0771E3421F2006ACC15 /* Yoga.c in Sources */ = {isa = PBXBuildFile; fileRef = AD58E0751E3421F2006ACC15 /* Yoga.c */; }; AD58E0791E342205006ACC15 /* tinyxml2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AD58E0781E342205006ACC15 /* tinyxml2.cpp */; }; + ADB61C821E3D38450093280F /* util.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ADB61C801E3D38450093280F /* util.cpp */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -61,6 +62,8 @@ AD58E0741E3421F2006ACC15 /* YGNodeList.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = YGNodeList.c; path = libs/yoga/yoga/YGNodeList.c; sourceTree = ""; }; AD58E0751E3421F2006ACC15 /* Yoga.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = Yoga.c; path = libs/yoga/yoga/Yoga.c; sourceTree = ""; }; AD58E0781E342205006ACC15 /* tinyxml2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = tinyxml2.cpp; path = libs/tinyxml2/tinyxml2.cpp; sourceTree = ""; }; + ADB61C801E3D38450093280F /* util.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = util.cpp; sourceTree = ""; }; + ADB61C811E3D38450093280F /* util.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = util.hpp; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -116,6 +119,8 @@ AD58E0671E2A7741006ACC15 /* image.hpp */, AD58E0691E2A774F006ACC15 /* texture.cpp */, AD58E06A1E2A774F006ACC15 /* texture.hpp */, + ADB61C801E3D38450093280F /* util.cpp */, + ADB61C811E3D38450093280F /* util.hpp */, AD58E06C1E2A78BD006ACC15 /* pch.h */, ); path = engine; @@ -191,6 +196,7 @@ AD58E06F1E2A80BC006ACC15 /* shape.cpp in Sources */, AD58E0651E2A76FD006ACC15 /* shader.cpp in Sources */, AD58E0761E3421F2006ACC15 /* YGNodeList.c in Sources */, + ADB61C821E3D38450093280F /* util.cpp in Sources */, AD58E06B1E2A774F006ACC15 /* texture.cpp in Sources */, AD3B1EC01E3B8B7600E918E3 /* layout.cpp in Sources */, AD58E0721E2A90EF006ACC15 /* app.cpp in Sources */, diff --git a/engine/app.cpp b/engine/app.cpp index 7564576..e85beef 100644 --- a/engine/app.cpp +++ b/engine/app.cpp @@ -109,8 +109,6 @@ void App::init() void App::update(float dt) { - glm::mat4 proj = glm::ortho(0.f, width, height, 0.f, -1.f, 1.f); - glClearColor(.1f, .1f, .1f, 1.f); glViewport(0, 0, (GLsizei)width, (GLsizei)height); glClear(GL_COLOR_BUFFER_BIT); @@ -129,23 +127,21 @@ void App::update(float dt) glEnable(GL_SCISSOR_TEST); for (auto& n : layout) { - glm::mat4 pivot = glm::translate(glm::vec3(.5f, .5f, 0.f)); - glm::mat4 scale = glm::scale(glm::vec3(n.m_size, 1.f)); - glm::mat4 pos = glm::translate(glm::vec3(n.m_pos, 0)); - auto mvp = proj * pos * scale * pivot; - auto box = n.m_clip; glScissor(box.x, height - box.y - box.w, box.z, box.w); - shader_color.u_vec4("col", n.color); - shader_color.use(); - shader_color.u_mat4("mvp", mvp); - plane.draw_fill(); - - shader_color.u_vec4("col", { 1, 1, 1, 1 }); - shader_color.use(); - shader_color.u_mat4("mvp", mvp); - plane.draw_stroke(); + if (n.m_widget) + { + shader_color.u_vec4("col", n.color); + shader_color.use(); + shader_color.u_mat4("mvp", n.m_widget->mvp); + plane.draw_fill(); + + shader_color.u_vec4("col", { 1, 1, 1, 1 }); + shader_color.use(); + shader_color.u_mat4("mvp", n.m_widget->mvp); + plane.draw_stroke(); + } } glDisable(GL_SCISSOR_TEST); tex.unbind(); diff --git a/engine/layout.cpp b/engine/layout.cpp index 86c843f..40f2e7a 100644 --- a/engine/layout.cpp +++ b/engine/layout.cpp @@ -1,15 +1,19 @@ #include "pch.h" #include "layout.h" +#include "util.hpp" + +Plane* WidgetBorder::m_plane; void Node::update(float width, float height) { YGNodeStyleSetWidth(y_node, width); YGNodeStyleSetHeight(y_node, height); YGNodeCalculateLayout(y_node, YGUndefined, YGUndefined, YGDirectionLTR); - update_internal({ 0, 0 }); + glm::mat4 proj = glm::ortho(0.f, m_size.x, m_size.y, 0.f, -1.f, 1.f); + update_internal({ 0, 0 }, proj); } -void Node::update_internal(glm::vec2 origin) +void Node::update_internal(const glm::vec2& origin, const glm::mat4& proj) { float x = YGNodeLayoutGetLeft(y_node); float y = YGNodeLayoutGetTop(y_node); @@ -17,12 +21,17 @@ void Node::update_internal(glm::vec2 origin) float h = YGNodeLayoutGetHeight(y_node); m_pos = origin + glm::vec2(x, y); m_size = glm::vec2(w, h); - if (!parent) - m_clip = glm::vec4(m_pos, m_size); - else - m_clip = rect_intersection(glm::vec4(m_pos, m_size), parent->m_clip); + m_clip = !parent ? glm::vec4(m_pos, m_size) : rect_intersection(glm::vec4(m_pos, m_size), parent->m_clip); + if (m_widget) + { + glm::mat4 pivot = glm::translate(glm::vec3(.5f, .5f, 0.f)); + glm::mat4 scale = glm::scale(glm::vec3(m_size, 1.f)); + glm::mat4 pos = glm::translate(glm::vec3(m_pos, 0)); + m_widget->mvp = proj * pos * scale * pivot; + m_widget->clip = m_clip; + } for (auto& c : m_children) - c.update_internal(m_pos); + c.update_internal(m_pos, proj); } void Node::parse_attributes(att::kAttribute ka, const tinyxml2::XMLAttribute* attr) @@ -179,9 +188,18 @@ void Node::load_internal(const tinyxml2::XMLElement* x_node) { m_name = x_node->Name(); auto attr = x_node->FirstAttribute(); + + att::kWidget widget_id = (att::kWidget)const_hash(x_node->Name()); + switch (widget_id) + { + case att::kWidget::Border: + m_widget = std::make_unique(); + break; + } + while (attr) { - parse_attributes((att::kAttribute)att::const_hash(attr->Name()), attr); + parse_attributes((att::kAttribute)const_hash(attr->Name()), attr); attr = attr->Next(); } diff --git a/engine/layout.h b/engine/layout.h index ad4f21c..252ec9e 100644 --- a/engine/layout.h +++ b/engine/layout.h @@ -1,14 +1,9 @@ #pragma once +#include "shape.hpp" +#include "util.hpp" namespace att { - uint16_t constexpr const_hash(char const *input) - { - return *input ? - static_cast(*input) + 33 * const_hash(input + 1) : - 5381; - } - enum class kAttribute : uint16_t { Width = const_hash("width"), @@ -107,13 +102,20 @@ namespace att class Widget { - +public: + glm::mat4 mvp; + glm::vec4 clip; + virtual void draw() { }; }; class WidgetBorder : public Widget { public: - static class Plane* m_plane; + static Plane* m_plane; + virtual void draw() override + { + m_plane->draw_fill(); + } }; class Node @@ -186,11 +188,12 @@ public: } void update(float width, float height); - void update_internal(glm::vec2 origin); + void update_internal(const glm::vec2& origin, const glm::mat4& proj); void parse_attributes(att::kAttribute ka, const tinyxml2::XMLAttribute* attr); void load(const char* path); void load_internal(const tinyxml2::XMLElement* x_node); void reload(); + void draw(); class iterator { diff --git a/engine/shader.cpp b/engine/shader.cpp index a409082..1664d25 100644 --- a/engine/shader.cpp +++ b/engine/shader.cpp @@ -80,18 +80,18 @@ void Shader::use() { glUseProgram(prog); } -void Shader::u_vec4(std::string name, const glm::vec4& v) +void Shader::u_vec4(const char* name, const glm::vec4& v) { - auto loc = glGetUniformLocation(prog, name.c_str()); + auto loc = glGetUniformLocation(prog, name); glUniform4fv(loc, 1, glm::value_ptr(v)); } -void Shader::u_mat4(std::string name, const glm::mat4& m) +void Shader::u_mat4(const char* name, const glm::mat4& m) { - auto loc = glGetUniformLocation(prog, name.c_str()); + auto loc = glGetUniformLocation(prog, name); glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(m)); } -void Shader::u_int(std::string name, int i) +void Shader::u_int(const char* name, int i) { - auto loc = glGetUniformLocation(prog, name.c_str()); + auto loc = glGetUniformLocation(prog, name); glUniform1i(loc, i); } diff --git a/engine/shader.hpp b/engine/shader.hpp index 236d1d2..db82e11 100644 --- a/engine/shader.hpp +++ b/engine/shader.hpp @@ -1,4 +1,5 @@ #pragma once +#include "util.hpp" class Shader { @@ -6,7 +7,19 @@ class Shader public: bool create(std::string vertex, std::string fragment); void use(); - void u_vec4(std::string name, const glm::vec4& v); - void u_mat4(std::string name, const glm::mat4& m); - void u_int(std::string name, int i); + void u_vec4(const char* name, const glm::vec4& v); + void u_mat4(const char* name, const glm::mat4& m); + void u_int(const char* name, int i); +}; + +enum class kShader : uint16_t +{ + Color = const_hash("color"), +}; + +class ShaderManager +{ +public: + static void use(kShader id); + static void use(const char* name); }; diff --git a/engine/shape.hpp b/engine/shape.hpp index bb87e6b..8a04faa 100644 --- a/engine/shape.hpp +++ b/engine/shape.hpp @@ -1,5 +1,4 @@ #pragma once -#include "layout.h" class Shape { @@ -10,7 +9,7 @@ protected: GLvoid* ioff[2]{ 0 }; struct vertex_t { glm::vec4 pos; glm::vec2 uvs; }; public: - att::AttrubutesMap attribs; +// att::AttrubutesMap attribs; bool create_buffers(GLvoid* idx, GLvoid* vertices, int isize, int vsize); void draw_fill() const; void draw_stroke() const; @@ -57,6 +56,7 @@ public: create_impl(w, h, div, idx, vertices); return create_buffers(idx, vertices, sizeof(idx), sizeof(vertices)); } +/* bool create(att::Divisions divisions, att::Width w, att::Height h) { const int div = divisions.value; @@ -80,6 +80,7 @@ public: const auto d = get_attribute(att::Divisions(1)); return create(d, w, h); } + */ }; class RectShape : public Shape diff --git a/engine/util.cpp b/engine/util.cpp new file mode 100644 index 0000000..1d94e5a --- /dev/null +++ b/engine/util.cpp @@ -0,0 +1,3 @@ +#include "pch.h" +#include "util.hpp" + diff --git a/engine/util.hpp b/engine/util.hpp new file mode 100644 index 0000000..7b414d6 --- /dev/null +++ b/engine/util.hpp @@ -0,0 +1,8 @@ +#pragma once + +uint16_t constexpr const_hash(char const *input) +{ + return *input ? + static_cast(*input) + 33 * const_hash(input + 1) : + 5381; +}