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
|
|
|
|
|
2003-08-15 19:41:26 +00:00
|
|
|
#ifndef RESOURCE_H
|
|
|
|
#define RESOURCE_H
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2003-08-15 19:41:26 +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;
|
2003-10-05 17:45:46 +00:00
|
|
|
class CMap;
|
2003-08-15 18:00:22 +00:00
|
|
|
class Costume;
|
2005-03-18 19:54:40 +00:00
|
|
|
class Font;
|
2003-08-15 18:00:22 +00:00
|
|
|
class KeyframeAnim;
|
|
|
|
class Material;
|
|
|
|
class Model;
|
2004-09-11 14:09:43 +00:00
|
|
|
class LipSynch;
|
2003-08-15 18:00:22 +00:00
|
|
|
|
|
|
|
class Resource {
|
|
|
|
public:
|
2004-02-24 22:43:32 +00:00
|
|
|
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) {}
|
2004-02-24 22:43:32 +00:00
|
|
|
virtual ~Resource() { }
|
2004-12-09 23:55:43 +00:00
|
|
|
void ref() { _ref++; }
|
2004-02-24 22:43:32 +00:00
|
|
|
void deref();
|
2004-12-09 23:55:43 +00:00
|
|
|
const char *filename() const { return _fname.c_str(); }
|
2004-02-24 22:43:32 +00:00
|
|
|
|
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; }
|
2004-02-24 22:43:32 +00:00
|
|
|
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();
|
2004-02-24 22:43:32 +00:00
|
|
|
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();
|
2004-02-24 22:43:32 +00:00
|
|
|
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;
|
2004-02-24 22:43:32 +00:00
|
|
|
};
|
2003-08-15 18:00:22 +00:00
|
|
|
|
|
|
|
class ResourceLoader {
|
|
|
|
public:
|
2004-02-24 22:43:32 +00:00
|
|
|
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;
|
|
|
|
|
2005-07-17 23:40:22 +00:00
|
|
|
bool exportResource(const char *filename);
|
2004-02-24 22:43:32 +00:00
|
|
|
Bitmap *loadBitmap(const char *fname);
|
|
|
|
CMap *loadColormap(const char *fname);
|
|
|
|
Costume *loadCostume(const char *fname, Costume *prevCost);
|
2005-03-18 19:54:40 +00:00
|
|
|
Font *loadFont(const char *fname);
|
2004-02-24 22:43:32 +00:00
|
|
|
KeyframeAnim *loadKeyframe(const char *fname);
|
|
|
|
Material *loadMaterial(const char *fname, const CMap &c);
|
|
|
|
Model *loadModel(const char *fname, const CMap &c);
|
2004-09-11 14:09:43 +00:00
|
|
|
LipSynch *loadLipSynch(const char *fname);
|
2004-02-24 22:43:32 +00:00
|
|
|
void uncache(const char *fname);
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2004-02-24 22:43:32 +00:00
|
|
|
ResourceLoader();
|
|
|
|
ResourceLoader(const ResourceLoader &);
|
2006-02-26 18:38:35 +00:00
|
|
|
~ResourceLoader();
|
2005-07-10 18:57:27 +00:00
|
|
|
const Lab *findFile(const char *filename) const;
|
2004-12-31 21:35:04 +00:00
|
|
|
private:
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2004-02-24 22:43:32 +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
|
|
|
|
2005-07-10 18:57:27 +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
|
|
|
|
2004-02-24 22:43:32 +00:00
|
|
|
// Shut up pointless g++ warning
|
|
|
|
friend class dummy;
|
2003-08-15 18:00:22 +00:00
|
|
|
};
|
|
|
|
|
2004-12-31 21:35:04 +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) {
|
2004-12-31 21:35:04 +00:00
|
|
|
g_resourceloader->uncache(_fname.c_str());
|
2004-02-24 22:43:32 +00:00
|
|
|
delete this;
|
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|