Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core/src/orxonox/core/Loader.cc @ 820

Last change on this file since 820 was 820, checked in by landauf, 16 years ago

added a Level-class and a first implementation of the Loader, using ClassTreeMask and TinyXML++

File size: 4.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#include "Loader.h"
29#include "Level.h"
30#include "BaseObject.h"
31#include "Identifier.h"
32#include "Iterator.h"
33#include "Debug.h"
34
35//#define TIXML_USE_TICPP
36#include "util/tinyxml/ticpp.h"
37
38namespace orxonox
39{
40    std::vector<std::pair<const Level*, ClassTreeMask> > Loader::levels_s;
41
42    bool Loader::open(const Level* level, const ClassTreeMask& mask)
43    {
44        Loader::add(level, mask);
45        return Loader::load(level, mask);
46    }
47
48    void Loader::close()
49    {
50        Loader::unload();
51        Loader::levels_s.clear();
52    }
53
54    void Loader::close(const Level* level)
55    {
56        Loader::unload(level);
57        Loader::remove(level);
58    }
59
60    void Loader::add(const Level* level, const ClassTreeMask& mask)
61    {
62        Loader::levels_s.insert(Loader::levels_s.end(), std::pair<const Level*, ClassTreeMask>(level, mask));
63    }
64
65    void Loader::remove(const Level* level)
66    {
67        for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it)
68        {
69            if ((*it).first == level)
70            {
71                Loader::levels_s.erase(it);
72                break;
73            }
74        }
75    }
76
77    bool Loader::load(const ClassTreeMask& mask)
78    {
79        bool success = true;
80        for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it)
81            if (!Loader::load((*it).first, (*it).second * mask))
82                success = false;
83
84        return success;
85    }
86
87    void Loader::unload(const ClassTreeMask& mask)
88    {
89        for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; )
90        {
91            if (mask.isIncluded(it->getIdentifier()))
92                delete (*(it++));
93            else
94                ++it;
95        }
96    }
97
98    bool Loader::reload(const ClassTreeMask& mask)
99    {
100        Loader::unload(mask);
101        return Loader::load(mask);
102    }
103
104    bool Loader::load(const Level* level, const ClassTreeMask& mask)
105    {
106        ClassTreeMask loadmask = level->getMask() * mask;
107
108        try
109        {
110            COUT(0) << "Start loading " << level->getFile() << "..." << std::endl;
111            COUT(3) << "Mask: " << loadmask << std::endl;
112
113            ticpp::Document xmlfile(level->getFile());
114            xmlfile.LoadFile();
115
116            for ( ticpp::Iterator< ticpp::Element > child = xmlfile.FirstChildElement(); child != child.end(); child++ )
117            {
118                Identifier* identifier = ID(child->Value());
119                if (identifier)
120                {
121                    COUT(4) << "  fabricating " << child->Value() << "..." << std::endl;
122                    BaseObject* newObject = identifier->fabricate();
123                }
124                else
125                {
126                    COUT(2) << "  Warning: '"<< child->Value() <<"' is not a valid classname." << std::endl;
127                }
128            }
129
130            COUT(0) << "Finished loading " << level->getFile() << "." << std::endl;
131
132            return true;
133        }
134        catch(ticpp::Exception& ex)
135        {
136            COUT(1) << "An error occurred in Loader.cc while loading " << level->getFile() << ":" << std::endl;
137            COUT(1) << ex.what() << std::endl;
138            COUT(1) << "Loading aborted." << std::endl;
139            return false;
140        }
141    }
142
143    void Loader::unload(const Level* level, const ClassTreeMask& mask)
144    {
145        for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; )
146        {
147            if ((it->getLevel() == level) && mask.isIncluded(it->getIdentifier()))
148                delete (*(it++));
149            else
150                ++it;
151        }
152    }
153
154    bool Loader::reload(const Level* level, const ClassTreeMask& mask)
155    {
156        Loader::unload(level, mask);
157        return Loader::load(level, mask);
158    }
159}
Note: See TracBrowser for help on using the repository browser.