From c34d1a1f44172d925891827310c16345d31c078b Mon Sep 17 00:00:00 2001 From: omigamedev Date: Wed, 15 Mar 2017 08:17:22 +0000 Subject: [PATCH] minor changes and added namespace to avoid conflicts when integrating with PanoPainter --- engine/app.cpp | 20 +++++++++++---- engine/app.h | 6 +++-- engine/asset.cpp | 3 ++- engine/font.cpp | 2 ++ engine/font.h | 1 + engine/image.cpp | 4 ++- engine/image.h | 4 +++ engine/layout.cpp | 5 +++- engine/layout.h | 62 +++++++++++++++++++++++++++------------------- engine/main.cpp | 2 ++ engine/shader.cpp | 2 ++ engine/shader.h | 4 +++ engine/shape.cpp | 8 ++++++ engine/shape.h | 4 +++ engine/texture.cpp | 19 +++++++++++--- engine/texture.h | 5 +++- 16 files changed, 112 insertions(+), 39 deletions(-) diff --git a/engine/app.cpp b/engine/app.cpp index faa966c..72acae1 100644 --- a/engine/app.cpp +++ b/engine/app.cpp @@ -1,6 +1,8 @@ #include "pch.h" #include "app.h" +using namespace ui; + App App::I; // singleton #ifdef __APPLE__ @@ -18,6 +20,13 @@ void App::create() height = 500; } +void App::clear() +{ + glClearColor(.1f, .1f, .1f, 1.f); + glViewport(0, 0, (GLsizei)width, (GLsizei)height); + glClear(GL_COLOR_BUFFER_BIT); +} + void App::initShaders() { static const char* shader_v = @@ -143,6 +152,7 @@ void App::initLayout() LOG("initializing layout updating after load"); layout[main_id]->update(width, height, zoom); LOG("initializing layout components"); + sidebar = layout[main_id]->find("sidebar"); if (auto* button = layout[main_id]->find("btn-close")) { button->on_click = [] { exit(0); }; @@ -264,11 +274,11 @@ void App::init() void App::update(float dt) { - glClearColor(.1f, .1f, .1f, 1.f); - glViewport(0, 0, (GLsizei)width, (GLsizei)height); - glClear(GL_COLOR_BUFFER_BIT); + //glClearColor(.1f, .1f, .1f, 1.f); + //glViewport(0, 0, (GLsizei)width, (GLsizei)height); + //glClear(GL_COLOR_BUFFER_BIT); - //layout.reload(); + layout.reload(); if (auto* main = layout[main_id]) main->update(width, height, zoom); @@ -295,7 +305,7 @@ void App::resize(float w, float h) width = w; height = h; if (auto* main = layout[main_id]) - main->update(w, h, zoom); + main->update(w , h, zoom); } void App::mouse_down(int button, float x, float y) diff --git a/engine/app.h b/engine/app.h index 7a739dd..ccc0a9a 100644 --- a/engine/app.h +++ b/engine/app.h @@ -8,6 +8,8 @@ class App { +public: + static App I; Sampler sampler; Texture2D tex; LayoutManager layout; @@ -17,9 +19,8 @@ class App NodePopupMenu* menu_file = nullptr; NodePopupMenu* menu_edit = nullptr; NodePopupMenu* menu_layers = nullptr; + NodeBorder* sidebar = nullptr; const uint16_t main_id = const_hash("main"); -public: - static App I; float width; float height; #ifdef __ANDROID__ @@ -32,6 +33,7 @@ public: void initAssets(); void initLayout(); void create(); + void clear(); void update(float dt); void resize(float w, float h); void mouse_down(int button, float x, float y); diff --git a/engine/asset.cpp b/engine/asset.cpp index c1eead5..ac66d05 100644 --- a/engine/asset.cpp +++ b/engine/asset.cpp @@ -51,7 +51,8 @@ void Asset::close() #ifdef __ANDROID__ AAsset_close(m_asset); #else - fclose(m_fp); + if (m_fp) + fclose(m_fp); if (m_data) delete m_data; m_data = nullptr; diff --git a/engine/font.cpp b/engine/font.cpp index 49b1664..b76c553 100644 --- a/engine/font.cpp +++ b/engine/font.cpp @@ -123,6 +123,8 @@ void TextMesh::draw() glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (GLvoid*)0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (GLvoid*)(sizeof(float) * 2)); glDrawElements(GL_TRIANGLES, font_array_count, GL_UNSIGNED_SHORT, 0); + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif // USE_VBO diff --git a/engine/font.h b/engine/font.h index 350f430..23f5855 100644 --- a/engine/font.h +++ b/engine/font.h @@ -1,6 +1,7 @@ #pragma once #include "texture.h" #include "util.h" +#include enum class kFont : uint16_t { diff --git a/engine/image.cpp b/engine/image.cpp index 50355d4..f63aa0f 100644 --- a/engine/image.cpp +++ b/engine/image.cpp @@ -4,9 +4,11 @@ #include +using namespace ui; + bool Image::load(std::string filename) { - //stbi_set_flip_vertically_on_load(true); + stbi_set_flip_vertically_on_load(false); Asset file; if (!(file.open(filename.c_str()) && file.read_all())) { diff --git a/engine/image.h b/engine/image.h index 8cad34e..3ffec44 100644 --- a/engine/image.h +++ b/engine/image.h @@ -1,5 +1,7 @@ #pragma once +namespace ui { + class Image { std::unique_ptr m_data; @@ -11,3 +13,5 @@ public: const uint8_t* data() const { return m_data.get(); } int size() const { return width * height * comp; } }; + +} diff --git a/engine/layout.cpp b/engine/layout.cpp index 8939c90..d8c3939 100644 --- a/engine/layout.cpp +++ b/engine/layout.cpp @@ -3,6 +3,8 @@ #include "util.h" #include "asset.h" +using namespace ui; + Plane NodeBorder::m_plane; Plane NodeImage::m_plane; Sampler NodeImage::m_sampler; @@ -137,8 +139,9 @@ void Node::update_internal(const glm::vec2& origin, const glm::mat4& proj) glm::mat4 pivot = glm::translate(glm::vec3(.5f, .5f, 0.f)); glm::mat4 scale = glm::scale(glm::vec3(m_size, 1.f)); + glm::mat4 prescale = glm::scale(glm::vec3(m_scale, 1.f)); glm::mat4 pos = glm::translate(glm::vec3(m_pos, 0)); - m_mvp = proj * pos * scale * pivot; + m_mvp = proj * pos * scale * pivot * prescale; m_proj = proj; for (int i = 0; i < m_children.size(); i++) diff --git a/engine/layout.h b/engine/layout.h index 303330e..66f3a62 100644 --- a/engine/layout.h +++ b/engine/layout.h @@ -4,6 +4,8 @@ #include "shader.h" #include "font.h" #include "asset.h" +#include +#include enum class kAttribute : uint16_t { @@ -106,7 +108,11 @@ public: std::function on_loaded; bool load(const char* path); bool reload(); - class Node* operator[](uint16_t id) { return m_layouts[id].get(); } + class Node* operator[](uint16_t id) + { + auto i = m_layouts.find(id); + return i == m_layouts.end() ? nullptr : i->second.get(); + } //Node& operator[](const char* ids) { return m_layouts[const_hash(ids)]; } }; @@ -128,6 +134,7 @@ public: bool m_destroyed = false; float m_zoom = 1.f; + glm::vec2 m_scale{ 1.f }; glm::vec2 m_pos; glm::vec2 m_size; glm::vec4 m_clip; @@ -195,6 +202,7 @@ public: void SetJustify(YGJustify value) { YGNodeStyleSetJustifyContent(y_node, value); } void SetAlign(YGAlign value) { YGNodeStyleSetAlignItems(y_node, value); } void SetPositioning(YGPositionType value) { YGNodeStyleSetPositionType(y_node, value); } + void SetAspectRatio(float ar) { YGNodeStyleSetAspectRatio(y_node, ar); } glm::vec4 rect_intersection(glm::vec4 a, glm::vec4 b) { @@ -287,7 +295,7 @@ public: class NodeBorder : public Node { public: - static Plane m_plane; + static ui::Plane m_plane; glm::vec4 m_color{ 0, 0, 0, 1 }; glm::vec4 m_border_color{ 1, 1, 1, 1 }; float m_thinkness{ 0 }; @@ -338,19 +346,20 @@ public: } virtual void draw() override { - ShaderManager::use(kShader::Color); - ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp); + using namespace ui; + ui::ShaderManager::use(kShader::Color); + ui::ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp); if (m_color.a != 1.f) glEnable(GL_BLEND); - ShaderManager::u_vec4(kShaderUniform::Col, m_color); + ui::ShaderManager::u_vec4(kShaderUniform::Col, m_color); m_plane.draw_fill(); if (m_thinkness > 0) { glLineWidth(m_thinkness); - ShaderManager::u_vec4(kShaderUniform::Col, m_border_color); + ui::ShaderManager::u_vec4(kShaderUniform::Col, m_border_color); m_plane.draw_stroke(); } @@ -417,12 +426,13 @@ public: } virtual void draw() override { + using namespace ui; glm::mat4 pos = glm::translate(glm::vec3(glm::floor(m_pos), 0)); m_mvp = m_proj * pos; - ShaderManager::use(kShader::Font); - ShaderManager::u_int(kShaderUniform::Tex, 0); - ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp); - ShaderManager::u_vec4(kShaderUniform::Col, m_color); + ui::ShaderManager::use(kShader::Font); + ui::ShaderManager::u_int(kShaderUniform::Tex, 0); + ui::ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp); + ui::ShaderManager::u_vec4(kShaderUniform::Col, m_color); glEnable(GL_BLEND); m_text_mesh.draw(); glDisable(GL_BLEND); @@ -432,9 +442,9 @@ public: class NodeImage : public Node { public: - static Plane m_plane; + static ui::Plane m_plane; static Sampler m_sampler; - bool m_use_atlas; + bool m_use_atlas = false; glm::vec4 m_region; glm::vec2 m_off; glm::vec2 m_sz; @@ -459,7 +469,7 @@ public: } virtual void create() override { - if (TextureManager::load(m_path.c_str())) + if (!m_path.empty() && TextureManager::load(m_path.c_str())) { auto tex_sz = TextureManager::get(m_tex_id).size(); m_off = m_region.xy / tex_sz; @@ -492,21 +502,22 @@ public: } virtual void draw() override { + using namespace ui; TextureManager::get(m_tex_id).bind(); m_sampler.bind(0); glEnable(GL_BLEND); if (m_use_atlas) { - ShaderManager::use(kShader::Atlas); - ShaderManager::u_vec2(kShaderUniform::Tof, m_off); - ShaderManager::u_vec2(kShaderUniform::Tsz, m_sz); + ui::ShaderManager::use(kShader::Atlas); + ui::ShaderManager::u_vec2(kShaderUniform::Tof, m_off); + ui::ShaderManager::u_vec2(kShaderUniform::Tsz, m_sz); } else { - ShaderManager::use(kShader::Texture); + ui::ShaderManager::use(kShader::Texture); } - ShaderManager::u_int(kShaderUniform::Tex, 0); - ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp); + ui::ShaderManager::u_int(kShaderUniform::Tex, 0); + ui::ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp); m_plane.draw_fill(); m_sampler.unbind(); TextureManager::get(m_tex_id).unbind(); @@ -787,7 +798,7 @@ public: class NodeViewport : public Node { public: - std::unique_ptr m_faces; + std::unique_ptr m_faces; std::unique_ptr m_sampler; uint16_t m_tex_id; glm::vec2 drag_start; @@ -798,7 +809,8 @@ public: virtual void draw() override { - glm::mat4 cam = glm::lookAt(glm::vec3(sinf(angle)*10, 0, -10), glm::vec3(0, 0, 0), glm::vec3(0, -1, 0)); + using namespace ui; + glm::mat4 cam = glm::lookAt(glm::vec3(sinf(angle) * 10, 0, -10), glm::vec3(0, 0, 0), glm::vec3(0, -1, 0)); glm::mat4 proj = glm::perspective(glm::radians(45.f), m_clip.z / m_clip.w, .1f, 100); GLint vp[4]; @@ -814,9 +826,9 @@ public: TextureManager::get(m_tex_id).bind(); m_sampler->bind(0); glEnable(GL_BLEND); - ShaderManager::use(kShader::Texture); - ShaderManager::u_int(kShaderUniform::Tex, 0); - ShaderManager::u_mat4(kShaderUniform::MVP, proj * cam); + ui::ShaderManager::use(kShader::Texture); + ui::ShaderManager::u_int(kShaderUniform::Tex, 0); + ui::ShaderManager::u_mat4(kShaderUniform::MVP, proj * cam); m_faces->draw_fill(); m_sampler->unbind(); TextureManager::get(m_tex_id).unbind(); @@ -831,7 +843,7 @@ public: } virtual void create() override { - m_faces = std::make_unique(); + m_faces = std::make_unique(); m_faces->create<1>(10, 10); m_sampler = std::make_unique(); m_sampler->create(); diff --git a/engine/main.cpp b/engine/main.cpp index 15610ef..a719f60 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -427,6 +427,7 @@ int main() float dt = (float)(t1 - t0) / 1000.0f; if (dt > 1.0f / 60.0f) { + App::I.clear(); App::I.update((float)(t1 - t0) / 1000.0f); t0 = t1; SwapBuffers(hDC); @@ -452,6 +453,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) break; case WM_SIZE: App::I.resize((float)LOWORD(lp), (float)HIWORD(lp)); + App::I.clear(); App::I.update(0.f); SwapBuffers(hDC); break; diff --git a/engine/shader.cpp b/engine/shader.cpp index 02b85f5..ebaf5c4 100644 --- a/engine/shader.cpp +++ b/engine/shader.cpp @@ -1,6 +1,8 @@ #include "pch.h" #include "shader.h" +using namespace ui; + std::map ShaderManager::m_shaders; Shader* ShaderManager::m_current; diff --git a/engine/shader.h b/engine/shader.h index 257ae81..5bddf97 100644 --- a/engine/shader.h +++ b/engine/shader.h @@ -1,6 +1,8 @@ #pragma once #include "util.h" +namespace ui { + enum class kShaderUniform : uint16_t { MVP = const_hash("mvp"), @@ -45,3 +47,5 @@ public: static void u_mat4(kShaderUniform id, const glm::mat4& m); static void u_int(kShaderUniform id, int i); }; + +} \ No newline at end of file diff --git a/engine/shape.cpp b/engine/shape.cpp index 678b09b..e92d3ed 100644 --- a/engine/shape.cpp +++ b/engine/shape.cpp @@ -1,6 +1,8 @@ #include "pch.h" #include "shape.h" +using namespace ui; + bool Shape::create_buffers(GLvoid* idx, GLvoid* vertices, int isize, int vsize) { glGenBuffers(2, buffers); @@ -46,6 +48,8 @@ void Shape::draw_fill() const glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs)); glDrawElements(GL_TRIANGLES, count[0], GL_UNSIGNED_SHORT, ioff[0]); + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif // USE_VBO @@ -64,6 +68,10 @@ void Shape::draw_stroke() const glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs)); glDrawElements(GL_LINES, count[1], GL_UNSIGNED_SHORT, ioff[1]); + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); #endif // USE_VBO } diff --git a/engine/shape.h b/engine/shape.h index 2fd7674..453b2d8 100644 --- a/engine/shape.h +++ b/engine/shape.h @@ -1,5 +1,7 @@ #pragma once +namespace ui { + class Shape { protected: @@ -149,3 +151,5 @@ public: return create_buffers(idx, vertices, sizeof(idx), sizeof(vertices)); } }; + +} diff --git a/engine/texture.cpp b/engine/texture.cpp index f8fe33b..f30dc2f 100644 --- a/engine/texture.cpp +++ b/engine/texture.cpp @@ -1,6 +1,5 @@ #include "pch.h" #include "texture.h" -#include "image.h" #include "util.h" std::map TextureManager::m_textures; @@ -15,6 +14,11 @@ bool TextureManager::load(const char* path) return true; } +void TextureManager::assign(uint16_t id, GLuint tex, int w/* = -1*/, int h/* = -1*/, GLuint format/* = GL_RGBA*/) +{ + m_textures[id].assign(tex, w, h, format); +} + Texture2D& TextureManager::get(uint16_t id) { return m_textures[id]; @@ -31,14 +35,23 @@ bool Texture2D::create(int width, int height, GLint format, const uint8_t* data) unbind(); return true; } -bool Texture2D::create(const Image& img) +bool Texture2D::create(const ui::Image& img) { static GLint formats[] = { 0, 0, GL_RGB, GL_RGBA }; return create(img.width, img.height, formats[img.comp - 1], img.data()); } + +void Texture2D::assign(GLuint tex, int w/* = -1*/, int h/* = -1*/, GLuint format/* = GL_RGBA*/) +{ + m_tex = tex; + m_width = w; + m_height = h; + m_format = format; +} + bool Texture2D::load(std::string filename) { - Image img; + ui::Image img; if (!img.load(filename)) return false; return create(img); diff --git a/engine/texture.h b/engine/texture.h index fa119ad..ee1b80e 100644 --- a/engine/texture.h +++ b/engine/texture.h @@ -1,4 +1,5 @@ #pragma once +#include "image.h" class Texture2D { @@ -8,7 +9,8 @@ class Texture2D GLint m_format; public: bool create(int width, int height, GLint format = GL_RGBA, const uint8_t* data = nullptr); - bool create(const class Image& img); + bool create(const ui::Image& img); + void assign(GLuint tex, int w = -1, int h = -1, GLuint format = GL_RGBA); bool load(std::string filename); void destroy() { glDeleteTextures(1, &m_tex); } void bind() const { glBindTexture(GL_TEXTURE_2D, m_tex); } @@ -35,5 +37,6 @@ class TextureManager public: static std::map m_textures; static bool load(const char* path); + static void assign(uint16_t id, GLuint tex, int w = -1, int h = -1, GLuint format = GL_RGBA); static Texture2D& get(uint16_t id); };