From fc6ce7335c064114ee9617ec7fae90fffc16f430 Mon Sep 17 00:00:00 2001 From: James Turk Date: Wed, 29 Jun 2005 04:30:40 +0000 Subject: [PATCH] FTGL crap --- SConstruct | 8 +-- include/photon.hpp | 2 + include/video/Font.hpp | 86 ++++++++++++++++++++++++++ include/video/FontResourceManager.hpp | 42 +++++++++++++ src/video/Font.cpp | 59 ++++++++++++++++++ src/video/FontResourceManager.cpp | 52 ++++++++++++++++ src/video/VideoCore.cpp | 4 +- test/Font_test.cpp | 88 +++++++++++++++++++++++++++ 8 files changed, 336 insertions(+), 5 deletions(-) create mode 100644 include/video/Font.hpp create mode 100644 include/video/FontResourceManager.hpp create mode 100644 src/video/Font.cpp create mode 100644 src/video/FontResourceManager.cpp create mode 100644 test/Font_test.cpp diff --git a/SConstruct b/SConstruct index 60897a6..e51eb9c 100644 --- a/SConstruct +++ b/SConstruct @@ -5,7 +5,7 @@ # James Turk (jpt2433@rit.edu) # # Version: -# $Id: SConstruct,v 1.12 2005/06/11 05:28:41 cozman Exp $ +# $Id: SConstruct,v 1.13 2005/06/29 04:30:40 cozman Exp $ import os,os.path import glob @@ -74,7 +74,7 @@ header.close() BuildDir('build', 'src', duplicate=0) lib = env.Library(os.path.join('lib',LIBRARY), source=SRC_FILES, - CPPPATH = 'include', CPPFLAGS = '-Wall -pedantic') + CPPPATH = ['include', '/usr/include/freetype2/'], CPPFLAGS = '-Wall -pedantic') env.Alias(LIBRARY,lib) env.Default(LIBRARY) @@ -89,8 +89,8 @@ test_srcs = glob.glob( os.path.join('test', '*_test.cpp') ) for test_src in test_srcs: test_name = test_src.replace('_test.cpp','') - tests.append(env.Program(test_name, source=test_src, CPPPATH = INC_DIRS, + tests.append(env.Program(test_name, source=test_src, CPPPATH = INC_DIRS+['/usr/include/freetype2/'], LIBPATH='./lib', CPPFLAGS = '-Wall -pedantic', - LIBS=['photon',OAL_LIB,'glfw',OGL_LIB,GLU_LIB,'physfs','corona'])) + LIBS=['photon',OAL_LIB,'glfw',OGL_LIB,GLU_LIB,'physfs','corona','freetype','ftgl'])) env.Alias('test',tests) diff --git a/include/photon.hpp b/include/photon.hpp index 1c7a39a..8610fb0 100644 --- a/include/photon.hpp +++ b/include/photon.hpp @@ -27,9 +27,11 @@ #include "util/filesys/filesys.hpp" #include "video/Pen.hpp" #include "video/Image.hpp" +#include "video/FontResourceManager.hpp" #include "video/Texture.hpp" #include "video/Color.hpp" #include "video/TextureResourceManager.hpp" #include "video/VideoCore.hpp" +#include "video/Font.hpp" #endif // PHOTON_HPP diff --git a/include/video/Font.hpp b/include/video/Font.hpp new file mode 100644 index 0000000..2a0bc24 --- /dev/null +++ b/include/video/Font.hpp @@ -0,0 +1,86 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: Font.hpp,v 1.1 2005/06/29 04:30:40 cozman Exp $ + +#ifndef PHOTON_VIDEO_FONT_HPP +#define PHOTON_VIDEO_FONT_HPP + +#include "video/FontResourceManager.hpp" +#include "ResourceManaged.hpp" + +namespace photon +{ +namespace video +{ + +// Class: Font +// Simple OO wrapper around a TrueType font that can be drawn to textures and +// rendered via OpenGL. +// +// Since Font is a child of , all memory management is +// taken care of. +// +// Operators: +// - Font = Font +// - bool : True if font is loaded, false if not. +// - ostream& << Font +class Font: public ResourceManaged +{ + +// Group: (Con/De)structors +public: + + + // Function: Font + // Default constructor, initalizes internal state of Font. + Font(); + + // Function: Font + // Copy constructor, copies another Font. + // + // Parameters: + // rhs - Font to construct copy of. + Font(const Font &rhs); + + // Function: Font + // Initializing constructor, loads Font via call to . + // + // Parameters: + // name - Name of the Font to open. + // + // See Also: + // + Font(const std::string& name); + +// Group: General +public: + + // Function: open + // Opens an TrueType font. + // + // Loading is done via FTGL. + // + // Parameters: + // name - Name of the Font to open. + void open(const std::string& name); + + Font& operator=(const Font &rhs); + operator bool() const; + +// Group: Writing +public: + void write(const std::string& str); + +private: + FTFont* font_; +}; + +} +} + +#endif //PHOTON_VIDEO_FONT_HPP diff --git a/include/video/FontResourceManager.hpp b/include/video/FontResourceManager.hpp new file mode 100644 index 0000000..4b9d1b1 --- /dev/null +++ b/include/video/FontResourceManager.hpp @@ -0,0 +1,42 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: FontResourceManager.hpp,v 1.1 2005/06/29 04:30:40 cozman Exp $ + +#ifndef PHOTON_VIDEO_FONTRESOURCEMANAGER_HPP +#define PHOTON_VIDEO_FONTRESOURCEMANAGER_HPP + +#include "ResourceManager.hpp" + +#include "FTGL/FTGL.h" +#include "FTGL/FTFont.h" + +namespace photon +{ +namespace video +{ + +class FontResource : public Resource +{ +public: + FTFont* font; +}; + +class FontResourceManager : public ResourceManager +{ +public: + void getFontData(const std::string& name, FTFont*& font); + +private: + virtual void loadResource(FontResource &res, const std::string& name); + virtual void freeResource(FontResource &res); +}; + +} +} + +#endif //PHOTON_VIDEO_FONTRESOURCEMANAGER_HPP diff --git a/src/video/Font.cpp b/src/video/Font.cpp new file mode 100644 index 0000000..441d3b0 --- /dev/null +++ b/src/video/Font.cpp @@ -0,0 +1,59 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: Font.cpp,v 1.1 2005/06/29 04:30:40 cozman Exp $ + +#include "video/Font.hpp" + + +namespace photon +{ +namespace video +{ + +Font::Font() +{ } + +Font::Font(const Font &rhs) : + ResourceManaged(rhs) +{ + resMgr_.getFontData(getName(), font_); +} + +Font::Font(const std::string& name) +{ + open(name); +} + +void Font::open(const std::string& name) +{ + ResourceManaged::open(name); + resMgr_.getFontData(getName(), font_); +} + +Font& Font::operator=(const Font &rhs) +{ + if(&rhs != this) + { + ResourceManaged::operator=(rhs); + resMgr_.getFontData(getName(), font_); + } + return *this; +} + +Font::operator bool() const +{ + return font_ != 0; +} + +void Font::write(const std::string& str) +{ + font_->Render(str.c_str()); +} + +} +} diff --git a/src/video/FontResourceManager.cpp b/src/video/FontResourceManager.cpp new file mode 100644 index 0000000..c3eba3f --- /dev/null +++ b/src/video/FontResourceManager.cpp @@ -0,0 +1,52 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: FontResourceManager.cpp,v 1.1 2005/06/29 04:30:40 cozman Exp $ + +#include "video/FontResourceManager.hpp" + +#include "util/FileBuffer.hpp" + +#include "FTGL/FTGLTextureFont.h" + + +namespace photon +{ +namespace video +{ + +void FontResourceManager::getFontData(const std::string& name, FTFont*& font) +{ + FontResource resource( getResource(name) ); + font = resource.font; +} + +void FontResourceManager::loadResource(FontResource &res, + const std::string& path) +{ + util::FileBuffer buf(path); + + std::vector data = buf.getData(); + + //res.font = new FTGLTextureFont((ubyte*)&data[0],data.size()); + res.font = new FTGLTextureFont("/usr/share/fonts/truetype/freefont/FreeMono.ttf"); + + if(!res.font || res.font->Error() != 0) + { + throw APIError("Failed to create FTGLTextureFont"); + } + + assert( res.font->FaceSize(6) ); +} + +void FontResourceManager::freeResource(FontResource &res) +{ + delete res.font; +} + +} +} diff --git a/src/video/VideoCore.cpp b/src/video/VideoCore.cpp index 24e9e4b..32defe5 100644 --- a/src/video/VideoCore.cpp +++ b/src/video/VideoCore.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: VideoCore.cpp,v 1.5 2005/06/27 04:24:16 cozman Exp $ +// $Id: VideoCore.cpp,v 1.6 2005/06/29 04:30:40 cozman Exp $ #include "video/VideoCore.hpp" @@ -13,6 +13,7 @@ #include "GL/gl.h" #include "GL/glu.h" +#include "FTGL/FTLibrary.h" namespace photon { @@ -24,6 +25,7 @@ VideoCore::VideoCore() : viewportWidth_(0), viewportHeight_(0) { initOpenGL(); + FTLibrary::Instance().Error(); // ignoring error, fix this } VideoCore::~VideoCore() diff --git a/test/Font_test.cpp b/test/Font_test.cpp new file mode 100644 index 0000000..2c76cfb --- /dev/null +++ b/test/Font_test.cpp @@ -0,0 +1,88 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: Font_test.cpp,v 1.1 2005/06/29 04:30:40 cozman Exp $ + +#include "photon.hpp" +using namespace photon; +#include + +// This is a simple framework for writing extremely simple Photon applications +// it simply displays a blank window and the framerate in the titlebar of +// the created window. This is meant to show basic interaction between the +// user and the more common features of the AppCore and the Kernel. + +class MainTask : public Task +{ + +public: + MainTask() : + Task("MainTask"), + app(AppCore::getInstance()), + video(video::VideoCore::getInstance()) + { + LogSinkPtr csp( new ConsoleSink("console") ); + log.addSink(csp); + + video.setOrthoView(800,600); + + video::Font::addResource("font","data/arial.ttf"); + + font.open("font"); + } + + void update() + { + static double t=0; + + if(app.getTime() - t > 1.0) + { + app.setTitle("FPS: " + + boost::lexical_cast(app.getFramerate()) ); + t = app.getTime(); + } + + video.clear(); + + glColor4ub(255,0,0,255); + glEnable(GL_TEXTURE_2D); + glScaled(1.0/.75,1,1); + font.write("he"); + glBegin(GL_QUADS); + glTexCoord2f(0,0); glVertex2f(150,200); + glTexCoord2f(1,0); glVertex2f(300,350); + glTexCoord2f(1,1); glVertex2f(150,300); + glTexCoord2f(0,1); glVertex2f(200,350); + glEnd(); + glColor4ub(255,255,255,255); + } + +private: + video::Font font; + + Log log; + AppCore& app; + video::VideoCore& video; +}; + +class ImageTest : public Application +{ +public: + + int main(const StrVec& args) + { + AppCore::getInstance().createDisplay(800,600,32,0,0,false); + + Kernel::getInstance().addTask(TaskPtr(new MainTask())); + + Kernel::getInstance().run(); + + return 0; + } +}; + +ENTRYPOINT(ImageTest)