]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/SceneryPager.cxx
Expose a radio function (receiveBeacon) to the Nasal subsystem
[flightgear.git] / src / Scenery / SceneryPager.cxx
1 // SceneryPager.cxx -- Interface to OSG database pager
2 //
3 // Copyright (C) 2007 Tim Moore timoore@redhat.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 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <simgear/math/SGMath.hxx>
24 #include "SceneryPager.hxx"
25 #include <algorithm>
26 #include <functional>
27
28 using namespace osg;
29 using namespace flightgear;
30
31 SceneryPager::SceneryPager()
32 {
33     _pagerRequests.reserve(48);
34     _deleteRequests.reserve(16);
35 }
36
37 SceneryPager::SceneryPager(const SceneryPager& rhs) :
38     DatabasePager(rhs)
39 {
40 }
41
42 SceneryPager::~SceneryPager()
43 {
44 }
45
46 void SceneryPager::queueRequest(const std::string& fileName, Group* group,
47                                 float priority, FrameStamp* frameStamp,
48                                 ref_ptr<Referenced>& databaseRequest,
49                                 osgDB::ReaderWriter::Options* options)
50 {
51     _pagerRequests.push_back(PagerRequest(fileName, group, priority,
52                                           frameStamp,
53                                           databaseRequest,
54                                           options));
55 }
56
57 void SceneryPager::queueDeleteRequest(osg::ref_ptr<osg::Object>& objptr)
58 {
59     _deleteRequests.push_back(objptr);
60     objptr = 0;
61 }
62
63 // Work around interface change in
64 // osgDB::DatabasePager::requestNodeFile
65 namespace
66 {
67 struct NodePathProxy
68 {
69     NodePathProxy(NodePath& nodePath)
70         : _nodePath(nodePath)
71     {
72     }
73     operator Group* () { return static_cast<Group*>(_nodePath.back()); }
74     operator NodePath& () { return _nodePath; }
75     NodePath& _nodePath;
76 };
77 }
78
79 void SceneryPager::PagerRequest::doRequest(SceneryPager* pager)
80 {
81     if (_group->getNumChildren() == 0) {
82         NodePath path;
83         path.push_back(_group.get());
84         pager->requestNodeFile(_fileName, NodePathProxy(path), _priority,
85                                _frameStamp.get(),
86                                *_databaseRequest,
87                                _options.get());
88     }
89 }
90
91 void SceneryPager::signalEndFrame()
92 {
93     using namespace std;
94     bool areDeleteRequests = false;
95     bool arePagerRequests = false;
96     if (!_deleteRequests.empty()) {
97         areDeleteRequests = true;
98         OpenThreads::ScopedLock<OpenThreads::Mutex>
99             lock(_fileRequestQueue->_childrenToDeleteListMutex);
100         ObjectList& deleteList = _fileRequestQueue->_childrenToDeleteList;
101         deleteList.insert(deleteList.end(),
102                           _deleteRequests.begin(),
103                           _deleteRequests.end());
104         _deleteRequests.clear();
105     }
106     if (!_pagerRequests.empty()) {
107         arePagerRequests = true;
108         for_each(_pagerRequests.begin(), _pagerRequests.end(),
109                  bind2nd(mem_fun_ref(&PagerRequest::doRequest), this));
110         _pagerRequests.clear();
111     }
112     if (areDeleteRequests && !arePagerRequests) {
113         _fileRequestQueue->updateBlock();
114     }
115     DatabasePager::signalEndFrame();
116 }
117