From 4de63e72c6f140bcc208355c0561a147ed13f857 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 20 Jan 2003 20:28:28 +0000 Subject: [PATCH] Improvements to the handling of multiple graphics contexts. Still needs alot of work, but at least it isn't crashes in trivial cases right now. --- include/osgText/Text | 3 +++ src/osgText/FTFont.cpp | 14 +++++++++++--- src/osgText/FTFont.h | 18 ++++++++++++------ src/osgText/Text.cpp | 24 ++++++++++++++++++++++-- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/include/osgText/Text b/include/osgText/Text index 1c3b529c8..5d0be040c 100644 --- a/include/osgText/Text +++ b/include/osgText/Text @@ -124,6 +124,9 @@ class OSGTEXT_EXPORT Text : public osg::Drawable void setEncodedText(EncodedText* encodedText) { _encodedText = encodedText; } const EncodedText* getEncodedText() const { return _encodedText.get(); } + /// override the compile to set up the alignment etc. + virtual void compile(osg::State& state) const; + protected: enum FontType diff --git a/src/osgText/FTFont.cpp b/src/osgText/FTFont.cpp index 9aa621efc..f237586ec 100644 --- a/src/osgText/FTFont.cpp +++ b/src/osgText/FTFont.cpp @@ -109,7 +109,9 @@ int FTFont::Descender() const float FTFont::Advance( const wchar_t* string) { // all are the same, a bit a hack - FTGlyphContainer* glyphList=_contextGlyphList[0]; + FTGlyphContainer* glyphList=getValidGlypContainer(); + + if (!glyphList) return 1.0f; const wchar_t* c = string; // wchar_t IS unsigned? float width = 0; @@ -128,7 +130,10 @@ float FTFont::Advance( const wchar_t* string) float FTFont::Advance( const char* string) { // all are the same, a bit a hack - FTGlyphContainer* glyphList=_contextGlyphList[0]; + FTGlyphContainer* glyphList=getValidGlypContainer(); + + if (!glyphList) return 1.0f; + const unsigned char* c = (unsigned char*)string; // This is ugly, what is the c++ way? float width = 0; @@ -145,7 +150,10 @@ float FTFont::Advance( std::vector::const_iterator first, std::vector::const_iterator last) { // all are the same, a bit a hack - FTGlyphContainer* glyphList=_contextGlyphList[0]; + FTGlyphContainer* glyphList=getValidGlypContainer(); + + if (!glyphList) return 1.0f; + float width = 0; for (std::vector::const_iterator c = first; diff --git a/src/osgText/FTFont.h b/src/osgText/FTFont.h index d157a30db..3d0895266 100644 --- a/src/osgText/FTFont.h +++ b/src/osgText/FTFont.h @@ -180,12 +180,7 @@ class FTGL_EXPORT FTFont */ FTSize charSize; - /** - * An object that holds a list of glyphs - */ - // mrn@changes -// FTGlyphContainer* glyphList; - + /** * The number of glyphs in this font */ @@ -207,6 +202,17 @@ class FTGL_EXPORT FTFont // mrn@changes typedef std::vector GlyphContextContainer; GlyphContextContainer _contextGlyphList; + + FTGlyphContainer* getValidGlypContainer() + { + for(GlyphContextContainer::iterator itr=_contextGlyphList.begin(); + itr!=_contextGlyphList.end(); + ++itr) + { + if ((*itr)!=0) return *itr; + } + return 0; + } private: diff --git a/src/osgText/Text.cpp b/src/osgText/Text.cpp index c55c58366..6e28ece95 100644 --- a/src/osgText/Text.cpp +++ b/src/osgText/Text.cpp @@ -128,7 +128,7 @@ setDefaults() _initAlignment=false; - _useDisplayList=false; + _useDisplayList=false; _encodedText = new EncodedText(); } @@ -191,6 +191,24 @@ void Text::accept(PrimitiveFunctor& functor) const functor.drawArrays( GL_QUADS, 0, 4); } +void Text::compile(State& state) const +{ + // ahhhh, this is bit doddy, the draw is potentially + // modifying the text object, this isn't thread safe. + Text* this_non_const = const_cast(this); + + if(!_font->isCreated() || !(this_non_const->_font->getFont()->Created(state.getContextID()))) + { + this_non_const->_font->create(state); + this_non_const->dirtyBound(); + } + + if(!_initAlignment) + { + this_non_const->initAlignment(); + } +} + void Text::drawImplementation(State& state) const { if(!_init) @@ -200,14 +218,16 @@ void Text::drawImplementation(State& state) const // modifying the text object, this isn't thread safe. Text* this_non_const = const_cast(this); - if(!_font->isCreated()) + if(!_font->isCreated() || !(this_non_const->_font->getFont()->Created(state.getContextID()))) { this_non_const->_font->create(state); this_non_const->dirtyBound(); } if(!_initAlignment) + { this_non_const->initAlignment(); + } // we must disable all the vertex arrays to prevent any state // propagating into text.