Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource2/src/orxonox/tools/ResourceCollection.cc @ 5624

Last change on this file since 5624 was 5624, checked in by rgrieder, 15 years ago

Added two classes to allow resource declaration via XML.

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "ResourceCollection.h"
30
31#include "util/Exception.h"
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34#include "ResourceLocation.h"
35
36namespace orxonox
37{
38    ResourceCollection::ResourceCollection(BaseObject* creator)
39        : BaseObject(creator)
40    {
41        RegisterObject(ResourceCollection);
42
43        // Default group is "General"
44        this->setResourceGroup(Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
45    }
46
47    ResourceCollection::~ResourceCollection()
48    {
49    }
50
51    void ResourceCollection::XMLPort(Element& xmlElement, XMLPort::Mode mode)
52    {
53        XMLPortParam(ResourceCollection, "resourceGroup", setResourceGroup, getResourceGroup, xmlElement, mode);
54        XMLPortObject(ResourceCollection, ResourceLocation, "", addResourceLocation, getResourceLocation, xmlElement, mode);
55    }
56
57    void ResourceCollection::setResourceGroup(const std::string& resourceGroup)
58    {
59        if (resourceGroup.empty())
60        {
61            COUT(2) << "Warning: \"\" is not a valid resource group." << std::endl;
62            return;
63        }
64        if (!resourceLocations_.empty())
65            ThrowException(InitialisationFailed, "ResourceGroup: Group change not allowed with locations already added!");
66        resourceGroup_ = resourceGroup;
67    }
68
69    void ResourceCollection::addResourceLocation(ResourceLocation* location)
70    {
71        location->load(resourceGroup_);
72        this->resourceLocations_.push_back(location);
73    }
74
75    void ResourceCollection::removeResourceLocation(ResourceLocation* location)
76    {
77        for (std::vector<ResourceLocation*>::iterator it = resourceLocations_.begin(); it != resourceLocations_.end(); )
78        {
79            if (*it == location)
80            {
81                it = resourceLocations_.erase(it);
82                location->unload(); // doesn't throw
83            }
84            else
85                ++it;
86        }
87    }
88
89    ResourceLocation* ResourceCollection::getResourceLocation(unsigned int index) const
90    {
91        if (index >= resourceLocations_.size())
92            return NULL;
93        else
94            return resourceLocations_[index];
95    }
96}
Note: See TracBrowser for help on using the repository browser.