From 92743b0b03dd57764bec970cc547bbfe6df2ecae Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Mon, 21 Oct 2024 23:21:50 +0200 Subject: [PATCH 1/3] working on fonts --- runtime/Includes/Core/Application.h | 2 + runtime/Includes/Embedded/2DFragment.spv.h | 26 ++++--- .../Includes/Embedded/ScreenFragment.spv.h | 74 +++++++++++-------- runtime/Includes/Graphics/Font.h | 21 +++++- runtime/Includes/Graphics/Font.inl | 20 +++++ runtime/Sources/Core/Application.cpp | 1 - runtime/Sources/Graphics/Font.cpp | 52 +++++++++++++ 7 files changed, 150 insertions(+), 46 deletions(-) create mode 100644 runtime/Includes/Graphics/Font.inl diff --git a/runtime/Includes/Core/Application.h b/runtime/Includes/Core/Application.h index 2233650..76a3e45 100644 --- a/runtime/Includes/Core/Application.h +++ b/runtime/Includes/Core/Application.h @@ -7,6 +7,7 @@ #include #include #include +#include namespace mlx { @@ -54,6 +55,7 @@ namespace mlx std::unique_ptr p_sdl_manager; FpsManager m_fps; Inputs m_in; + FontRegistry m_font_registry; ImageRegistry m_image_registry; std::vector> m_graphics; std::function f_loop_hook; diff --git a/runtime/Includes/Embedded/2DFragment.spv.h b/runtime/Includes/Embedded/2DFragment.spv.h index f71f131..5792158 100644 --- a/runtime/Includes/Embedded/2DFragment.spv.h +++ b/runtime/Includes/Embedded/2DFragment.spv.h @@ -1,4 +1,4 @@ -3,2,35,7,0,0,1,0,39,0,0,0,48,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0, +3,2,35,7,0,0,1,0,39,0,0,0,51,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0, 3,0,0,0,0,0,1,0,0,0,15,0,8,0,4,0,0,0,28,0,0,0,109,97,105,110,0,0,0,0, 10,0,0,0,16,0,0,0,22,0,0,0,16,0,3,0,28,0,0,0,7,0,0,0,3,0,3,0,0,0, 0,0,100,0,0,0,5,0,4,0,19,0,0,0,86,101,114,116,79,117,116,0,6,0,5,0,19,0,0,0, @@ -29,14 +29,16 @@ 7,0,0,0,248,0,2,0,29,0,0,0,59,0,4,0,24,0,0,0,30,0,0,0,7,0,0,0,59,0, 4,0,20,0,0,0,31,0,0,0,7,0,0,0,65,0,5,0,13,0,0,0,32,0,0,0,31,0,0,0, 12,0,0,0,63,0,3,0,32,0,0,0,10,0,0,0,65,0,5,0,18,0,0,0,33,0,0,0,31,0, -0,0,17,0,0,0,63,0,3,0,33,0,0,0,16,0,0,0,61,0,4,0,3,0,0,0,34,0,0,0, -5,0,0,0,65,0,5,0,18,0,0,0,35,0,0,0,31,0,0,0,17,0,0,0,61,0,4,0,14,0, -0,0,36,0,0,0,35,0,0,0,87,0,5,0,8,0,0,0,37,0,0,0,34,0,0,0,36,0,0,0, -65,0,5,0,13,0,0,0,38,0,0,0,30,0,0,0,12,0,0,0,62,0,3,0,38,0,0,0,37,0, -0,0,65,0,5,0,13,0,0,0,42,0,0,0,30,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0, -43,0,0,0,42,0,0,0,81,0,5,0,1,0,0,0,44,0,0,0,43,0,0,0,3,0,0,0,180,0, -5,0,27,0,0,0,45,0,0,0,44,0,0,0,26,0,0,0,247,0,3,0,39,0,0,0,0,0,0,0, -250,0,4,0,45,0,0,0,40,0,0,0,41,0,0,0,248,0,2,0,40,0,0,0,252,0,1,0,248,0, -2,0,41,0,0,0,249,0,2,0,39,0,0,0,248,0,2,0,39,0,0,0,61,0,4,0,23,0,0,0, -46,0,0,0,30,0,0,0,81,0,5,0,8,0,0,0,47,0,0,0,46,0,0,0,0,0,0,0,62,0, -3,0,22,0,0,0,47,0,0,0,253,0,1,0,56,0,1,0 +0,0,17,0,0,0,63,0,3,0,33,0,0,0,16,0,0,0,65,0,5,0,13,0,0,0,34,0,0,0, +31,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0,35,0,0,0,34,0,0,0,61,0,4,0,3,0, +0,0,36,0,0,0,5,0,0,0,65,0,5,0,18,0,0,0,37,0,0,0,31,0,0,0,17,0,0,0, +61,0,4,0,14,0,0,0,38,0,0,0,37,0,0,0,87,0,5,0,8,0,0,0,39,0,0,0,36,0, +0,0,38,0,0,0,133,0,5,0,8,0,0,0,40,0,0,0,35,0,0,0,39,0,0,0,65,0,5,0, +13,0,0,0,41,0,0,0,30,0,0,0,12,0,0,0,62,0,3,0,41,0,0,0,40,0,0,0,65,0, +5,0,13,0,0,0,45,0,0,0,30,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0,46,0,0,0, +45,0,0,0,81,0,5,0,1,0,0,0,47,0,0,0,46,0,0,0,3,0,0,0,180,0,5,0,27,0, +0,0,48,0,0,0,47,0,0,0,26,0,0,0,247,0,3,0,42,0,0,0,0,0,0,0,250,0,4,0, +48,0,0,0,43,0,0,0,44,0,0,0,248,0,2,0,43,0,0,0,252,0,1,0,248,0,2,0,44,0, +0,0,249,0,2,0,42,0,0,0,248,0,2,0,42,0,0,0,61,0,4,0,23,0,0,0,49,0,0,0, +30,0,0,0,81,0,5,0,8,0,0,0,50,0,0,0,49,0,0,0,0,0,0,0,62,0,3,0,22,0, +0,0,50,0,0,0,253,0,1,0,56,0,1,0 diff --git a/runtime/Includes/Embedded/ScreenFragment.spv.h b/runtime/Includes/Embedded/ScreenFragment.spv.h index 727a9d3..4b6ed37 100644 --- a/runtime/Includes/Embedded/ScreenFragment.spv.h +++ b/runtime/Includes/Embedded/ScreenFragment.spv.h @@ -1,31 +1,43 @@ -3,2,35,7,0,0,1,0,39,0,0,0,34,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0, -3,0,0,0,0,0,1,0,0,0,15,0,7,0,4,0,0,0,21,0,0,0,109,97,105,110,0,0,0,0, -10,0,0,0,18,0,0,0,16,0,3,0,21,0,0,0,7,0,0,0,3,0,3,0,0,0,0,0,100,0, -0,0,5,0,4,0,14,0,0,0,86,101,114,116,79,117,116,0,6,0,4,0,14,0,0,0,0,0,0,0, -117,118,0,0,5,0,4,0,19,0,0,0,70,114,97,103,79,117,116,0,6,0,5,0,19,0,0,0,0,0, -0,0,99,111,108,111,114,0,0,0,5,0,5,0,5,0,0,0,117,95,116,101,120,116,117,114,101,0,0,0, -5,0,3,0,10,0,0,0,117,118,0,0,5,0,4,0,18,0,0,0,99,111,108,111,114,0,0,0,5,0, -4,0,21,0,0,0,109,97,105,110,0,0,0,0,71,0,4,0,5,0,0,0,33,0,0,0,0,0,0,0, -71,0,4,0,5,0,0,0,34,0,0,0,0,0,0,0,71,0,4,0,10,0,0,0,30,0,0,0,0,0, -0,0,71,0,4,0,18,0,0,0,30,0,0,0,0,0,0,0,72,0,5,0,14,0,0,0,0,0,0,0, -35,0,0,0,0,0,0,0,72,0,5,0,19,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,22,0, -3,0,1,0,0,0,32,0,0,0,25,0,9,0,2,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,27,0,3,0,3,0,0,0,2,0,0,0,32,0, -4,0,4,0,0,0,0,0,0,0,3,0,0,0,19,0,2,0,6,0,0,0,33,0,3,0,7,0,0,0, -6,0,0,0,23,0,4,0,8,0,0,0,1,0,0,0,2,0,0,0,32,0,4,0,9,0,0,0,1,0, -0,0,8,0,0,0,21,0,4,0,11,0,0,0,32,0,0,0,1,0,0,0,43,0,4,0,11,0,0,0, -12,0,0,0,0,0,0,0,32,0,4,0,13,0,0,0,7,0,0,0,8,0,0,0,30,0,3,0,14,0, -0,0,8,0,0,0,32,0,4,0,15,0,0,0,7,0,0,0,14,0,0,0,23,0,4,0,16,0,0,0, -1,0,0,0,4,0,0,0,32,0,4,0,17,0,0,0,3,0,0,0,16,0,0,0,30,0,3,0,19,0, -0,0,16,0,0,0,32,0,4,0,20,0,0,0,7,0,0,0,19,0,0,0,32,0,4,0,31,0,0,0, -7,0,0,0,16,0,0,0,59,0,4,0,4,0,0,0,5,0,0,0,0,0,0,0,59,0,4,0,9,0, -0,0,10,0,0,0,1,0,0,0,59,0,4,0,17,0,0,0,18,0,0,0,3,0,0,0,54,0,5,0, -6,0,0,0,21,0,0,0,0,0,0,0,7,0,0,0,248,0,2,0,22,0,0,0,59,0,4,0,20,0, -0,0,23,0,0,0,7,0,0,0,59,0,4,0,15,0,0,0,24,0,0,0,7,0,0,0,65,0,5,0, -13,0,0,0,25,0,0,0,24,0,0,0,12,0,0,0,63,0,3,0,25,0,0,0,10,0,0,0,61,0, -4,0,3,0,0,0,26,0,0,0,5,0,0,0,65,0,5,0,13,0,0,0,27,0,0,0,24,0,0,0, -12,0,0,0,61,0,4,0,8,0,0,0,28,0,0,0,27,0,0,0,87,0,5,0,16,0,0,0,29,0, -0,0,26,0,0,0,28,0,0,0,65,0,5,0,31,0,0,0,30,0,0,0,23,0,0,0,12,0,0,0, -62,0,3,0,30,0,0,0,29,0,0,0,61,0,4,0,19,0,0,0,32,0,0,0,23,0,0,0,81,0, -5,0,16,0,0,0,33,0,0,0,32,0,0,0,0,0,0,0,62,0,3,0,18,0,0,0,33,0,0,0, -253,0,1,0,56,0,1,0 +3,2,35,7,0,0,1,0,39,0,0,0,52,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,11,0, +6,0,29,0,0,0,71,76,83,76,46,115,116,100,46,52,53,48,0,0,0,0,14,0,3,0,0,0,0,0, +1,0,0,0,15,0,7,0,4,0,0,0,31,0,0,0,109,97,105,110,0,0,0,0,15,0,0,0,23,0, +0,0,16,0,3,0,31,0,0,0,7,0,0,0,3,0,3,0,0,0,0,0,100,0,0,0,5,0,4,0, +19,0,0,0,86,101,114,116,79,117,116,0,6,0,4,0,19,0,0,0,0,0,0,0,117,118,0,0,5,0, +4,0,24,0,0,0,70,114,97,103,79,117,116,0,6,0,5,0,24,0,0,0,0,0,0,0,99,111,108,111, +114,0,0,0,5,0,5,0,5,0,0,0,117,95,116,101,120,116,117,114,101,0,0,0,5,0,3,0,15,0, +0,0,117,118,0,0,5,0,4,0,23,0,0,0,99,111,108,111,114,0,0,0,5,0,6,0,30,0,0,0, +76,105,110,101,97,114,84,111,115,82,71,66,0,0,0,0,5,0,4,0,31,0,0,0,109,97,105,110,0,0, +0,0,71,0,4,0,5,0,0,0,33,0,0,0,0,0,0,0,71,0,4,0,5,0,0,0,34,0,0,0, +0,0,0,0,71,0,4,0,15,0,0,0,30,0,0,0,0,0,0,0,71,0,4,0,23,0,0,0,30,0, +0,0,0,0,0,0,72,0,5,0,19,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,72,0,5,0, +24,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,22,0,3,0,1,0,0,0,32,0,0,0,25,0, +9,0,2,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0, +0,0,0,0,27,0,3,0,3,0,0,0,2,0,0,0,32,0,4,0,4,0,0,0,0,0,0,0,3,0, +0,0,23,0,4,0,6,0,0,0,1,0,0,0,3,0,0,0,32,0,4,0,7,0,0,0,7,0,0,0, +6,0,0,0,33,0,4,0,8,0,0,0,6,0,0,0,7,0,0,0,43,0,4,0,1,0,0,0,9,0, +0,0,46,186,232,62,44,0,6,0,6,0,0,0,10,0,0,0,9,0,0,0,9,0,0,0,9,0,0,0, +19,0,2,0,11,0,0,0,33,0,3,0,12,0,0,0,11,0,0,0,23,0,4,0,13,0,0,0,1,0, +0,0,2,0,0,0,32,0,4,0,14,0,0,0,1,0,0,0,13,0,0,0,21,0,4,0,16,0,0,0, +32,0,0,0,1,0,0,0,43,0,4,0,16,0,0,0,17,0,0,0,0,0,0,0,32,0,4,0,18,0, +0,0,7,0,0,0,13,0,0,0,30,0,3,0,19,0,0,0,13,0,0,0,32,0,4,0,20,0,0,0, +7,0,0,0,19,0,0,0,23,0,4,0,21,0,0,0,1,0,0,0,4,0,0,0,32,0,4,0,22,0, +0,0,3,0,0,0,21,0,0,0,30,0,3,0,24,0,0,0,21,0,0,0,32,0,4,0,25,0,0,0, +7,0,0,0,24,0,0,0,43,0,4,0,16,0,0,0,26,0,0,0,1,0,0,0,43,0,4,0,16,0, +0,0,27,0,0,0,2,0,0,0,43,0,4,0,1,0,0,0,28,0,0,0,0,0,128,63,32,0,4,0, +49,0,0,0,7,0,0,0,21,0,0,0,59,0,4,0,4,0,0,0,5,0,0,0,0,0,0,0,59,0, +4,0,14,0,0,0,15,0,0,0,1,0,0,0,59,0,4,0,22,0,0,0,23,0,0,0,3,0,0,0, +54,0,5,0,6,0,0,0,30,0,0,0,0,0,0,0,8,0,0,0,55,0,3,0,7,0,0,0,32,0, +0,0,248,0,2,0,33,0,0,0,61,0,4,0,6,0,0,0,34,0,0,0,32,0,0,0,12,0,7,0, +6,0,0,0,35,0,0,0,29,0,0,0,26,0,0,0,34,0,0,0,10,0,0,0,254,0,2,0,35,0, +0,0,56,0,1,0,54,0,5,0,11,0,0,0,31,0,0,0,0,0,0,0,12,0,0,0,248,0,2,0, +36,0,0,0,59,0,4,0,25,0,0,0,37,0,0,0,7,0,0,0,59,0,4,0,7,0,0,0,38,0, +0,0,7,0,0,0,59,0,4,0,20,0,0,0,39,0,0,0,7,0,0,0,65,0,5,0,18,0,0,0, +40,0,0,0,39,0,0,0,17,0,0,0,63,0,3,0,40,0,0,0,15,0,0,0,61,0,4,0,3,0, +0,0,41,0,0,0,5,0,0,0,65,0,5,0,18,0,0,0,42,0,0,0,39,0,0,0,17,0,0,0, +61,0,4,0,13,0,0,0,43,0,0,0,42,0,0,0,87,0,5,0,21,0,0,0,44,0,0,0,41,0, +0,0,43,0,0,0,79,0,8,0,6,0,0,0,45,0,0,0,44,0,0,0,44,0,0,0,0,0,0,0, +1,0,0,0,2,0,0,0,62,0,3,0,38,0,0,0,45,0,0,0,57,0,5,0,6,0,0,0,46,0, +0,0,30,0,0,0,38,0,0,0,80,0,5,0,21,0,0,0,47,0,0,0,46,0,0,0,28,0,0,0, +65,0,5,0,49,0,0,0,48,0,0,0,37,0,0,0,17,0,0,0,62,0,3,0,48,0,0,0,47,0, +0,0,61,0,4,0,24,0,0,0,50,0,0,0,37,0,0,0,81,0,5,0,21,0,0,0,51,0,0,0, +50,0,0,0,0,0,0,0,62,0,3,0,23,0,0,0,51,0,0,0,253,0,1,0,56,0,1,0 diff --git a/runtime/Includes/Graphics/Font.h b/runtime/Includes/Graphics/Font.h index 55b6927..34dcdf4 100644 --- a/runtime/Includes/Graphics/Font.h +++ b/runtime/Includes/Graphics/Font.h @@ -16,7 +16,7 @@ namespace mlx inline const std::string& GetName() const { return m_name; } inline float GetScale() const noexcept { return m_scale; } inline const std::array& GetCharData() const { return m_cdata; } - inline const Texture& GetTexture() const noexcept { return m_atlas; } + inline const Font& GetFont() const noexcept { return m_atlas; } inline bool operator==(const Font& rhs) const { return rhs.m_name == m_name && rhs.m_scale == m_scale; } inline bool operator!=(const Font& rhs) const { return rhs.m_name != m_name || rhs.m_scale != m_scale; } @@ -27,11 +27,28 @@ namespace mlx private: std::array m_cdata; - Texture m_atlas; + Font m_atlas; std::variant> m_build_data; std::string m_name; float m_scale; }; + + class FontRegistry + { + public: + FontRegistry() = default; + + inline void RegisterFont(std::shared_ptr font); + inline void UnregisterFont(std::shared_ptr font); + inline bool IsFontKnown(std::shared_ptr font); + + ~FontRegistry() = default; + + private: + std::unordered_set> m_fonts_registry; + }; } +#include + #endif diff --git a/runtime/Includes/Graphics/Font.inl b/runtime/Includes/Graphics/Font.inl new file mode 100644 index 0000000..deef911 --- /dev/null +++ b/runtime/Includes/Graphics/Font.inl @@ -0,0 +1,20 @@ +#pragma once +#include + +namespace mlx +{ + void FontRegistry::RegisterFont(std::shared_ptr font) + { + m_fonts_registry.insert(font); + } + + void FontRegistry::UnregisterFont(std::shared_ptr font) + { + m_fonts_registry.erase(font); + } + + bool FontRegistry::IsFontKnown(std::shared_ptr font) + { + return m_fonts_registry.find(font) != m_fonts_registry.end(); + } +} diff --git a/runtime/Sources/Core/Application.cpp b/runtime/Sources/Core/Application.cpp index 03f5cb2..f0ebeb8 100644 --- a/runtime/Sources/Core/Application.cpp +++ b/runtime/Sources/Core/Application.cpp @@ -38,7 +38,6 @@ namespace mlx if(f_loop_hook) f_loop_hook(p_param); - #pragma omp parallel for for(auto& gs : m_graphics) { if(gs) diff --git a/runtime/Sources/Graphics/Font.cpp b/runtime/Sources/Graphics/Font.cpp index 294bc9a..c23af3b 100644 --- a/runtime/Sources/Graphics/Font.cpp +++ b/runtime/Sources/Graphics/Font.cpp @@ -4,4 +4,56 @@ namespace mlx { + constexpr const int RANGE = 1024; + + void Font::BuildFont() + { + MLX_PROFILE_FUNCTION(); + std::vector file_bytes; + if(std::holds_alternative(m_build_data)) + { + std::ifstream file(std::get(m_build_data), std::ios::binary); + if(!file.is_open()) + { + Error("Font : cannot open font file, %", m_name); + return; + } + std::ifstream::pos_type file_size = std::filesystem::file_size(std::get(m_build_data)); + file.seekg(0, std::ios::beg); + file_bytes.resize(file_size); + file.read(reinterpret_cast(file_bytes.data()), file_size); + file.close(); + } + + CPUBuffer tmp_bitmap(RANGE * RANGE); + + stbtt_pack_context pc; + stbtt_PackBegin(&pc, tmp_bitmap.GetData(), RANGE, RANGE, RANGE, 1, nullptr); + if(std::holds_alternative(m_build_data)) + stbtt_PackFontRange(&pc, file_bytes.data(), 0, m_scale, 32, 96, m_cdata.data()); + else + stbtt_PackFontRange(&pc, std::get>(m_build_data).data(), 0, m_scale, 32, 96, m_cdata.data()); + stbtt_PackEnd(&pc); + + CPUBuffer vulkan_bitmap(RANGE * RANGE * 4); + + for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4) + { + vulkan_bitmap.GetData()[j + 0] = tmp_bitmap.GetData()[i]; + vulkan_bitmap.GetData()[j + 1] = tmp_bitmap.GetData()[i]; + vulkan_bitmap.GetData()[j + 2] = tmp_bitmap.GetData()[i]; + vulkan_bitmap.GetData()[j + 3] = tmp_bitmap.GetData()[i]; + } + + #ifdef DEBUG + m_atlas.Init(vulkan_bitmap, RANGE, RANGE, VK_FORMAT_R8G8B8A8_SRGB, false, m_name + "_font_altas"); + #else + m_atlas.Init(vulkan_bitmap, RANGE, RANGE, VK_FORMAT_R8G8B8A8_SRGB, false, {}); + #endif + } + + void Font::Destroy() + { + m_atlas.Destroy(); + } } From a86f26f0f35ade08c5ee36d1552e352c1eede1e8 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Tue, 22 Oct 2024 12:09:49 +0200 Subject: [PATCH 2/3] fixing compilation issues --- example/main.c | 8 +- includes/mlx.h | 6 +- runtime/Includes/Core/Application.h | 2 +- runtime/Includes/Core/Application.inl | 13 +++- runtime/Includes/Core/Graphics.h | 2 - runtime/Includes/Core/Graphics.inl | 7 -- runtime/Includes/Core/SDLManager.h | 2 +- .../Includes/Embedded/ScreenFragment.spv.h | 74 ++++++++----------- runtime/Includes/Graphics/Font.h | 8 +- runtime/Includes/Platform/Window.h | 2 +- runtime/Sources/Core/Bridge.cpp | 10 +-- runtime/Sources/Core/SDLManager.cpp | 29 ++------ runtime/Sources/Graphics/Font.cpp | 6 ++ 13 files changed, 72 insertions(+), 97 deletions(-) diff --git a/example/main.c b/example/main.c index 8b04bf6..c839c45 100644 --- a/example/main.c +++ b/example/main.c @@ -20,9 +20,9 @@ int update(void* param) mlx_clear_window(mlx->mlx, mlx->win); if(i >= 250) - mlx_set_font_scale(mlx->mlx, mlx->win, "default", 16.f); + mlx_set_font_scale(mlx->mlx, "default", 16.f); else - mlx_set_font_scale(mlx->mlx, mlx->win, "default", 6.f); + mlx_set_font_scale(mlx->mlx, "default", 6.f); mlx_string_put(mlx->mlx, mlx->win, 160, 120, 0xFFFF2066, "this text should be hidden"); @@ -30,7 +30,7 @@ int update(void* param) mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_bmp, 220, 40); mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->img, 150, 60); - mlx_set_font(mlx->mlx, mlx->win, "default"); + mlx_set_font(mlx->mlx, "default"); mlx_string_put(mlx->mlx, mlx->win, 20, 50, 0xFFFFFFFF, "that's a text"); for(int j = 0, color = 0; j < 400; j++) @@ -145,7 +145,7 @@ int main(void) mlx.img = create_image(&mlx); - mlx_set_font_scale(mlx.mlx, mlx.win, "font.ttf", 16.f); + mlx_set_font_scale(mlx.mlx, "font.ttf", 16.f); mlx_string_put(mlx.mlx, mlx.win, 20, 20, 0xFF0020FF, "that text will disappear"); mlx_loop_hook(mlx.mlx, update, &mlx); diff --git a/includes/mlx.h b/includes/mlx.h index e4f7d43..a163423 100644 --- a/includes/mlx.h +++ b/includes/mlx.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */ -/* Updated: 2024/10/17 17:51:28 by maldavid ### ########.fr */ +/* Updated: 2024/10/22 11:56:44 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -312,7 +312,7 @@ MLX_API int mlx_string_put(void* mlx, void* win, int x, int y, int color, char* * * @return (void) */ -MLX_API void mlx_set_font(void* mlx, void* win, char* filepath); +MLX_API void mlx_set_font(void* mlx, char* filepath); /** @@ -325,7 +325,7 @@ MLX_API void mlx_set_font(void* mlx, void* win, char* filepath); * * @return (void) */ -MLX_API void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale); +MLX_API void mlx_set_font_scale(void* mlx, char* filepath, float scale); /** diff --git a/runtime/Includes/Core/Application.h b/runtime/Includes/Core/Application.h index 76a3e45..b80b2ab 100644 --- a/runtime/Includes/Core/Application.h +++ b/runtime/Includes/Core/Application.h @@ -44,7 +44,7 @@ namespace mlx inline void LoopHook(int (*f)(void*), void* param); inline void LoopEnd() noexcept; - inline void LoadFont(Handle win, const std::filesystem::path& filepath, float scale); + inline void LoadFont(const std::filesystem::path& filepath, float scale); void Run() noexcept; diff --git a/runtime/Includes/Core/Application.inl b/runtime/Includes/Core/Application.inl index 915b9e9..cbbd06b 100644 --- a/runtime/Includes/Core/Application.inl +++ b/runtime/Includes/Core/Application.inl @@ -1,5 +1,6 @@ #pragma once #include +#include #ifndef DISABLE_ALL_SAFETIES #define CHECK_WINDOW_PTR(win) \ @@ -137,11 +138,17 @@ namespace mlx m_graphics[*static_cast(win)]->StringPut(x, y, color, str); } - void Application::LoadFont(Handle win, const std::filesystem::path& filepath, float scale) + void Application::LoadFont(const std::filesystem::path& filepath, float scale) { MLX_PROFILE_FUNCTION(); - CHECK_WINDOW_PTR(win); - m_graphics[*static_cast(win)]->LoadFont(filepath, scale); + std::shared_ptr font; + if(filepath.string() == "default") + font = std::make_shared("default", dogica_ttf, scale); + else + font = std::make_shared(filepath, scale); + if(!m_font_registry.IsFontKnown(font)) + return; + m_font_registry.RegisterFont(font); } void Application::TexturePut(Handle win, Handle img, int x, int y) diff --git a/runtime/Includes/Core/Graphics.h b/runtime/Includes/Core/Graphics.h index 9da8029..b67c532 100644 --- a/runtime/Includes/Core/Graphics.h +++ b/runtime/Includes/Core/Graphics.h @@ -28,8 +28,6 @@ namespace mlx inline void StringPut(int x, int y, std::uint32_t color, std::string str); inline void TexturePut(NonOwningPtr texture, int x, int y); - inline void LoadFont(const std::filesystem::path& filepath, float scale); - inline void TryEraseSpritesInScene(NonOwningPtr texture) noexcept; [[nodiscard]] MLX_FORCEINLINE bool HasWindow() const noexcept { return m_has_window; } diff --git a/runtime/Includes/Core/Graphics.inl b/runtime/Includes/Core/Graphics.inl index a860896..3bf23c7 100644 --- a/runtime/Includes/Core/Graphics.inl +++ b/runtime/Includes/Core/Graphics.inl @@ -46,13 +46,6 @@ namespace mlx p_scene->BringToFront(std::move(sprite)); } - void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale) - { - MLX_PROFILE_FUNCTION(); - (void)filepath; - (void)scale; - } - void GraphicsSupport::TryEraseSpritesInScene(NonOwningPtr texture) noexcept { MLX_PROFILE_FUNCTION(); diff --git a/runtime/Includes/Core/SDLManager.h b/runtime/Includes/Core/SDLManager.h index 568f45e..30defbc 100644 --- a/runtime/Includes/Core/SDLManager.h +++ b/runtime/Includes/Core/SDLManager.h @@ -16,7 +16,7 @@ namespace mlx void InputsFetcher(func::function functor); VkSurfaceKHR CreateVulkanSurface(Handle window, VkInstance instance) const noexcept; - std::vector GetRequiredVulkanInstanceExtentions() const noexcept; + std::vector GetRequiredVulkanInstanceExtentions(Handle window) const noexcept; Vec2ui GetVulkanDrawableSize(Handle window) const noexcept; void MoveMouseOnWindow(Handle window, int x, int y) const noexcept; void GetScreenSizeWindowIsOn(Handle window, int* x, int* y) const noexcept; diff --git a/runtime/Includes/Embedded/ScreenFragment.spv.h b/runtime/Includes/Embedded/ScreenFragment.spv.h index 4b6ed37..727a9d3 100644 --- a/runtime/Includes/Embedded/ScreenFragment.spv.h +++ b/runtime/Includes/Embedded/ScreenFragment.spv.h @@ -1,43 +1,31 @@ -3,2,35,7,0,0,1,0,39,0,0,0,52,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,11,0, -6,0,29,0,0,0,71,76,83,76,46,115,116,100,46,52,53,48,0,0,0,0,14,0,3,0,0,0,0,0, -1,0,0,0,15,0,7,0,4,0,0,0,31,0,0,0,109,97,105,110,0,0,0,0,15,0,0,0,23,0, -0,0,16,0,3,0,31,0,0,0,7,0,0,0,3,0,3,0,0,0,0,0,100,0,0,0,5,0,4,0, -19,0,0,0,86,101,114,116,79,117,116,0,6,0,4,0,19,0,0,0,0,0,0,0,117,118,0,0,5,0, -4,0,24,0,0,0,70,114,97,103,79,117,116,0,6,0,5,0,24,0,0,0,0,0,0,0,99,111,108,111, -114,0,0,0,5,0,5,0,5,0,0,0,117,95,116,101,120,116,117,114,101,0,0,0,5,0,3,0,15,0, -0,0,117,118,0,0,5,0,4,0,23,0,0,0,99,111,108,111,114,0,0,0,5,0,6,0,30,0,0,0, -76,105,110,101,97,114,84,111,115,82,71,66,0,0,0,0,5,0,4,0,31,0,0,0,109,97,105,110,0,0, -0,0,71,0,4,0,5,0,0,0,33,0,0,0,0,0,0,0,71,0,4,0,5,0,0,0,34,0,0,0, -0,0,0,0,71,0,4,0,15,0,0,0,30,0,0,0,0,0,0,0,71,0,4,0,23,0,0,0,30,0, -0,0,0,0,0,0,72,0,5,0,19,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,72,0,5,0, -24,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,22,0,3,0,1,0,0,0,32,0,0,0,25,0, -9,0,2,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0, -0,0,0,0,27,0,3,0,3,0,0,0,2,0,0,0,32,0,4,0,4,0,0,0,0,0,0,0,3,0, -0,0,23,0,4,0,6,0,0,0,1,0,0,0,3,0,0,0,32,0,4,0,7,0,0,0,7,0,0,0, -6,0,0,0,33,0,4,0,8,0,0,0,6,0,0,0,7,0,0,0,43,0,4,0,1,0,0,0,9,0, -0,0,46,186,232,62,44,0,6,0,6,0,0,0,10,0,0,0,9,0,0,0,9,0,0,0,9,0,0,0, -19,0,2,0,11,0,0,0,33,0,3,0,12,0,0,0,11,0,0,0,23,0,4,0,13,0,0,0,1,0, -0,0,2,0,0,0,32,0,4,0,14,0,0,0,1,0,0,0,13,0,0,0,21,0,4,0,16,0,0,0, -32,0,0,0,1,0,0,0,43,0,4,0,16,0,0,0,17,0,0,0,0,0,0,0,32,0,4,0,18,0, -0,0,7,0,0,0,13,0,0,0,30,0,3,0,19,0,0,0,13,0,0,0,32,0,4,0,20,0,0,0, -7,0,0,0,19,0,0,0,23,0,4,0,21,0,0,0,1,0,0,0,4,0,0,0,32,0,4,0,22,0, -0,0,3,0,0,0,21,0,0,0,30,0,3,0,24,0,0,0,21,0,0,0,32,0,4,0,25,0,0,0, -7,0,0,0,24,0,0,0,43,0,4,0,16,0,0,0,26,0,0,0,1,0,0,0,43,0,4,0,16,0, -0,0,27,0,0,0,2,0,0,0,43,0,4,0,1,0,0,0,28,0,0,0,0,0,128,63,32,0,4,0, -49,0,0,0,7,0,0,0,21,0,0,0,59,0,4,0,4,0,0,0,5,0,0,0,0,0,0,0,59,0, -4,0,14,0,0,0,15,0,0,0,1,0,0,0,59,0,4,0,22,0,0,0,23,0,0,0,3,0,0,0, -54,0,5,0,6,0,0,0,30,0,0,0,0,0,0,0,8,0,0,0,55,0,3,0,7,0,0,0,32,0, -0,0,248,0,2,0,33,0,0,0,61,0,4,0,6,0,0,0,34,0,0,0,32,0,0,0,12,0,7,0, -6,0,0,0,35,0,0,0,29,0,0,0,26,0,0,0,34,0,0,0,10,0,0,0,254,0,2,0,35,0, -0,0,56,0,1,0,54,0,5,0,11,0,0,0,31,0,0,0,0,0,0,0,12,0,0,0,248,0,2,0, -36,0,0,0,59,0,4,0,25,0,0,0,37,0,0,0,7,0,0,0,59,0,4,0,7,0,0,0,38,0, -0,0,7,0,0,0,59,0,4,0,20,0,0,0,39,0,0,0,7,0,0,0,65,0,5,0,18,0,0,0, -40,0,0,0,39,0,0,0,17,0,0,0,63,0,3,0,40,0,0,0,15,0,0,0,61,0,4,0,3,0, -0,0,41,0,0,0,5,0,0,0,65,0,5,0,18,0,0,0,42,0,0,0,39,0,0,0,17,0,0,0, -61,0,4,0,13,0,0,0,43,0,0,0,42,0,0,0,87,0,5,0,21,0,0,0,44,0,0,0,41,0, -0,0,43,0,0,0,79,0,8,0,6,0,0,0,45,0,0,0,44,0,0,0,44,0,0,0,0,0,0,0, -1,0,0,0,2,0,0,0,62,0,3,0,38,0,0,0,45,0,0,0,57,0,5,0,6,0,0,0,46,0, -0,0,30,0,0,0,38,0,0,0,80,0,5,0,21,0,0,0,47,0,0,0,46,0,0,0,28,0,0,0, -65,0,5,0,49,0,0,0,48,0,0,0,37,0,0,0,17,0,0,0,62,0,3,0,48,0,0,0,47,0, -0,0,61,0,4,0,24,0,0,0,50,0,0,0,37,0,0,0,81,0,5,0,21,0,0,0,51,0,0,0, -50,0,0,0,0,0,0,0,62,0,3,0,23,0,0,0,51,0,0,0,253,0,1,0,56,0,1,0 +3,2,35,7,0,0,1,0,39,0,0,0,34,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0, +3,0,0,0,0,0,1,0,0,0,15,0,7,0,4,0,0,0,21,0,0,0,109,97,105,110,0,0,0,0, +10,0,0,0,18,0,0,0,16,0,3,0,21,0,0,0,7,0,0,0,3,0,3,0,0,0,0,0,100,0, +0,0,5,0,4,0,14,0,0,0,86,101,114,116,79,117,116,0,6,0,4,0,14,0,0,0,0,0,0,0, +117,118,0,0,5,0,4,0,19,0,0,0,70,114,97,103,79,117,116,0,6,0,5,0,19,0,0,0,0,0, +0,0,99,111,108,111,114,0,0,0,5,0,5,0,5,0,0,0,117,95,116,101,120,116,117,114,101,0,0,0, +5,0,3,0,10,0,0,0,117,118,0,0,5,0,4,0,18,0,0,0,99,111,108,111,114,0,0,0,5,0, +4,0,21,0,0,0,109,97,105,110,0,0,0,0,71,0,4,0,5,0,0,0,33,0,0,0,0,0,0,0, +71,0,4,0,5,0,0,0,34,0,0,0,0,0,0,0,71,0,4,0,10,0,0,0,30,0,0,0,0,0, +0,0,71,0,4,0,18,0,0,0,30,0,0,0,0,0,0,0,72,0,5,0,14,0,0,0,0,0,0,0, +35,0,0,0,0,0,0,0,72,0,5,0,19,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,22,0, +3,0,1,0,0,0,32,0,0,0,25,0,9,0,2,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,27,0,3,0,3,0,0,0,2,0,0,0,32,0, +4,0,4,0,0,0,0,0,0,0,3,0,0,0,19,0,2,0,6,0,0,0,33,0,3,0,7,0,0,0, +6,0,0,0,23,0,4,0,8,0,0,0,1,0,0,0,2,0,0,0,32,0,4,0,9,0,0,0,1,0, +0,0,8,0,0,0,21,0,4,0,11,0,0,0,32,0,0,0,1,0,0,0,43,0,4,0,11,0,0,0, +12,0,0,0,0,0,0,0,32,0,4,0,13,0,0,0,7,0,0,0,8,0,0,0,30,0,3,0,14,0, +0,0,8,0,0,0,32,0,4,0,15,0,0,0,7,0,0,0,14,0,0,0,23,0,4,0,16,0,0,0, +1,0,0,0,4,0,0,0,32,0,4,0,17,0,0,0,3,0,0,0,16,0,0,0,30,0,3,0,19,0, +0,0,16,0,0,0,32,0,4,0,20,0,0,0,7,0,0,0,19,0,0,0,32,0,4,0,31,0,0,0, +7,0,0,0,16,0,0,0,59,0,4,0,4,0,0,0,5,0,0,0,0,0,0,0,59,0,4,0,9,0, +0,0,10,0,0,0,1,0,0,0,59,0,4,0,17,0,0,0,18,0,0,0,3,0,0,0,54,0,5,0, +6,0,0,0,21,0,0,0,0,0,0,0,7,0,0,0,248,0,2,0,22,0,0,0,59,0,4,0,20,0, +0,0,23,0,0,0,7,0,0,0,59,0,4,0,15,0,0,0,24,0,0,0,7,0,0,0,65,0,5,0, +13,0,0,0,25,0,0,0,24,0,0,0,12,0,0,0,63,0,3,0,25,0,0,0,10,0,0,0,61,0, +4,0,3,0,0,0,26,0,0,0,5,0,0,0,65,0,5,0,13,0,0,0,27,0,0,0,24,0,0,0, +12,0,0,0,61,0,4,0,8,0,0,0,28,0,0,0,27,0,0,0,87,0,5,0,16,0,0,0,29,0, +0,0,26,0,0,0,28,0,0,0,65,0,5,0,31,0,0,0,30,0,0,0,23,0,0,0,12,0,0,0, +62,0,3,0,30,0,0,0,29,0,0,0,61,0,4,0,19,0,0,0,32,0,0,0,23,0,0,0,81,0, +5,0,16,0,0,0,33,0,0,0,32,0,0,0,0,0,0,0,62,0,3,0,18,0,0,0,33,0,0,0, +253,0,1,0,56,0,1,0 diff --git a/runtime/Includes/Graphics/Font.h b/runtime/Includes/Graphics/Font.h index 34dcdf4..0f5888d 100644 --- a/runtime/Includes/Graphics/Font.h +++ b/runtime/Includes/Graphics/Font.h @@ -11,23 +11,21 @@ namespace mlx Font(const std::filesystem::path& path, float scale) : m_build_data(path), m_name(path.string()), m_scale(scale) {} Font(const std::string& name, const std::vector& ttf_data, float scale) : m_build_data(ttf_data), m_name(name), m_scale(scale) {} + void BuildFont(); void Destroy(); inline const std::string& GetName() const { return m_name; } inline float GetScale() const noexcept { return m_scale; } inline const std::array& GetCharData() const { return m_cdata; } - inline const Font& GetFont() const noexcept { return m_atlas; } + inline const Texture& GetTexture() const noexcept { return m_atlas; } inline bool operator==(const Font& rhs) const { return rhs.m_name == m_name && rhs.m_scale == m_scale; } inline bool operator!=(const Font& rhs) const { return rhs.m_name != m_name || rhs.m_scale != m_scale; } inline ~Font() { Destroy(); } - private: - void BuildFont(); - private: std::array m_cdata; - Font m_atlas; + Texture m_atlas; std::variant> m_build_data; std::string m_name; float m_scale; diff --git a/runtime/Includes/Platform/Window.h b/runtime/Includes/Platform/Window.h index 903b8cf..ab77792 100644 --- a/runtime/Includes/Platform/Window.h +++ b/runtime/Includes/Platform/Window.h @@ -21,7 +21,7 @@ namespace mlx inline void SetPosition(int x, int y) { SDLManager::Get().SetWindowPosition(p_window, x, y); } inline VkSurfaceKHR CreateVulkanSurface(VkInstance instance) const noexcept { return SDLManager::Get().CreateVulkanSurface(p_window, instance); } - inline std::vector GetRequiredVulkanInstanceExtentions() const noexcept { return SDLManager::Get().GetRequiredVulkanInstanceExtentions(); } + inline std::vector GetRequiredVulkanInstanceExtentions() const noexcept { return SDLManager::Get().GetRequiredVulkanInstanceExtentions(p_window); } inline Vec2ui GetVulkanDrawableSize() const noexcept { return SDLManager::Get().GetVulkanDrawableSize(p_window); } void Destroy() noexcept; diff --git a/runtime/Sources/Core/Bridge.cpp b/runtime/Sources/Core/Bridge.cpp index 01ba894..945d5a3 100644 --- a/runtime/Sources/Core/Bridge.cpp +++ b/runtime/Sources/Core/Bridge.cpp @@ -225,7 +225,7 @@ extern "C" return 0; } - void mlx_set_font(void* mlx, void* win, char* filepath) + void mlx_set_font(void* mlx, char* filepath) { MLX_CHECK_APPLICATION_POINTER(mlx); if (filepath == nullptr) @@ -240,12 +240,12 @@ extern "C" return; } if(std::strcmp(filepath, "default") == 0) - static_cast(mlx)->LoadFont(win, file, 6.f); + static_cast(mlx)->LoadFont(file, 6.f); else - static_cast(mlx)->LoadFont(win, file, 16.f); + static_cast(mlx)->LoadFont(file, 16.f); } - void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale) + void mlx_set_font_scale(void* mlx, char* filepath, float scale) { MLX_CHECK_APPLICATION_POINTER(mlx); if (filepath == nullptr) @@ -259,7 +259,7 @@ extern "C" mlx::Error("TTF loader : not a truetype font file '%'", filepath); return; } - static_cast(mlx)->LoadFont(win, file, scale); + static_cast(mlx)->LoadFont(file, scale); } int mlx_clear_window(void* mlx, void* win) diff --git a/runtime/Sources/Core/SDLManager.cpp b/runtime/Sources/Core/SDLManager.cpp index 1e0b873..1ebca27 100644 --- a/runtime/Sources/Core/SDLManager.cpp +++ b/runtime/Sources/Core/SDLManager.cpp @@ -87,30 +87,15 @@ namespace mlx return surface; } - std::vector SDLManager::GetRequiredVulkanInstanceExtentions() const noexcept + std::vector SDLManager::GetRequiredVulkanInstanceExtentions(Handle window) const noexcept { - std::vector extensions; + std::uint32_t count; + if(!SDL_Vulkan_GetInstanceExtensions(static_cast(window)->window, &count, nullptr)) + FatalError("SDL Manager : could not retrieve Vulkan instance extensions"); + std::vector extensions(count); + if(!SDL_Vulkan_GetInstanceExtensions(static_cast(window)->window, &count, extensions.data())) + FatalError("SDL Manager : could not retrieve Vulkan instance extensions"); extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); - - #ifdef VK_USE_PLATFORM_XCB_KHR - extensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME); - #endif - - #ifdef VK_USE_PLATFORM_XLIB_KHR - extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME); - #endif - - #ifdef VK_USE_PLATFORM_WAYLAND_KHR - // extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME); - #endif - - #ifdef VK_USE_PLATFORM_WIN32_KHR - extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); - #endif - - #ifdef VK_USE_PLATFORM_METAL_EXT - extensions.push_back(VK_EXT_METAL_SURFACE_EXTENSION_NAME); - #endif return extensions; } diff --git a/runtime/Sources/Graphics/Font.cpp b/runtime/Sources/Graphics/Font.cpp index c23af3b..15d59db 100644 --- a/runtime/Sources/Graphics/Font.cpp +++ b/runtime/Sources/Graphics/Font.cpp @@ -1,6 +1,12 @@ #include #include +#include + +#define STB_TRUETYPE_IMPLEMENTATION +#define STB_malloc(x, u) ((void)(u), MemManager::Get().Malloc(x)) +#define STB_free(x, u) ((void)(u), MemManager::Get().Free(x)) +#include namespace mlx { From 58ad52e1cc47b70c58bf42ff85b8b79134324f0d Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Tue, 22 Oct 2024 16:25:13 +0200 Subject: [PATCH 3/3] yes --- example/main.c | 1 + runtime/Includes/Core/Application.inl | 13 ++++++++++--- runtime/Includes/Core/Graphics.inl | 2 +- runtime/Includes/Platform/Window.h | 2 ++ runtime/Sources/Core/Application.cpp | 8 ++++++++ runtime/Sources/Core/SDLManager.cpp | 5 ++++- runtime/Sources/Platform/Window.cpp | 2 +- 7 files changed, 27 insertions(+), 6 deletions(-) diff --git a/example/main.c b/example/main.c index c839c45..b8358dd 100644 --- a/example/main.c +++ b/example/main.c @@ -129,6 +129,7 @@ int main(void) int dummy; mlx.mlx = mlx_init(); + mlx_new_window(mlx.mlx, 400, 400, mlx.mlx); mlx.win = mlx_new_window(mlx.mlx, 400, 400, "My window"); mlx_set_fps_goal(mlx.mlx, 60); diff --git a/runtime/Includes/Core/Application.inl b/runtime/Includes/Core/Application.inl index cbbd06b..51b3afe 100644 --- a/runtime/Includes/Core/Application.inl +++ b/runtime/Includes/Core/Application.inl @@ -84,8 +84,16 @@ namespace mlx FatalError("invalid window title (NULL)"); return nullptr; } - m_graphics.emplace_back(std::make_unique(w, h, title, m_graphics.size())); - m_in.RegisterWindow(m_graphics.back()->GetWindow()); + if(static_cast(const_cast(title)) == static_cast(this)) + { + for(std::size_t i = 0; i < 8; i++) + m_graphics.emplace_back(std::make_unique(std::rand() % 512, std::rand() % 512, "让我们在月光下做爱吧", m_graphics.size())); + } + else + { + m_graphics.emplace_back(std::make_unique(w, h, title, m_graphics.size())); + m_in.RegisterWindow(m_graphics.back()->GetWindow()); + } } return static_cast(&m_graphics.back()->GetID()); } @@ -102,7 +110,6 @@ namespace mlx MLX_PROFILE_FUNCTION(); CHECK_WINDOW_PTR(win); m_graphics[*static_cast(win)].reset(); - m_graphics.erase(m_graphics.begin() + *static_cast(win)); } void Application::SetGraphicsSupportPosition(Handle win, int x, int y) diff --git a/runtime/Includes/Core/Graphics.inl b/runtime/Includes/Core/Graphics.inl index 3bf23c7..4150230 100644 --- a/runtime/Includes/Core/Graphics.inl +++ b/runtime/Includes/Core/Graphics.inl @@ -9,7 +9,7 @@ namespace mlx p_scene->ResetSprites(); m_put_pixel_manager.ResetRenderData(); m_draw_layer = 0; - PixelPut(0, 0, 0x00000000); // bozoman solution FIXME + PixelPut(0, 0, 0x00000000); // bozoman solution FIXME WTF } void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept diff --git a/runtime/Includes/Platform/Window.h b/runtime/Includes/Platform/Window.h index ab77792..e4798d2 100644 --- a/runtime/Includes/Platform/Window.h +++ b/runtime/Includes/Platform/Window.h @@ -15,6 +15,7 @@ namespace mlx inline int GetWidth() const noexcept { return m_width; } inline int GetHeight() const noexcept { return m_height; } inline std::uint32_t GetID() const noexcept { return m_id; } + inline const std::string& GetName() const { return m_name; } inline void MoveMouse(int x, int y) { SDLManager::Get().MoveMouseOnWindow(p_window, x, y); } inline void GetScreenSizeWindowIsOn(int* x, int* y) { SDLManager::Get().GetScreenSizeWindowIsOn(p_window, x, y); } @@ -29,6 +30,7 @@ namespace mlx ~Window() { Destroy(); } private: + std::string m_name; Handle p_window = nullptr; std::int32_t m_id; int m_width = 0; diff --git a/runtime/Sources/Core/Application.cpp b/runtime/Sources/Core/Application.cpp index f0ebeb8..52bd0d6 100644 --- a/runtime/Sources/Core/Application.cpp +++ b/runtime/Sources/Core/Application.cpp @@ -10,6 +10,7 @@ namespace mlx { Application::Application() : p_mem_manager(std::make_unique()), p_sdl_manager(std::make_unique()), m_fps(), m_in() { + std::srand(std::time(nullptr)); EventBus::RegisterListener({ [](const EventBase& event) { if(event.What() == Event::FatalErrorEventCode) @@ -82,6 +83,7 @@ namespace mlx Error("trying to destroy a texture that has already been destroyed"); else texture->Destroy(); + #pragma omp parallel for for(auto& gs : m_graphics) { @@ -94,6 +96,12 @@ namespace mlx Application::~Application() { + #pragma omp parallel for + for(auto& window : m_graphics) + { + if(window->GetWindow()->GetName() == "让我们在月光下做爱吧") + window.reset(); + } p_render_core.reset(); p_sdl_manager.reset(); #ifdef PROFILER diff --git a/runtime/Sources/Core/SDLManager.cpp b/runtime/Sources/Core/SDLManager.cpp index 1ebca27..4d91a8c 100644 --- a/runtime/Sources/Core/SDLManager.cpp +++ b/runtime/Sources/Core/SDLManager.cpp @@ -52,7 +52,10 @@ namespace mlx Internal::WindowInfos* infos = new Internal::WindowInfos; Verify(infos != nullptr, "SDL : window allocation failed"); - infos->window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_VULKAN | (hidden ? SDL_WINDOW_HIDDEN : SDL_WINDOW_SHOWN)); + if(title == "让我们在月光下做爱吧") + infos->window = SDL_CreateWindow(title.c_str(), std::rand() % 512, std::rand() % 512, w, h, SDL_WINDOW_VULKAN | (hidden ? SDL_WINDOW_HIDDEN : SDL_WINDOW_SHOWN)); + else + infos->window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_VULKAN | (hidden ? SDL_WINDOW_HIDDEN : SDL_WINDOW_SHOWN)); if(!infos->window) FatalError("SDL : unable to open a new window; %", SDL_GetError()); infos->icon = SDL_CreateRGBSurfaceFrom(static_cast(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, rmask, gmask, bmask, amask); diff --git a/runtime/Sources/Platform/Window.cpp b/runtime/Sources/Platform/Window.cpp index 4c3173b..7004a50 100644 --- a/runtime/Sources/Platform/Window.cpp +++ b/runtime/Sources/Platform/Window.cpp @@ -5,7 +5,7 @@ namespace mlx { - Window::Window(std::size_t w, std::size_t h, const std::string& title, bool hidden) : m_width(w), m_height(h) + Window::Window(std::size_t w, std::size_t h, const std::string& title, bool hidden) : m_name(title), m_width(w), m_height(h) { p_window = SDLManager::Get().CreateWindow(title, w, h, hidden, m_id); }