]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/SceneryPager.cxx
Merge commit 'refs/merge-requests/13' of git://gitorious.org/fg/flightgear into next
[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/scene/model/SGPagedLOD.hxx>
24 #include <simgear/math/SGMath.hxx>
25 #include "SceneryPager.hxx"
26 #include <algorithm>
27 #include <functional>
28
29 using namespace osg;
30 using namespace flightgear;
31
32 SceneryPager::SceneryPager()
33 {
34     _pagerRequests.reserve(48);
35     _deleteRequests.reserve(16);
36     setExpiryDelay(120.0);
37 }
38
39 SceneryPager::SceneryPager(const SceneryPager& rhs) :
40     DatabasePager(rhs)
41 {
42 }
43
44 SceneryPager::~SceneryPager()
45 {
46 }
47
48 void SceneryPager::requestNodeFile(const std::string& fileName, Group* group,
49                                    float priority, const FrameStamp* framestamp,
50                                    ref_ptr<Referenced>& databaseRequest,
51 #if SG_OSG_MIN_VERSION_REQUIRED(2,9,5)
52                                    const osg::Referenced* options
53 #else
54                                    osgDB::ReaderWriter::Options* options
55 #endif
56                                    )
57 {
58     simgear::SGPagedLOD *sgplod = dynamic_cast<simgear::SGPagedLOD*>(group);
59     if(sgplod)
60         DatabasePager::requestNodeFile(fileName, group, priority, framestamp,
61                                        databaseRequest,
62                                        sgplod->getReaderWriterOptions());
63     else
64         DatabasePager::requestNodeFile(fileName, group, priority, framestamp,
65                                        databaseRequest,
66                                        options);
67 }
68
69 void SceneryPager::queueRequest(const std::string& fileName, Group* group,
70                                 float priority, FrameStamp* frameStamp,
71                                 ref_ptr<Referenced>& databaseRequest,
72                                 osgDB::ReaderWriter::Options* options)
73 {
74     _pagerRequests.push_back(PagerRequest(fileName, group, priority,
75                                           frameStamp,
76                                           databaseRequest,
77                                           options));
78 }
79
80 void SceneryPager::queueDeleteRequest(osg::ref_ptr<osg::Object>& objptr)
81 {
82     _deleteRequests.push_back(objptr);
83     objptr = 0;
84 }
85 void SceneryPager::signalEndFrame()
86 {
87     using namespace std;
88     bool areDeleteRequests = false;
89     bool arePagerRequests = false;
90     if (!_deleteRequests.empty()) {
91         areDeleteRequests = true;
92         OpenThreads::ScopedLock<OpenThreads::Mutex>
93             lock(_fileRequestQueue->_childrenToDeleteListMutex);
94         ObjectList& deleteList = _fileRequestQueue->_childrenToDeleteList;
95         deleteList.insert(deleteList.end(),
96                           _deleteRequests.begin(),
97                           _deleteRequests.end());
98         _deleteRequests.clear();
99     }
100     if (!_pagerRequests.empty()) {
101         arePagerRequests = true;
102         for_each(_pagerRequests.begin(), _pagerRequests.end(),
103                  bind2nd(mem_fun_ref(&PagerRequest::doRequest), this));
104         _pagerRequests.clear();
105     }
106     if (areDeleteRequests && !arePagerRequests) {
107         _fileRequestQueue->updateBlock();
108     }
109     DatabasePager::signalEndFrame();
110 }
111