]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/canvas_mgr.cxx
Fix for bug 1304 - crash loading XML route
[flightgear.git] / src / Canvas / canvas_mgr.cxx
1 // Canvas with 2D rendering api
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 "canvas_mgr.hxx"
20
21 #include <Cockpit/od_gauge.hxx>
22 #include <Main/fg_props.hxx>
23 #include <Scripting/NasalModelData.hxx>
24 #include <Viewer/CameraGroup.hxx>
25
26 #include <simgear/canvas/Canvas.hxx>
27
28 namespace sc = simgear::canvas;
29
30 //------------------------------------------------------------------------------
31 static sc::Placements addSceneObjectPlacement( SGPropertyNode* placement,
32                                                sc::CanvasPtr canvas )
33 {
34   int module_id = placement->getIntValue("module-id", -1);
35   if( module_id < 0 )
36     return sc::Placements();
37
38   FGNasalModelData* model_data =
39     FGNasalModelData::getByModuleId( static_cast<unsigned int>(module_id) );
40
41   if( !model_data )
42     return sc::Placements();
43
44   if( !model_data->getNode() )
45     return sc::Placements();
46
47   return FGODGauge::set_texture
48   (
49     model_data->getNode(),
50     placement,
51     canvas->getTexture(),
52     canvas->getCullCallback(),
53     canvas
54   );
55 }
56
57 //------------------------------------------------------------------------------
58 CanvasMgr::CanvasMgr():
59   simgear::canvas::CanvasMgr( fgGetNode("/canvas/by-index", true) ),
60   _cb_model_reinit
61   (
62     this,
63     &CanvasMgr::handleModelReinit,
64     fgGetNode("/sim/signals/model-reinit", true)
65   )
66 {
67
68 }
69
70 //----------------------------------------------------------------------------
71 void CanvasMgr::init()
72 {
73   _gui_camera = flightgear::getGUICamera(flightgear::CameraGroup::getDefault());
74   assert(_gui_camera.valid());
75
76   sc::Canvas::addPlacementFactory
77   (
78     "object",
79     boost::bind
80     (
81       &FGODGauge::set_aircraft_texture,
82       _1,
83       boost::bind(&sc::Canvas::getTexture, _2),
84       boost::bind(&sc::Canvas::getCullCallback, _2),
85       _2
86     )
87   );
88   sc::Canvas::addPlacementFactory("scenery-object", &addSceneObjectPlacement);
89
90   simgear::canvas::CanvasMgr::init();
91 }
92
93 //----------------------------------------------------------------------------
94 void CanvasMgr::shutdown()
95 {
96   simgear::canvas::CanvasMgr::shutdown();
97
98   sc::Canvas::removePlacementFactory("object");
99   sc::Canvas::removePlacementFactory("scenery-object");
100
101   _gui_camera = 0;
102 }
103
104 //------------------------------------------------------------------------------
105 unsigned int
106 CanvasMgr::getCanvasTexId(const simgear::canvas::CanvasPtr& canvas) const
107 {
108   if( !canvas )
109     return 0;
110
111   osg::Texture2D* tex = canvas->getTexture();
112   if( !tex )
113     return 0;
114
115 //  osgViewer::Viewer::Contexts contexts;
116 //  globals->get_renderer()->getViewer()->getContexts(contexts);
117 //
118 //  if( contexts.empty() )
119 //    return 0;
120
121   osg::ref_ptr<osg::Camera> guiCamera;
122   if( !_gui_camera.lock(guiCamera) )
123     return 0;
124
125   osg::State* state = guiCamera->getGraphicsContext()->getState(); //contexts[0]->getState();
126   if( !state )
127     return 0;
128
129   osg::Texture::TextureObject* tobj =
130     tex->getTextureObject( state->getContextID() );
131   if( !tobj )
132     return 0;
133
134   return tobj->_id;
135 }
136
137 //----------------------------------------------------------------------------
138 void CanvasMgr::handleModelReinit(SGPropertyNode*)
139 {
140   for(size_t i = 0; i < _elements.size(); ++i)
141     boost::static_pointer_cast<sc::Canvas>(_elements[i])
142       ->reloadPlacements("object");
143 }