From f7fdafbff4d5e483b2ecc1bcf0c52788ca879ab6 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 22 Mar 2011 02:02:54 +0000 Subject: [PATCH] - did some code refactoring, wanted to load some textures threaded but bailed out due to opengl's hideous problems related to multi-threads --- source/glest_game/game/game.cpp | 301 +++++++++++------- source/glest_game/game/game.h | 8 +- source/glest_game/graphics/renderer.cpp | 16 +- source/glest_game/graphics/renderer.h | 1 + source/glest_game/main/main.cpp | 19 +- .../menu/menu_state_custom_game.cpp | 91 +++--- source/shared_lib/include/graphics/texture.h | 1 + .../include/platform/common/simple_threads.h | 21 +- .../platform/common/simple_threads.cpp | 81 ++++- 9 files changed, 367 insertions(+), 172 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 8011f0e0..858e7e0b 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -42,6 +42,11 @@ Game *thisGamePtr = NULL; const float PHOTO_MODE_MAXHEIGHT = 500.0; +Game::Game() : ProgramState(NULL) { + originalDisplayMsgCallback = NULL; + aiInterfaces.clear(); +} + Game::Game(Program *program, const GameSettings *gameSettings): ProgramState(program), lastMousePos(0), isFirstRender(true) { @@ -191,43 +196,15 @@ Texture2D * Game::findFactionLogoTexture(const GameSettings *settings, Logger *l return result; } -string Game::findFactionLogoFile(const GameSettings *settings, Logger *logger,string factionLogoFilter) { - string result = ""; - if(settings == NULL) { - result = ""; - } - //Logger &logger= Logger::getInstance(); - string mapName= settings->getMap(); - string tilesetName= settings->getTileset(); - string techName= settings->getTech(); - string scenarioName= settings->getScenario(); - bool loadingImageUsed=false; - - if(logger != NULL) { - logger->setState(Lang::getInstance().get("Loading")); - - if(scenarioName.empty()){ - logger->setSubtitle(formatString(mapName)+" - "+formatString(tilesetName)+" - "+formatString(techName)); - } - else{ - logger->setSubtitle(formatString(scenarioName)); - } - } - - Config &config = Config::getInstance(); - //good_fpu_control_registers(NULL,__FILE__,__FUNCTION__,__LINE__); - - //bool skipCustomLoadScreen = true; - bool skipCustomLoadScreen = false; - - string scenarioDir = ""; - if(skipCustomLoadScreen == false && settings->getScenarioDir() != "") { +string Game::extractScenarioLogoFile(const GameSettings *settings, string factionLogoFilter, + string &result, Logger *logger, bool & loadingImageUsed) { + string scenarioDir = ""; + if(settings->getScenarioDir() != "") { scenarioDir = settings->getScenarioDir(); if(EndsWith(scenarioDir, ".xml") == true) { scenarioDir = scenarioDir.erase(scenarioDir.size() - 4, 4); scenarioDir = scenarioDir.erase(scenarioDir.size() - settings->getScenario().size(), settings->getScenario().size() + 1); } - // use a scenario based loading screen vector loadScreenList; findAll(scenarioDir + factionLogoFilter, loadScreenList, false, false); if(loadScreenList.size() > 0) { @@ -244,96 +221,58 @@ string Game::findFactionLogoFile(const GameSettings *settings, Logger *logger,st } SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s]\n",__FILE__,__FUNCTION__,__LINE__,settings->getScenarioDir().c_str(),settings->getScenario().c_str(),scenarioDir.c_str()); } + return scenarioDir; +} - // give CPU time to update other things to avoid apperance of hanging - //sleep(0); - //SDL_PumpEvents(); +string Game::extractFactionLogoFile(bool &loadingImageUsed, string factionName, Logger *logger, + string scenarioDir, string techName, string factionLogoFilter) { + string result = ""; + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Searching for faction loading screen\n",__FILE__,__FUNCTION__,__LINE__); + //for ( int i=0; i < settings->getFactionCount(); ++i ) { + // if( settings->getFactionControl(i) == ctHuman || + // (settings->getFactionControl(i) == ctNetwork && settings->getThisFactionIndex() == i)) { - if(skipCustomLoadScreen == false && loadingImageUsed == false){ - // try to use a faction related loading screen - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Searching for faction loading screen\n",__FILE__,__FUNCTION__,__LINE__); - for ( int i=0; i < settings->getFactionCount(); ++i ) { - if( settings->getFactionControl(i) == ctHuman || - (settings->getFactionControl(i) == ctNetwork && settings->getThisFactionIndex() == i)) { + //printf("In [%s::%s Line: %d] looking for loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,settings->getFactionTypeName(i).c_str()); + //if(settings->getFactionTypeName(i) == formatString(GameConstants::OBSERVER_SLOTNAME)) { + if(factionName == formatString(GameConstants::OBSERVER_SLOTNAME)) { + string data_path = getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + const string factionLogo = data_path + "data/core/misc_textures/observer.jpg"; + //printf("In [%s::%s Line: %d] looking for loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,factionLogo.c_str()); - //printf("In [%s::%s Line: %d] looking for loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,settings->getFactionTypeName(i).c_str()); - if(settings->getFactionTypeName(i) == formatString(GameConstants::OBSERVER_SLOTNAME)) { - string data_path = getGameReadWritePath(GameConstants::path_data_CacheLookupKey); - const string factionLogo = data_path + "data/core/misc_textures/observer.jpg"; - //printf("In [%s::%s Line: %d] looking for loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,factionLogo.c_str()); + if(fileExists(factionLogo) == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] found loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,factionLogo.c_str()); - if(fileExists(factionLogo) == true) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] found loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,factionLogo.c_str()); - - result = factionLogo; - if(logger != NULL) { - logger->loadLoadingScreen(result); - } - loadingImageUsed = true; - } - } - else if(settings->getFactionTypeName(i) == formatString(GameConstants::RANDOMFACTION_SLOTNAME)) { - string data_path = getGameReadWritePath(GameConstants::path_data_CacheLookupKey); - const string factionLogo = data_path + "data/core/misc_textures/random.jpg"; - //printf("In [%s::%s Line: %d] looking for loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,factionLogo.c_str()); - - if(fileExists(factionLogo) == true) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] found loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,factionLogo.c_str()); - - result = factionLogo; - if(logger != NULL) { - logger->loadLoadingScreen(result); - } - loadingImageUsed = true; - } - } - else { - vector pathList=config.getPathListForType(ptTechs,scenarioDir); - for(int idx = 0; idx < pathList.size(); idx++) { - string currentPath = pathList[idx]; - endPathWithSlash(currentPath); - string path = currentPath + techName + "/" + "factions" + "/" + settings->getFactionTypeName(i); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] possible loading screen dir '%s'\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); - if(isdir(path.c_str()) == true) { - endPathWithSlash(path); - - vector loadScreenList; - findAll(path + factionLogoFilter, loadScreenList, false, false); - if(loadScreenList.size() > 0) { - string factionLogo = path + loadScreenList[0]; - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] looking for loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,factionLogo.c_str()); - - if(fileExists(factionLogo) == true) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] found loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,factionLogo.c_str()); - - result = factionLogo; - if(logger != NULL) { - logger->loadLoadingScreen(result); - } - loadingImageUsed = true; - break; - } - } - } - - if(loadingImageUsed == true) { - break; - } - } - } - break; + result = factionLogo; + if(logger != NULL) { + logger->loadLoadingScreen(result); } + loadingImageUsed = true; } } - if(skipCustomLoadScreen == false && loadingImageUsed == false){ - // try to use a tech related loading screen - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Searching for tech loading screen\n",__FILE__,__FUNCTION__,__LINE__); + //else if(settings->getFactionTypeName(i) == formatString(GameConstants::RANDOMFACTION_SLOTNAME)) { + else if(factionName == formatString(GameConstants::RANDOMFACTION_SLOTNAME)) { + string data_path = getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + const string factionLogo = data_path + "data/core/misc_textures/random.jpg"; + //printf("In [%s::%s Line: %d] looking for loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,factionLogo.c_str()); + if(fileExists(factionLogo) == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] found loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,factionLogo.c_str()); + + result = factionLogo; + if(logger != NULL) { + logger->loadLoadingScreen(result); + } + loadingImageUsed = true; + } + } + else { + Config &config = Config::getInstance(); vector pathList=config.getPathListForType(ptTechs,scenarioDir); for(int idx = 0; idx < pathList.size(); idx++) { string currentPath = pathList[idx]; endPathWithSlash(currentPath); - string path = currentPath + techName; + //string path = currentPath + techName + "/" + "factions" + "/" + settings->getFactionTypeName(i); + string path = currentPath + techName + "/" + "factions" + "/" + factionName; SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] possible loading screen dir '%s'\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); if(isdir(path.c_str()) == true) { endPathWithSlash(path); @@ -356,15 +295,157 @@ string Game::findFactionLogoFile(const GameSettings *settings, Logger *logger,st } } } + if(loadingImageUsed == true) { break; } } } + //break; + //} + //} + return result; +} + +string Game::extractTechLogoFile(string scenarioDir, string techName, + string factionLogoFilter, Logger *logger, bool &loadingImageUsed) { + string result = ""; + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Searching for tech loading screen\n",__FILE__,__FUNCTION__,__LINE__); + Config &config = Config::getInstance(); + vector pathList = config.getPathListForType(ptTechs, scenarioDir); + for(int idx = 0; idx < pathList.size(); idx++) { + string currentPath = pathList[idx]; + endPathWithSlash(currentPath); + string path = currentPath + techName; + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] possible loading screen dir '%s'\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); + if(isdir(path.c_str()) == true) { + endPathWithSlash(path); + + vector loadScreenList; + findAll(path + factionLogoFilter, loadScreenList, false, false); + if(loadScreenList.size() > 0) { + string factionLogo = path + loadScreenList[0]; + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] looking for loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,factionLogo.c_str()); + + if(fileExists(factionLogo) == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] found loading screen '%s'\n",__FILE__,__FUNCTION__,__LINE__,factionLogo.c_str()); + + result = factionLogo; + if(logger != NULL) { + logger->loadLoadingScreen(result); + } + loadingImageUsed = true; + break; + } + } + } + if(loadingImageUsed == true) { + break; + } + } + + return result; +} + +string Game::findFactionLogoFile(const GameSettings *settings, Logger *logger,string factionLogoFilter) { + string result = ""; + if(settings == NULL) { + result = ""; + } + string mapName= settings->getMap(); + string tilesetName= settings->getTileset(); + string techName= settings->getTech(); + string scenarioName= settings->getScenario(); + bool loadingImageUsed=false; + + if(logger != NULL) { + logger->setState(Lang::getInstance().get("Loading")); + + if(scenarioName.empty()) { + logger->setSubtitle(formatString(mapName) + " - " + + formatString(tilesetName) + " - " + formatString(techName)); + } + else { + logger->setSubtitle(formatString(scenarioName)); + } + } + + string scenarioDir = ""; + bool skipCustomLoadScreen = false; + if(skipCustomLoadScreen == false) { + scenarioDir = extractScenarioLogoFile(settings, + factionLogoFilter, result, logger, loadingImageUsed); + } + // try to use a faction related loading screen + if(skipCustomLoadScreen == false && loadingImageUsed == false) { + for(int i=0; i < settings->getFactionCount(); ++i ) { + if( settings->getFactionControl(i) == ctHuman || + (settings->getFactionControl(i) == ctNetwork && settings->getThisFactionIndex() == i)) { + result = extractFactionLogoFile(loadingImageUsed, settings->getFactionTypeName(i), + logger,scenarioDir, techName, factionLogoFilter); + break; + } + } + } + + // try to use a tech related loading screen + if(skipCustomLoadScreen == false && loadingImageUsed == false){ + result = extractTechLogoFile(scenarioDir, techName, + factionLogoFilter, logger, loadingImageUsed); + } return result; } +vector Game::processTech(string techName) { + vector logoFiles; + bool enableFactionTexturePreview = Config::getInstance().getBool("FactionPreview","true"); + if(enableFactionTexturePreview) { + string currentTechName_factionPreview = techName; + + vector factions; + vector techPaths = Config::getInstance().getPathListForType(ptTechs); + for(int idx = 0; idx < techPaths.size(); idx++) { + string &techPath = techPaths[idx]; + endPathWithSlash(techPath); + findAll(techPath + techName + "/factions/*.", factions, false, false); + + if(factions.size() > 0) { + for(unsigned int factionIdx = 0; factionIdx < factions.size(); ++factionIdx) { + bool loadingImageUsed = false; + string factionLogo = ""; + string currentFactionName_factionPreview = factions[factionIdx]; + + factionLogo = Game::extractFactionLogoFile( + loadingImageUsed, + currentFactionName_factionPreview, + NULL, + "", + techName, + "preview_screen.*"); + + if(factionLogo == "") { + factionLogo = Game::extractFactionLogoFile( + loadingImageUsed, + currentFactionName_factionPreview, + NULL, + "", + techName, + "loading_screen.*"); + } + if(factionLogo != "") { + Texture2D *texture = Renderer::preloadTexture(factionLogo); + logoFiles.push_back(texture); + // Renderer::findFactionLogoTexture(factionLogo); + } + } + } + } + } + + return logoFiles; +} + void Game::load() { load(lgt_All); } diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index 0c01aa58..04bdd90b 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -51,7 +51,7 @@ enum LoadGameItem { // ===================================================== //class Game: public ProgramState, public SimpleTaskCallbackInterface { -class Game: public ProgramState { +class Game: public ProgramState, public FileCRCPreCacheThreadCallbackInterface { public: enum Speed{ sFast, @@ -115,6 +115,7 @@ private: bool gameStarted; public: + Game(); Game(Program *program, const GameSettings *gameSettings); ~Game(); @@ -171,9 +172,14 @@ public: static Texture2D * findFactionLogoTexture(const GameSettings *settings, Logger *logger,string factionLogoFilter="loading_screen.*", bool useTechDefaultIfFilterNotFound=true); static string findFactionLogoFile(const GameSettings *settings, Logger *logger, string factionLogoFilter="loading_screen.*"); + static string extractScenarioLogoFile(const GameSettings *settings, string factionLogoFilter,string &result, Logger *logger, bool &loadingImageUsed); + static string extractFactionLogoFile(bool &loadingImageUsed, string factionName, Logger *logger,string scenarioDir, string techName, string factionLogoFilter); + static string extractTechLogoFile(string scenarioDir, string techName,string factionLogoFilter, Logger *logger, bool &loadingImageUsed); + bool getGameOver() { return gameOver; } bool hasGameStarted() { return gameStarted;} + virtual vector processTech(string techName); private: //render diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 5ddce848..52f9bd7f 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -5165,7 +5165,7 @@ uint64 Renderer::getCurrentPixelByteCount(ResourceScope rs) const { return result; } -Texture2D * Renderer::findFactionLogoTexture(string logoFilename) { +Texture2D * Renderer::preloadTexture(string logoFilename) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] logoFilename [%s]\n",__FILE__,__FUNCTION__,__LINE__,logoFilename.c_str()); Texture2D *result = NULL; @@ -5184,7 +5184,7 @@ Texture2D * Renderer::findFactionLogoTexture(string logoFilename) { result = renderer.newTexture2D(rsGlobal); result->setMipmap(true); result->load(logoFilename); - renderer.initTexture(rsGlobal,result); + //renderer.initTexture(rsGlobal,result); crcFactionPreviewTextureCache[logoFilename] = result; } } @@ -5192,6 +5192,18 @@ Texture2D * Renderer::findFactionLogoTexture(string logoFilename) { return result; } +Texture2D * Renderer::findFactionLogoTexture(string logoFilename) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] logoFilename [%s]\n",__FILE__,__FUNCTION__,__LINE__,logoFilename.c_str()); + + Texture2D *result = preloadTexture(logoFilename); + if(result != NULL && result->getInited() == false) { + Renderer &renderer= Renderer::getInstance(); + renderer.initTexture(rsGlobal,result); + } + + return result; +} + void Renderer::cycleShowDebugUILevel() { if((showDebugUILevel & debugui_fps) != debugui_fps || (showDebugUILevel & debugui_unit_titles) != debugui_unit_titles) { diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index 0824439d..adba714c 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -434,6 +434,7 @@ public: void renderProgressBar(int size, int x, int y, Font2D *font,int customWidth=-1, string prefixLabel=""); static Texture2D * findFactionLogoTexture(string logoFilename); + static Texture2D * preloadTexture(string logoFilename); int getCachedSurfaceDataSize() const { return mapSurfaceData.size(); } private: diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index b2176f23..8cde6192 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -685,7 +685,8 @@ void MainWindow::eventKeyDown(char key){ if(key == configKeys.getCharKey("HotKeyShowDebug")) { Renderer &renderer= Renderer::getInstance(); - if(keystate.mod & (KMOD_LCTRL | KMOD_RCTRL)) { + //if(keystate.mod & (KMOD_LCTRL | KMOD_RCTRL)) { + if(keystate.mod & (KMOD_LALT | KMOD_RALT)) { renderer.cycleShowDebugUILevel(); } else { @@ -1821,6 +1822,7 @@ int glestMain(int argc, char** argv) { // Setup the file crc thread std::auto_ptr preCacheThread; + Game preCacheThreadGame; //float pingTime = Socket::getAveragePingMS("soft-haus.com"); //printf("Ping time = %f\n",pingTime); @@ -2083,6 +2085,7 @@ int glestMain(int argc, char** argv) { preCacheThread.reset(new FileCRCPreCacheThread()); preCacheThread->setUniqueID(__FILE__); preCacheThread->setTechDataPaths(techDataPaths); + preCacheThread->setFileCRCPreCacheThreadCallbackInterface(&preCacheThreadGame); preCacheThread->start(); } @@ -2095,9 +2098,23 @@ int glestMain(int argc, char** argv) { //string test = lang.get("ExitGameServer?"); //printf("[%s]",test.c_str()); + //time_t lastTextureLoadEvent = time(NULL); //main loop while(Window::handleEvent()) { program->loop(); + // Because OpenGL really doesn't do multi-threading well +// if(difftime(time(NULL),lastTextureLoadEvent) >= 3) { +// lastTextureLoadEvent = time(NULL); +// vector textureList = preCacheThread->getPendingTextureList(1); +// for(unsigned int i = 0; i < textureList.size(); ++i) { +// Texture2D * factionLogo = textureList[i]; +// if(factionLogo != NULL) { +// printf("\n\n\n\n|||||||||||||||||||||||||| Load texture [%s]\n",factionLogo->getPath().c_str()); +// //Renderer::findFactionLogoTexture(factionLogo); +// renderer.initTexture(rsGlobal,factionLogo); +// } +// } +// } } if(preCacheThread.get() != NULL) { diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 3c7f02b3..1426d60a 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -2224,53 +2224,54 @@ void MenuStateCustomGame::loadGameSettings(GameSettings *gameSettings) { gameSettings->setNetworkFramePeriod(config.getInt("NetworkSendFrameCount","20")); gameSettings->setNetworkPauseGameForLaggedClients(((listBoxNetworkPauseGameForLaggedClients.getSelectedItemIndex() != 0))); - if( gameSettings->getTileset() != "") { - if(lastCheckedCRCTilesetName != gameSettings->getTileset()) { - //console.addLine("Checking tileset CRC [" + gameSettings->getTileset() + "]"); - lastCheckedCRCTilesetValue = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTilesets,""), string("/") + gameSettings->getTileset() + string("/*"), ".xml", NULL); - lastCheckedCRCTilesetName = gameSettings->getTileset(); + if(hasNetworkGameSettings() == true) { + if( gameSettings->getTileset() != "") { + if(lastCheckedCRCTilesetName != gameSettings->getTileset()) { + //console.addLine("Checking tileset CRC [" + gameSettings->getTileset() + "]"); + lastCheckedCRCTilesetValue = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTilesets,""), string("/") + gameSettings->getTileset() + string("/*"), ".xml", NULL); + lastCheckedCRCTilesetName = gameSettings->getTileset(); + } + gameSettings->setTilesetCRC(lastCheckedCRCTilesetValue); + } + + if(config.getBool("DisableServerLobbyTechtreeCRCCheck","false") == false) { + if(gameSettings->getTech() != "") { + if(lastCheckedCRCTechtreeName != gameSettings->getTech()) { + //console.addLine("Checking techtree CRC [" + gameSettings->getTech() + "]"); + lastCheckedCRCTechtreeValue = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs,""), "/" + gameSettings->getTech() + "/*", ".xml", NULL); + + reloadFactions(true); + factionCRCList.clear(); + for(unsigned int factionIdx = 0; factionIdx < factionFiles.size(); ++factionIdx) { + string factionName = factionFiles[factionIdx]; + int32 factionCRC = 0; + if(factionName != GameConstants::RANDOMFACTION_SLOTNAME && + factionName != GameConstants::OBSERVER_SLOTNAME) { + factionCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs,""), "/" + gameSettings->getTech() + "/factions/" + factionName + "/*", ".xml", NULL, true); + } + factionCRCList.push_back(make_pair(factionName,factionCRC)); + } + //console.addLine("Found factions: " + intToStr(factionCRCList.size())); + lastCheckedCRCTechtreeName = gameSettings->getTech(); + } + + gameSettings->setFactionCRCList(factionCRCList); + gameSettings->setTechCRC(lastCheckedCRCTechtreeValue); + } + } + + if(gameSettings->getMap() != "") { + if(lastCheckedCRCMapName != gameSettings->getMap()) { + Checksum checksum; + string file = Map::getMapPath(gameSettings->getMap(),"",false); + //console.addLine("Checking map CRC [" + file + "]"); + checksum.addFile(file); + lastCheckedCRCMapValue = checksum.getSum(); + lastCheckedCRCMapName = gameSettings->getMap(); + } + gameSettings->setMapCRC(lastCheckedCRCMapValue); } - gameSettings->setTilesetCRC(lastCheckedCRCTilesetValue); } - - if(config.getBool("DisableServerLobbyTechtreeCRCCheck","false") == false) { - if(gameSettings->getTech() != "") { - if(lastCheckedCRCTechtreeName != gameSettings->getTech()) { - //console.addLine("Checking techtree CRC [" + gameSettings->getTech() + "]"); - lastCheckedCRCTechtreeValue = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs,""), "/" + gameSettings->getTech() + "/*", ".xml", NULL); - - reloadFactions(true); - factionCRCList.clear(); - for(unsigned int factionIdx = 0; factionIdx < factionFiles.size(); ++factionIdx) { - string factionName = factionFiles[factionIdx]; - int32 factionCRC = 0; - if(factionName != GameConstants::RANDOMFACTION_SLOTNAME && - factionName != GameConstants::OBSERVER_SLOTNAME) { - factionCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs,""), "/" + gameSettings->getTech() + "/factions/" + factionName + "/*", ".xml", NULL, true); - } - factionCRCList.push_back(make_pair(factionName,factionCRC)); - } - //console.addLine("Found factions: " + intToStr(factionCRCList.size())); - lastCheckedCRCTechtreeName = gameSettings->getTech(); - } - - gameSettings->setFactionCRCList(factionCRCList); - gameSettings->setTechCRC(lastCheckedCRCTechtreeValue); - } - } - - if(gameSettings->getMap() != "") { - if(lastCheckedCRCMapName != gameSettings->getMap()) { - Checksum checksum; - string file = Map::getMapPath(gameSettings->getMap(),"",false); - //console.addLine("Checking map CRC [" + file + "]"); - checksum.addFile(file); - lastCheckedCRCMapValue = checksum.getSum(); - lastCheckedCRCMapName = gameSettings->getMap(); - } - gameSettings->setMapCRC(lastCheckedCRCMapValue); - } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); } diff --git a/source/shared_lib/include/graphics/texture.h b/source/shared_lib/include/graphics/texture.h index 302ac92e..7f55e1a5 100644 --- a/source/shared_lib/include/graphics/texture.h +++ b/source/shared_lib/include/graphics/texture.h @@ -72,6 +72,7 @@ public: WrapMode getWrapMode() const {return wrapMode;} bool getPixmapInit() const {return pixmapInit;} Format getFormat() const {return format;} + bool getInited() const {return inited;} void setMipmap(bool mipmap) {this->mipmap= mipmap;} void setWrapMode(WrapMode wrapMode) {this->wrapMode= wrapMode;} diff --git a/source/shared_lib/include/platform/common/simple_threads.h b/source/shared_lib/include/platform/common/simple_threads.h index a5593d9c..588f45cc 100644 --- a/source/shared_lib/include/platform/common/simple_threads.h +++ b/source/shared_lib/include/platform/common/simple_threads.h @@ -15,13 +15,23 @@ #include #include #include "util.h" +#include "texture.h" #include "leak_dumper.h" using namespace std; using namespace Shared::Util; +using namespace Shared::Graphics; namespace Shared { namespace PlatformCommon { +// +// This interface describes the methods a callback object must implement +// +class FileCRCPreCacheThreadCallbackInterface { +public: + virtual vector processTech(string techName) = 0; +}; + // ===================================================== // class FileCRCPreCacheThread // ===================================================== @@ -31,13 +41,22 @@ class FileCRCPreCacheThread : public BaseThread protected: vector techDataPaths; vector workerThreadTechPaths; + FileCRCPreCacheThreadCallbackInterface *processTechCB; + + Mutex mutexPendingTextureList; + vector pendingTextureList; + + void addPendingTexture(Texture2D *texture); + void addPendingTextureList(vector textureList); public: FileCRCPreCacheThread(); - FileCRCPreCacheThread(vector techDataPaths,vector workerThreadTechPaths); + FileCRCPreCacheThread(vector techDataPaths,vector workerThreadTechPaths,FileCRCPreCacheThreadCallbackInterface *processTechCB); virtual void execute(); void setTechDataPaths(vector value) { this->techDataPaths = value; } void setWorkerThreadTechPaths(vector value) { this->workerThreadTechPaths = value; } + void setFileCRCPreCacheThreadCallbackInterface(FileCRCPreCacheThreadCallbackInterface *value) { processTechCB = value; } + vector getPendingTextureList(int maxTexturesToGet); }; // ===================================================== diff --git a/source/shared_lib/sources/platform/common/simple_threads.cpp b/source/shared_lib/sources/platform/common/simple_threads.cpp index 50656a19..27e6628f 100644 --- a/source/shared_lib/sources/platform/common/simple_threads.cpp +++ b/source/shared_lib/sources/platform/common/simple_threads.cpp @@ -28,12 +28,15 @@ const static int MAX_FileCRCPreCacheThread_WORKER_THREADS = 3; FileCRCPreCacheThread::FileCRCPreCacheThread() : BaseThread() { techDataPaths.clear(); workerThreadTechPaths.clear(); + processTechCB = NULL; } FileCRCPreCacheThread::FileCRCPreCacheThread(vector techDataPaths, - vector workerThreadTechPaths) { - this->techDataPaths = techDataPaths; - this->workerThreadTechPaths = workerThreadTechPaths; + vector workerThreadTechPaths, + FileCRCPreCacheThreadCallbackInterface *processTechCB) { + this->techDataPaths = techDataPaths; + this->workerThreadTechPaths = workerThreadTechPaths; + this->processTechCB = processTechCB; } void FileCRCPreCacheThread::execute() { @@ -93,7 +96,10 @@ void FileCRCPreCacheThread::execute() { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] workerIdx = %d, currentWorkerMax = %d, endConsumerIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,workerIdx,currentWorkerMax,endConsumerIndex); - FileCRCPreCacheThread *workerThread = new FileCRCPreCacheThread(techDataPaths,workerTechList); + FileCRCPreCacheThread *workerThread = + new FileCRCPreCacheThread(techDataPaths, + workerTechList, + this->processTechCB); workerThread->setUniqueID(__FILE__); preCacheWorkerThreadList.push_back(workerThread); workerThread->start(); @@ -111,15 +117,21 @@ void FileCRCPreCacheThread::execute() { hasRunningWorkerThread = false; for(unsigned int idx = 0; idx < preCacheWorkerThreadList.size(); idx++) { FileCRCPreCacheThread *workerThread = preCacheWorkerThreadList[idx]; - if(workerThread != NULL && workerThread->getRunningStatus() == true) { - hasRunningWorkerThread = true; - if(getQuitStatus() == true) { - workerThread->signalQuit(); + + if(workerThread != NULL) { + //vector textureList = workerThread->getPendingTextureList(-1); + //addPendingTextureList(textureList); + + if(workerThread->getRunningStatus() == true) { + hasRunningWorkerThread = true; + if(getQuitStatus() == true) { + workerThread->signalQuit(); + } + } + else if(workerThread->getRunningStatus() == false) { + delete workerThread; + preCacheWorkerThreadList[idx] = NULL; } - } - else if(workerThread != NULL && workerThread->getRunningStatus() == false) { - delete workerThread; - preCacheWorkerThreadList[idx] = NULL; } } @@ -159,6 +171,16 @@ void FileCRCPreCacheThread::execute() { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] cached CRC value for Tech [%s] is [%d] took %.3f seconds.\n",__FILE__,__FUNCTION__,__LINE__,techName.c_str(),techCRC,difftime(time(NULL),elapsedTime)); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] cached CRC value for Tech [%s] is [%d] took %.3f seconds.\n",__FILE__,__FUNCTION__,__LINE__,techName.c_str(),techCRC,difftime(time(NULL),elapsedTime)); } + +// if(processTechCB) { +// vector files = processTechCB->processTech(techName); +// for(unsigned int logoIdx = 0; logoIdx < files.size(); ++logoIdx) { +// addPendingTexture(files[logoIdx]); +// +// if(SystemFlags::VERBOSE_MODE_ENABLED) printf("--------------------- CRC worker thread added texture [%s] for tech [%s] ---------------------------\n",files[logoIdx]->getPath().c_str(),techName.c_str()); +// } +// } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("--------------------- CRC worker thread END for tech [%s] ---------------------------\n",techName.c_str()); if(getQuitStatus() == true) { @@ -181,6 +203,41 @@ void FileCRCPreCacheThread::execute() { deleteSelfIfRequired(); } +void FileCRCPreCacheThread::addPendingTextureList(vector textureList) { + for(unsigned int textureIdx = 0; textureIdx < textureList.size(); ++textureIdx) { + this->addPendingTexture(textureList[textureIdx]); + } +} + +void FileCRCPreCacheThread::addPendingTexture(Texture2D *texture) { + if(texture == NULL) { + return; + } + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(&mutexPendingTextureList,mutexOwnerId); + pendingTextureList.push_back(texture); + safeMutex.ReleaseLock(); +} + +vector FileCRCPreCacheThread::getPendingTextureList(int maxTexturesToGet) { + vector result; + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(&mutexPendingTextureList,mutexOwnerId); + unsigned int listCount = pendingTextureList.size(); + if(listCount > 0) { + if(maxTexturesToGet >= 0) { + listCount = maxTexturesToGet; + } + for(unsigned int i = 0; i < listCount; ++i) { + result.push_back(pendingTextureList[i]); + } + pendingTextureList.erase(pendingTextureList.begin() + 0, pendingTextureList.begin() + listCount); + } + safeMutex.ReleaseLock(); + + return result; +} + SimpleTaskThread::SimpleTaskThread( SimpleTaskCallbackInterface *simpleTaskInterface, unsigned int executionCount, unsigned int millisecsBetweenExecutions,