diff --git a/engine.xcodeproj/project.pbxproj b/engine.xcodeproj/project.pbxproj index 38d4695..edf8c7f 100644 --- a/engine.xcodeproj/project.pbxproj +++ b/engine.xcodeproj/project.pbxproj @@ -22,6 +22,8 @@ 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 */; }; + AD95AEC61E41EDEC002DD03A /* font.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AD95AEC31E41EDEC002DD03A /* font.cpp */; }; + AD95AEC71E41EDEC002DD03A /* pch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AD95AEC51E41EDEC002DD03A /* pch.cpp */; }; ADB61C821E3D38450093280F /* util.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ADB61C801E3D38450093280F /* util.cpp */; }; /* End PBXBuildFile section */ @@ -62,6 +64,9 @@ 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 = ""; }; + AD95AEC31E41EDEC002DD03A /* font.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = font.cpp; sourceTree = ""; }; + AD95AEC41E41EDEC002DD03A /* font.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = font.h; sourceTree = ""; }; + AD95AEC51E41EDEC002DD03A /* pch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = pch.cpp; sourceTree = ""; }; ADB61C801E3D38450093280F /* util.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = util.cpp; sourceTree = ""; }; ADB61C811E3D38450093280F /* util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = util.h; sourceTree = ""; }; /* End PBXFileReference section */ @@ -106,6 +111,9 @@ AD58E0511E107411006ACC15 /* engine */ = { isa = PBXGroup; children = ( + AD95AEC31E41EDEC002DD03A /* font.cpp */, + AD95AEC41E41EDEC002DD03A /* font.h */, + AD95AEC51E41EDEC002DD03A /* pch.cpp */, AD3B1EBE1E3B8B7600E918E3 /* layout.cpp */, AD3B1EBF1E3B8B7600E918E3 /* layout.h */, AD58E0631E2A76FD006ACC15 /* shader.cpp */, @@ -200,9 +208,11 @@ AD58E06B1E2A774F006ACC15 /* texture.cpp in Sources */, AD3B1EC01E3B8B7600E918E3 /* layout.cpp in Sources */, AD58E0721E2A90EF006ACC15 /* app.cpp in Sources */, + AD95AEC61E41EDEC002DD03A /* font.cpp in Sources */, AD58E0531E107411006ACC15 /* main.cpp in Sources */, AD58E0681E2A7741006ACC15 /* image.cpp in Sources */, AD58E0771E3421F2006ACC15 /* Yoga.c in Sources */, + AD95AEC71E41EDEC002DD03A /* pch.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/engine/app.cpp b/engine/app.cpp index e821674..c20daf8 100644 --- a/engine/app.cpp +++ b/engine/app.cpp @@ -55,14 +55,12 @@ void App::init() static const char* shader_font_v = "#version 150\n" "uniform mat4 mvp;" - "uniform vec2 texoff;" - "uniform vec2 texsz;" - "in vec4 pos;" + "in vec2 pos;" "in vec2 uvs;" "out vec2 uv;" "void main(){" - " uv = texoff + uvs * texsz;" - " gl_Position = mvp * vec4(pos.xyz, 1.f);" + " uv = uvs;" + " gl_Position = mvp * vec4(pos, 0, 1);" "}"; static const char* shader_font_f = "#version 150\n" @@ -70,7 +68,8 @@ void App::init() "in vec2 uv;" "out vec4 frag;" "void main(){" - " frag = texture(tex, uv.xy);" + //" frag = vec4(1,0,0,1);" + " frag = texture(tex, uv);" "}"; #ifdef _WIN32 @@ -107,21 +106,35 @@ void App::init() if (!tex.load("data/uvs.jpg")) printf("error loading image\n"); +#if _WIN32 FILE* font_file = fopen("C:\\Windows\\Fonts\\arial.ttf", "rb"); +#else + FILE* font_file = fopen("/Library/Fonts/Arial.ttf", "rb"); +#endif if (font_file) { fseek(font_file, 0, SEEK_END); long sz = ftell(font_file); auto data = std::make_unique(sz); fseek(font_file, 0, SEEK_SET); - int bytes = fread(data.get(), 1, sz, font_file); - int w = 512; - int h = 512; - int num_chars = 96; + auto bytes = fread(data.get(), 1, sz, font_file); + assert(bytes==sz); auto bitmap = std::make_unique(w*h); chars.resize(num_chars); - int ret = stbtt_BakeFontBitmap(data.get(), 0, 50, bitmap.get(), w, h, 32, num_chars, chars.data()); + int ret = stbtt_BakeFontBitmap(data.get(), 0, 50, bitmap.get(), w, h, start_char, num_chars, chars.data()); font_tex.create(w, h, GL_RED, bitmap.get()); + + glGenBuffers(2, font_buffers); + glGenVertexArrays(1, &font_array); + + glBindVertexArray(font_array); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, font_buffers[1]); + glBindBuffer(GL_ARRAY_BUFFER, font_buffers[0]); + 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)); + glBindVertexArray(0); } plane.create<1>(400, 400); @@ -159,6 +172,7 @@ void App::update(float dt) layout[main_id].update(width, height); } + /* glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_SCISSOR_TEST); for (auto& n : layout[main_id]) @@ -171,10 +185,65 @@ void App::update(float dt) } } glDisable(GL_SCISSOR_TEST); - +*/ glm::mat4 proj = glm::ortho(0.f, width, height, 0.f, -1.f, 1.f); glm::mat4 tran = glm::translate(glm::vec3(200, 200, 0)); + + if (chars.size()) + { + static char str[64]; + static int counter = 0; + counter++; + sprintf(str, "frame %04d", counter); + const auto len = strlen(str); + float x = 0; + float y = 0; + std::vector v; + std::vector idx; + for (int i = 0; i < len; i++) + { + int c = str[i] - start_char; + stbtt_aligned_quad q; + stbtt_GetBakedQuad(chars.data(), w, h, c, &x, &y, &q, true); + auto n = v.size(); + v.emplace_back(q.x0, q.y1, q.s0, q.t1); + v.emplace_back(q.x0, q.y0, q.s0, q.t0); + v.emplace_back(q.x1, q.y0, q.s1, q.t0); + v.emplace_back(q.x1, q.y1, q.s1, q.t1); + idx.push_back(n+0); + idx.push_back(n+1); + idx.push_back(n+2); + idx.push_back(n+0); + idx.push_back(n+2); + idx.push_back(n+3); + } + font_array_count = (int)idx.size(); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, font_buffers[1]); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx.size() * sizeof(GLushort), idx.data(), GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, font_buffers[0]); + glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(glm::vec4), v.data(), GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } + + glActiveTexture(GL_TEXTURE0); + font_tex.bind(); + sampler.bind(0); + ShaderManager::use(kShader::Font); + ShaderManager::u_int(kShaderUniform::Tex, 0); + ShaderManager::u_mat4(kShaderUniform::MVP, proj*tran); + +// plane.draw_fill(); + glBindVertexArray(font_array); + glDrawElements(GL_TRIANGLES, font_array_count, GL_UNSIGNED_SHORT, 0); + glBindVertexArray(0); + + font_tex.unbind(); + sampler.unbind(); + + +/* static int i = 0; i = (i + 1) % 32; auto c = chars["hgfrr56789opk0876ryuewighfcuisdbn"[i] - 32]; @@ -195,6 +264,7 @@ void App::update(float dt) //plane.draw_fill(); font_tex.unbind(); sampler.unbind(); + */ } void App::resize(float w, float h) diff --git a/engine/app.h b/engine/app.h index 580df95..3df4d0a 100644 --- a/engine/app.h +++ b/engine/app.h @@ -14,6 +14,13 @@ class App Texture2D font_tex; std::vector chars; Plane plane; + GLuint font_array; + int font_array_count = 0; + GLuint font_buffers[2]; + const int w = 512; + const int h = 512; + const int num_chars = 96; + const int start_char = 32; public: static App I; float width; diff --git a/engine/layout.cpp b/engine/layout.cpp index 3079201..396a74f 100644 --- a/engine/layout.cpp +++ b/engine/layout.cpp @@ -266,10 +266,10 @@ bool LayoutManager::load(const char* path) } printf("Parsing layout: %s\n", id_str); uint16_t id = const_hash(id_str); - auto& p = m_layouts.try_emplace(id); - if (p.second) + auto p = m_layouts.find(id); + if (p == m_layouts.end()) { - auto& node = p.first->second; + auto& node = m_layouts[id]; node.m_manager = this; // try to copy the old size values if (old.count(id)) diff --git a/engine/layout.h b/engine/layout.h index c90aee6..dcae025 100644 --- a/engine/layout.h +++ b/engine/layout.h @@ -87,7 +87,7 @@ public: ShaderManager::u_vec4(kShaderUniform::Col, m_border_color); m_plane.draw_stroke(); } - virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr) + virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr) override { switch (id) { @@ -114,6 +114,8 @@ public: case kAttribute::Thickness: m_thinkness = attr->FloatValue(); break; + default: + break; } } }; @@ -169,7 +171,7 @@ public: ShaderManager::u_vec4(kShaderUniform::Col, m_border_color); m_shape->draw_stroke(); } - virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr) + virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr) override { switch (id) { @@ -199,6 +201,8 @@ public: case kAttribute::Type: m_type = (kShapeType)const_hash(attr->Value()); break; + default: + break; } } }; diff --git a/engine/util.cpp b/engine/util.cpp index 1d94e5a..67f91e4 100644 --- a/engine/util.cpp +++ b/engine/util.cpp @@ -1,3 +1,3 @@ #include "pch.h" -#include "util.hpp" +#include "util.h" diff --git a/libs/glm b/libs/glm index f84437d..9f1aae0 160000 --- a/libs/glm +++ b/libs/glm @@ -1 +1 @@ -Subproject commit f84437dc56786b17eebebf1666bde9a93188fc3e +Subproject commit 9f1aae08f71d1a1939d3bb8dcb771722d836cf9f diff --git a/libs/stb/stb b/libs/stb/stb index 59a5a15..3e7f2d6 160000 --- a/libs/stb/stb +++ b/libs/stb/stb @@ -1 +1 @@ -Subproject commit 59a5a155b463c2adb8ce6d7eca348c097062430a +Subproject commit 3e7f2d6ebddfe54bd9a9491db1349513a357bfe5 diff --git a/libs/tinyxml2 b/libs/tinyxml2 index 1f5ab7c..5b733ff 160000 --- a/libs/tinyxml2 +++ b/libs/tinyxml2 @@ -1 +1 @@ -Subproject commit 1f5ab7cd0bbbf3bb9902813c99fb5ae84e495c18 +Subproject commit 5b733ff4810049d24085c87d989c1d06dcdf8f82 diff --git a/libs/yoga b/libs/yoga index c217553..8d74e01 160000 --- a/libs/yoga +++ b/libs/yoga @@ -1 +1 @@ -Subproject commit c217553cf8d12312bc4deeee6d373160314496fa +Subproject commit 8d74e01f41f062d732bdc4de34dddf213b0fb173