Jeck - Cache and resource manager merged, streamlined.

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.
This commit is contained in:
wagic.jeck
2009-09-03 09:28:16 +00:00
parent 7214248494
commit f220d2e9b9
49 changed files with 1587 additions and 1233 deletions
-8
View File
@@ -29,8 +29,6 @@
#include "../include/MTGCard.h"
#include "../include/MTGGameZones.h"
#include "../include/TexturesCache.h"
#include "../include/CardEffect.h"
#define MAX_STATE 6
@@ -47,11 +45,6 @@
#define GAME_TYPE_RANDOM2 3
class MTGAllCards;
class TexturesCache;
class GameApp: public JApp
{
@@ -83,7 +76,6 @@ class GameApp: public JApp
void LoadGameStates();
void SetNextState(int state);
static WResourceManager * CommonRes;
static hgeParticleSystem * Particles[6];
static int HasMusic;
static string systemError;
+27 -33
View File
@@ -8,7 +8,7 @@
#include "../include/GameState.h"
#include "../include/SimpleMenu.h"
#include "../include/TexturesCache.h"
#include "../include/WResourceManager.h"
#include "../include/CardGui.h"
#include "../include/GameOptions.h"
#include "../include/PriceList.h"
@@ -144,13 +144,11 @@ class GameStateDeckViewer: public GameState, public JGuiListener
pricelist = NEW PriceList(RESPATH"/settings/prices.dat",mParent->collection);
playerdata = NEW PlayerData(mParent->collection);
sellMenu = NULL;
myCollection = NEW DeckDataWrapper(NEW MTGDeck(options.profileFile(PLAYER_COLLECTION,"",false).c_str(), &cache,mParent->collection));
myCollection = NEW DeckDataWrapper(NEW MTGDeck(options.profileFile(PLAYER_COLLECTION,"",false).c_str(), mParent->collection));
displayed_deck = myCollection;
myDeck = NULL;
menuFont = GameApp::CommonRes->GetJLBFont(Constants::MENU_FONT);
mFont = GameApp::CommonRes->GetJLBFont(Constants::MAIN_FONT);
menuFont = resources.GetJLBFont(Constants::MENU_FONT);
mFont = resources.GetJLBFont(Constants::MAIN_FONT);
menu = NEW SimpleMenu(11,this,menuFont,SCREEN_WIDTH/2-100,20);
@@ -160,29 +158,29 @@ class GameStateDeckViewer: public GameState, public JGuiListener
//icon images
mIcons[Constants::MTG_COLOR_ARTIFACT] = GameApp::CommonRes->GetQuad("c_artifact");
mIcons[Constants::MTG_COLOR_LAND] = GameApp::CommonRes->GetQuad("c_land");
mIcons[Constants::MTG_COLOR_WHITE] = GameApp::CommonRes->GetQuad("c_white");
mIcons[Constants::MTG_COLOR_RED] = GameApp::CommonRes->GetQuad("c_red");
mIcons[Constants::MTG_COLOR_BLACK] = GameApp::CommonRes->GetQuad("c_black");
mIcons[Constants::MTG_COLOR_BLUE] = GameApp::CommonRes->GetQuad("c_blue");
mIcons[Constants::MTG_COLOR_GREEN] = GameApp::CommonRes->GetQuad("c_green");
mIcons[Constants::MTG_COLOR_ARTIFACT] = resources.GetQuad("c_artifact");
mIcons[Constants::MTG_COLOR_LAND] = resources.GetQuad("c_land");
mIcons[Constants::MTG_COLOR_WHITE] = resources.GetQuad("c_white");
mIcons[Constants::MTG_COLOR_RED] = resources.GetQuad("c_red");
mIcons[Constants::MTG_COLOR_BLACK] = resources.GetQuad("c_black");
mIcons[Constants::MTG_COLOR_BLUE] = resources.GetQuad("c_blue");
mIcons[Constants::MTG_COLOR_GREEN] = resources.GetQuad("c_green");
for (int i=0; i < 7; i++){
mIcons[i]->SetHotSpot(16,16);
}
pspIconsTexture = GameApp::CommonRes->LoadTexture("iconspsp.png", TEX_TYPE_USE_VRAM);
//Grab a texture in VRAM.
pspIconsTexture = resources.RetrieveTexture("iconspsp.png",RETRIEVE_VRAM);
for (int i=0; i < 8; i++){
pspIcons[i] = NEW JQuad(pspIconsTexture, i*32, 0, 32, 32);
pspIcons[i] = resources.RetrieveQuad("iconspsp.png", i*32, 0, 32, 32);
pspIcons[i]->SetHotSpot(16,16);
}
backQuad = GameApp::CommonRes->GetQuad("back");
backQuad = resources.GetQuad("back");
//menuFont = NEW JLBFont("graphics/f3",16);
menuFont = GameApp::CommonRes->GetJLBFont("f3");
menuFont = resources.GetJLBFont("f3");
welcome_menu = NEW SimpleMenu(10,this,menuFont,20,20);
int nbDecks = fillDeckMenu(welcome_menu,options.profileFile());
welcome_menu->Add(nbDecks+1, "--NEW--");
@@ -193,7 +191,7 @@ class GameStateDeckViewer: public GameState, public JGuiListener
JSoundSystem::GetInstance()->StopMusic(GameApp::music);
SAFE_DELETE(GameApp::music);
}
GameApp::music = GameApp::CommonRes->ssLoadMusic("track1.mp3");
GameApp::music = resources.ssLoadMusic("track1.mp3");
if (GameApp::music){
JSoundSystem::GetInstance()->PlayMusic(GameApp::music, true);
}
@@ -225,9 +223,10 @@ class GameStateDeckViewer: public GameState, public JGuiListener
}
SAFE_DELETE(welcome_menu);
SAFE_DELETE(menu);
SAFE_DELETE(pspIconsTexture);
resources.Release(pspIconsTexture);
for (int i=0; i < 8; i++){
SAFE_DELETE(pspIcons[i]);
pspIcons[i] = NULL; //The quads these point to are released with the texture.
}
SAFE_DELETE(myCollection);
SAFE_DELETE(myDeck);
@@ -505,7 +504,7 @@ class GameStateDeckViewer: public GameState, public JGuiListener
}
void renderOnScreenMenu(){
JLBFont * font = GameApp::CommonRes->GetJLBFont(Constants::MAIN_FONT);
JLBFont * font = resources.GetJLBFont(Constants::MAIN_FONT);
font->SetColor(ARGB(255,255,255,255));
JRenderer * r = JRenderer::GetInstance();
float pspIconsSize = 0.5;
@@ -617,15 +616,10 @@ class GameStateDeckViewer: public GameState, public JGuiListener
JQuad * quad = backQuad;
int showName = 1;
if (cache.isInCache(card) || last_user_activity > (abs(2-id) + 1)* NO_USER_ACTIVITY_SHOWCARD_DELAY){
quad = cache.getQuad(card);
showName = 0;
}
quad = resources.RetrieveCard(card);
if (quad){
showName = 0;
int quadAlpha = alpha;
if ( !displayed_deck->cards[card]) quadAlpha /=2;
quad->SetColor(ARGB(mAlpha,quadAlpha,quadAlpha,quadAlpha));
@@ -641,7 +635,7 @@ class GameStateDeckViewer: public GameState, public JGuiListener
}else{
Pos pos = Pos(x, y, scale* 285/250, 0.0, 255);
CardGui::alternateRender(card, pos);
quad = cache.getThumb(card);
quad = resources.RetrieveCard(card,CACHE_THUMB);
if (quad){
float _scale = 285 * scale / quad->mHeight;
quad->SetColor(ARGB(40,255,255,255));
@@ -727,12 +721,12 @@ class GameStateDeckViewer: public GameState, public JGuiListener
int loadDeck(int deckid){
SAFE_DELETE(myCollection);
string profile = options[Options::ACTIVE_PROFILE].str;
myCollection = NEW DeckDataWrapper(NEW MTGDeck(options.profileFile(PLAYER_COLLECTION,"",false).c_str(), &cache,mParent->collection));
myCollection = NEW DeckDataWrapper(NEW MTGDeck(options.profileFile(PLAYER_COLLECTION,"",false).c_str(), mParent->collection));
displayed_deck = myCollection;
char deckname[256];
sprintf(deckname,"deck%i.txt",deckid);
SAFE_DELETE(myDeck);
myDeck = NEW DeckDataWrapper(NEW MTGDeck(options.profileFile(deckname).c_str(), &cache,mParent->collection));
myDeck = NEW DeckDataWrapper(NEW MTGDeck(options.profileFile(deckname).c_str(), mParent->collection));
MTGCard * current = myDeck->getNext();
while (current){
int howmanyinDeck = myDeck->cards[current];
-3
View File
@@ -17,13 +17,10 @@ class GameStateMenu: public GameState, public JGuiListener
SimpleMenu* gameTypeMenu;
int hasChosenGameType;
JQuad * mIcons[10];
JTexture * mIconsTexture;
JTexture * bgTexture;
JTexture * movingWTexture;
JQuad * mBg;
JQuad * mMovingW;
JTexture * splashTex;
JQuad * splashQuad;
float mCreditsYPos;
int currentState;
//JMusic * bgMusic;
+1 -2
View File
@@ -4,7 +4,7 @@
#include "MTGCard.h"
#include "MTGGameZones.h"
#include "MTGAbility.h"
#include "TexturesCache.h"
#include "WResourceManager.h"
#include "ManaCost.h"
#include "Blocker.h"
#include "Damage.h"
@@ -15,7 +15,6 @@ class MTGCardInstance;
class MTGPlayerCards;
class MTGAbility;
class MTGCard;
class TexturesCache;
class ManaCost;
class UntapBlockers;
class CardDescriptor;
+3 -7
View File
@@ -5,7 +5,7 @@
#include "../include/MTGDefinitions.h"
#include "../include/GameApp.h"
#include "../include/TexturesCache.h"
#include "../include/WResourceManager.h"
#include <string>
@@ -48,16 +48,13 @@ private:
void initCounters();
public:
TexturesCache * mCache;
vector<int> ids;
map<int, MTGCard *> collection;
MTGAllCards();
~MTGAllCards();
MTGAllCards(TexturesCache * cache);
MTGCard * _(int id);
void destroyAllCards();
MTGAllCards(const char * config_file, const char * set_name);
MTGAllCards(const char * config_file, const char * set_name, TexturesCache * cache);
MTGCard * getCardById(int id);
MTGCard * getCardByName(string name);
int load(const char * config_file, const char * setName, int autoload = 1);
@@ -80,14 +77,13 @@ class MTGDeck{
int total_cards;
public:
TexturesCache * mCache;
MTGAllCards * database;
map <int,int> cards;
string meta_desc;
string meta_name;
int totalCards();
MTGDeck(TexturesCache * cache, MTGAllCards * _allcards);
MTGDeck(const char * config_file, TexturesCache * cache, MTGAllCards * _allcards, int meta_only = 0);
MTGDeck(MTGAllCards * _allcards);
MTGDeck(const char * config_file, MTGAllCards * _allcards, int meta_only = 0);
int addRandomCards(int howmany, int * setIds = NULL, int nbSets = 0, int rarity = -1, const char * subtype = NULL, int * colors = NULL, int nbcolors = 0);
int add(int cardid);
int add(MTGDeck * deck); // adds the contents of "deck" into myself
-84
View File
@@ -1,84 +0,0 @@
#ifndef _TEXTURES_CACHE_H
#define _TEXTURES_CACHE_H
#define CACHE_SIZE_PIXELS 2000000
#define CACHE_CARD 1
#define CACHE_THUMB 2
#include <JGE.h>
#include <JTypes.h>
#include <map>
using std::map;
#include "MTGDeck.h"
class MTGCard;
class CachedTexture{
protected:
JTexture* tex;
JQuad* quad;
public:
int lastTime;
int nbpixels;
JQuad * getQuad();
void init(string filename);
CachedTexture(MTGCard * card, int type);
CachedTexture(string filename);
~CachedTexture();
};
class TexturesCache{
protected:
int lastTime;
int nb_textures;
int delete_previous;
int totalsize;
map<string,CachedTexture *> cache;
public:
int isInCache(MTGCard * card, int type=CACHE_CARD);
TexturesCache();
~TexturesCache();
int removeOldestQuad();
void removeQuad(string id);
int cleanup();
CachedTexture * getCacheByCard(MTGCard * card, int type=CACHE_CARD);
JQuad * getQuad(MTGCard * card, int type=CACHE_CARD);
JQuad * getThumb(MTGCard * card){return getQuad(card, CACHE_THUMB);};
JQuad * getQuad(string path,MTGCard * card = NULL, int type=0);
};
extern TexturesCache cache;
class SampleCached{
public:
int lastTime;
JSample * sample;
SampleCached(int _lastTime, JSample * _sample):lastTime(_lastTime),sample(_sample){};
~SampleCached(){SAFE_DELETE(sample);};
};
class SampleCache{
protected:
int lastTime;
map<string, SampleCached *> cache;
static SampleCache * mInstance;
void cleanCache();
void cleanOldest();
~SampleCache();
public:
static SampleCache * GetInstance();
static void DestroyInstance();
SampleCache(){lastTime = 0;};
JSample * getSample(string filename);
};
#endif
+108 -38
View File
@@ -1,57 +1,127 @@
#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
class WResourceManager: public JResourceManager
{
public:
WResourceManager();
~WResourceManager();
//Wrapped from JResourceManager
void RemoveAll(){jrm->RemoveAll();}
void RemoveGraphics(){jrm->RemoveGraphics();}
void RemoveSound(){jrm->RemoveSound();}
void RemoveFont(){jrm->RemoveFont();}
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);
int CreateTexture(const string &textureName);
JTexture* GetTexture(const string &textureName);
JTexture* GetTexture(int id);
unsigned int nowTime();
int CreateQuad(const string &quadName, const string &textureName, float x, float y, float width, float height);
JQuad* GetQuad(const string &quadName);
JQuad* GetQuad(int id);
int LoadJLBFont(const string &fontName, int height);
JLBFont* GetJLBFont(const string &fontName);
JLBFont* GetJLBFont(int id);
int LoadMusic(const string &musicName);
JMusic* GetMusic(const string &musicName);
JMusic* GetMusic(int id);
int LoadSample(const string &sampleName);
JSample* GetSample(const string &sampleName);
JSample* GetSample(int id);
//Wrapped from other bits, if we want them.
JTexture* LoadTexture(const char* filename, int mode = 0, int textureFormat = TEXTURE_FORMAT);
//Wrapped from JSoundSystem
JMusic * ssLoadMusic(const char *fileName);
JSample * ssLoadSample(const char *fileName);
//Our New redirect system.
string graphicsFile(const string filename, const string specific = "", bool bFont = false);
//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);
private:
JResourceManager * jrm;
map<string,string> stopgap;
};
//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