FlightGear next
FGCanvasSystemAdapter.cxx
Go to the documentation of this file.
1// Integrate Canvas into FlightGear
2//
3// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
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
20
21#include <Main/globals.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
31namespace 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 if( globals->get_renderer() )
72 globals->get_renderer()->addCanvasCamera(camera);
73 }
74
75 //----------------------------------------------------------------------------
76 void FGCanvasSystemAdapter::removeCamera(osg::Camera* camera) const
77 {
78 if( globals->get_renderer() )
79 globals->get_renderer()->removeCanvasCamera(camera);
80 }
81
82 //----------------------------------------------------------------------------
83 osg::ref_ptr<osg::Image> FGCanvasSystemAdapter::getImage(const std::string& path) const
84 {
85 SGPath p(SGPath::fromUtf8(path));
86 osg::ref_ptr<simgear::SGReaderWriterOptions> localReaderWriterOptions = simgear::SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions());
87 localReaderWriterOptions->setLoadOriginHint(simgear::SGReaderWriterOptions::LoadOriginHint::ORIGIN_CANVAS);
88
89 if( p.isAbsolute() )
90 {
91 const SGPath valid_path = SGPath(p).validate(false);
92 if( !valid_path.isNull() )
93 return osgDB::readRefImageFile(valid_path.utf8Str(), localReaderWriterOptions);
94
95 SG_LOG(SG_IO, SG_ALERT, "canvas::Image: reading '" << path << "' denied");
96 }
97 else
98 {
99 SGPath tpath = globals->resolve_resource_path(path);
100 if( !tpath.isNull() )
101 return osgDB::readRefImageFile(tpath.utf8Str(), localReaderWriterOptions);
102
103 SG_LOG(SG_IO, SG_ALERT, "canvas::Image: No such image: '" << path << "'");
104 }
105
106 return 0;
107 }
108
109 //----------------------------------------------------------------------------
110 SGSubsystem*
112 {
113 return globals->get_subsystem_mgr()->get_subsystem(name);
114 }
115
116 //----------------------------------------------------------------------------
117 simgear::HTTP::Client* FGCanvasSystemAdapter::getHTTPClient() const
118 {
119 auto http = globals->get_subsystem<FGHTTPClient>();
120
121 if( http )
122 return http->client();
123
124 SG_LOG( SG_IO,
125 SG_ALERT,
126 "FGCanvasSystemAdapter: Failed to get HTTP subsystem" );
127 return nullptr;
128 }
129
130}
#define p(x)
simgear::HTTP::Client * client()
void addCamera(osg::Camera *camera) const override
simgear::HTTP::Client * getHTTPClient() const override
osg::ref_ptr< osg::Image > getImage(const std::string &path) const override
SGSubsystem * getSubsystem(const std::string &name) const override
void removeCamera(osg::Camera *camera) const override
simgear::canvas::FontPtr getFont(const std::string &name) const override
const char * name
FGGlobals * globals
Definition globals.cxx:142