1 // Canvas with 2D rendering api
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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.
19 #include "canvas_mgr.hxx"
22 #include <Main/fg_props.hxx>
25 #include <osg/Texture2D>
30 //------------------------------------------------------------------------------
31 CanvasMgr::CanvasMgr():
32 _props( fgGetNode("/canvas", true) )
37 //------------------------------------------------------------------------------
38 CanvasMgr::~CanvasMgr()
43 //------------------------------------------------------------------------------
44 void CanvasMgr::init()
46 _props->addChangeListener(this);
47 triggerChangeRecursive(_props);
50 //------------------------------------------------------------------------------
51 void CanvasMgr::reinit()
56 //------------------------------------------------------------------------------
57 void CanvasMgr::shutdown()
59 _props->removeChangeListener(this);
62 //------------------------------------------------------------------------------
63 void CanvasMgr::bind()
67 //------------------------------------------------------------------------------
68 void CanvasMgr::unbind()
72 //------------------------------------------------------------------------------
73 void CanvasMgr::update(double delta_time_sec)
75 for( size_t i = 0; i < _canvases.size(); ++i )
77 _canvases[i]->update(delta_time_sec);
80 //------------------------------------------------------------------------------
81 void CanvasMgr::childAdded( SGPropertyNode * parent,
82 SGPropertyNode * child )
84 if( parent != _props )
87 if( child->getNameString() == "texture" )
91 //------------------------------------------------------------------------------
92 void CanvasMgr::childRemoved( SGPropertyNode * parent,
93 SGPropertyNode * child )
95 if( parent != _props )
98 if( child->getNameString() == "texture" )
100 size_t index = child->getIndex();
102 if( index >= _canvases.size() )
103 SG_LOG(SG_GL, SG_WARN, "can't removed unknown texture[" << index << "]!");
105 // remove the canvas...
106 _canvases[index].reset();
110 //------------------------------------------------------------------------------
111 unsigned int CanvasMgr::getCanvasTexId(size_t index) const
113 if( index >= _canvases.size()
114 || !_canvases[index] )
117 return _canvases[index]->getTexId();
120 //------------------------------------------------------------------------------
121 void CanvasMgr::textureAdded(SGPropertyNode* node)
123 size_t index = node->getIndex();
125 if( index >= _canvases.size() )
127 if( index > _canvases.size() )
128 SG_LOG(SG_GL, SG_WARN, "Skipping unused texture slot(s)!");
130 _canvases.resize(index + 1);
132 else if( _canvases[index] )
133 SG_LOG(SG_GL, SG_WARN, "texture[" << index << "] already exists!");
135 _canvases[index].reset( new Canvas() );
136 _canvases[index]->reset(node);
139 //------------------------------------------------------------------------------
140 void CanvasMgr::triggerChangeRecursive(SGPropertyNode* node)
142 node->getParent()->fireChildAdded(node);
144 if( node->nChildren() == 0 && node->getType() != simgear::props::NONE )
145 return node->fireValueChanged();
147 for( int i = 0; i < node->nChildren(); ++i )
148 triggerChangeRecursive( node->getChild(i) );