From fec5f41e6dd5c4b68cddc001c6648a0a0e555439 Mon Sep 17 00:00:00 2001 From: codereader Date: Tue, 1 Dec 2020 19:02:18 +0100 Subject: [PATCH] #5436: Create IGLFont interface and let OpenGLModule return shared references to those FTGL wrappers. Declare drawString() method on the font itself. --- include/igl.h | 27 +++++++++++++++++++++++++-- radiant/render/GLFont.cpp | 16 +++++++++++++--- radiant/render/GLFont.h | 25 +++++++------------------ radiant/render/OpenGLModule.cpp | 12 +++++++++--- radiant/render/OpenGLModule.h | 4 +++- 5 files changed, 57 insertions(+), 27 deletions(-) diff --git a/include/igl.h b/include/igl.h index 229f89747f..12f430682d 100644 --- a/include/igl.h +++ b/include/igl.h @@ -55,8 +55,25 @@ inline gl::ISharedGLContextHolder& GlobalOpenGLContext() const char* const MODULE_OPENGL("OpenGL"); -namespace wxutil { class GLWidget; } -class wxGLContext; +class IGLFont +{ +public: + using Ptr = std::shared_ptr; + + enum class Style + { + Sans, // free sans + Mono, // free mono + }; + + virtual ~IGLFont() {} + + // Returns the line spacing of this font + virtual float getLineHeight() const = 0; + + /// \brief Renders \p string at the current raster-position of the current context. + virtual void drawString(const std::string& string) = 0; +}; class OpenGLBinding : public RegisterableModule @@ -64,11 +81,17 @@ class OpenGLBinding : public: virtual ~OpenGLBinding() {} + // Acquires a shared reference to the font of the given style and size + virtual IGLFont::Ptr getFont(IGLFont::Style style, std::size_t size) = 0; + + // Deprecated virtual int getFontHeight() = 0; + // Deprecated /// \brief Renders \p string at the current raster-position of the current context. virtual void drawString(const std::string& string) const = 0; + // Deprecated /// \brief Renders \p character at the current raster-position of the current context. virtual void drawChar(char character) const = 0; }; diff --git a/radiant/render/GLFont.cpp b/radiant/render/GLFont.cpp index 329d9fff4d..fc7a16e45f 100644 --- a/radiant/render/GLFont.cpp +++ b/radiant/render/GLFont.cpp @@ -9,7 +9,7 @@ namespace gl { GLFont::GLFont(Style style, unsigned int size) : - _pixelHeight(0), + _lineHeight(0), _ftglFont(nullptr) { // Load the locally-provided TTF font file @@ -18,14 +18,14 @@ GLFont::GLFont(Style style, unsigned int size) : .getRuntimeDataPath() + "ui/fonts/"; - fontpath += style == FONT_SANS ? "FreeSans.ttf" : "FreeMono.ttf"; + fontpath += style == Style::Sans ? "FreeSans.ttf" : "FreeMono.ttf"; _ftglFont = FTGL::ftglCreatePixmapFont(fontpath.c_str()); if (_ftglFont) { FTGL::ftglSetFontFaceSize(_ftglFont, size, 0); - _pixelHeight = static_cast(FTGL::ftglGetFontLineHeight(_ftglFont)); + _lineHeight = FTGL::ftglGetFontLineHeight(_ftglFont); } else { @@ -42,4 +42,14 @@ GLFont::~GLFont() } } +float GLFont::getLineHeight() const +{ + return _lineHeight; +} + +void GLFont::drawString(const std::string& string) +{ + FTGL::ftglRenderFont(_ftglFont, string.c_str(), FTGL::RENDER_ALL); +} + } // namespace diff --git a/radiant/render/GLFont.h b/radiant/render/GLFont.h index aa8a9f9d24..ce55df2762 100644 --- a/radiant/render/GLFont.h +++ b/radiant/render/GLFont.h @@ -2,39 +2,28 @@ #include #include +#include "igl.h" namespace gl { -class GLFont +class GLFont : + public IGLFont { private: - int _pixelHeight; - FTGL::FTGLfont* _ftglFont; + float _lineHeight; + FTGL::FTGLfont* _ftglFont; public: - enum Style - { - FONT_SANS, // free sans - FONT_MONO, // free mono - }; - // the constructor will allocate the FTGL font GLFont(Style style, unsigned int size); // Destructor frees the FTGL object again ~GLFont(); - FTGL::FTGLfont* getFtglFont() - { - return _ftglFont; - } + float getLineHeight() const override; - int getPixelHeight() const - { - return _pixelHeight; - } + void drawString(const std::string& string) override; }; -typedef std::shared_ptr GLFontPtr; } // namespace diff --git a/radiant/render/OpenGLModule.cpp b/radiant/render/OpenGLModule.cpp index 8e22669e27..1a996260e3 100644 --- a/radiant/render/OpenGLModule.cpp +++ b/radiant/render/OpenGLModule.cpp @@ -22,7 +22,7 @@ void OpenGLModule::onGLDebugMessage(GLenum source, GLenum type, GLuint id, GLenu void OpenGLModule::sharedContextCreated() { // Initialise the font before firing the extension initialised signal - _font.reset(new gl::GLFont(gl::GLFont::FONT_SANS, 14)); + _font.reset(new gl::GLFont(IGLFont::Style::Sans, 14)); #ifdef ENABLE_KHR_DEBUG_EXTENSION // Debugging @@ -40,11 +40,17 @@ void OpenGLModule::sharedContextDestroyed() _font.reset(); } +IGLFont::Ptr OpenGLModule::getFont(IGLFont::Style style, std::size_t size) +{ + // No caching in this first implementation + return std::make_shared(style, size); +} + void OpenGLModule::drawString(const std::string& string) const { if (_font) { - FTGL::ftglRenderFont(_font->getFtglFont(), string.c_str(), FTGL::RENDER_ALL); + _font->drawString(string); } } @@ -56,7 +62,7 @@ void OpenGLModule::drawChar(char character) const int OpenGLModule::getFontHeight() { - return _font ? _font->getPixelHeight() : 0; + return _font ? _font->getLineHeight() : 0; } const std::string& OpenGLModule::getName() const diff --git a/radiant/render/OpenGLModule.h b/radiant/render/OpenGLModule.h index 021d1e2aea..65bf3759f2 100644 --- a/radiant/render/OpenGLModule.h +++ b/radiant/render/OpenGLModule.h @@ -19,7 +19,7 @@ class OpenGLModule : private: const std::string _unknownError; - gl::GLFontPtr _font; + IGLFont::Ptr _font; sigc::connection _contextCreated; sigc::connection _contextDestroyed; @@ -27,6 +27,8 @@ class OpenGLModule : public: OpenGLModule(); + IGLFont::Ptr getFont(IGLFont::Style style, std::size_t size) override; + void drawString(const std::string& string) const override; void drawChar(char character) const override; int getFontHeight() override;