Erwan
-moved card backs to Cache instead of ResourceManager
This commit is contained in:
@@ -29,6 +29,7 @@ struct CardGui : public PlayGuiObject {
|
||||
void RenderBig(const Pos&);
|
||||
virtual void Update(float dt);
|
||||
static void alternateRender(MTGCard * card, const Pos& pos);
|
||||
static JQuad * alternateThumbQuad(MTGCard * card);
|
||||
virtual ostream& toString(ostream&) const;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
#ifndef _TEXTURES_CACHE_H
|
||||
#define _TEXTURES_CACHE_H
|
||||
|
||||
#define MAX_CACHE_OBJECTS 100
|
||||
#define CACHE_SIZE_PIXELS 2000000
|
||||
|
||||
#define CACHE_CARD 1
|
||||
#define CACHE_CARD 1
|
||||
#define CACHE_THUMB 2
|
||||
|
||||
#include <JGE.h>
|
||||
@@ -18,22 +17,22 @@ using std::map;
|
||||
|
||||
class MTGCard;
|
||||
|
||||
class CardTexture{
|
||||
class CachedTexture{
|
||||
protected:
|
||||
int mtgid;
|
||||
|
||||
JTexture* tex;
|
||||
JQuad* quad;
|
||||
public:
|
||||
int lastTime;
|
||||
int type;
|
||||
int nbpixels;
|
||||
int getId();
|
||||
|
||||
|
||||
JQuad * getQuad();
|
||||
|
||||
CardTexture(MTGCard * card, int type);
|
||||
~CardTexture();
|
||||
void init(string filename);
|
||||
CachedTexture(MTGCard * card, int type);
|
||||
CachedTexture(string filename);
|
||||
~CachedTexture();
|
||||
};
|
||||
|
||||
|
||||
@@ -43,17 +42,18 @@ class TexturesCache{
|
||||
int nb_textures;
|
||||
int delete_previous;
|
||||
int totalsize;
|
||||
CardTexture * cache[MAX_CACHE_OBJECTS];
|
||||
map<string,CachedTexture *> cache;
|
||||
public:
|
||||
int isInCache(MTGCard * card, int type=CACHE_CARD);
|
||||
TexturesCache();
|
||||
~TexturesCache();
|
||||
int getOldestQuad();
|
||||
void removeQuad(int id);
|
||||
int removeOldestQuad();
|
||||
void removeQuad(string id);
|
||||
int cleanup();
|
||||
int getCacheById(int id, int type=CACHE_CARD);
|
||||
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;
|
||||
|
||||
|
||||
@@ -68,18 +68,8 @@ void CardGui::Render()
|
||||
icon = GameApp::CommonRes->GetQuad("c_blue");
|
||||
if (icon) icon->SetHotSpot(16,16);
|
||||
|
||||
{
|
||||
JQuad* q;
|
||||
// Draw the "unknown" card model
|
||||
switch(color)
|
||||
{
|
||||
case Constants::MTG_COLOR_GREEN: q = GameApp::CommonRes->GetQuad("green_thumb"); break;
|
||||
case Constants::MTG_COLOR_BLUE : q = GameApp::CommonRes->GetQuad("blue_thumb"); break;
|
||||
case Constants::MTG_COLOR_RED : q = GameApp::CommonRes->GetQuad("red_thumb"); break;
|
||||
case Constants::MTG_COLOR_BLACK: q = GameApp::CommonRes->GetQuad("black_thumb"); break;
|
||||
case Constants::MTG_COLOR_WHITE: q = GameApp::CommonRes->GetQuad("white_thumb"); break;
|
||||
default: q = GameApp::CommonRes->GetQuad("black_thumb"); break;
|
||||
}
|
||||
JQuad* q = alternateThumbQuad(card);
|
||||
if (q){
|
||||
q->SetColor(ARGB(static_cast<unsigned char>(actA),255,255,255));
|
||||
renderer->RenderQuad(q, actX, actY, actT, scale, scale);
|
||||
}
|
||||
@@ -103,6 +93,20 @@ void CardGui::Render()
|
||||
PlayGuiObject::Render();
|
||||
}
|
||||
|
||||
JQuad * CardGui::alternateThumbQuad(MTGCard * card){
|
||||
JRenderer * renderer = JRenderer::GetInstance();
|
||||
JQuad * q;
|
||||
switch(card->getColor())
|
||||
{
|
||||
case Constants::MTG_COLOR_GREEN: q = cache.getQuad("sets/green_thumb.jpg");break;
|
||||
case Constants::MTG_COLOR_BLUE : q = cache.getQuad("sets/blue_thumb.jpg");break;
|
||||
case Constants::MTG_COLOR_RED : q = cache.getQuad("sets/red_thumb.jpg");break;
|
||||
case Constants::MTG_COLOR_BLACK: q = cache.getQuad("sets/black_thumb.jpg");break;
|
||||
case Constants::MTG_COLOR_WHITE: q = cache.getQuad("sets/white_thumb.jpg");break;
|
||||
default: q = cache.getQuad("sets/black_thumb.jpg");break;
|
||||
}
|
||||
return q;
|
||||
}
|
||||
|
||||
void CardGui::alternateRender(MTGCard * card, const Pos& pos){
|
||||
// Draw the "unknown" card model
|
||||
@@ -110,12 +114,12 @@ void CardGui::alternateRender(MTGCard * card, const Pos& pos){
|
||||
JQuad * q;
|
||||
switch(card->getColor())
|
||||
{
|
||||
case Constants::MTG_COLOR_GREEN: q = GameApp::CommonRes->GetQuad("green"); break;
|
||||
case Constants::MTG_COLOR_BLUE : q = GameApp::CommonRes->GetQuad("blue"); break;
|
||||
case Constants::MTG_COLOR_RED : q = GameApp::CommonRes->GetQuad("red"); break;
|
||||
case Constants::MTG_COLOR_BLACK: q = GameApp::CommonRes->GetQuad("black"); break;
|
||||
case Constants::MTG_COLOR_WHITE: q = GameApp::CommonRes->GetQuad("white"); break;
|
||||
default: q = GameApp::CommonRes->GetQuad("black"); break;
|
||||
case Constants::MTG_COLOR_GREEN: q = cache.getQuad("sets/green.jpg");break;
|
||||
case Constants::MTG_COLOR_BLUE : q = cache.getQuad("sets/blue.jpg");break;
|
||||
case Constants::MTG_COLOR_RED : q = cache.getQuad("sets/red.jpg");break;
|
||||
case Constants::MTG_COLOR_BLACK: q = cache.getQuad("sets/black.jpg");break;
|
||||
case Constants::MTG_COLOR_WHITE: q = cache.getQuad("sets/white.jpg");break;
|
||||
default: q = cache.getQuad("sets/black.jpg");break;
|
||||
}
|
||||
float scale = pos.actZ * 250 / q->mHeight;
|
||||
q->SetColor(ARGB((int)pos.actA,255,255,255));
|
||||
|
||||
@@ -127,38 +127,6 @@ void GameApp::Create()
|
||||
CommonRes->CreateTexture("backdrop.jpg");
|
||||
CommonRes->CreateTexture("handback.png");
|
||||
|
||||
CommonRes->CreateTexture("red.jpg");
|
||||
CommonRes->CreateTexture("white.jpg");
|
||||
CommonRes->CreateTexture("blue.jpg");
|
||||
CommonRes->CreateTexture("black.jpg");
|
||||
CommonRes->CreateTexture("green.jpg");
|
||||
CommonRes->CreateQuad("red", "red.jpg", 0, 0, 200, 285);
|
||||
CommonRes->CreateQuad("white", "white.jpg", 0, 0, 200, 285);
|
||||
CommonRes->CreateQuad("blue", "blue.jpg", 0, 0, 200, 285);
|
||||
CommonRes->CreateQuad("black", "black.jpg", 0, 0, 200, 285);
|
||||
CommonRes->CreateQuad("green", "green.jpg", 0, 0, 200, 285);
|
||||
CommonRes->GetQuad("red")->SetHotSpot(100, 145);
|
||||
CommonRes->GetQuad("white")->SetHotSpot(100, 145);
|
||||
CommonRes->GetQuad("blue")->SetHotSpot(100, 145);
|
||||
CommonRes->GetQuad("black")->SetHotSpot(100, 145);
|
||||
CommonRes->GetQuad("green")->SetHotSpot(100, 145);
|
||||
|
||||
CommonRes->CreateTexture("red_thumb.jpg");
|
||||
CommonRes->CreateTexture("white_thumb.jpg");
|
||||
CommonRes->CreateTexture("blue_thumb.jpg");
|
||||
CommonRes->CreateTexture("black_thumb.jpg");
|
||||
CommonRes->CreateTexture("green_thumb.jpg");
|
||||
CommonRes->CreateQuad("red_thumb", "red_thumb.jpg", 0, 0, 28, 40);
|
||||
CommonRes->CreateQuad("white_thumb", "white_thumb.jpg", 0, 0, 28, 40);
|
||||
CommonRes->CreateQuad("blue_thumb", "blue_thumb.jpg", 0, 0, 28, 40);
|
||||
CommonRes->CreateQuad("black_thumb", "black_thumb.jpg", 0, 0, 28, 40);
|
||||
CommonRes->CreateQuad("green_thumb", "green_thumb.jpg", 0, 0, 28, 40);
|
||||
CommonRes->GetQuad("red_thumb")->SetHotSpot(14, 20);
|
||||
CommonRes->GetQuad("white_thumb")->SetHotSpot(14, 20);
|
||||
CommonRes->GetQuad("blue_thumb")->SetHotSpot(14, 20);
|
||||
CommonRes->GetQuad("black_thumb")->SetHotSpot(14, 20);
|
||||
CommonRes->GetQuad("green_thumb")->SetHotSpot(14, 20);
|
||||
|
||||
CommonRes->CreateTexture("BattleIcon.png");
|
||||
CommonRes->CreateTexture("DefenderIcon.png");
|
||||
CommonRes->CreateTexture("shadow.png");
|
||||
|
||||
@@ -381,7 +381,7 @@ void ManaCost::Dump(){
|
||||
}
|
||||
}
|
||||
|
||||
for (int i=0; i< nbhybrids; i++){
|
||||
for (unsigned int i=0; i< nbhybrids; i++){
|
||||
ManaCostHybrid * h = hybrids[i];
|
||||
|
||||
sprintf(buf, "H:{%c:%i}/{%c:%i}", Constants::MTGColorChars[h->color1], h->value1, Constants::MTGColorChars[h->color2], h->value2);
|
||||
|
||||
@@ -71,17 +71,8 @@ ShopItem::ShopItem(int id, JLBFont *font, int _cardid, float _xy[], bool hasFocu
|
||||
|
||||
thumb = cache.getThumb(card);
|
||||
|
||||
if (!thumb){
|
||||
switch(card->getColor())
|
||||
{
|
||||
case Constants::MTG_COLOR_GREEN: thumb = GameApp::CommonRes->GetQuad("green_thumb"); break;
|
||||
case Constants::MTG_COLOR_BLUE : thumb = GameApp::CommonRes->GetQuad("blue_thumb"); break;
|
||||
case Constants::MTG_COLOR_RED : thumb = GameApp::CommonRes->GetQuad("red_thumb"); break;
|
||||
case Constants::MTG_COLOR_BLACK: thumb = GameApp::CommonRes->GetQuad("black_thumb"); break;
|
||||
case Constants::MTG_COLOR_WHITE: thumb = GameApp::CommonRes->GetQuad("white_thumb"); break;
|
||||
default: thumb = GameApp::CommonRes->GetQuad("black_thumb"); break;
|
||||
}
|
||||
}
|
||||
if (!thumb) thumb = CardGui::alternateThumbQuad(card);
|
||||
|
||||
if (thumb){
|
||||
mesh=NEW hgeDistortionMesh(2,2);
|
||||
mesh->SetTexture(thumb->mTex);
|
||||
|
||||
@@ -10,8 +10,6 @@ TexturesCache::TexturesCache(){
|
||||
totalsize = 0;
|
||||
delete_previous = 0;
|
||||
lastTime = 0;
|
||||
for (int i=0; i<MAX_CACHE_OBJECTS;i++)
|
||||
cache[i] = NULL;
|
||||
#ifdef WIN32
|
||||
char buf [4096];
|
||||
sprintf(buf, " Init TextureCache : %p\n", this);
|
||||
@@ -21,81 +19,102 @@ TexturesCache::TexturesCache(){
|
||||
|
||||
TexturesCache::~TexturesCache(){
|
||||
LOG("==Destroying TexturesCache==");
|
||||
for (int i = 0; i < nb_textures; i++){
|
||||
delete cache[i];
|
||||
for (map<string,CachedTexture*>::iterator it = cache.begin(); it != cache.end(); ++it){
|
||||
delete it->second;
|
||||
}
|
||||
LOG("==Destroying TexturesCache Successful==");
|
||||
}
|
||||
|
||||
int TexturesCache::isInCache(MTGCard * card, int type){
|
||||
int cache_id = getCacheById(card->getId(), type);
|
||||
if (cache_id == -1)
|
||||
return 0;
|
||||
return 1;
|
||||
CachedTexture * tex = getCacheByCard(card, type);
|
||||
if (tex) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TexturesCache::getCacheById(int id, int type){
|
||||
for (int i=0; i<nb_textures;i++){
|
||||
if (cache[i]->type == type && cache[i]->getId() == id){
|
||||
return i;
|
||||
}
|
||||
CachedTexture * TexturesCache::getCacheByCard(MTGCard *card, int type){
|
||||
char _filename[512];
|
||||
if (type == CACHE_THUMB){
|
||||
sprintf(_filename, "sets/%s/thumbnails/%s", card->getSetName(), card->getImageName());
|
||||
}else{
|
||||
sprintf(_filename, "sets/%s/%s", card->getSetName(), card->getImageName());
|
||||
}
|
||||
return -1;
|
||||
string filename = _filename;
|
||||
return cache[filename];
|
||||
}
|
||||
|
||||
int TexturesCache::getOldestQuad(){
|
||||
|
||||
int TexturesCache::removeOldestQuad(){
|
||||
int oldest = -1;
|
||||
int result = -1;
|
||||
for (int i= 0; i < nb_textures; i++){
|
||||
if (oldest == -1 || oldest > cache[i]->lastTime){
|
||||
oldest = cache[i]->lastTime;
|
||||
result = i;
|
||||
string result = "";
|
||||
for (map<string,CachedTexture*>::iterator it = cache.begin(); it != cache.end(); ++it){
|
||||
if (it->second && (oldest == -1 || oldest > it->second->lastTime)){
|
||||
oldest = it->second->lastTime;
|
||||
result = it->first;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
if (oldest != -1){
|
||||
removeQuad(result);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void TexturesCache::removeQuad(int id){
|
||||
void TexturesCache::removeQuad(string id){
|
||||
totalsize -= cache[id]->nbpixels;
|
||||
delete cache[id];
|
||||
cache[id] = cache[nb_textures - 1];
|
||||
cache[nb_textures - 1] = NULL;
|
||||
cache.erase(id);
|
||||
nb_textures--;
|
||||
}
|
||||
|
||||
int TexturesCache::cleanup(){
|
||||
int maxSize = options[Options::CACHESIZE].number * 100000;
|
||||
if (!maxSize) maxSize = CACHE_SIZE_PIXELS;
|
||||
while (nb_textures >= MAX_CACHE_OBJECTS - 1 || totalsize > maxSize){
|
||||
int i = getOldestQuad();
|
||||
if (i == -1) return 0;
|
||||
removeQuad(i);
|
||||
while (totalsize > maxSize){
|
||||
int result = removeOldestQuad();
|
||||
if (!result) return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
JQuad * TexturesCache::getQuad(MTGCard * card, int type){
|
||||
int cache_id = getCacheById(card->getId(), type);
|
||||
if (cache_id == -1){
|
||||
|
||||
//Not found in the cache, we have to load the file and put it in the cache
|
||||
JQuad * TexturesCache::getQuad(string filename,MTGCard * card, int type){
|
||||
CachedTexture * ctex = cache[filename];
|
||||
if (!ctex){
|
||||
if (cleanup()){
|
||||
cache_id = nb_textures;
|
||||
cache[cache_id] = NEW CardTexture(card, type);
|
||||
totalsize+= cache[cache_id]->nbpixels;
|
||||
if (card) cache[filename] = NEW CachedTexture(card,type);
|
||||
else cache[filename] = NEW CachedTexture(filename);
|
||||
totalsize+= cache[filename]->nbpixels;
|
||||
fprintf(stderr, "Total Size of cache in pixels: %i\n", totalsize);
|
||||
nb_textures++;
|
||||
}else{
|
||||
//Error
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
cache[cache_id]->lastTime = lastTime++;
|
||||
return cache[cache_id]->getQuad();
|
||||
cache[filename]->lastTime = lastTime++;
|
||||
return cache[filename]->getQuad();
|
||||
}
|
||||
|
||||
int CardTexture::getId(){
|
||||
return mtgid;
|
||||
JQuad * TexturesCache::getQuad(MTGCard * card, int type){
|
||||
char _filename[512];
|
||||
if (type == CACHE_THUMB){
|
||||
sprintf(_filename, "sets/%s/thumbnails/%s", card->getSetName(), card->getImageName());
|
||||
}else{
|
||||
sprintf(_filename, "sets/%s/%s", card->getSetName(), card->getImageName());
|
||||
}
|
||||
string filename = _filename;
|
||||
return getQuad(filename,card,type);
|
||||
}
|
||||
|
||||
CardTexture::CardTexture(MTGCard * card, int _type): type(_type){
|
||||
|
||||
CachedTexture::CachedTexture(string filename){
|
||||
quad = NULL;
|
||||
tex = NULL;
|
||||
nbpixels = 0;
|
||||
lastTime = 0;
|
||||
if (fileExists(filename.c_str())) init(filename);
|
||||
}
|
||||
|
||||
CachedTexture::CachedTexture(MTGCard * card, int _type){
|
||||
LOG("==Creating CardTexture Object");
|
||||
JFileSystem* fs = JFileSystem::GetInstance();
|
||||
char filename[100];
|
||||
@@ -111,7 +130,7 @@ CardTexture::CardTexture(MTGCard * card, int _type): type(_type){
|
||||
|
||||
if (fileExists(filename)){
|
||||
fs->DetachZipFile();
|
||||
tex = JRenderer::GetInstance()->LoadTexture(filename, false,GU_PSM_5551);
|
||||
init(filename);
|
||||
}else{
|
||||
char zipname[100];
|
||||
sprintf(zipname, "Res/sets/%s/%s.zip", card->getSetName(),card->getSetName());
|
||||
@@ -122,23 +141,26 @@ CardTexture::CardTexture(MTGCard * card, int _type): type(_type){
|
||||
}else{
|
||||
sprintf(filename, "%s", card->getImageName());
|
||||
}
|
||||
tex = JRenderer::GetInstance()->LoadTexture(filename, false,GU_PSM_5551);
|
||||
init(filename);
|
||||
}
|
||||
}
|
||||
|
||||
LOG("CardTexture Object Creation succesful");
|
||||
}
|
||||
|
||||
void CachedTexture::init(string filename){
|
||||
tex = JRenderer::GetInstance()->LoadTexture(filename.c_str(), false,GU_PSM_5551);
|
||||
if (tex){
|
||||
quad = NEW JQuad(tex, 0.0f, 0.0f, tex->mWidth, tex->mHeight);
|
||||
quad->SetHotSpot(tex->mWidth / 2, tex->mHeight / 2);
|
||||
nbpixels = tex->mTexHeight * tex->mTexWidth;
|
||||
}
|
||||
mtgid = card->getId();
|
||||
LOG("CardTexture Object Creation succesful");
|
||||
}
|
||||
|
||||
JQuad * CardTexture::getQuad(){
|
||||
JQuad * CachedTexture::getQuad(){
|
||||
return quad;
|
||||
}
|
||||
|
||||
CardTexture::~CardTexture(){
|
||||
CachedTexture::~CachedTexture(){
|
||||
LOG("==Deleting CardTexture Object");
|
||||
SAFE_DELETE(quad);
|
||||
SAFE_DELETE(tex);
|
||||
|
||||
Reference in New Issue
Block a user