diff --git a/projects/mtg/include/GameStateMenu.h b/projects/mtg/include/GameStateMenu.h index 54cb66bde..d32a25a54 100644 --- a/projects/mtg/include/GameStateMenu.h +++ b/projects/mtg/include/GameStateMenu.h @@ -141,6 +141,7 @@ class GameStateMenu: public GameState, public JGuiListener SAFE_DELETE(subMenuController); + SAFE_DELETE(mIconsTexture); for (int i = 0; i < 10 ; i++){ diff --git a/projects/mtg/include/MTGDeck.h b/projects/mtg/include/MTGDeck.h index 738f6f2b1..343fce361 100644 --- a/projects/mtg/include/MTGDeck.h +++ b/projects/mtg/include/MTGDeck.h @@ -13,8 +13,10 @@ #include "../include/GameApp.h" #include "../include/TexturesCache.h" #include + using std::string; + class GameApp; class MTGCard; @@ -40,9 +42,6 @@ class MtgSets{ class MTGAllCards { protected: int conf_read_mode; - int conf_fd; - char * conf_buffer; - int read_cursor; int colorsCount[MTG_NB_COLORS]; int total_cards; GameApp * parent; @@ -64,11 +63,11 @@ class MTGAllCards { int countByType(const char * _type); int countByColor(int color); int countBySet(int setId); - int readConfLine(int set_id); + int readConfLine(ifstream &file, int set_id); int totalCards(); int randomCardId(); private: - int processConfLine(char* file, MTGCard* card); + int processConfLine(string s, MTGCard* card); }; diff --git a/projects/mtg/include/utils.h b/projects/mtg/include/utils.h index 132a48ba6..3dcf1b0d4 100644 --- a/projects/mtg/include/utils.h +++ b/projects/mtg/include/utils.h @@ -26,23 +26,13 @@ #include #include -#define BUFSIZE 600 - - using std::string; -template - char ( &_ArraySizeHelper( T (&array)[N] ))[N]; -#define countof( array ) (sizeof( _ArraySizeHelper( array ) )) - - int lowercase(string source); int filesize(const char * filename); -int read_file (const char * filename, char * buffer, int filesize); -int readline (char * in_buffer, char * out_buffer, int cursor); int readfile_to_ints(const char * filename, int * out_buffer); int fileExists(const char * filename); #endif diff --git a/projects/mtg/src/GameLauncher.cpp b/projects/mtg/src/GameLauncher.cpp index 59a324023..4c25f8ab5 100644 --- a/projects/mtg/src/GameLauncher.cpp +++ b/projects/mtg/src/GameLauncher.cpp @@ -24,7 +24,7 @@ JApp* JGameLauncher::GetGameApp() //------------------------------------------------------------------------------------- char *JGameLauncher::GetName() { - return strdup("Wagic"); + return "Wagic"; } diff --git a/projects/mtg/src/MTGDeck.cpp b/projects/mtg/src/MTGDeck.cpp index 0132add4a..bca45434f 100644 --- a/projects/mtg/src/MTGDeck.cpp +++ b/projects/mtg/src/MTGDeck.cpp @@ -31,8 +31,7 @@ int MtgSets::Add(const char * name){ } -int MTGAllCards::processConfLine(char *buffer, MTGCard *card){ - string s = buffer; +int MTGAllCards::processConfLine(string s, MTGCard *card){ unsigned int i = s.find_first_of("="); if (i == string::npos) return 0; string key = s.substr(0,i); @@ -132,15 +131,14 @@ void MTGAllCards::init(){ int MTGAllCards::load(const char * config_file, const char * set_name,int autoload){ conf_read_mode = 0; - int file_size = filesize(config_file); - conf_buffer = (char *) malloc(file_size); - read_cursor = 0; - // conf_fd = sceIoOpen(config_file, PSP_O_RDONLY, 0777); - read_file(config_file, conf_buffer, file_size ); int set_id = MtgSets::SetsList->Add(set_name); - if (autoload){ - while(readConfLine(set_id)){}; + + std::ifstream setFile(config_file); + + if (setFile){ + while(readConfLine(setFile, set_id)){}; } + return total_cards; } @@ -153,6 +151,11 @@ MTGAllCards::~MTGAllCards(){ void MTGAllCards::destroyAllCards(){ for (int i= 0; i < total_cards; i++){ +#ifdef WIN32 +char buf[4096]; +sprintf(buf,"deleting %s\n", collection[i]->getName()); + OutputDebugString(buf); +#endif delete collection[i]; }; @@ -227,33 +230,33 @@ int MTGAllCards::totalCards(){ return (total_cards); } -int MTGAllCards::readConfLine(int set_id){ - char buffer[BUFSIZE]; - read_cursor = readline(conf_buffer, buffer, read_cursor); - if (read_cursor){ +int MTGAllCards::readConfLine(std::ifstream &file, int set_id){ + + string s; + int result = 0; + if(std::getline(file,s)) result = 1; switch(conf_read_mode) { case 0: - conf_read_mode = 1; - collection[total_cards] = NEW MTGCard(mCache,set_id); + if (s.find("[card]") != string::npos){ + collection[total_cards] = NEW MTGCard(mCache,set_id); + conf_read_mode = 1; + } break; case 1: - if (buffer[0] == '[' && buffer[1] == '/'){ - conf_read_mode = 0; - total_cards++; + if (s.find("[/card]") != string::npos){ + conf_read_mode = 0; + total_cards++; }else{ - processConfLine(buffer, collection[total_cards]); + processConfLine(s, collection[total_cards]); } break; default: break; } - }else{ - free (conf_buffer); - } - - return read_cursor; + + return result; } diff --git a/projects/mtg/src/SimpleMenu.cpp b/projects/mtg/src/SimpleMenu.cpp index 5fe7ec5c9..c21d409a7 100644 --- a/projects/mtg/src/SimpleMenu.cpp +++ b/projects/mtg/src/SimpleMenu.cpp @@ -25,8 +25,9 @@ void SimpleMenu::Render(){ renderer->FillRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT,ARGB(70,0,0,0)); renderer->FillRoundRect(mX,mY,mWidth,mHeight,10,ARGB(255,17,17,17)); renderer->FillRoundRect(mX+2,mY+2,mWidth - 4,mHeight-4,10,ARGB(255,62,62,62)); - if (displaytitle) + if (displaytitle){ mFont->DrawString(title.c_str(), mX+10, mY+10); + } for (int i = startId; i < startId + maxItems ; i++){ if (i > mCount-1) break; ((SimpleMenuItem * )mObjects[i])->RenderWithOffset(-20*startId); @@ -48,3 +49,5 @@ void SimpleMenu::Add(int id, const char * text){ JGuiController::Add(NEW SimpleMenuItem(id, mFont, text, mWidth/2 + mX + 10, mY + 10 + y, (mCount == 0))); if (mCount <= maxItems) mHeight += 20; } + + diff --git a/projects/mtg/src/TexturesCache.cpp b/projects/mtg/src/TexturesCache.cpp index 43c406eb1..1ee4c30d6 100644 --- a/projects/mtg/src/TexturesCache.cpp +++ b/projects/mtg/src/TexturesCache.cpp @@ -126,6 +126,7 @@ SampleCache * SampleCache::GetInstance(){ } JSample * SampleCache::getSample(string filename){ + lastTime++; map::iterator it = cache.find(filename); if (it == cache.end()){ if (cache.size() >10) cleanOldest(); //Poor man's limit @@ -134,10 +135,11 @@ JSample * SampleCache::getSample(string filename){ cleanCache(); sample = JSoundSystem::GetInstance()->LoadSample(filename.c_str()); } - lastTime++; + cache[filename] = NEW SampleCached(lastTime, sample); return sample; }else{ + it->second->lastTime = lastTime; return (it->second->sample); } } diff --git a/projects/mtg/src/utils.cpp b/projects/mtg/src/utils.cpp index 1574d68c3..c34164ac1 100644 --- a/projects/mtg/src/utils.cpp +++ b/projects/mtg/src/utils.cpp @@ -34,72 +34,6 @@ int filesize(const char * filename){ return file_size; } -int read_file (const char * filename, char * buffer, int file_size){ - int a = 0; -#if defined (WIN32) || defined (LINUX) - FILE * file = fopen(filename, "rb"); - a = fread(buffer, 1, file_size, file); - fclose(file); -#else - int file = sceIoOpen(filename,PSP_O_RDONLY, 0777); - - a = sceIoRead(file, buffer, file_size); - sceIoClose(file); -#endif - - fprintf(stderr, "%s is %d characters long.\n", filename, strlen(&buffer[0]) ); - return a; - -} - -int readline (char * in_buffer, char * out_buffer, int cursor){ - char a; - int found = 0; - int i = 0; - - //int read_ok = 0; - while (found == 0){ - a = in_buffer[cursor]; - cursor++; - if (a == '\r'){ - a = in_buffer[cursor]; - cursor ++; - } - if (a == 0){ - found = 1; - cursor = 0; - }else{ - if(a == '\n' || i==(BUFSIZE - 1)){ - found = 1; - out_buffer[i] = 0; - - if (a != '\n'){ - int endofline = 0; - while (!endofline){ - //int read; - a = in_buffer[cursor]; - - cursor++; - if (a == 0 || a=='\n'){ - endofline = 1; - fprintf(stderr, "buffer overflow in readline %s\n", out_buffer); - } - if (a == 0 ){ - cursor = 0; - } - } - } - }else{ - out_buffer[i] = a; - i++; - } - - } - } - out_buffer[i] = 0; - return(cursor); -} - int readfile_to_ints(const char * filename, int * out_buffer){