Skip to content

Commit

Permalink
#5436: Create IGLFont interface and let OpenGLModule return shared re…
Browse files Browse the repository at this point in the history
…ferences to those FTGL wrappers. Declare drawString() method on the font itself.
  • Loading branch information
codereader committed Dec 1, 2020
1 parent 275d194 commit fec5f41
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
27 changes: 25 additions & 2 deletions include/igl.h
Expand Up @@ -55,20 +55,43 @@ inline gl::ISharedGLContextHolder& GlobalOpenGLContext()

const char* const MODULE_OPENGL("OpenGL");

namespace wxutil { class GLWidget; }
class wxGLContext;
class IGLFont
{
public:
using Ptr = std::shared_ptr<IGLFont>;

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
{
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;
};
Expand Down
16 changes: 13 additions & 3 deletions radiant/render/GLFont.cpp
Expand Up @@ -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
Expand All @@ -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<int>(FTGL::ftglGetFontLineHeight(_ftglFont));
_lineHeight = FTGL::ftglGetFontLineHeight(_ftglFont);
}
else
{
Expand All @@ -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
25 changes: 7 additions & 18 deletions radiant/render/GLFont.h
Expand Up @@ -2,39 +2,28 @@

#include <memory>
#include <FTGL/ftgl.h>
#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<GLFont> GLFontPtr;

} // namespace
12 changes: 9 additions & 3 deletions radiant/render/OpenGLModule.cpp
Expand Up @@ -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
Expand All @@ -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<gl::GLFont>(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);
}
}

Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion radiant/render/OpenGLModule.h
Expand Up @@ -19,14 +19,16 @@ class OpenGLModule :
private:
const std::string _unknownError;

gl::GLFontPtr _font;
IGLFont::Ptr _font;

sigc::connection _contextCreated;
sigc::connection _contextDestroyed;

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;
Expand Down

0 comments on commit fec5f41

Please sign in to comment.