]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/FGCanvasSystemAdapter.cxx
Replace the NOAA METAR URL with the new, updated one
[flightgear.git] / src / Canvas / FGCanvasSystemAdapter.cxx
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
19 #include "FGCanvasSystemAdapter.hxx"
20
21 #include <Main/globals.hxx>
22 #include <Main/util.hxx>
23 #include <Network/HTTPClient.hxx>
24 #include <Viewer/renderer.hxx>
25
26 #include <osgDB/ReadFile>
27 #include <stdexcept>
28
29 namespace canvas
30 {
31   //----------------------------------------------------------------------------
32   simgear::canvas::FontPtr
33   FGCanvasSystemAdapter::getFont(const std::string& name) const
34   {
35     SGPath path = globals->resolve_resource_path("Fonts/" + name);
36     if( path.isNull() )
37     {
38       SG_LOG
39       (
40         SG_GL,
41         SG_ALERT,
42         "canvas::Text: No such font: " << name
43       );
44       return simgear::canvas::FontPtr();
45     }
46
47     SG_LOG
48     (
49       SG_GL,
50       SG_INFO,
51       "canvas::Text: using font file " << path
52     );
53
54     simgear::canvas::FontPtr font = osgText::readFontFile(path.local8BitStr());
55     if( !font )
56       SG_LOG
57       (
58         SG_GL,
59         SG_ALERT,
60         "canvas::Text: Failed to open font file " << path
61       );
62
63     return font;
64   }
65
66   //----------------------------------------------------------------------------
67   void FGCanvasSystemAdapter::addCamera(osg::Camera* camera) const
68   {
69     globals->get_renderer()->addCamera(camera, false);
70   }
71
72   //----------------------------------------------------------------------------
73   void FGCanvasSystemAdapter::removeCamera(osg::Camera* camera) const
74   {
75     if( globals->get_renderer() )
76       globals->get_renderer()->removeCamera(camera);
77   }
78
79   //----------------------------------------------------------------------------
80   osg::ref_ptr<osg::Image> FGCanvasSystemAdapter::getImage(const std::string& path) const
81   {
82       SGPath p(SGPath::fromUtf8(path));
83     if( p.isAbsolute() )
84     {
85       SGPath valid_path = fgValidatePath(p, false);
86       if( !valid_path.isNull() )
87         return osgDB::readImageFile(valid_path.local8BitStr());
88
89       SG_LOG(SG_IO, SG_ALERT, "canvas::Image: reading '" << path << "' denied");
90     }
91     else
92     {
93       SGPath tpath = globals->resolve_resource_path(path);
94       if( !tpath.isNull() )
95         return osgDB::readRefImageFile(tpath.local8BitStr());
96
97       SG_LOG(SG_IO, SG_ALERT, "canvas::Image: No such image: '" << path << "'");
98     }
99
100     return 0;
101   }
102
103   //----------------------------------------------------------------------------
104   SGSubsystem*
105   FGCanvasSystemAdapter::getSubsystem(const std::string& name) const
106   {
107     return globals->get_subsystem(name.c_str());
108   }
109
110   //----------------------------------------------------------------------------
111   simgear::HTTP::Client* FGCanvasSystemAdapter::getHTTPClient() const
112   {
113     FGHTTPClient* http = globals->get_subsystem<FGHTTPClient>();
114
115     if( http )
116       return http->client();
117
118     SG_LOG( SG_IO,
119             SG_ALERT,
120             "FGCanvasSystemAdapter: Failed to get HTTP subsystem" );
121     return 0;
122   }
123
124 }