From d83382d2367eb3f7e5569f003f37ae2520d12383 Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Sun, 17 Jul 2022 20:53:38 +0400 Subject: [PATCH] Rework Profiler to work with VFS --- components/resource/stats.cpp | 64 +++++++++++++++++++++++++++-------- components/resource/stats.hpp | 11 +++++- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/components/resource/stats.cpp b/components/resource/stats.cpp index ba39b638b5..690e41bbaf 100644 --- a/components/resource/stats.cpp +++ b/components/resource/stats.cpp @@ -84,6 +84,26 @@ static void setupStatCollection() } } +class SetFontVisitor : public osg::NodeVisitor +{ +public: + SetFontVisitor(osgText::Font* font) + : osg::NodeVisitor(TRAVERSE_ALL_CHILDREN) + , mFont(font) + {} + + void apply(osg::Drawable& node) override + { + if (osgText::Text* text = dynamic_cast(&node)) + { + text->setFont(mFont); + } + } + +private: + osgText::Font* mFont; +}; + StatsHandler::StatsHandler(bool offlineCollect, VFS::Manager* vfs): _key(osgGA::GUIEventAdapter::KEY_F4), _initialized(false), @@ -91,8 +111,7 @@ StatsHandler::StatsHandler(bool offlineCollect, VFS::Manager* vfs): _offlineCollect(offlineCollect), _statsWidth(1280.0f), _statsHeight(1024.0f), - _characterSize(18.0f), - _VFS(vfs) + _characterSize(18.0f) { _camera = new osg::Camera; _camera->getOrCreateStateSet()->setGlobalDefaults(); @@ -100,29 +119,49 @@ StatsHandler::StatsHandler(bool offlineCollect, VFS::Manager* vfs): _camera->setProjectionResizePolicy(osg::Camera::FIXED); _resourceStatsChildNum = 0; + + if (osgDB::Registry::instance()->getReaderWriterForExtension("ttf") && vfs->exists(sFontName)) + { + Files::IStreamPtr streamPtr = vfs->get(sFontName); + _textFont = osgText::readRefFontStream(*streamPtr.get()); + } } Profiler::Profiler(bool offlineCollect, VFS::Manager* vfs): - _offlineCollect(offlineCollect) + _offlineCollect(offlineCollect), + _initFonts(false) { + _characterSize = 18; + _font.clear(); + if (osgDB::Registry::instance()->getReaderWriterForExtension("ttf") && vfs->exists(sFontName)) { - _font = vfs->getAbsoluteFileName(sFontName); + Files::IStreamPtr streamPtr = vfs->get(sFontName); + _textFont = osgText::readRefFontStream(*streamPtr.get()); } - else - _font.clear(); - - _characterSize = 18; setKeyEventTogglesOnScreenStats(osgGA::GUIEventAdapter::KEY_F3); setupStatCollection(); } +void Profiler::setUpFonts() +{ + if (_textFont != nullptr) + { + SetFontVisitor visitor(_textFont); + _switch->accept(visitor); + } + + _initFonts = true; +} + bool Profiler::handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa) { osgViewer::ViewerBase* viewer = nullptr; bool handled = StatsHandler::handle(ea, aa); + if (_initialized && !_initFonts) + setUpFonts(); auto* view = dynamic_cast(&aa); if (view) @@ -457,13 +496,10 @@ void StatsHandler::setUpScene(osgViewer::ViewerBase *viewer) statsText->setText(""); statsText->setDrawCallback(new ResourceStatsTextDrawCallback(viewer->getViewerStats(), statNames)); - if (osgDB::Registry::instance()->getReaderWriterForExtension("ttf") && _VFS->exists(sFontName)) + if (_textFont) { - Files::IStreamPtr streamPtr = _VFS->get(sFontName); - osg::ref_ptr font = osgText::readRefFontStream(*streamPtr.get()); - - staticText->setFont(font); - statsText->setFont(font); + staticText->setFont(_textFont); + statsText->setFont(_textFont); } } } diff --git a/components/resource/stats.hpp b/components/resource/stats.hpp index bb6cf09eda..e51474c6cb 100644 --- a/components/resource/stats.hpp +++ b/components/resource/stats.hpp @@ -13,6 +13,11 @@ namespace osg class Switch; } +namespace osgText +{ + class Font; +} + namespace VFS { class Manager; @@ -27,7 +32,11 @@ namespace Resource bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) override; private: + void setUpFonts(); + bool _offlineCollect; + bool _initFonts; + osg::ref_ptr _textFont; }; class StatsHandler : public osgGA::GUIEventHandler @@ -65,7 +74,7 @@ namespace Resource int _resourceStatsChildNum; - VFS::Manager* _VFS; + osg::ref_ptr _textFont; }; void CollectStatistics(osgViewer::ViewerBase* viewer);