wave-glut/lib/glut-font/test/test.cpp

Go to the documentation of this file.
00001 /*
00002  * test.cpp
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Program to test the glut-font library.
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include <iostream>
00012 
00013 #include "glut-demo/glut-demo.h"
00014 #include "glut-font/glut-font.h"
00015 #include "perf/perf.h"
00016 #include "util/file.h"
00017 
00018 
00019 
00020 static const int s_desiredHeight                = 36;   // pixel height of line
00021 
00022 
00023 ////////////////////////////////////////////////////////////////////////////////
00024 //
00025 //      static helper methods
00026 //
00027 ////////////////////////////////////////////////////////////////////////////////
00028 
00029 static void
00030 writeLine
00031 (
00032 IN glut::Font * font,
00033 IN const char * text
00034 )
00035 {
00036         ASSERT(font, "null");
00037         ASSERT(text, "null");
00038 
00039         glut::font_rect_t fr = font->getBoundingRect(text);
00040         if (fr.rise > 0) {
00041                 glTranslatef(0, fr.rise, 0);
00042         }
00043 
00044         glColor3f(0.3, 0.3, 0.3);
00045         glBegin(GL_LINES);
00046                 glVertex3f(-fr.lead, 0, 0);
00047                 glVertex3f(fr.trail, 0, 0);
00048                 glVertex3f(0, -fr.rise, 0);
00049                 glVertex3f(0, fr.drop, 0);
00050         glEnd();
00051         glBegin(GL_LINE_LOOP);
00052                 glVertex3f(-fr.lead, -fr.rise, 0);
00053                 glVertex3f(fr.trail, -fr.rise, 0);
00054                 glVertex3f(fr.trail, fr.drop, 0);
00055                 glVertex3f(-fr.lead, fr.drop, 0);
00056         glEnd();
00057 
00058         glColor3f(1.0, 0.6, 1.0);
00059         font->display(0, 0, 0, text);
00060 
00061         glTranslatef(0, fr.drop + 5, 0);
00062 }
00063 
00064 
00065 
00066 class Drawer : public glut::DemoHost {
00067 public:
00068         Drawer(IN const char * dir) {
00069                         ASSERT(dir, "null");
00070                         m_fontDir = dir;
00071                         m_index = -2;
00072                 }
00073         ~Drawer(void) throw() { }
00074 
00075         // glut::Host class interface methods ----------------------------------
00076         void onInit(void) {
00077                         // first, find all true type files...
00078                         this->addFiles("ttf");
00079 
00080                         // create font manager
00081                         m_fontMgr = glut::FontManager::create();
00082                         ASSERT_THROW(m_fontMgr,
00083                             "failed to create font manager");
00084 
00085                         this->updateFont(-1);   // start with default
00086                 }
00087 
00088         bool displayAxes(void) { return false; }
00089 
00090         void onKey(IN int key, IN int mods) {
00091                         int deltaIndex = 0;
00092                         switch (key) {
00093                         case 'n':       deltaIndex = +1;        break;
00094                         case 'N':       deltaIndex = -1;        break;
00095                         default:
00096                                 // ignore!
00097                                 break;
00098                         }
00099 
00100                         int nMax = m_paths.size();
00101                         int newIndex = m_index + deltaIndex;
00102                         if (newIndex < -1) {
00103                                 newIndex = nMax - 1;
00104                         } else if (newIndex >= nMax) {
00105                                 newIndex = -1;
00106                         }
00107                         this->updateFont(newIndex);
00108                 }
00109 
00110         void display2D(IN int width, IN int height) {
00111                         ASSERT(m_currFont, "null");
00112 
00113                         int nMax = m_paths.size();
00114                         const int bufsize = 1024;
00115                         char buffer[bufsize];
00116                         snprintf(buffer, bufsize, "font: %s",
00117                             m_currFont->getName());
00118                         glut::Font * defFont = glut::getDefaultFont();
00119                         defFont->display(1, 47, 0, buffer);
00120                         snprintf(buffer, bufsize, "font %d of %d",
00121                             m_index + 1, nMax);
00122                         defFont->display(1, 62, 0, buffer);
00123                         snprintf(buffer, bufsize, "font size: %4.1f point",
00124                             m_currFont->getFaceSize());
00125                         defFont->display(1, 77, 0, buffer);
00126                         defFont->display(1, 92, 0, "(n)ext font");
00127                         int yStart = 92 + 16;
00128 
00129                         glMatrixMode(GL_MODELVIEW);
00130                         glPushMatrix();
00131                         glTranslatef(16, yStart, 0);
00132                         writeLine(m_currFont, "Hello World!");
00133                         writeLine(m_currFont, "This is line 2...");
00134                         writeLine(m_currFont, "abcdefghijklmnopqrstuvwxyz");
00135                         writeLine(m_currFont, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
00136                         writeLine(m_currFont, "0123456789!@#$%^&*()-=_+");
00137                         writeLine(m_currFont, "{}[]|\\<>,.?/");
00138                         writeLine(m_currFont, "ao");
00139                         writeLine(m_currFont, "___");
00140                         writeLine(m_currFont, "You have +3 dollars to spend!");
00141                         writeLine(m_currFont, "This is not a true story");
00142                         writeLine(m_currFont, "    ...but it could be.");
00143 
00144                         glPopMatrix();
00145                 }
00146         
00147 private:
00148         // private typedefs ----------------------------------------------------
00149 
00150         // private helper methods ----------------------------------------------
00151         void addFiles(IN const char * ext) {
00152                         ASSERT(ext, "null extension");
00153                         VecString files;
00154                         walkDirectoryTree(m_fontDir.c_str(), ext, files);
00155                         int nFiles = files.size();
00156                         for (int i = 0; i < nFiles; ++i) {
00157                                 const char * path = files[i].c_str();
00158                                 std::string full_path;
00159                                 appendPath(m_fontDir.c_str(), path, full_path);
00160                                 DPRINTF("Font file: '%s'", full_path.c_str());
00161 
00162                                 m_paths.push_back(full_path);
00163                         }
00164                 }
00165 
00166         void updateFont(IN int newIndex) {
00167                         if (m_index == newIndex)
00168                                 return;         // nothing to do!
00169                         m_index = newIndex;
00170 
00171                         int nMax = m_paths.size();
00172                         const char * path = NULL;
00173                         if (m_index >= 0 && m_index < nMax) {
00174                                 path = m_paths[m_index].c_str();
00175                         }
00176                         m_currFont = m_fontMgr->getFont(path);
00177                         ASSERT(m_currFont, "null font?");
00178 
00179                         // resize
00180                         scaleFontToPixelHeight(m_currFont, "M", s_desiredHeight);
00181                 }
00182 
00183         // private member data -------------------------------------------------
00184         std::string                     m_fontDir;
00185         VecString                       m_paths;
00186         smart_ptr<glut::FontManager>    m_fontMgr;
00187         smart_ptr<glut::Font>           m_currFont;
00188         int                             m_index;
00189 };
00190 
00191 
00192 
00193 ////////////////////////////////////////////////////////////////////////////////
00194 //
00195 //      entry point
00196 //
00197 ////////////////////////////////////////////////////////////////////////////////
00198 
00199 int
00200 main
00201 (
00202 IN int argc,
00203 IN const char * argv[]
00204 )
00205 {
00206         ASSERT(2 == argc, "Usage: glut-font-test <font-dir>");
00207         const char * font_dir = argv[1];
00208         DPRINTF("Using font directory: '%s'", font_dir);
00209 
00210         try {
00211                 // title
00212                 std::string title = "glut-font test: ";
00213                 title += font_dir;
00214 
00215                 // main loop
00216                 smart_ptr<glut::DemoHost> host = new Drawer(font_dir);
00217                 ASSERT(host, "out of memory");
00218                 glut::startDemo(argc, argv, title.c_str(), host);
00219 
00220         } catch (std::exception& e) {
00221                 DPRINTF("Exception: %s", e.what());
00222         }
00223 
00224         // only reach here on error
00225         return -1;
00226 }
00227