]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/FGCanvasSystemAdapter.cxx
std:: namespace fixes, AIBase cleanup.
[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 <Scripting/NasalSys.hxx>
23 #include <Viewer/renderer.hxx>
24
25 #include <osgDB/ReadFile>
26 #include <stdexcept>
27
28 namespace canvas
29 {
30   //----------------------------------------------------------------------------
31   simgear::canvas::FontPtr
32   FGCanvasSystemAdapter::getFont(const std::string& name) const
33   {
34     SGPath path = globals->resolve_resource_path("Fonts/" + name);
35     if( path.isNull() )
36     {
37       SG_LOG
38       (
39         SG_GL,
40         SG_ALERT,
41         "canvas::Text: No such font: " << name
42       );
43       return simgear::canvas::FontPtr();
44     }
45
46     SG_LOG
47     (
48       SG_GL,
49       SG_INFO,
50       "canvas::Text: using font file " << path.str()
51     );
52
53     simgear::canvas::FontPtr font = osgText::readFontFile(path.c_str());
54     if( !font )
55       SG_LOG
56       (
57         SG_GL,
58         SG_ALERT,
59         "canvas::Text: Failed to open font file " << path.c_str()
60       );
61
62     return font;
63   }
64
65   //----------------------------------------------------------------------------
66   void FGCanvasSystemAdapter::addCamera(osg::Camera* camera) const
67   {
68     globals->get_renderer()->addCamera(camera, false);
69   }
70
71   //----------------------------------------------------------------------------
72   void FGCanvasSystemAdapter::removeCamera(osg::Camera* camera) const
73   {
74     globals->get_renderer()->removeCamera(camera);
75   }
76
77   //----------------------------------------------------------------------------
78   osg::Image* FGCanvasSystemAdapter::getImage(const std::string& path) const
79   {
80     SGPath tpath = globals->resolve_resource_path(path);
81     if( tpath.isNull() || !tpath.exists() )
82     {
83       SG_LOG(SG_GL, SG_ALERT, "canvas::Image: No such image: " << path);
84       return 0;
85     }
86
87     return osgDB::readImageFile(tpath.c_str());
88   }
89
90   /**
91    * Get current FGNasalSys instance.
92    */
93   static FGNasalSys* getNasalSys()
94   {
95     static FGNasalSys* nasal_sys = 0;
96     // TODO if Nasal is able to be removed and/or recreated at runtime we need
97     //      to ensure that always the current instance is used
98     if( !nasal_sys )
99     {
100       nasal_sys = dynamic_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
101       if( !nasal_sys )
102         throw std::runtime_error("FGCanvasSystemAdapter: no NasalSys");
103     }
104
105     return nasal_sys;
106   }
107
108   //----------------------------------------------------------------------------
109   naContext FGCanvasSystemAdapter::getNasalContext() const
110   {
111     return getNasalSys()->context();
112   }
113
114   //----------------------------------------------------------------------------
115   int FGCanvasSystemAdapter::gcSave(naRef r)
116   {
117     return getNasalSys()->gcSave(r);
118   }
119
120   //----------------------------------------------------------------------------
121   void FGCanvasSystemAdapter::gcRelease(int key)
122   {
123     getNasalSys()->gcRelease(key);
124   }
125
126   //------------------------------------------------------------------------------
127   naRef FGCanvasSystemAdapter::callMethod( naRef code,
128                                            naRef self,
129                                            int argc,
130                                            naRef* args,
131                                            naRef locals )
132   {
133     return getNasalSys()->callMethod(code, self, argc, args, locals);
134   }
135
136 }