]> git.mxchange.org Git - flightgear.git/commitdiff
Add a subclass of fntTexFont that defers font loading
authortimoore <timoore>
Tue, 6 May 2008 21:31:01 +0000 (21:31 +0000)
committertimoore <timoore>
Tue, 6 May 2008 21:31:01 +0000 (21:31 +0000)
Loading is deferred until the first render so that it has a chance of
happening in the proper graphics context.

projects/VC7.1/FlightGear.vcproj
src/GUI/Makefile.am
src/GUI/SafeTexFont.cxx [new file with mode: 0644]
src/GUI/SafeTexFont.hxx [new file with mode: 0644]
src/GUI/new_gui.cxx

index 8dd557f5dfbc38420128508369aa655fc9e543a6..5d26d2f2921647c4126c65a8e00cf594ab2f5c1f 100755 (executable)
                        <File
                                RelativePath="..\..\src\GUI\trackball.h">
                        </File>
+                       <File
+                               RelativePath="..\..\src\GUI\SafeTexFont.cxx">
+                       </File>
+                       <File
+                               RelativePath="..\..\src\GUI\SafeTexFont.hxx">
+                       </File>
+
                </Filter>
                <Filter
                        Name="Lib_Input"
index f956c1407254e4259231922adada191001b359cf..960580cf4fbf6eabe723fcbbf827958fa3c0edf1 100644 (file)
@@ -11,7 +11,8 @@ libGUI_a_SOURCES = \
        trackball.c trackball.h \
        AirportList.cxx AirportList.hxx \
         property_list.cxx property_list.hxx \
-        layout.cxx layout-props.cxx layout.hxx
+        layout.cxx layout-props.cxx layout.hxx \
+       SafeTexFont.cxx SafeTexFont.hxx
 
 INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/src
 
diff --git a/src/GUI/SafeTexFont.cxx b/src/GUI/SafeTexFont.cxx
new file mode 100644 (file)
index 0000000..e40f489
--- /dev/null
@@ -0,0 +1,76 @@
+// Copyright (C) 2008  Tim Moore
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#include "SafeTexFont.hxx"
+
+using namespace flightgear;
+
+int SafeTexFont::load(const char *fname, GLenum mag, GLenum min)
+{
+    struct stat buf;
+    if (stat(fname, &buf) == -1) {
+        _status = ERROR;
+        return FNT_FALSE;
+    }
+    _name = fname;
+    _mag = mag;
+    _min = min;
+    return FNT_TRUE;
+}
+
+bool SafeTexFont::ensureTextureLoaded()
+{
+    if (_status != ERROR) {
+        if (_status == LOADED) {
+            return true;
+        } else {
+            int loadStatus = fntTexFont::load(_name.c_str(), _mag, _min);
+            if (loadStatus == FNT_TRUE) {
+                _status = LOADED;
+                return true;
+            } else {
+                _status = ERROR;
+                _error = ulGetError();
+                return false;
+            }    
+        }
+    } else {
+        return false;
+    }
+}
+
+void SafeTexFont::begin()
+{
+    if (ensureTextureLoaded())
+        fntTexFont::begin();
+}
+
+void SafeTexFont::putch(sgVec3 curpos, float pointsize, float italic, char c)
+{
+    if (ensureTextureLoaded())
+        fntTexFont::putch(curpos, pointsize, italic, c);
+}
+
+void SafeTexFont::puts(sgVec3 curpos, float pointsize, float italic,
+                       const char *s)
+{
+    if (ensureTextureLoaded())
+        fntTexFont::puts(curpos, pointsize, italic, s);
+}
diff --git a/src/GUI/SafeTexFont.hxx b/src/GUI/SafeTexFont.hxx
new file mode 100644 (file)
index 0000000..c1ca718
--- /dev/null
@@ -0,0 +1,64 @@
+// Copyright (C) 2008  Tim Moore
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+#ifndef FLIGHTGEAR_SAFETEXFONT_HXX
+#define FLIGHTGEAR_SAFETEXFONT_HXX 1
+
+#include <string>
+
+#include <plib/fnt.h>
+
+namespace flightgear
+{
+/**
+ * Subclass of plib's fntTexFont that defers the loading of the font
+ * texture until just before rendering, insuring that it happens in
+ * the proper graphics context. 
+ */
+class SafeTexFont : public fntTexFont
+{
+public:
+    SafeTexFont() : _status(NOT_LOADED) {}
+    /** Load the texture for this font.
+     * @param mag OpenGL texture magnification; default is GL_NEAREST
+     * @param min OpenGL texture minification; default is
+     * GL_LINEAR_MIPMAP_LINEAR.
+     * @return the plib value FNT_FALSE if the font file doesn't
+     * exist; otherwise returns FNT_TRUE.
+     */
+    int load(const char *fname, GLenum mag = GL_NEAREST, 
+             GLenum min = GL_LINEAR_MIPMAP_LINEAR);
+    void begin();
+    void putch(sgVec3 curpos, float pointsize, float italic, char c);
+    void puts(sgVec3 curpos, float pointsize, float italic, const char *s);
+    enum FontStatus
+    {
+        ERROR = -1,
+        NOT_LOADED = 0,
+        LOADED = 1
+    };
+    FontStatus getStatus() { return _status; }
+    std::string& fntError() { return _error; }
+protected:
+    bool ensureTextureLoaded();
+    std::string _name;
+    std::string _error;
+    GLenum _mag;
+    GLenum _min;
+    FontStatus _status;
+};
+}
+#endif
index 633d32b3e2e57a56409ad60ed03851168bc89783..ce98f5eb78128a6e1f4bf306f287bd5e57d64f52 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "menubar.hxx"
 #include "dialog.hxx"
+#include "SafeTexFont.hxx"
 
 extern puFont FONT_HELVETICA_14;
 extern puFont FONT_SANS_12B;
@@ -432,7 +433,7 @@ FGFontCache::getfnt(const char *name, float size, float slant)
     SGPath path = getfntpath(name);
 
     fnt *f = new fnt();
-    f->texfont = new fntTexFont;
+    f->texfont = new flightgear::SafeTexFont;
 
     if (f->texfont->load((char *)path.c_str())) {
         f->pufont = new puFont;