scummvm/resource.h

133 lines
3.6 KiB
C
Raw Normal View History

2003-08-15 18:00:22 +00:00
// Residual - Virtual machine to run LucasArts' 3D adventure games
2006-02-15 19:43:48 +00:00
// Copyright (C) 2003-2006 The ScummVM-Residual Team (www.scummvm.org)
2003-08-15 18:00:22 +00:00
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#ifndef RESOURCE_H
#define RESOURCE_H
2003-08-15 18:00:22 +00:00
#include "lab.h"
2004-02-21 15:20:01 +00:00
#include "map"
2005-01-01 12:27:57 +00:00
2003-08-15 18:00:22 +00:00
#include <list>
#include <string>
class Bitmap;
class CMap;
2003-08-15 18:00:22 +00:00
class Costume;
class Font;
2003-08-15 18:00:22 +00:00
class KeyframeAnim;
class Material;
class Model;
class LipSynch;
2003-08-15 18:00:22 +00:00
class Resource {
public:
Resource(const char *filename) :
2004-12-09 23:55:43 +00:00
_fname(filename), _ref(0), _luaRef(false) { }
Resource(const Resource &r) : _fname(r._fname), _ref(0), _luaRef(false) {}
virtual ~Resource() { }
2004-12-09 23:55:43 +00:00
void ref() { _ref++; }
void deref();
2004-12-09 23:55:43 +00:00
const char *filename() const { return _fname.c_str(); }
2004-12-09 23:55:43 +00:00
void luaRef() { if (!_luaRef) { ref(); _luaRef = true; } }
void luaGc() { if (_luaRef) { _luaRef = false; deref(); } }
2003-08-15 18:00:22 +00:00
private:
2004-12-09 23:55:43 +00:00
std::string _fname;
int _ref;
bool _luaRef;
2003-08-15 18:00:22 +00:00
};
template <class T>
class ResPtr {
public:
2004-12-09 23:55:43 +00:00
ResPtr() { _ptr = NULL; }
ResPtr(const ResPtr &p) { _ptr = p._ptr; if (_ptr != NULL) _ptr->ref(); }
ResPtr(T* ptr) { _ptr = ptr; if (_ptr != NULL) _ptr->ref(); }
operator T*() { return _ptr; }
operator const T*() const { return _ptr; }
T& operator *() { return *_ptr; }
const T& operator *() const { return *_ptr; }
T* operator ->() { return _ptr; }
const T* operator ->() const { return _ptr; }
ResPtr& operator =(T* ptr) {
2004-12-09 23:55:43 +00:00
if (_ptr == ptr) return *this;
if (_ptr != NULL) _ptr->deref();
_ptr = ptr;
if (_ptr != NULL) _ptr->ref();
return *this;
}
ResPtr& operator =(const ResPtr& p) {
2004-12-09 23:55:43 +00:00
if (this == &p || _ptr == p._ptr) return *this;
if (_ptr != NULL) _ptr->deref();
_ptr = p._ptr;
if (_ptr != NULL) _ptr->ref();
return *this;
}
2004-12-09 23:55:43 +00:00
~ResPtr() { if (_ptr != NULL) _ptr->deref(); }
2003-08-15 18:00:22 +00:00
private:
2004-12-09 23:55:43 +00:00
T* _ptr;
};
2003-08-15 18:00:22 +00:00
class ResourceLoader {
public:
bool fileExists(const char *filename) const;
Block *getFileBlock(const char *filename) const;
std::FILE *openNewStream(const char *filename) const;
int fileLength(const char *filename) const;
bool exportResource(const char *filename);
Bitmap *loadBitmap(const char *fname);
CMap *loadColormap(const char *fname);
Costume *loadCostume(const char *fname, Costume *prevCost);
Font *loadFont(const char *fname);
KeyframeAnim *loadKeyframe(const char *fname);
Material *loadMaterial(const char *fname, const CMap &c);
Model *loadModel(const char *fname, const CMap &c);
LipSynch *loadLipSynch(const char *fname);
void uncache(const char *fname);
2003-08-15 18:00:22 +00:00
ResourceLoader();
ResourceLoader(const ResourceLoader &);
~ResourceLoader();
const Lab *findFile(const char *filename) const;
private:
2003-08-15 18:00:22 +00:00
typedef std::list<Lab *> LabList;
2004-12-09 23:55:43 +00:00
LabList _labs;
2003-08-15 18:00:22 +00:00
// const Lab *findFile(const char *filename) const;
2003-08-15 18:00:22 +00:00
2004-12-10 07:26:03 +00:00
typedef std::map<std::string, Resource *> CacheType;
CacheType _cache;
2003-08-15 18:00:22 +00:00
// Shut up pointless g++ warning
friend class dummy;
2003-08-15 18:00:22 +00:00
};
extern ResourceLoader *g_resourceloader;
2003-08-15 18:00:22 +00:00
inline void Resource::deref() {
2004-12-09 23:55:43 +00:00
if (--_ref == 0) {
g_resourceloader->uncache(_fname.c_str());
delete this;
}
2003-08-15 18:00:22 +00:00
}
#endif