This is pretty major, so there'll probably be something wrong with it... even though I did spend a few hours looking.
NOTES:
* If you've Retrieved it, don't delete it--- Use resources.Release(Whatever).
Textures automatically release subordinate quads.
* Most of the time, use resources.RetrieveQuad to grab a quad. Should handle everything for you.
RetrieveQuad will load the required texture, if needed.
Only managed resources have a resource name ("back", "simon", etc).
Managed resources can be retrieved with GetTexture/GetQuad/GetWhatever.
Non managed quads lookup by position/dimensions, defaulting to the whole texture.
* Use resources.RetrieveTexture only when you need to do something special to it.
Calling retrieve texture with RETRIEVE_MANAGE will permanently add a texture to the manager
RETRIEVE_LOCK and RETRIEVE_VRAM will lock a texture. It will not leave the cache until
Release(JTexture*) is called, or as a last resort during cache overflow.
* Try to only store (as a class member) pointers to textures retrieved with RETRIEVE_MANAGE.
All others may become invalid, although locked textures do have a high degree of stability. It's
pretty safe to store a locked texture if you're not going to load much between uses.
There's a lot going on here, so I might have missed something... but it runs through the test suite alright.
TODO:
* When called without any arguments, RetrieveQuad sometimes leaves a thin border around the image.
This can be bypassed by specifying a quad one or two pixels less than the image size. Why?
* I've had a crash while runing the Demo mode, something to do with receiveEventMinus?
This hasn't exactly reproduced on a clean SVN copy, (being a hang, rather than a crash) so
I've probably done something to worsen the problem somehow? I'll look into it tomorrow.
* Clean up lock/unlock system, memory usage. Streamline interface, consider phasing out calls using GetWhatever() format.
127 lines
4.5 KiB
C++
127 lines
4.5 KiB
C++
#ifndef _WRESOURCEMANAGER_H_
|
|
#define _WRESOURCEMANAGER_H_
|
|
#include <JResourceManager.h>
|
|
#include <JSoundSystem.h>
|
|
#include <JTypes.h>
|
|
#include "MTGDeck.h"
|
|
#include "MTGCard.h"
|
|
|
|
#define CACHE_SIZE_PIXELS 2000000
|
|
|
|
class WCachedResource{
|
|
public:
|
|
friend class WResourceManager;
|
|
bool isLocked(); //Is the resource locked?
|
|
void lock(); //Lock it.
|
|
void unlock(bool force = false); //Unlock it. If force, then set locks to 0.
|
|
void hit(); //Update resource last used time.
|
|
|
|
WCachedResource();
|
|
|
|
protected:
|
|
unsigned int lastTime;
|
|
unsigned char locks; //Remember to unlock when we're done using locked stuff, or else this'll be useless.
|
|
};
|
|
|
|
class WCachedTexture: public WCachedResource{
|
|
public:
|
|
friend class WResourceManager;
|
|
WCachedTexture();
|
|
~WCachedTexture();
|
|
|
|
JTexture * GetTexture(); //Return this texture as is. Does not make a new one.
|
|
JQuad * GetQuad(float offX=0.0f, float offY=0.0f, float width=0.0f, float height=0.0f); //Get us a new/existing quad.
|
|
JQuad * GetCard(float offX=0.0f, float offY=0.0f, float width=0.0f, float height=0.0f); //Same as above, but centered when new.
|
|
bool ReleaseQuad(JQuad* quad); //We're done with this quad, so delete and stop tracking. True if existed.
|
|
protected:
|
|
JTexture * texture;
|
|
vector<JQuad*> trackedQuads;
|
|
};
|
|
|
|
class WCachedSample: public WCachedResource{
|
|
public:
|
|
friend class WResourceManager;
|
|
WCachedSample();
|
|
~WCachedSample();
|
|
|
|
JSample * GetSample(); //Return this sample.
|
|
protected:
|
|
JSample * sample;
|
|
};
|
|
|
|
enum ENUM_RETRIEVE_STYLE{
|
|
RETRIEVE_EXISTING, //Only returns a resource if it already exists. Does not lock or unlock.
|
|
RETRIEVE_NORMAL, //Returns or creates a resource. Does not change lock status.
|
|
RETRIEVE_LOCK, //As above, locks cached resource.
|
|
RETRIEVE_UNLOCK, //As above, unlocks cached resource.
|
|
RETRIEVE_RESOURCE, //Only retrieves a managed resource.
|
|
RETRIEVE_VRAM, //If we create the texture, use vram.
|
|
RETRIEVE_MANAGE, //Permanently adds retrieved resource to resource manager.
|
|
};
|
|
|
|
enum ENUM_CACHE_SUBTYPE{
|
|
CACHE_CARD,
|
|
CACHE_THUMB
|
|
};
|
|
|
|
|
|
//This class is a wrapper for JResourceManager
|
|
class WResourceManager: public JResourceManager
|
|
{
|
|
public:
|
|
WResourceManager();
|
|
~WResourceManager();
|
|
|
|
JQuad * RetrieveCard(MTGCard * card, int type = CACHE_CARD, int style = RETRIEVE_NORMAL);
|
|
JSample * RetrieveSample(string filename, int style = RETRIEVE_NORMAL);
|
|
JTexture * RetrieveTexture(string filename, int style = RETRIEVE_NORMAL);
|
|
JQuad * RetrieveQuad(string filename, float offX=0.0f, float offY=0.0f, float width=0.0f, float height=0.0f, string resname="", int style = RETRIEVE_NORMAL);
|
|
void Release(JTexture * tex);
|
|
void Release(JQuad * quad);
|
|
void Release(JSample * sample);
|
|
|
|
unsigned int nowTime();
|
|
|
|
//Our file redirect system.
|
|
string graphicsFile(const string filename, const string specific = "");
|
|
string cardFile(const string filename, const string setname, const string specific = "");
|
|
string musicFile(const string filename, const string specific = "");
|
|
string sfxFile(const string filename, const string specific = "");
|
|
int fileOK(string filename, bool relative = false);
|
|
|
|
|
|
//Not part of our interface, but left public to maintain JResourceManager compatibility
|
|
//These are for managed resources only.
|
|
int CreateTexture(const string &textureName);
|
|
int CreateQuad(const string &quadName, const string &textureName, float x=0.0f, float y=0.0f, float width=0.0f, float height=0.0f);
|
|
int LoadJLBFont(const string &fontName, int height);
|
|
int LoadMusic(const string &musicName);
|
|
int LoadSample(const string &sampleName);
|
|
|
|
//Wrapped from JSoundSystem. TODO: Privatize.
|
|
JMusic * ssLoadMusic(const char *fileName);
|
|
JSample * ssLoadSample(const char *fileName);
|
|
|
|
private:
|
|
bool RemoveOldestTexture();
|
|
bool RemoveOldestSample();
|
|
bool cleanup();
|
|
|
|
WCachedTexture * getCachedTexture(string filename, bool makenew = true, int mode = 0, int format = TEXTURE_FORMAT);
|
|
WCachedTexture * getCachedCard(MTGCard * card, int type = CACHE_CARD, bool makenew = true);
|
|
WCachedSample * getCachedSample(string filename, bool makenew = true);
|
|
|
|
void FlattenTimes(); //To prevent bad cache timing on int overflow
|
|
//For cached stuff
|
|
map<string,WCachedTexture*> textureCache;
|
|
map<string,WCachedSample*> sampleCache;
|
|
|
|
//Current access time.
|
|
int lastTime;
|
|
//Statistics of record.
|
|
int nb_textures;
|
|
int totalsize;
|
|
};
|
|
|
|
extern WResourceManager resources;
|
|
#endif |