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