From: Rudolf Polzer Date: Thu, 18 Nov 2010 15:52:51 +0000 (+0100) Subject: refactoring font code to hopefully be closer to be able to exchange the font system... X-Git-Tag: xonotic-v0.5.0~144^2~3 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=3ce82871e397e352c034d34795e849d08ca6792b;p=xonotic%2Fnetradiant.git refactoring font code to hopefully be closer to be able to exchange the font system by something not call list based --- diff --git a/include/igl.h b/include/igl.h index 7ea3b944..8f7d7dc0 100644 --- a/include/igl.h +++ b/include/igl.h @@ -1976,6 +1976,7 @@ typedef unsigned int GLhandleARB; #endif +#include "gtkutil/glfont.h" /// \brief A module which wraps a runtime-binding of the standard OpenGL functions. /// Provides convenience functions for querying availabiliy of extensions, rendering text and error-checking. @@ -1997,7 +1998,7 @@ struct OpenGLBinding /// \brief Asserts that there no OpenGL errors have occurred since the last call to glGetError. void (*assertNoErrors)(const char *file, int line); - GLuint m_font; + GLFont *m_font; // MUST be set! int m_fontHeight; int m_fontAscent; int m_fontDescent; @@ -2005,8 +2006,7 @@ struct OpenGLBinding /// \brief Renders \p string at the current raster-position of the current context. void drawString(const char* string) const { - m_glListBase(m_font); - m_glCallLists(GLsizei(strlen(string)), GL_UNSIGNED_BYTE, reinterpret_cast(string)); + m_font->printString(string); } /// \brief Renders \p character at the current raster-position of the current context. diff --git a/libs/gtkutil/glfont.cpp b/libs/gtkutil/glfont.cpp index f2095a8b..eee1113e 100644 --- a/libs/gtkutil/glfont.cpp +++ b/libs/gtkutil/glfont.cpp @@ -20,10 +20,45 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "glfont.h" +#include "igl.h" + +// generic string printing with call lists +class GLFontCallList: public GLFont +{ + GLuint m_displayList; + int m_pixelHeight; + int m_pixelAscent; + int m_pixelDescent; + public: + GLFontCallList(GLuint displayList, int asc, int desc, int pixelHeight) : m_displayList(displayList), m_pixelHeight(pixelHeight), m_pixelAscent(asc), m_pixelDescent(desc) + { + } + virtual ~GLFontCallList() + { + glDeleteLists(m_displayList, 256); + } + void printString(const char *s) + { + GlobalOpenGL().m_glListBase(m_displayList); + GlobalOpenGL().m_glCallLists(GLsizei(strlen(s)), GL_UNSIGNED_BYTE, reinterpret_cast(s)); + } + virtual int getPixelAscent() const + { + return m_pixelAscent; + } + virtual int getPixelDescent() const + { + return m_pixelDescent; + } + virtual int getPixelHeight() const + { + return m_pixelHeight; + } +}; + #ifdef _WIN32 #include #endif -#include #include "debugging/debugging.h" // LordHavoc: this is code for direct Xlib bitmap character fetching, as an @@ -38,7 +73,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include -GLFont glfont_create(const char* font_string) +GLFont *glfont_create(const char* font_string) { GLuint font_list_base; XFontStruct *fontInfo; @@ -91,20 +126,14 @@ GLFont glfont_create(const char* font_string) /* *height = fontInfo->ascent + fontInfo->descent; *width = fontInfo->max_bounds.width; */ - return GLFont(font_list_base, fontInfo->ascent + fontInfo->descent); -} - -void glfont_release(GLFont& font) -{ - glDeleteLists(font.getDisplayList(), 256); - font = GLFont(0, 0); + return new GLFontCallList(font_list_base, fontInfo->ascent, fontInfo->descent, fontInfo->ascent + fontInfo->descent); } #else #include -GLFont glfont_create(const char* font_string) +GLFont *glfont_create(const char* font_string) { GLuint font_list_base = glGenLists (256); gint font_height = 0, font_ascent = 0, font_descent = 0; @@ -148,25 +177,16 @@ GLFont glfont_create(const char* font_string) if(font_height > 256) font_height = 16; - return GLFont(font_list_base, font_ascent, font_descent, font_height); -} - -void glfont_release(GLFont& font) -{ - glDeleteLists(font.getDisplayList(), 256); - font = GLFont(0, 0, 0, 0); + return new GLFontCallList(font_list_base, font_ascent, font_descent, font_height); } #endif - - // new font code ripped from ZeroRadiant (not in use yet) #include #include -#include "igl.h" -class GLFontInternal +class GLFontInternal: public GLFont { const char *font_string; int font_height; @@ -185,10 +205,13 @@ class GLFontInternal int font_ascent_pango_units; int font_descent_pango_units; - //ft2_context = pango_ft2_get_context(72, 72); +#if !PANGO_VERSION_CHECK(1,22,0) + ft2_context = pango_ft2_get_context(72, 72); +#else fontmap = pango_ft2_font_map_new(); pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP(fontmap), 72, 72); ft2_context = pango_font_map_create_context(fontmap); +#endif font_desc = pango_font_description_from_string(font_string); //pango_font_description_set_size(font_desc, 10 * PANGO_SCALE); @@ -215,7 +238,7 @@ class GLFontInternal y_offset_bitmap_render_pango_units = (font_ascent * PANGO_SCALE) - font_ascent_pango_units; } - ~GLFontInternal() + virtual ~GLFontInternal() { g_object_unref(G_OBJECT(ft2_context)); g_object_unref(G_OBJECT(fontmap)); @@ -231,7 +254,7 @@ class GLFontInternal // just a hair outside of the viewport (meaning the current raster position is invalid), // then no text will be rendered. The solution to this is a very hacky one. You can search // Google for "glDrawPixels clipping". - void printString(const char *s) + virtual void printString(const char *s) { // The idea for this code initially came from the font-pangoft2.c example that comes with GtkGLExt. @@ -302,4 +325,17 @@ class GLFontInternal g_object_unref(G_OBJECT(layout)); } + + virtual int getPixelAscent() const + { + return font_ascent; + } + virtual int getPixelDescent() const + { + return font_descent; + } + virtual int getPixelHeight() const + { + return font_height; + } }; diff --git a/libs/gtkutil/glfont.h b/libs/gtkutil/glfont.h index 454c6a97..0d1e0000 100644 --- a/libs/gtkutil/glfont.h +++ b/libs/gtkutil/glfont.h @@ -26,33 +26,17 @@ typedef unsigned int GLuint; class GLFont { - GLuint m_displayList; - int m_pixelHeight; - int m_pixelAscent; - int m_pixelDescent; -public: - GLFont(GLuint displayList, int asc, int desc, int pixelHeight) : m_displayList(displayList), m_pixelHeight(pixelHeight), m_pixelAscent(asc), m_pixelDescent(desc) - { - } - GLuint getDisplayList() const - { - return m_displayList; - } - int getPixelHeight() const - { - return m_pixelHeight; - } - int getPixelAscent() const - { - return m_pixelAscent; - } - int getPixelDescent() const - { - return m_pixelDescent; - } + public: + virtual int getPixelHeight() const = 0; + virtual int getPixelAscent() const = 0; + virtual int getPixelDescent() const = 0; + virtual void printString(const char *s) = 0; + virtual ~GLFont() + { + } }; -GLFont glfont_create(const char* font_string); -void glfont_release(GLFont& font); +GLFont *glfont_create(const char* font_string); +// release with delete #endif diff --git a/radiant/mainframe.cpp b/radiant/mainframe.cpp index ea4edf31..18c82f95 100644 --- a/radiant/mainframe.cpp +++ b/radiant/mainframe.cpp @@ -3316,13 +3316,10 @@ void GridStatus_onTextureLockEnabledChanged() } } -namespace -{ - GLFont g_font(0, 0, 0, 0); -} - void GlobalGL_sharedContextCreated() { + GLFont *g_font = NULL; + // report OpenGL information globalOutputStream() << "GL_VENDOR: " << reinterpret_cast(glGetString (GL_VENDOR)) << "\n"; globalOutputStream() << "GL_RENDERER: " << reinterpret_cast(glGetString (GL_RENDERER)) << "\n"; @@ -3347,10 +3344,10 @@ void GlobalGL_sharedContextCreated() g_font = glfont_create(fontname); #endif - GlobalOpenGL().m_font = g_font.getDisplayList(); - GlobalOpenGL().m_fontHeight = g_font.getPixelHeight(); - GlobalOpenGL().m_fontAscent = g_font.getPixelAscent(); - GlobalOpenGL().m_fontDescent = g_font.getPixelDescent(); + GlobalOpenGL().m_font = g_font; + GlobalOpenGL().m_fontHeight = g_font->getPixelHeight(); + GlobalOpenGL().m_fontAscent = g_font->getPixelAscent(); + GlobalOpenGL().m_fontDescent = g_font->getPixelDescent(); } void GlobalGL_sharedContextDestroyed()