2005-03-02 08:37:40 +00:00
|
|
|
//This file is part of Photon (http://photon.sourceforge.net)
|
|
|
|
//Copyright (C) 2004-2005 James Turk
|
|
|
|
//
|
|
|
|
// Author:
|
|
|
|
// James Turk (jpt2433@rit.edu)
|
|
|
|
//
|
|
|
|
// Version:
|
2005-06-14 00:28:36 +00:00
|
|
|
// $Id: ResourceManaged.hpp,v 1.4 2005/06/14 00:28:36 cozman Exp $
|
2005-03-02 08:37:40 +00:00
|
|
|
|
|
|
|
#ifndef PHOTON_RESOURCEMANAGED_HPP
|
|
|
|
#define PHOTON_RESOURCEMANAGED_HPP
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "ResourceManager.hpp"
|
|
|
|
|
|
|
|
namespace photon
|
|
|
|
{
|
|
|
|
|
|
|
|
// Class: ResourceManaged
|
|
|
|
// Base template class, to be used as a base from which classes which have
|
|
|
|
// resources that can be controlled, such as textures and music, can be
|
|
|
|
// derived. Resource managed classes rely on a <ResourceManager>.
|
|
|
|
//
|
|
|
|
// Children:
|
|
|
|
// <Texture>, <Image>
|
|
|
|
//
|
|
|
|
// <Audio>, <Music>, <Sound>
|
|
|
|
template<class ResMgrT>
|
|
|
|
class ResourceManaged
|
|
|
|
{
|
|
|
|
// Group: (Con/De)structors
|
|
|
|
public:
|
|
|
|
// Function: ResourceManaged
|
|
|
|
// Default constructor.
|
|
|
|
ResourceManaged();
|
|
|
|
|
|
|
|
// Function: ResourceManaged
|
|
|
|
// Initializing constructor, calls <open> with a filename/zipname.
|
|
|
|
//
|
|
|
|
// Parameters:
|
2005-06-10 05:48:59 +00:00
|
|
|
// name - name of resource
|
2005-03-02 08:37:40 +00:00
|
|
|
//
|
|
|
|
// See Also:
|
|
|
|
// <open>
|
2005-06-10 05:48:59 +00:00
|
|
|
ResourceManaged(const std::string& name);
|
2005-03-02 08:37:40 +00:00
|
|
|
|
|
|
|
// Function: ~ResourceManaged
|
|
|
|
// Destructor, calls <release>.
|
|
|
|
virtual ~ResourceManaged();
|
|
|
|
|
|
|
|
ResourceManaged& operator=(const ResourceManaged &rhs);
|
|
|
|
|
|
|
|
// Group: General
|
|
|
|
public:
|
|
|
|
// Function: open
|
|
|
|
// Opens new resource via the associated <ResourceManager>.
|
|
|
|
//
|
|
|
|
// Parameters:
|
2005-06-10 05:48:59 +00:00
|
|
|
// name - name of resource
|
|
|
|
virtual void open(const std::string& name);
|
2005-03-02 08:37:40 +00:00
|
|
|
|
|
|
|
// Function: release
|
|
|
|
// Removes a reference to the resource, releasing if needed.
|
|
|
|
// Generally called by destructor, so should rarely be called.
|
|
|
|
virtual void release();
|
|
|
|
|
2005-06-10 05:48:59 +00:00
|
|
|
// Group: Resource Manager Access
|
2005-03-02 08:37:40 +00:00
|
|
|
|
2005-06-10 05:48:59 +00:00
|
|
|
// Function: cleanUp
|
|
|
|
// Cleans up any unused resources of the type.
|
|
|
|
// (Ex. Image::cleanUp() will unload all images.)
|
2005-06-10 07:06:06 +00:00
|
|
|
virtual void cleanUp();
|
2005-06-10 05:48:59 +00:00
|
|
|
|
|
|
|
// Function: addResource
|
|
|
|
// Define a new named resource.
|
|
|
|
// (Ex. Image::addResource("monkey","images/monkey.png") would
|
|
|
|
// make it so that any attempts to load "monkey" would load the image
|
|
|
|
// images/monkey.png)
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// name - Name to give to resource.
|
|
|
|
// path - Path of resource data file.
|
|
|
|
static void addResource(const std::string& name, const std::string& path);
|
|
|
|
|
|
|
|
// Function: addResource
|
|
|
|
// Define a new unaliased resource. (name == path).
|
|
|
|
// (Ex. Image::addResource("images/monkey.png") is essentially the same as
|
|
|
|
// Image::addResource("images/monkey.png","images/monkey.png")
|
|
|
|
//
|
|
|
|
// Parameters:.
|
|
|
|
// path - Path of resource data file.
|
|
|
|
static void addResource(const std::string& path);
|
2005-03-02 08:37:40 +00:00
|
|
|
|
2005-06-10 07:06:06 +00:00
|
|
|
protected:
|
2005-06-14 00:28:36 +00:00
|
|
|
std::string resName_;
|
2005-03-02 08:37:40 +00:00
|
|
|
static ResMgrT resMgr_;
|
|
|
|
};
|
|
|
|
|
|
|
|
//and he said "the implementation shall follow, as it is written"
|
|
|
|
|
|
|
|
template<class ResMgrT>
|
2005-06-14 00:28:36 +00:00
|
|
|
ResourceManaged<ResMgrT>::ResourceManaged()
|
|
|
|
{ }
|
2005-03-02 08:37:40 +00:00
|
|
|
|
|
|
|
template<class ResMgrT>
|
2005-06-14 00:28:36 +00:00
|
|
|
ResourceManaged<ResMgrT>::ResourceManaged(const std::string& name) :
|
|
|
|
resName_(name)
|
|
|
|
{ }
|
2005-03-02 08:37:40 +00:00
|
|
|
|
|
|
|
template<class ResMgrT>
|
|
|
|
ResourceManaged<ResMgrT>::~ResourceManaged()
|
|
|
|
{
|
|
|
|
release();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class ResMgrT>
|
|
|
|
ResourceManaged<ResMgrT>&
|
|
|
|
ResourceManaged<ResMgrT>::operator=(const ResourceManaged<ResMgrT> &rhs)
|
|
|
|
{
|
|
|
|
if(this != &rhs)
|
|
|
|
{
|
2005-06-14 00:28:36 +00:00
|
|
|
release();
|
|
|
|
resName_ = rhs.resName_;
|
2005-03-02 08:37:40 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class ResMgrT>
|
2005-06-10 05:48:59 +00:00
|
|
|
void ResourceManaged<ResMgrT>::open(const std::string& name)
|
2005-03-02 08:37:40 +00:00
|
|
|
{
|
|
|
|
release();
|
2005-06-14 00:28:36 +00:00
|
|
|
resName_ = name;
|
2005-03-02 08:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class ResMgrT>
|
|
|
|
void ResourceManaged<ResMgrT>::release()
|
|
|
|
{
|
2005-06-14 00:28:36 +00:00
|
|
|
if(!resName_.empty()) // release is a no-op on an invalid resource
|
|
|
|
{
|
|
|
|
resMgr_.delRef(resName_); // decrement the refcount
|
|
|
|
resName_.clear(); // empty string = invalid resource
|
|
|
|
}
|
2005-06-10 05:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class ResMgrT>
|
|
|
|
void ResourceManaged<ResMgrT>::cleanUp()
|
|
|
|
{
|
|
|
|
resMgr_.cleanUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class ResMgrT>
|
|
|
|
void ResourceManaged<ResMgrT>::addResource(const std::string& name,
|
|
|
|
const std::string& path)
|
|
|
|
{
|
|
|
|
resMgr_.newResource(name,path);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class ResMgrT>
|
|
|
|
void ResourceManaged<ResMgrT>::addResource(const std::string& path)
|
|
|
|
{
|
|
|
|
resMgr_.newResource(path,path);
|
2005-03-02 08:37:40 +00:00
|
|
|
}
|
|
|
|
|
2005-06-14 00:28:36 +00:00
|
|
|
// define the resource manager static instance
|
|
|
|
template <class T>
|
|
|
|
T ResourceManaged<T>::resMgr_;
|
|
|
|
|
2005-03-02 08:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //PHOTON_RESOURCEMANAGED_HPP
|