From 88e67cefcb759902c6bd344373be0b1cd0cf7f6b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 11 Apr 2021 23:35:26 +0300 Subject: [PATCH] Redesign Text as an ObjectInstance It's rare to need several copies of the same text, but positioning texts is useful. Text no longer copies the Technique but instead sets the texture in setup_render. --- source/render/text.cpp | 27 +++++++++++++-------------- source/render/text.h | 18 ++++++++++-------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/source/render/text.cpp b/source/render/text.cpp index 3b2380c4..b43061c5 100644 --- a/source/render/text.cpp +++ b/source/render/text.cpp @@ -1,5 +1,6 @@ #include "meshbuilder.h" #include "program.h" +#include "renderer.h" #include "text.h" #include "texture2d.h" @@ -8,7 +9,8 @@ using namespace std; namespace Msp { namespace GL { -Text::Text(const Font &f, const Technique *tech, const string &tex_slot): +Text::Text(const Font &f, const Technique *tech, Tag tex_slot): + ObjectInstance(object), font(f), mesh((TEXCOORD2, VERTEX2)), horz_align(0.0f), @@ -20,12 +22,11 @@ Text::Text(const Font &f, const Technique *tech, const string &tex_slot): set_technique(tech, tex_slot); } -void Text::set_technique(const Technique *tech, const string &ts) +void Text::set_technique(const Technique *tech, Tag tex_slot) { if(tech) { - const char *tex_slot = ts.c_str(); - if(!*tex_slot && tech->has_pass(Tag())) + if(!tex_slot.id && tech->has_pass(Tag())) if(const Program *shprog = tech->get_pass(Tag()).get_shader_program()) { if(shprog->get_uniform_location(Tag("font_tex"))>=0) @@ -38,17 +39,14 @@ void Text::set_technique(const Technique *tech, const string &ts) tex_slot = "base_color_map"; } - if(tex_slot) - { - technique = *tech; - technique.replace_texture(tex_slot, font.get_texture()); - object.set_technique(&technique); - } - else - object.set_technique(tech); + object.set_technique(tech); + texture_slot = tex_slot; } else + { object.set_technique(0); + texture_slot = Tag(); + } } void Text::set_text(const string &text, StringCodec::Decoder &dec) @@ -110,9 +108,10 @@ void Text::set_alignment(float h, float v) } } -void Text::render(Renderer &renderer, Tag tag) const +void Text::setup_render(Renderer &renderer, Tag tag) const { - object.render(renderer, tag); + ObjectInstance::setup_render(renderer, tag); + renderer.set_texture(texture_slot, &font.get_texture()); } } // namespace GL diff --git a/source/render/text.h b/source/render/text.h index 916d6d7b..dda0a4f0 100644 --- a/source/render/text.h +++ b/source/render/text.h @@ -4,16 +4,18 @@ #include "font.h" #include "mesh.h" #include "object.h" +#include "objectinstance.h" #include "technique.h" namespace Msp { namespace GL { /** -Creates an object consisting of the visual representation of a string. Can be -used with an ObjectInstance to further customize the appearance. +Creates an object consisting of the visual representation of a string. If you +derive from Text to customize it, make sure you call Text::setup_render or +otherwise bind the appropriate texture. */ -class Text: public Renderable +class Text: public ObjectInstance { public: enum HorizontalAlign @@ -35,20 +37,20 @@ private: const Font &font; Mesh mesh; Object object; - Technique technique; + Tag texture_slot; float horz_align; float vert_offset; float width; public: - Text(const Font &, const Technique * = 0, const std::string & = std::string()); + Text(const Font &, const Technique * = 0, Tag = Tag()); const Mesh *get_mesh() const { return &mesh; } /** Sets technique to render with, replacing the given texture slot with the font texture. If no texture slot is specified, heuristics are used to choose a suitable one. */ - void set_technique(const Technique *, const std::string & = std::string()); + void set_technique(const Technique *, Tag = Tag()); const Technique *get_technique() const { return object.get_technique(); } @@ -77,9 +79,9 @@ public: float get_width() const { return width; } - virtual void render(Renderer &, Tag = Tag()) const; + virtual void setup_render(Renderer &, Tag) const; - operator const Object &() const { return object; } + DEPRECATED operator const Object &() const { return object; } }; } // namespace GL -- 2.43.0