From a08091d2daa8a810b042ee629bdd7ced9f43a452 Mon Sep 17 00:00:00 2001 From: JaoSchmidt Date: Sun, 8 Dec 2024 00:50:31 -0300 Subject: [PATCH] #5 progess beyond on text rendering - The texture on draw function's parameter is now a reference instead of a shared_ptr - Erros where detected and listed on issue #5 I create this commit to shove text rendering on the side for now and focus at python scripting --- .../textRendering}/resources/fonts/Arial.ttf | Bin Example/textRendering/src/main.cpp | 36 +++++------ Pain/include/CoreRender/Renderer/Renderer2d.h | 24 +++++-- Pain/include/CoreRender/Text/Font.h | 10 ++- Pain/include/CoreRender/Text/FontManager.h | 11 ++++ Pain/include/CoreRender/Texture.h | 7 ++- Pain/include/ECS/Components/Sprite.h | 4 +- Pain/include/Misc/BasicShape.h | 4 +- .../{ => default}/shaders/Renderer2dText.glsl | 0 .../{ => default}/shaders/SprayParticles.glsl | 0 .../{ => default}/shaders/Texture.glsl | 0 .../{ => default}/shaders/Triangles.glsl | 0 Pain/src/CoreRender/Renderer/Draw2d.cpp | 14 +++-- Pain/src/CoreRender/Renderer/Renderer2d.cpp | 43 +++++++++---- Pain/src/CoreRender/Text/Font.cpp | 59 +++++++++++++++--- Pain/src/CoreRender/Texture.cpp | 39 +++++++++--- Pain/src/Misc/BasicShape.cpp | 3 +- 17 files changed, 183 insertions(+), 71 deletions(-) rename {Pain => Example/textRendering}/resources/fonts/Arial.ttf (100%) create mode 100644 Pain/include/CoreRender/Text/FontManager.h rename Pain/resources/{ => default}/shaders/Renderer2dText.glsl (100%) rename Pain/resources/{ => default}/shaders/SprayParticles.glsl (100%) rename Pain/resources/{ => default}/shaders/Texture.glsl (100%) rename Pain/resources/{ => default}/shaders/Triangles.glsl (100%) diff --git a/Pain/resources/fonts/Arial.ttf b/Example/textRendering/resources/fonts/Arial.ttf similarity index 100% rename from Pain/resources/fonts/Arial.ttf rename to Example/textRendering/resources/fonts/Arial.ttf diff --git a/Example/textRendering/src/main.cpp b/Example/textRendering/src/main.cpp index 8a4998b..104523b 100644 --- a/Example/textRendering/src/main.cpp +++ b/Example/textRendering/src/main.cpp @@ -1,3 +1,4 @@ +#include "imgui.h" #include #include @@ -10,49 +11,48 @@ class ShapesController : public pain::ImGuiInstance const void onImGuiUpdate() override { ImGui::Begin("Shapes Controller"); - // ImGui::Image((ImTextureID)m_font.getAtlasTexture().getRendererId(), + // ImGui::Image((ImTextureID)m_font->getAtlasTexture().getRendererId(), // {512, 512}, {0, 1}, {1, 0}); + ImGui::InputFloat("emShape", &m_emSize); + if (ImGui::Button("Reset texture")) { + m_font.reset(pain::Font::create( + "resources/default/fonts/Epilogue-Black.ttf", m_emSize)); + } ImGui::End(); } - // ShapesController(pain::Font &font) : m_font(font) {} - // pain::Font &m_font; + float m_emSize = 0; + ShapesController(pain::Font *font) : m_font(font) {} + std::unique_ptr m_font; }; class MainScene : public pain::Scene { public: - MainScene() - : pain::Scene(), - m_font(std::make_shared("resources/fonts/Arial.ttf")) - { - } + MainScene() : pain::Scene() {} void init(std::shared_ptr pCamera) { m_orthocamera = pCamera; pain::Renderer2d::init(m_orthocamera); auto &a = m_orthocamera->addComponent(); a.bind(); - - ShapesController *sc = new ShapesController(); + ShapesController *sc = new ShapesController( + pain::Font::create("resources/default/fonts/Epilogue-Black.ttf", 40.0)); pain::Application::Get().addImGuiInstance(sc); m_sc = sc; - m_textureAtlas = std::make_shared(m_font->getAtlasTexture()); + // *m_texture = m_font->getAtlasTexture(); } void onUpdate(double deltaTimeSec) override {} void onRender(double currentTime) override { - // pain::Renderer2d::drawQuad({0.5f, 0.5f}, {0.3f, 0.3f}, - // {0.8f, 0.9f, 0.3f, 1.0f}, - // m_textureAtlas, 1.f); - pain::Renderer2d::drawString({-0.8f, 0.f}, "Hello World", *m_font.get(), - {1.f, 1.f, 1.f, 1.f}, m_textureAtlas); + pain::Renderer2d::drawQuad({0.5f, 0.5f}, {0.3f, 0.3f}, {1.f, 1.f, 1.f, 1.f}, + m_sc->m_font->getAtlasTexture(), 1.f); + // pain::Renderer2d::drawString({-0.8f, 0.f}, "Hello World", *m_font.get(), + // {1.f, 1.f, 1.f, 1.f}); } void onEvent(const SDL_Event &event) override {} private: - std::shared_ptr m_textureAtlas; - std::shared_ptr m_font; ShapesController *m_sc; std::shared_ptr m_orthocamera; }; diff --git a/Pain/include/CoreRender/Renderer/Renderer2d.h b/Pain/include/CoreRender/Renderer/Renderer2d.h index 2494efb..14ee65d 100644 --- a/Pain/include/CoreRender/Renderer/Renderer2d.h +++ b/Pain/include/CoreRender/Renderer/Renderer2d.h @@ -70,20 +70,33 @@ class EXPORT Renderer2d /** Draws a quad */ static void drawQuad(const glm::vec2 &position, const glm::vec2 &size, const glm::vec4 &tintColor, - const std::shared_ptr &texture = nullptr, + const Texture *texture = nullptr, const float tilingFactor = 1.0f, const std::array &textureCoordinate = { glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f)}); + static void drawQuad(const glm::vec2 &position, const glm::vec2 &size, + const glm::vec4 &tintColor, const Texture &texture, + const float tilingFactor = 1.0f, + const std::array &textureCoordinate = { + glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), + glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f)}); + /** Draws a quad with rotation, new rotationRadians (angle)*/ static void drawQuad(const glm::vec2 &position, const glm::vec2 &size, const glm::vec4 &tintColor, const float rotationRadians, - const std::shared_ptr &texture = nullptr, + const Texture *texture = nullptr, const float tilingFactor = 1.0f, const std::array &textureCoordinate = { glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f)}); + static void drawQuad(const glm::vec2 &position, const glm::vec2 &size, + const glm::vec4 &tintColor, const float rotationRadians, + const Texture &texture, const float tilingFactor = 1.0f, + const std::array &textureCoordinate = { + glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), + glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f)}); // ================================================================= // // Draw Tri // ================================================================= // @@ -103,8 +116,7 @@ class EXPORT Renderer2d /** Draws a string of glyphs from a font atlas */ static void drawString(const glm::vec2 &position, const char *string, - const Font &font, const glm::vec4 &color, - std::shared_ptr &t); + const Font &font, const glm::vec4 &color); static const glm::mat4 getTransform(const glm::vec2 &position, const glm::vec2 &size, @@ -119,7 +131,7 @@ class EXPORT Renderer2d // ================================================================= // static void initBatches(); static void bindTextures(); - static float allocateTextures(const std::shared_ptr &texture); + static float allocateTextures(const Texture &texture); static void allocateQuad(const glm::mat4 &transform, const glm::vec4 &tintColor, const float tilingFactor, @@ -183,7 +195,7 @@ class EXPORT Renderer2d static const uint32_t MaxTextureSlots = 32; static std::shared_ptr m_whiteTexture; - static std::array, MaxTextureSlots> m_textureSlots; + static std::array m_textureSlots; static uint32_t m_textureSlotIndex; // clang-format off diff --git a/Pain/include/CoreRender/Text/Font.h b/Pain/include/CoreRender/Text/Font.h index 50ec66a..a06645a 100644 --- a/Pain/include/CoreRender/Text/Font.h +++ b/Pain/include/CoreRender/Text/Font.h @@ -9,8 +9,10 @@ namespace pain class Font { public: - Font(const char *fontFilename); + static Font *create(const char *fontFilename); + static Font *create(const char *fontFilename, double emSize); const inline Texture &getAtlasTexture() const { return m_atlasTexture; }; + inline Texture &getAtlasTexture() { return m_atlasTexture; }; const inline msdf_atlas::FontGeometry &getFontGeometry() const { return m_fontGeometry; @@ -36,15 +38,17 @@ class Font static constexpr double maxCornerAngle = 3.0; // methods - Texture generateAtlas(const char *fontFilename); + Texture generateAtlas(const char *fontFilename, double emSize); msdf_atlas::Charset getLatinCharset(); template GenFunc> Texture - createAtlasTexture(const char *fontName, float fontSize, + createAtlasTexture(const char *fontName, const std::vector &glyphs, const msdf_atlas::FontGeometry &fontGeometry, const float width, const float height); + Font(const char *fontFilename, double emSize); }; + } // namespace pain diff --git a/Pain/include/CoreRender/Text/FontManager.h b/Pain/include/CoreRender/Text/FontManager.h new file mode 100644 index 0000000..cd64a27 --- /dev/null +++ b/Pain/include/CoreRender/Text/FontManager.h @@ -0,0 +1,11 @@ +#pragma once + +namespace pain +{ +class FontManager +{ +public: + FontManager(const char *fontFilename); +}; + +} // namespace pain diff --git a/Pain/include/CoreRender/Texture.h b/Pain/include/CoreRender/Texture.h index 873a6e0..3790f34 100644 --- a/Pain/include/CoreRender/Texture.h +++ b/Pain/include/CoreRender/Texture.h @@ -19,14 +19,15 @@ class Texture void bind() const; void setData(const void *data, uint32_t size); bool operator==(const Texture &other) const; + Texture clone(); const uint32_t getRendererId() const { return m_rendererId; } const uint32_t getWidth() const { return m_width; } const uint32_t getHeight() const { return m_height; } MOVABLES(Texture); - // NONCOPYABLE(Texture); - COPIES(Texture); + NONCOPYABLE(Texture); + // COPIES(Texture); private: std::string m_path; @@ -81,6 +82,8 @@ class Texture "Missing entry in types array"); return types[static_cast(format)]; } + Texture(std::string &path, uint32_t width, uint32_t height, + uint32_t dataFormat, uint32_t internalFormat, uint32_t rendererId); }; } // namespace pain diff --git a/Pain/include/ECS/Components/Sprite.h b/Pain/include/ECS/Components/Sprite.h index ef7843d..ee0e38c 100644 --- a/Pain/include/ECS/Components/Sprite.h +++ b/Pain/include/ECS/Components/Sprite.h @@ -7,10 +7,10 @@ namespace pain struct SpriteComponent { glm::vec2 m_size{0.1f, 0.1f}; glm::vec4 m_color{1.0f, 1.0f, 1.0f, 1.0f}; - std::shared_ptr m_ptexture; + Texture *m_ptexture; const float m_tilingFactor; SpriteComponent(const glm::vec2 &size, const glm::vec4 &color, - float tilingFactor, std::shared_ptr ptexture) + float tilingFactor, Texture *ptexture) : m_size(size), m_color(color), m_ptexture(ptexture), m_tilingFactor(tilingFactor) { diff --git a/Pain/include/Misc/BasicShape.h b/Pain/include/Misc/BasicShape.h index 4041dc3..be3c466 100644 --- a/Pain/include/Misc/BasicShape.h +++ b/Pain/include/Misc/BasicShape.h @@ -7,12 +7,12 @@ namespace pain { -class EXPORT RectangleSprite : public GameObject +class RectangleSprite : public GameObject { public: RectangleSprite(Scene *scene, const glm::vec2 &position, const glm::vec2 &size, const glm::vec4 &color, - const std::shared_ptr &ptexture, float tilingFactor); + Texture *ptexture, float tilingFactor); void onUpdate(double dt) {}; void onEvent(const SDL_Event &e) {}; diff --git a/Pain/resources/shaders/Renderer2dText.glsl b/Pain/resources/default/shaders/Renderer2dText.glsl similarity index 100% rename from Pain/resources/shaders/Renderer2dText.glsl rename to Pain/resources/default/shaders/Renderer2dText.glsl diff --git a/Pain/resources/shaders/SprayParticles.glsl b/Pain/resources/default/shaders/SprayParticles.glsl similarity index 100% rename from Pain/resources/shaders/SprayParticles.glsl rename to Pain/resources/default/shaders/SprayParticles.glsl diff --git a/Pain/resources/shaders/Texture.glsl b/Pain/resources/default/shaders/Texture.glsl similarity index 100% rename from Pain/resources/shaders/Texture.glsl rename to Pain/resources/default/shaders/Texture.glsl diff --git a/Pain/resources/shaders/Triangles.glsl b/Pain/resources/default/shaders/Triangles.glsl similarity index 100% rename from Pain/resources/shaders/Triangles.glsl rename to Pain/resources/default/shaders/Triangles.glsl diff --git a/Pain/src/CoreRender/Renderer/Draw2d.cpp b/Pain/src/CoreRender/Renderer/Draw2d.cpp index d443638..8a5644f 100644 --- a/Pain/src/CoreRender/Renderer/Draw2d.cpp +++ b/Pain/src/CoreRender/Renderer/Draw2d.cpp @@ -42,7 +42,7 @@ uint32_t Renderer2d::m_sprayIndexCount = 0; // texture initializer std::shared_ptr Renderer2d::m_whiteTexture = nullptr; -std::array, Renderer2d::MaxTextureSlots> +std::array Renderer2d::m_textureSlots = {nullptr}; uint32_t Renderer2d::m_textureSlotIndex = 1; // at init, there is 1 white texture @@ -93,7 +93,7 @@ void Renderer2d::initBatches() m_quadTextureShader->bind(); m_quadTextureShader->uploadUniformIntArray("u_Textures", samplers, MaxTextureSlots); - m_textureSlots[0] = m_whiteTexture; + m_textureSlots[0] = m_whiteTexture.get(); // =============================================================== // // Triangles @@ -185,8 +185,10 @@ void Renderer2d::drawBatches(const glm::mat4 &viewProjectionMatrix) m_quadVertexBuffer->setData((void *)m_quadVertexBufferBase, quadDataSize); // bind textures - for (uint32_t i = 0; i < m_textureSlotIndex; i++) + for (uint32_t i = 0; i < m_textureSlotIndex; i++) { + PLOG_I("bind texture id = {}", m_textureSlots[i]->getRendererId()); m_textureSlots[i]->bindToSlot(i); + } m_quadTextureShader->bind(); const uint32_t quadCount = @@ -275,13 +277,13 @@ void Renderer2d::goBackToFirstVertex() m_textVertexBufferPtr = m_textVertexBufferBase; } -float Renderer2d::allocateTextures(const std::shared_ptr &texture) +float Renderer2d::allocateTextures(const Texture &texture) { // NOTE: this can be optimized later to avoid searching the texture float textureIndex = 0.0f; // tries to get texture from the m_textureSlots for (uint32_t i = 1; i < m_textureSlotIndex; i++) { - if (*m_textureSlots[i].get() == *texture.get()) { + if (*m_textureSlots[i] == texture) { textureIndex = (float)i; break; } @@ -289,7 +291,7 @@ float Renderer2d::allocateTextures(const std::shared_ptr &texture) // otherwise use it to allocate new texture if (textureIndex == 0.0f) { textureIndex = (float)m_textureSlotIndex; - m_textureSlots[m_textureSlotIndex] = texture; + m_textureSlots[m_textureSlotIndex] = &texture; m_textureSlotIndex++; } P_ASSERT_W(textureIndex != 0.0f, diff --git a/Pain/src/CoreRender/Renderer/Renderer2d.cpp b/Pain/src/CoreRender/Renderer/Renderer2d.cpp index a8ec480..3f814dd 100644 --- a/Pain/src/CoreRender/Renderer/Renderer2d.cpp +++ b/Pain/src/CoreRender/Renderer/Renderer2d.cpp @@ -104,25 +104,46 @@ void Renderer2d::drawIndexed(const std::shared_ptr &vertexArray, void Renderer2d::drawQuad(const glm::vec2 &position, const glm::vec2 &size, const glm::vec4 &tintColor, - const std::shared_ptr &texture, - float tilingFactor, // will prevent fancy tiling + const Texture *texture, // Raw pointer version + float tilingFactor, const std::array &textureCoordinate) { const float texIndex = - texture ? allocateTextures(texture) : 0.0f; // White Texture if nullptr + texture ? allocateTextures(*texture) : 0.0f; // White texture if nullptr + const glm::mat4 transform = getTransform(position, size); + allocateQuad(transform, tintColor, tilingFactor, texIndex, textureCoordinate); +} + +void Renderer2d::drawQuad(const glm::vec2 &position, const glm::vec2 &size, + const glm::vec4 &tintColor, + const Texture &texture, // Reference version + float tilingFactor, + const std::array &textureCoordinate) +{ + const float texIndex = allocateTextures(texture); const glm::mat4 transform = getTransform(position, size); allocateQuad(transform, tintColor, tilingFactor, texIndex, textureCoordinate); } void Renderer2d::drawQuad(const glm::vec2 &position, const glm::vec2 &size, const glm::vec4 &tintColor, - const float rotationRadians, - const std::shared_ptr &texture, + const float rotationRadians, const Texture &texture, + float tilingFactor, + const std::array &textureCoordinate) +{ + const float texIndex = allocateTextures(texture); + const glm::mat4 transform = getTransform(position, size, rotationRadians); + allocateQuad(transform, tintColor, tilingFactor, texIndex, textureCoordinate); +} + +void Renderer2d::drawQuad(const glm::vec2 &position, const glm::vec2 &size, + const glm::vec4 &tintColor, + const float rotationRadians, const Texture *texture, float tilingFactor, const std::array &textureCoordinate) { const float texIndex = - texture ? allocateTextures(texture) : 0.0f; // White Texture if nullptr + texture ? allocateTextures(*texture) : 0.0f; // White Texture if nullptr const glm::mat4 transform = getTransform(position, size, rotationRadians); allocateQuad(transform, tintColor, tilingFactor, texIndex, textureCoordinate); } @@ -159,15 +180,13 @@ void Renderer2d::drawSprayParticle(const Particle &p) // Draw Text // ================================================================= // void Renderer2d::drawString(const glm::vec2 &position, const char *string, - const Font &font, const glm::vec4 &color, - std::shared_ptr &t) + const Font &font, const glm::vec4 &color) { const auto &fontGeometry = font.getFontGeometry(); const auto &metrics = fontGeometry.getMetrics(); - const Texture &fontAtlas = font.getAtlasTexture(); const double &spaceGlyphAdvance = fontGeometry.getGlyph(' ')->getAdvance(); - m_fontAtlasTexture = t.get(); + m_fontAtlasTexture = &font.getAtlasTexture(); double x = 0.0; double fsScale = 1.0 / (metrics.ascenderY - metrics.descenderY); @@ -209,8 +228,8 @@ void Renderer2d::drawString(const glm::vec2 &position, const char *string, // offset quadMin += glm::vec2(x, y); quadMax += glm::vec2(x, y); - float texelWidth = 1.0f / fontAtlas.getWidth(); - float texelHeight = 1.0f / fontAtlas.getHeight(); + float texelWidth = 1.0f / m_fontAtlasTexture->getWidth(); + float texelHeight = 1.0f / m_fontAtlasTexture->getHeight(); texCoordMin *= glm::vec2(texelWidth, texelHeight); texCoordMax *= glm::vec2(texelWidth, texelHeight); diff --git a/Pain/src/CoreRender/Text/Font.cpp b/Pain/src/CoreRender/Text/Font.cpp index ab10d02..1978427 100644 --- a/Pain/src/CoreRender/Text/Font.cpp +++ b/Pain/src/CoreRender/Text/Font.cpp @@ -2,22 +2,46 @@ #include "CoreFiles/Application.h" #include "CoreFiles/LogWrapper.h" +#include "SDL_image.h" +#include "SDL_surface.h" namespace pain { -Font::Font(const char *fontFilename) - : m_atlasTexture(generateAtlas(fontFilename)) + +Font *Font::create(const char *fontFilename) { + try { + return new Font(fontFilename, 40.0); + } catch (const std::exception &e) { + return getDefault(); + } +} +Font *Font::create(const char *fontFilename, double emSize) +{ + try { + return new Font(fontFilename, emSize); + } catch (const std::exception &e) { + return getDefault(); + } } -Texture Font::generateAtlas(const char *fontFilename) +Font::Font(const char *fontFilename, double emSize) + : m_atlasTexture(generateAtlas(fontFilename, emSize)) +{ +} + +Texture Font::generateAtlas(const char *fontFilename, double emSize) { // Initialize instance of FreeType library msdfgen::FreetypeHandle *ft = msdfgen::initializeFreetype(); P_ASSERT(ft, "Could not load FreeType library"); // Load font file msdfgen::FontHandle *font = msdfgen::loadFont(ft, fontFilename); - P_ASSERT(font, "Could not load font from \"{}\"", fontFilename); + if (!font) { + PLOG_W("Font file not found \"{}\"", fontFilename); + throw std::runtime_error(std::string("Font file not found \"") + + std::string(fontFilename) + std::string("\"")); + } // Storage for glyph geometry and their coordinates in the atlas // FontGeometry is a helper class that loads a set of glyphs from a single // font. It can also be used to get additional font metrics, kerning @@ -35,7 +59,7 @@ Texture Font::generateAtlas(const char *fontFilename) PLOG_I("Loaded {} glyphs from font {}, out of {}", numOfLoadedGlyphs, fontFilename, charset.size()); - double emSize = 40.0; + // double emSize = 40.0; // Apply MSDF edge coloring. See edge-coloring.h for other coloring // strategies. @@ -53,7 +77,7 @@ Texture Font::generateAtlas(const char *fontFilename) // setPixelRange or setUnitRange packer.setPixelRange(2.0); packer.setMiterLimit(1.0); - packer.setScale(emSize); + // packer.setScale(emSize); // Compute atlas layout - pack glyphs packer.pack(m_glyphs.data(), m_glyphs.size()); // Get final atlas dimensions @@ -63,7 +87,7 @@ Texture Font::generateAtlas(const char *fontFilename) // atlas bitmap. Texture texture = createAtlasTexture( - textureKey, (float)emSize, m_glyphs, m_fontGeometry, width, height); + textureKey, m_glyphs, m_fontGeometry, width, height); // Cleanup msdfgen::destroyFont(font); msdfgen::deinitializeFreetype(ft); @@ -72,7 +96,7 @@ Texture Font::generateAtlas(const char *fontFilename) template GenFunc> Texture -Font::createAtlasTexture(const char *fontName, float fontSize, +Font::createAtlasTexture(const char *fontName, const std::vector &glyphs, const msdf_atlas::FontGeometry &fontGeometry, const float width, const float height) @@ -80,18 +104,34 @@ Font::createAtlasTexture(const char *fontName, float fontSize, msdf_atlas::GeneratorAttributes attributes; attributes.config.overlapSupport = true; attributes.scanlinePass = true; + + // config generator and generate msdf_atlas::ImmediateAtlasGenerator> generator(width, height); generator.setAttributes(attributes); generator.setThreadCount(Application::getProcessorCount() / 2); generator.generate(glyphs.data(), (int)glyphs.size()); + // TODO: savePng and IMG_SavePNG can correclty save atlas textures with png + // format. However, the Texture ojbect for most fonts isn't being generated + // correctly. That is there is probably some error with the setData texture + // function + // msdfgen::savePng(generator.atlasStorage(), "output.png"); msdfgen::BitmapConstRef bitmap = (msdfgen::BitmapConstRef)generator.atlasStorage(); + // Tranform the msdfgen bitmap into a Texture Texture texture = Texture(bitmap.width, bitmap.height, ImageFormat::RGB8); texture.setData((void *)bitmap.pixels, bitmap.width * bitmap.height * 3); + + // For tests only, transform into a SDL_Surface + // SDL_Surface *surf = SDL_CreateRGBSurfaceFrom( + // (void *)bitmap.pixels, bitmap.width, bitmap.height, 24, bitmap.width * + // 3, 0x0000FF, 0x00FF00, 0xFF0000, 0x000000); + // IMG_SavePNG(surf, "surfaceOutput.png"); + // SDL_FreeSurface(surf); + return texture; } @@ -113,7 +153,8 @@ msdf_atlas::Charset Font::getLatinCharset() Font *Font::getDefault() { - static Font m_defaultFont("resources/fonts/Arial.ttf"); + static Font m_defaultFont("resources/default/fonts/OpenSans-Regular.ttf", + 40.0); return &m_defaultFont; } diff --git a/Pain/src/CoreRender/Texture.cpp b/Pain/src/CoreRender/Texture.cpp index af8afa6..1002cf7 100644 --- a/Pain/src/CoreRender/Texture.cpp +++ b/Pain/src/CoreRender/Texture.cpp @@ -31,6 +31,15 @@ Texture::Texture(uint32_t width, uint32_t height, ImageFormat format) glTextureParameteri(m_rendererId, GL_TEXTURE_WRAP_T, GL_REPEAT); } +void Texture::setData(const void *data, uint32_t size) +{ + uint32_t bytesPerPixel = m_dataFormat == GL_RGBA ? 4 : 3; + P_ASSERT_W(size == m_width * m_height * bytesPerPixel, + "Data must be entire texture!"); + glTextureSubImage2D(m_rendererId, 0, 0, 0, m_width, m_height, m_dataFormat, + GL_UNSIGNED_BYTE, data); +} + /* * Sets a texture given a specific texture image path */ @@ -62,13 +71,6 @@ Texture::Texture(const std::string &path) glTexImage2D(GL_TEXTURE_2D, 0, m_dataFormat, surface->w, surface->h, 0, m_dataFormat, GL_UNSIGNED_BYTE, surface->pixels); } -void Texture::setData(const void *data, uint32_t size) -{ - uint32_t bpp = m_dataFormat == GL_RGBA ? 4 : 3; - P_ASSERT_W(size == m_width * m_height * bpp, "Data must be entire texture!"); - glTextureSubImage2D(m_rendererId, 0, 0, 0, m_width, m_height, m_dataFormat, - GL_UNSIGNED_BYTE, data); -} // bind texture unit i.e. has a slot in a sample array void Texture::bindToSlot(uint32_t slot) const @@ -76,6 +78,25 @@ void Texture::bindToSlot(uint32_t slot) const glBindTextureUnit(slot, m_rendererId); } void Texture::bind() const { glBindTexture(GL_TEXTURE_2D, m_rendererId); } -Texture::~Texture() { glDeleteTextures(1, &m_rendererId); } - +Texture::~Texture() +{ + PLOG_I("Deleted id = {}", m_rendererId); + glDeleteTextures(1, &m_rendererId); +} +Texture Texture::clone() +{ + return Texture(m_path, m_width, m_height, m_dataFormat, m_internalFormat, + m_rendererId); +} +Texture::Texture(std::string &path, uint32_t width, uint32_t height, + uint32_t dataFormat, uint32_t internalFormat, + uint32_t rendererId) +{ + m_path = path; + m_width = width; + m_height = height; + m_dataFormat = dataFormat; + m_internalFormat = internalFormat; + m_rendererId = rendererId; +} } // namespace pain diff --git a/Pain/src/Misc/BasicShape.cpp b/Pain/src/Misc/BasicShape.cpp index 9efb612..42844d6 100644 --- a/Pain/src/Misc/BasicShape.cpp +++ b/Pain/src/Misc/BasicShape.cpp @@ -7,8 +7,7 @@ namespace pain RectangleSprite::RectangleSprite(Scene *scene, const glm::vec2 &position, const glm::vec2 &size, const glm::vec4 &color, - const std::shared_ptr &ptexture, - float tilingFactor) + Texture *ptexture, float tilingFactor) : GameObject(scene) { addComponent(position);