diff --git a/ADVect/ADVect.cpp b/ADVect/ADVect.cpp index b645ee7..51ce492 100644 --- a/ADVect/ADVect.cpp +++ b/ADVect/ADVect.cpp @@ -22,8 +22,8 @@ namespace { SDL_Window* window; - u16 window_width = 1280; - u16 window_height = 720; + u16 window_width = 1920; + u16 window_height = 1080; u64 init_time, last_time, current_time, delta_t; bool running = false, rolling = true; @@ -37,7 +37,7 @@ namespace { ADVect::MarkupTextTransitionTrack m_text{ .current{}, - .pos_x = 280, .pos_y = 90, .w = 800, .h = 500, + .pos_x = 280, .pos_y = 90, .w = 1500, .h = 500, .opacity = 1.0f, .fill = 0xFFFFFFFF }; ADVect::TextTrack speaker = { @@ -79,7 +79,7 @@ namespace ADVect { } bgfx::Init bgfxInit; - bgfxInit.type = bgfx::RendererType::Count; // Automatically choose a renderer. + bgfxInit.type = bgfx::RendererType::OpenGL; // Automatically choose a renderer. bgfxInit.resolution.width = window_width; bgfxInit.resolution.height = window_height; bgfxInit.resolution.reset = BGFX_RESET_VSYNC; @@ -244,7 +244,7 @@ namespace ADVect { } int main(int argc, char* argv[]) { - std::filesystem::current_path("E:\\Archive\\Projects\\NouVeL\\ADVect\\runtime"); + std::filesystem::current_path("../../ADVect/runtime"); //std::filesystem::current_path("/Users/lachrymal/Projects/NouVeL/ADVect/runtime/"); NVL::Environment::ENVIRONMENT.enter({ { @@ -294,25 +294,25 @@ int main(int argc, char* argv[]) { } }); - //std::vector scenes = NVL::Parse::ParseFile("dialogue.nvl"); - //if (scenes.empty()) { - // std::cerr << "Main: Empty NVL parse, check file" << std::endl; - // return EXIT_FAILURE; - //} + std::vector scenes = NVL::Parse::ParseFile("dialogue.nvl"); + if (scenes.empty()) { + std::cerr << "Main: Empty NVL parse, check file" << std::endl; + return EXIT_FAILURE; + } - //auto a = NVL::Compiler::Compile(scenes, 0); - //auto b = NVL::Compiler::Serialize(a); + auto e = NVL::Compiler::Compile(scenes, 0); + // auto b = NVL::Compiler::Serialize(a); - std::ifstream f; - f.open("mmoker.nvlb", std::ios::binary); + // std::ifstream f; + // f.open("mmoker.nvlb", std::ios::binary); - std::stringstream c; - c << f.rdbuf(); - f.close(); + // std::stringstream c; + // c << f.rdbuf(); + // f.close(); - std::istringstream d(c.str()); + // std::istringstream d(c.str()); - auto e = NVL::Compiler::Deserialize(d); + // auto e = NVL::Compiler::Deserialize(d); if (!ADVect::Init("ADV Test", e)) return EXIT_FAILURE; ADVect::Run(); diff --git a/ADVect/Graphics.cpp b/ADVect/Graphics.cpp index d0c2304..18cb733 100644 --- a/ADVect/Graphics.cpp +++ b/ADVect/Graphics.cpp @@ -1,11 +1,8 @@ #include "Graphics.h" -#include -#include FT_FREETYPE_H #include #include -#include #include #define STB_IMAGE_IMPLEMENTATION @@ -20,7 +17,7 @@ namespace { switch (bgfx::getRendererType()) { case bgfx::RendererType::Noop: - case bgfx::RendererType::Direct3D9: shaderPath = "shaders/dx9/"; break; + // case bgfx::RendererType::Direct3D9: shaderPath = "shaders/dx9/"; break; case bgfx::RendererType::Direct3D11: case bgfx::RendererType::Direct3D12: shaderPath = "shaders/dx11/"; break; case bgfx::RendererType::Gnm: shaderPath = "shaders/pssl/"; break; @@ -71,53 +68,6 @@ namespace { FT_Library library; FT_Error error; - struct Font { - FT_Face face = nullptr; - std::unordered_map, u32, u32, i32, i32, i32, i32>> cache{}; - - std::tuple, u32, u32, i32, i32, i32, i32>& get_char(NVL::Char c) { - if (cache.find(c) == cache.end()) { - FT_GlyphSlot slot = face->glyph; - - error = FT_Load_Char(face, c, FT_LOAD_RENDER); - if (error) { - std::cerr << "ADV: Failed to load character" << std::endl; - } - - if (slot->bitmap.width != 0) { - const bgfx::Memory* buf = bgfx::copy(slot->bitmap.buffer, slot->bitmap.pitch * slot->bitmap.rows); - - cache.emplace(c, std::make_tuple( - bgfx::createTexture2D( - slot->bitmap.width, - slot->bitmap.rows, - false, - 1, - bgfx::TextureFormat::A8, - BGFX_TEXTURE_NONE | BGFX_SAMPLER_UVW_CLAMP, - buf - ), - slot->bitmap.width, - slot->bitmap.rows, - slot->bitmap_left, - slot->bitmap_top, - slot->advance.x, - slot->advance.y - )); - } - else cache.emplace(c, std::make_tuple( - std::nullopt, - 0, - 0, - 0, - 0, - slot->advance.x, - slot->advance.y)); - } - return cache[c]; - } - }; - std::optional get_font(const char* path) { Font f; @@ -188,17 +138,6 @@ namespace { bgfx::VertexLayout pcvDecl; std::unordered_map imgs; - - inline Font* ResolveStyleFlags(u32 style_flags) { - Font* f = ®ular; - switch (style_flags) { - case TEXT_NONE: break; - case TEXT_BOLD: f = &bold; break; - case TEXT_ITALIC: break; - case TEXT_ITALIC | TEXT_BOLD: break; - } - return f; - } inline void HandleMarkupFlags(const NVL::Environment::MarkupSegment& s, u32& style_flags, const NVL::String*& ruby) { for (const auto& e : s.efs) { @@ -216,6 +155,59 @@ namespace { } namespace ADVect::Graphics { + std::tuple, u32, u32, i32, i32, i32, i32>& Font::get_char(NVL::Char c) { + if (cache.find(c) == cache.end()) { + FT_GlyphSlot slot = face->glyph; + + error = FT_Load_Char(face, c, FT_LOAD_RENDER); + if (error) { + std::cerr << "ADV: Failed to load character" << std::endl; + } + + if (slot->bitmap.width != 0) { + const bgfx::Memory* buf = bgfx::copy(slot->bitmap.buffer, slot->bitmap.pitch * slot->bitmap.rows); + + cache.emplace(c, std::make_tuple( + bgfx::createTexture2D( + slot->bitmap.width, + slot->bitmap.rows, + false, + 1, + bgfx::TextureFormat::A8, + BGFX_TEXTURE_NONE | BGFX_SAMPLER_UVW_CLAMP, + buf + ), + slot->bitmap.width, + slot->bitmap.rows, + slot->bitmap_left, + slot->bitmap_top, + slot->advance.x, + slot->advance.y + )); + } + else cache.emplace(c, std::make_tuple( + std::nullopt, + 0, + 0, + 0, + 0, + slot->advance.x, + slot->advance.y)); + } + return cache[c]; + } + + Font* ResolveStyleFlags(u32 style_flags) { + Font* f = ®ular; + switch (style_flags) { + case TEXT_NONE: break; + case TEXT_BOLD: f = &bold; break; + case TEXT_ITALIC: break; + case TEXT_ITALIC | TEXT_BOLD: break; + } + return f; + } + bool Init(u16 width, u16 height) { error = FT_Init_FreeType(&library); if (error) { @@ -312,36 +304,6 @@ namespace ADVect::Graphics { DrawTexture(tex, pos_x, pos_y, w, h, (BGFX_STATE_DEFAULT | BGFX_STATE_BLEND_ALPHA) & ~(BGFX_STATE_WRITE_Z | BGFX_STATE_DEPTH_TEST_LESS), a_program); } - template - void RenderGlyph(NVL::Char c, std::conditional_t pos_x, std::conditional_t pos_y, u32 col, u32 style_flags) { - Font* f = ResolveStyleFlags(style_flags); - - auto& [tx, w, h, l, t, x, y] = f->get_char(c); - - pos_x += l; - - if (auto& buf = tx; DoRender && tx && w != 0) { - DrawTextureStencilAlpha(*buf, pos_x, pos_y - (h - t), w, h); - } - - pos_x += x >> 6; - pos_y += y >> 6; - } - - template - void RenderString(const NVL::String& s, std::conditional_t pos_x, std::conditional_t pos_y, u32 col, u32 style_flags) { - for (const auto& c : s) { - RenderGlyph(c, pos_x, pos_y, col, style_flags); - } - } - - template - void RenderString(NVL::String::const_iterator cbegin, NVL::String::const_iterator cend, std::conditional_t pos_x, std::conditional_t pos_y, u32 col, u32 style_flags) { - while (cbegin < cend) { - RenderGlyph(*cbegin++, pos_x, pos_y, col, style_flags); - } - } - void RenderStringCentered(const NVL::String& s, i32 pos_x, i32 pos_y, u32 col, u32 style_flags = TEXT_NONE) { i32 copy_x = 0, copy_y = 0; RenderString(s, copy_x, copy_y, col, style_flags); @@ -358,27 +320,27 @@ namespace ADVect::Graphics { NVL::String::const_iterator last_copy = last; i32 copy_x = pos_x, copy_y = pos_y; while (last != std::min(pos, halt)) { - RenderGlyph(*last++, pos_x, pos_y, col, style_flags); + RenderGlyph(*last++, pos_x, pos_y, col, style_flags); if (pos_x - reset_x > w) { pos_x = reset_x; pos_y -= f->face->size->metrics.height / 64; } if (last >= halt) { - RenderString(last_copy, pos, copy_x, copy_y, col, style_flags); + RenderString(last_copy, pos, copy_x, copy_y, col, style_flags); return copy_x; }; } } else while (last != end) { // Loop through all segments i32 copy_x = pos_x, old_x = pos_x, copy_y = pos_y; - RenderString(last, pos, copy_x, copy_y, col, style_flags); + RenderString(last, pos, copy_x, copy_y, col, style_flags); if (copy_x - reset_x > w) { // if walk exceeded max w copy_x = reset_x, copy_y = pos_y; // try walk again from leftmost of box, see if a soft break works; reset y from last walk - RenderString(last, pos, copy_x, copy_y, col, style_flags); + RenderString(last, pos, copy_x, copy_y, col, style_flags); if (copy_x - reset_x > w) { // soft break won't work, go back to old x and hard break pos_x = old_x; while (last != pos) { - RenderGlyph(*last++, pos_x, pos_y, col, style_flags); + RenderGlyph(*last++, pos_x, pos_y, col, style_flags); if (pos_x - reset_x > w) { pos_x = reset_x; pos_y -= f->face->size->metrics.height / 64; @@ -390,12 +352,12 @@ namespace ADVect::Graphics { pos_x = reset_x; pos_y -= f->face->size->metrics.height / 64; last++; - RenderString(last, std::min(pos, halt), pos_x, pos_y, col, style_flags); + RenderString(last, std::min(pos, halt), pos_x, pos_y, col, style_flags); if (last >= halt) return copy_x; } } else { - RenderString(last, std::min(pos, halt), pos_x, pos_y, col, style_flags); + RenderString(last, std::min(pos, halt), pos_x, pos_y, col, style_flags); if (last >= halt) return copy_x; } diff --git a/ADVect/Graphics.h b/ADVect/Graphics.h index 349683c..1d04f76 100644 --- a/ADVect/Graphics.h +++ b/ADVect/Graphics.h @@ -2,8 +2,12 @@ #include +#include #include +#include +#include FT_FREETYPE_H + #include #include @@ -27,18 +31,50 @@ namespace ADVect::Graphics { }; ImageTexture* GetImageTextureFromFile(const std::string& file); + struct Font { + FT_Face face = nullptr; + std::unordered_map, u32, u32, i32, i32, i32, i32>> cache{}; + + std::tuple, u32, u32, i32, i32, i32, i32>& get_char(NVL::Char c); + }; + void DrawTexture(const bgfx::TextureHandle& tex, i32 pos_x, i32 pos_y, u32 w, u32 h, u64 state, const bgfx::ProgramHandle& pg); void DrawTextureImage(const bgfx::TextureHandle& tex, i32 pos_x, i32 pos_y, u32 w, u32 h); void DrawTextureImage(const ImageTexture& img, i32 pos_x, i32 pos_y); void DrawTextureImageAlpha(const ImageTexture& img, i32 pos_x, i32 pos_y, f32 alpha); void DrawTextureStencilAlpha(const bgfx::TextureHandle& tex, i32 pos_x, i32 pos_y, u32 w, u32 h); - template - void RenderGlyph(NVL::Char c, std::conditional_t pos_x, std::conditional_t pos_y, u32 col, u32 style_flags = TEXT_NONE); - template - void RenderString(const NVL::String& s, std::conditional_t pos_x, std::conditional_t pos_y, u32 col, u32 style_flags = TEXT_NONE); - template - void RenderString(NVL::String::const_iterator cbegin, NVL::String::const_iterator cend, std::conditional_t pos_x, std::conditional_t pos_y, u32 col, u32 style_flags = TEXT_NONE); + Font* ResolveStyleFlags(u32 style_flags); + + template + void RenderGlyph(NVL::Char c, std::conditional_t pos_x, std::conditional_t pos_y, u32 col, u32 style_flags) { + Font* f = ResolveStyleFlags(style_flags); + + auto& [tx, w, h, l, t, x, y] = f->get_char(c); + + pos_x += l; + + if (auto& buf = tx; DoRender && tx && w != 0) { + DrawTextureStencilAlpha(*buf, pos_x, pos_y - (h - t), w, h); + } + + pos_x += x >> 6; + pos_y += y >> 6; + } + + template + void RenderString(const NVL::String& s, std::conditional_t pos_x, std::conditional_t pos_y, u32 col, u32 style_flags) { + for (const auto& c : s) { + RenderGlyph(c, pos_x, pos_y, col, style_flags); + } + } + + template + void RenderString(NVL::String::const_iterator cbegin, NVL::String::const_iterator cend, std::conditional_t pos_x, std::conditional_t pos_y, u32 col, u32 style_flags) { + while (cbegin < cend) { + RenderGlyph(*cbegin++, pos_x, pos_y, col, style_flags); + } + } i32 RenderSubstringBox(const NVL::String& s, i32& pos_x, i32& pos_y, i32 reset_x, u32 w, u32 col, u32 style_flags = TEXT_NONE, size_t s_end = NVL::String::npos); void RenderSubstringMarkupBox(const NVL::Environment::MarkupString& ms, i32 pos_x, i32 pos_y, u32 w, u32 col, size_t end = NVL::String::npos); diff --git a/ADVect/runtime/grad.png b/ADVect/runtime/grad.png index c3b034f..87d575a 100644 Binary files a/ADVect/runtime/grad.png and b/ADVect/runtime/grad.png differ diff --git a/ADVect/runtime/image.png b/ADVect/runtime/image.png index 86aa2c7..b7b0d05 100644 Binary files a/ADVect/runtime/image.png and b/ADVect/runtime/image.png differ diff --git a/ADVect/runtime/image2.jpg b/ADVect/runtime/image2.jpg index c99637b..86949e8 100644 Binary files a/ADVect/runtime/image2.jpg and b/ADVect/runtime/image2.jpg differ diff --git a/ADVect/runtime/image3.png b/ADVect/runtime/image3.png index 27727d2..0cfe790 100644 Binary files a/ADVect/runtime/image3.png and b/ADVect/runtime/image3.png differ diff --git a/ADVect/runtime/shaders/glsl/AlphaStencil.vert.bin b/ADVect/runtime/shaders/glsl/AlphaStencil.vert.bin index 609ed0d..a92af7e 100644 Binary files a/ADVect/runtime/shaders/glsl/AlphaStencil.vert.bin and b/ADVect/runtime/shaders/glsl/AlphaStencil.vert.bin differ diff --git a/ADVect/runtime/shaders/glsl/ImageAlpha.vert.bin b/ADVect/runtime/shaders/glsl/ImageAlpha.vert.bin index 609ed0d..a92af7e 100644 Binary files a/ADVect/runtime/shaders/glsl/ImageAlpha.vert.bin and b/ADVect/runtime/shaders/glsl/ImageAlpha.vert.bin differ diff --git a/ADVect/runtime/shaders/spirv/AlphaStencil.frag.bin b/ADVect/runtime/shaders/spirv/AlphaStencil.frag.bin index 8024bbb..3f498cc 100644 Binary files a/ADVect/runtime/shaders/spirv/AlphaStencil.frag.bin and b/ADVect/runtime/shaders/spirv/AlphaStencil.frag.bin differ diff --git a/ADVect/runtime/shaders/spirv/AlphaStencil.vert.bin b/ADVect/runtime/shaders/spirv/AlphaStencil.vert.bin index 2e64c84..2b627b3 100644 Binary files a/ADVect/runtime/shaders/spirv/AlphaStencil.vert.bin and b/ADVect/runtime/shaders/spirv/AlphaStencil.vert.bin differ diff --git a/ADVect/runtime/shaders/spirv/ImageAlpha.frag.bin b/ADVect/runtime/shaders/spirv/ImageAlpha.frag.bin index 25937c0..e7d302e 100644 Binary files a/ADVect/runtime/shaders/spirv/ImageAlpha.frag.bin and b/ADVect/runtime/shaders/spirv/ImageAlpha.frag.bin differ diff --git a/ADVect/runtime/shaders/spirv/ImageAlpha.vert.bin b/ADVect/runtime/shaders/spirv/ImageAlpha.vert.bin index 2e64c84..2b627b3 100644 Binary files a/ADVect/runtime/shaders/spirv/ImageAlpha.vert.bin and b/ADVect/runtime/shaders/spirv/ImageAlpha.vert.bin differ diff --git a/ADVect/shader.py b/ADVect/shader.py index 09db318..68f3fde 100644 --- a/ADVect/shader.py +++ b/ADVect/shader.py @@ -1,17 +1,14 @@ import subprocess, os -#SHADERC = "..\\out\\install\\x64-Debug\\bin\\shaderc" -SHADERC = "/Users/lachrymal/Projects/NouVeL/build/ADVect/ext/bgfx/Debug/shaderc" -#OUT = "..\\out\\build\\x64-Debug\\ADVect\\shaders\\" -# OUT = "..\\build\\ADVect\\shaders\\" -OUT = "/Users/lachrymal/Projects/NouVeL/ADVect/runtime/shaders/" +SHADERC = "/home/lach/Projects/NouVeL/build/ADVect/ext/bgfx/cmake/bgfx/shaderc" +OUT = "/home/lach/Projects/NouVeL/ADVect/runtime/shaders/" P = lambda location, platform, frag, vert: { 'location': location, 'platform': platform, 'frag': frag, 'vert': vert } plats = [ # P('glsl', 'windows', '140', '140'), # P('dx11', 'windows', 'ps_5_0', 'vs_5_0'), -# P('spirv', 'windows', 'spirv', 'spirv'), - P('metal', 'osx', 'metal', 'metal') + P('spirv', 'linux', 'spirv', 'spirv'), + # P('metal', 'osx', 'metal', 'metal') ] for root, dirs, _ in os.walk('shaders'): @@ -22,15 +19,15 @@ for root, dirs, _ in os.walk('shaders'): "--platform", config['platform'], "--profile", config['frag'], "--varyingdef", os.path.join(root, name, "varying.def.sc"), - "-i", "/Users/lachrymal/Projects/NouVeL/ADVect/ext/bgfx/bgfx/examples/common", - "-i", "/Users/lachrymal/Projects/NouVeL/ADVect/ext/bgfx/bgfx/src", + "-i", "/home/lach/Projects/NouVeL/ADVect/ext/bgfx/bgfx/examples/common", + "-i", "/home/lach/Projects/NouVeL/ADVect/ext/bgfx/bgfx/src", "-o", OUT + config['location'] + "\\" + name + ".frag.bin"]) subprocess.run([SHADERC, "-f", os.path.join(root, name, name + '.vert'), "--type", "vertex", "--platform", config['platform'], "--profile", config['vert'], "--varyingdef", os.path.join(root, name, "varying.def.sc"), - "-i", "/Users/lachrymal/Projects/NouVeL/ADVect/ext/bgfx/bgfx/examples/common", - "-i", "/Users/lachrymal/Projects/NouVeL/ADVect/ext/bgfx/bgfx/src", + "-i", "/home/lach/Projects/NouVeL/ADVect/ext/bgfx/bgfx/examples/common", + "-i", "/home/lach/Projects/NouVeL/ADVect/ext/bgfx/bgfx/src", "-o", OUT + config['location'] + "\\" + name + ".vert.bin"]) diff --git a/NVL/Compiler.cpp b/NVL/Compiler.cpp index 717943e..348f502 100644 --- a/NVL/Compiler.cpp +++ b/NVL/Compiler.cpp @@ -7,7 +7,7 @@ namespace { } void SerializeString(std::ostringstream& s, const NVL::String& st) { - u16 v = st.size() * sizeof NVL::Char; + u16 v = st.size() * sizeof(NVL::Char); s.write(reinterpret_cast(&v), sizeof v); s.write(reinterpret_cast(st.c_str()), v); } @@ -45,9 +45,9 @@ namespace { NVL::String DeserializeString(std::istringstream& s) { u16 size{}; DeserializeDirect(s, size); - u8* str = new u8[size + sizeof NVL::Char]; + u8* str = new u8[size + sizeof(NVL::Char)]; s.read(reinterpret_cast(str), size); - reinterpret_cast(str)[size / sizeof NVL::Char] = u'\0'; + reinterpret_cast(str)[size / sizeof(NVL::Char)] = u'\0'; NVL::String st = NVL::String(reinterpret_cast(str)); delete[] str; return st; diff --git a/README.md b/README.md index 59c5271..6e9b2d7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # NouVeL NVL Script Engine + +## Building +Use `CMakeLists.txt` in the root directory. The default build target is `Game`. diff --git a/flake.nix b/flake.nix index 069a556..1af8934 100644 --- a/flake.nix +++ b/flake.nix @@ -20,6 +20,7 @@ gdb pkg-config # jetbrains.clion + python3 xorg.xorgproto xorg.libX11