]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/modellib.cxx
model.[ch]xx:
[simgear.git] / simgear / scene / model / modellib.cxx
1 // modellib.cxx - implement an SSG model library.
2
3 #ifdef HAVE_CONFIG_H
4 #  include <simgear_config.h>
5 #endif
6
7 #include <simgear/compiler.h>
8 #include <simgear/props/props.hxx>
9
10 #include "model.hxx"
11 #include "animation.hxx"
12 #include "personality.hxx"
13
14 #include "modellib.hxx"
15
16
17 \f
18 ////////////////////////////////////////////////////////////////////////
19 // Implementation of SGModelLib.
20 ////////////////////////////////////////////////////////////////////////
21
22 SGModelLib::SGModelLib ()
23 {
24 }
25
26 SGModelLib::~SGModelLib ()
27 {
28 }
29
30 void
31 SGModelLib::flush1()
32 {
33     // This routine is disabled because I believe I see multiple
34     // problems with it.
35     //
36     // 1. It blindly deletes all managed models that aren't used
37     //    elsewhere.  Is this what we really want????  In the one
38     //    FlightGear case that calls this method, this clearly is not the
39     //    intention.  I believe it makes more sense to simply leave items
40     //    in the lbrary, even if they are not currently used, they will be
41     //    there already when/if we want to use them later.
42     //
43     // 2. This routine only does a deRef() on the model.  This doesn't actually
44     //    delete the ssg tree so there is a memory leak.
45
46     SG_LOG( SG_GENERAL, SG_ALERT,
47             "WARNGING: a disabled/broken routine has been called.  This should be fixed!" );
48
49     return;
50
51     map<string, ssgSharedPtr<ssgEntity> >::iterator it = _table.begin();
52     while (it != _table.end()) {
53                                 // If there is only one reference, it's
54                                 // ours; no one else is using the item.
55         if (!it->second.isShared()) {
56             string key = it->first;
57             _table.erase(it);
58             it = _table.upper_bound(key);
59         } else
60             it++;
61     }
62 }
63
64 static int
65 personality_pretrav_callback(ssgEntity * entity, int mask)
66 {
67     ((SGPersonalityBranch *)entity)->_old_current = SGAnimation::current_object;
68     SGAnimation::current_object = (SGPersonalityBranch *)entity;
69     return 1;
70 }
71
72 static int
73 personality_posttrav_callback(ssgEntity * entity, int mask)
74 {
75     SGAnimation::current_object = ((SGPersonalityBranch *)entity)->_old_current;
76     ((SGPersonalityBranch *)entity)->_old_current = 0;
77     return 1;
78 }
79
80 ssgEntity *
81 SGModelLib::load_model( const string &fg_root,
82                            const string &path,
83                            SGPropertyNode *prop_root,
84                            double sim_time_sec,
85                            bool cache_object,
86                            SGModelData *data )
87 {
88     ssgBranch *personality_branch = new SGPersonalityBranch;
89     personality_branch->setTravCallback(SSG_CALLBACK_PRETRAV, personality_pretrav_callback);
90     personality_branch->setTravCallback(SSG_CALLBACK_POSTTRAV, personality_posttrav_callback);
91
92                                 // FIXME: normalize path to
93                                 // avoid duplicates.
94     map<string, ssgSharedPtr<ssgEntity> >::iterator it = _table.find(path);
95     if (!cache_object || it == _table.end()) {
96         ssgSharedPtr<ssgEntity> model = sgLoad3DModel(fg_root, path, prop_root,
97                                                       sim_time_sec, 0, data );
98         if (cache_object)
99             _table[path] = model;      // add one reference to keep it around
100
101         personality_branch->addKid( model );
102     } else {
103         personality_branch->addKid( it->second );
104     }
105     return personality_branch;
106 }
107
108
109 // end of modellib.cxx