| 1 | // Integrate Canvas into FlightGear |
| 2 | // |
| 3 | // Copyright (C) 2012 Thomas Geymayer <[email protected]> |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or |
| 6 | // modify it under the terms of the GNU General Public License as |
| 7 | // published by the Free Software Foundation; either version 2 of the |
| 8 | // License, or (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, but |
| 11 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | // General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program; if not, write to the Free Software |
| 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | |
| 19 | #include "FGCanvasSystemAdapter.hxx" |
| 20 | |
| 21 | #include <Main/globals.hxx> |
| 22 | #include <Network/HTTPClient.hxx> |
| 23 | #include <Viewer/renderer.hxx> |
| 24 | |
| 25 | #include <osg/Version> |
| 26 | #include <osgDB/ReadFile> |
| 27 | #include <stdexcept> |
| 28 | |
| 29 | #include <simgear/scene/util/SGReaderWriterOptions.hxx> |
| 30 | |
| 31 | namespace canvas |
| 32 | { |
| 33 | //---------------------------------------------------------------------------- |
| 34 | simgear::canvas::FontPtr |
| 35 | FGCanvasSystemAdapter::getFont(const std::string& name) const |
| 36 | { |
| 37 | SGPath path = globals->resolve_resource_path("Fonts/" + name); |
| 38 | if( path.isNull() ) |
| 39 | { |
| 40 | SG_LOG |
| 41 | ( |
| 42 | SG_GL, |
| 43 | SG_ALERT, |
| 44 | "canvas::Text: No such font: " << name |
| 45 | ); |
| 46 | return simgear::canvas::FontPtr(); |
| 47 | } |
| 48 | |
| 49 | SG_LOG |
| 50 | ( |
| 51 | SG_GL, |
| 52 | SG_DEBUG, |
| 53 | "canvas::Text: using font file " << path |
| 54 | ); |
| 55 | |
| 56 | simgear::canvas::FontPtr font = osgText::readFontFile(path.utf8Str()); |
| 57 | if( !font ) |
| 58 | SG_LOG |
| 59 | ( |
| 60 | SG_GL, |
| 61 | SG_ALERT, |
| 62 | "canvas::Text: Failed to open font file " << path |
| 63 | ); |
| 64 | |
| 65 | return font; |
| 66 | } |
| 67 | |
| 68 | //---------------------------------------------------------------------------- |
| 69 | void FGCanvasSystemAdapter::addCamera(osg::Camera* camera) const |
| 70 | { |
| 71 | globals->get_renderer()->addCamera(camera, false); |
| 72 | } |
| 73 | |
| 74 | //---------------------------------------------------------------------------- |
| 75 | void FGCanvasSystemAdapter::removeCamera(osg::Camera* camera) const |
| 76 | { |
| 77 | if( globals->get_renderer() ) |
| 78 | globals->get_renderer()->removeCamera(camera); |
| 79 | } |
| 80 | |
| 81 | //---------------------------------------------------------------------------- |
| 82 | osg::ref_ptr<osg::Image> FGCanvasSystemAdapter::getImage(const std::string& path) const |
| 83 | { |
| 84 | SGPath p(SGPath::fromUtf8(path)); |
| 85 | osg::ref_ptr<simgear::SGReaderWriterOptions> localReaderWriterOptions = simgear::SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions()); |
| 86 | localReaderWriterOptions->setLoadOriginHint(simgear::SGReaderWriterOptions::LoadOriginHint::ORIGIN_CANVAS); |
| 87 | |
| 88 | if( p.isAbsolute() ) |
| 89 | { |
| 90 | const SGPath valid_path = SGPath(p).validate(false); |
| 91 | if( !valid_path.isNull() ) |
| 92 | return osgDB::readRefImageFile(valid_path.utf8Str(), localReaderWriterOptions); |
| 93 | |
| 94 | SG_LOG(SG_IO, SG_ALERT, "canvas::Image: reading '" << path << "' denied"); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | SGPath tpath = globals->resolve_resource_path(path); |
| 99 | if( !tpath.isNull() ) |
| 100 | return osgDB::readRefImageFile(tpath.utf8Str(), localReaderWriterOptions); |
| 101 | |
| 102 | SG_LOG(SG_IO, SG_ALERT, "canvas::Image: No such image: '" << path << "'"); |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | //---------------------------------------------------------------------------- |
| 109 | SGSubsystem* |
| 110 | FGCanvasSystemAdapter::getSubsystem(const std::string& name) const |
| 111 | { |
| 112 | return globals->get_subsystem_mgr()->get_subsystem(name.c_str()); |
| 113 | } |
| 114 | |
| 115 | //---------------------------------------------------------------------------- |
| 116 | simgear::HTTP::Client* FGCanvasSystemAdapter::getHTTPClient() const |
| 117 | { |
| 118 | auto http = globals->get_subsystem<FGHTTPClient>(); |
| 119 | |
| 120 | if( http ) |
| 121 | return http->client(); |
| 122 | |
| 123 | SG_LOG( SG_IO, |
| 124 | SG_ALERT, |
| 125 | "FGCanvasSystemAdapter: Failed to get HTTP subsystem" ); |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
| 129 | } |