void ConfigManager::buildSoundIndex() { int need_to_reserve = 0; int total_channels = 32; bool newBuild = main_sfx_index.empty(); #ifdef DEBUG_BUILD ElapsedTimer loadingTime; loadingTime.start(); #endif if(newBuild) { //build array table main_sfx_index.resize(static_cast<size_t>(main_sound.size()) - 1); for(unsigned long i = 1; i < main_sound.size(); i++) { obj_sound_index sound; if(main_sound.contains(i)) { obj_sound &snd = main_sound[i]; #if defined(__unix__) || defined(__APPLE__) || defined(_WIN32) FileMapper fileMap; if(fileMap.open_file(snd.absPath.c_str())) { sound.chunk = Mix_LoadWAV_RW(SDL_RWFromMem(fileMap.data(), static_cast<int>(fileMap.size())), 1); fileMap.close_file(); } #else sound.chunk = Mix_LoadWAV(snd.absPath.toUtf8().data()); #endif sound.path = snd.absPath; if(!sound.chunk) pLogWarning("Fail to load sound-%d: %s", i, Mix_GetError()); else need_to_reserve += (snd.channel >= 0 ? 1 : 0); sound.channel = snd.channel; } main_sfx_index[static_cast<size_t>(i) - 1] = sound; } } else { for(unsigned long i = 1; (i < main_sound.size()) && (i <= static_cast<unsigned long>(main_sfx_index.size())); i++) { if(main_sound.contains(i)) { obj_sound_index &sound = main_sfx_index[static_cast<size_t>(i) - 1]; obj_sound &snd = main_sound[i]; sound.setPath(snd.absPath); if(sound.need_reload) { #if defined(__unix__) || defined(__APPLE__) || defined(_WIN32) FileMapper fileMap; if(fileMap.open_file(snd.absPath.c_str())) { sound.chunk = Mix_LoadWAV_RW(SDL_RWFromMem(fileMap.data(), static_cast<int>(fileMap.size())), static_cast<int>(fileMap.size())); fileMap.close_file(); } #else sound.chunk = Mix_LoadWAV(snd.absPath.toUtf8().data()); #endif } if(!sound.chunk) pLogWarning("Fail to load sound-%d: %s", i, Mix_GetError()); else need_to_reserve += (snd.channel >= 0 ? 1 : 0); sound.channel = snd.channel; } } } if(need_to_reserve > 0) { total_channels = (total_channels + need_to_reserve + 32); total_channels = Mix_AllocateChannels(total_channels); } //Final channel definition (use reserved channels at end of channels set) //int set_channel = (total_channels-1); //for(int i=0; i < main_sfx_index.size(); i++) //{ // obj_sound_index &sound = main_sfx_index[i]; // if(sound.channel>=0) // { // sound.channel = set_channel--; // } //} if(need_to_reserve == total_channels) need_to_reserve = 0; //else // need_to_reserve=set_channel; #ifndef DEBUG_BUILD Mix_ReserveChannels(need_to_reserve) #endif #define RESERVE_CHANS_COMMAND Mix_ReserveChannels(need_to_reserve) D_pLogDebug("Loading of sounds passed in %d milliseconds", static_cast<int>(loadingTime.elapsed())); D_pLogDebug("Reserved audio channels: %d", RESERVE_CHANS_COMMAND); D_pLogDebug("SFX Index entries: %d", main_sfx_index.size()); #undef RESERVE_CHANS_COMMAND SDL_ClearError(); }
FIBITMAP *GraphicsHelps::loadImage(std::string file, bool convertTo32bit) { #ifdef DEBUG_BUILD ElapsedTimer loadingTime; ElapsedTimer fReadTime; ElapsedTimer imgConvTime; loadingTime.start(); fReadTime.start(); #endif #if defined(__unix__) || defined(__APPLE__) || defined(_WIN32) FileMapper fileMap; if(!fileMap.open_file(file.c_str())) return NULL; FIMEMORY *imgMEM = FreeImage_OpenMemory(reinterpret_cast<unsigned char *>(fileMap.data()), static_cast<unsigned int>(fileMap.size())); FREE_IMAGE_FORMAT formato = FreeImage_GetFileTypeFromMemory(imgMEM); if(formato == FIF_UNKNOWN) return NULL; FIBITMAP *img = FreeImage_LoadFromMemory(formato, imgMEM, 0); FreeImage_CloseMemory(imgMEM); fileMap.close_file(); if(!img) return NULL; #else FREE_IMAGE_FORMAT formato = FreeImage_GetFileType(file.toUtf8().data(), 0); if(formato == FIF_UNKNOWN) return NULL; FIBITMAP *img = FreeImage_Load(formato, file.toUtf8().data()); if(!img) return NULL; #endif #ifdef DEBUG_BUILD long long fReadTimeElapsed = static_cast<long long>(fReadTime.elapsed()); long long imgConvertElapsed = 0; #endif if(convertTo32bit) { #ifdef DEBUG_BUILD imgConvTime.start(); #endif FIBITMAP *temp; temp = FreeImage_ConvertTo32Bits(img); if(!temp) return NULL; FreeImage_Unload(img); img = temp; #ifdef DEBUG_BUILD imgConvertElapsed = static_cast<long long>(imgConvTime.elapsed()); #endif } #ifdef DEBUG_BUILD D_pLogDebug("File read of texture %s passed in %d milliseconds", file.c_str(), static_cast<int>(fReadTimeElapsed)); D_pLogDebug("Conv to 32-bit of %s passed in %d milliseconds", file.c_str(), static_cast<int>(imgConvertElapsed)); D_pLogDebug("Total Loading of image %s passed in %d milliseconds", file.c_str(), static_cast<int>(loadingTime.elapsed())); #endif return img; }
int main() { //cout << FileFormats::WriteExtendedLvlFile(FileFormats::dummyLvlDataArray()) << endl; LevelData level; printLine(); cout << "\n\nSMBX64 Level Read Header test:" << endl; FileFormats::ReadSMBX64LvlFileHeader("test.lvl", level); cout << level.meta.filename << "\n"; cout << level.meta.path << "\n"; if(!level.meta.ReadFileValid) { cout << "Invalid file\n" << FileFormats::errorString; } else { printLevelInfo(level); } printLine(); cout << "\n\nSMBX64 Level Read test:" << endl; FileFormats::OpenLevelFile("test.lvl", level); cout << level.meta.filename << "\n"; cout << level.meta.path << "\n"; if(!level.meta.ReadFileValid) { cout << "Invalid file\n" << FileFormats::errorString; } else { printLevelInfo(level); } FileFormats::SaveLevelFile(level, "test_out_64.lvl", FileFormats::LVL_SMBX64, 64); FileFormats::SaveLevelFile(level, "test_out_45.lvl", FileFormats::LVL_SMBX64, 45); FileFormats::SaveLevelFile(level, "test_out_1.lvl", FileFormats::LVL_SMBX64, 1); printLine(); cout << "\n\nSMBX65-38A Level Read Header test:" << endl; FileFormats::ReadSMBX38ALvlFileHeader("test_65-38a.lvl", level); cout << level.meta.filename << "\n"; cout << level.meta.path << "\n"; if(!level.meta.ReadFileValid) { cout << "Invalid file\n" << FileFormats::errorString; } else { printLevelInfo(level); } printLine(); cout << "\n\nSMBX65-38A Level Read test:" << endl; FileFormats::OpenLevelFile("test_65-38a.lvl", level); cout << level.meta.filename << "\n"; cout << level.meta.path << "\n"; if(!level.meta.ReadFileValid) { cout << "Invalid file\n" << FileFormats::errorString; } else { printLevelInfo(level); } printLine(); cout << "\n\nPGE-X Level Read Header test:" << endl; FileFormats::ReadExtendedLvlFileHeader("test.lvlx", level); cout << level.meta.filename << "\n"; cout << level.meta.path << "\n"; if(!level.meta.ReadFileValid) { cout << "Invalid file\n" << FileFormats::errorString; } else { printLevelInfo(level); } printLine(); cout << "\n\nPGE-X Level Read test:" << endl; FileFormats::OpenLevelFile("test.lvlx", level); cout << level.meta.filename << "\n"; cout << level.meta.path << "\n"; if(!level.meta.ReadFileValid) { cout << "Invalid file\n" << FileFormats::errorString; } else { printLevelInfo(level); } FileFormats::smbx64LevelPrepare(level); FileFormats::smbx64LevelSortBlocks(level); FileFormats::smbx64LevelSortBGOs(level); FileFormats::WriteExtendedLvlFileF("test_out.lvlx", level); WorldData world; printLine(); cout << "\n\nSMBX64 World Read Header test:" << endl; FileFormats::ReadSMBX64WldFileHeader("test.wld", world); cout << world.meta.filename << "\n"; cout << world.meta.path << "\n"; if(!world.meta.ReadFileValid) { cout << "Invalid file\n" << FileFormats::errorString; } else { printWorldInfo(world); } printLine(); cout << "\n\nSMBX64 World Read test:" << endl; FileFormats::OpenWorldFile("test.wld", world); cout << world.meta.filename << "\n"; cout << world.meta.path << "\n"; if(!world.meta.ReadFileValid) { cout << "Invalid file\n" << FileFormats::errorString; } else { printWorldInfo(world); } FileFormats::WriteSMBX64WldFileF("test_out_64.wld", world, 64); printLine(); cout << "\n\nPGE-X World Read Header test:" << endl; FileFormats::ReadExtendedWldFileHeader("test.wldx", world); cout << world.meta.filename << "\n"; cout << world.meta.path << "\n"; if(!world.meta.ReadFileValid) { cout << "Invalid file\n" << FileFormats::errorString; } else { printWorldInfo(world); } printLine(); cout << "\n\nPGE-X World Read test:" << endl; FileFormats::OpenWorldFile("test.wldx", world); cout << world.meta.filename << "\n"; cout << world.meta.path << "\n"; if(!world.meta.ReadFileValid) { cout << "Invalid file\n" << FileFormats::errorString; } else { printWorldInfo(world); } FileFormats::WriteExtendedWldFileF("test_out.wldx", world); PGE_FileFormats_misc::FileInfo x("shit.txt"); cout << "\n\n\n"; cout << "Name: " << x.filename() << endl; cout << "FPat: " << x.fullPath() << endl; cout << "BNam: " << x.basename() << endl; cout << "Sufx: " << x.suffix() << endl; cout << "Dirt: " << x.dirpath() << endl; //Deep tests of the level file formats #define ENABLE_SMBX64_DEEPTEST #define ENABLE_SMBX38A_DEEPTEST #define ENABLE_PGEX_DEEPTEST //required SMBX64 deeptest to pre-generate LVLX files! #ifdef ENABLE_SMBX64_DEEPTEST /**********************DEEP TEST OF SMBX64 files*********************/ { //#define GENERATE_LVLX_FILES cout << "==================DEEP TEST OF SMBX64==================\n"; std::string path = "../PGEFileLib_test_files/smbx64/"; std::string wpath = "../PGEFileLib_test_files/smbx64_out/"; #ifdef GENERATE_LVLX_FILES QString xpath = "../PGEFileLib_test_files/pgex/"; #endif TheDir testDir(path); testDir.mkdir("../smbx64_out/"); #ifdef GENERATE_LVLX_FILES testDir.mkdir("../pgex/"); #endif vector<string> files = testDir.entryList(); std::ofstream niout; niout.open("invalid_s64.log", std::ios::out); std::ofstream timesout; timesout.open("times_s64.log", std::ios::out); ElapsedTimer meter; meter.start(); for(size_t i=0; i<files.size(); i++) { std::string file = files[i]; PGE_FileFormats_misc::TextFileInput fileI(path+file, false); LevelData FileDataNew; FileDataNew = FileFormats::CreateLevelData(); fileI.seek(0, PGE_FileFormats_misc::TextInput::begin); meter.restart(); if(FileFormats::ReadSMBX64LvlFile(fileI, FileDataNew)) { clock_t got = meter.elapsed(); timesout << flString(file, 30) << " READ -> " << flString(std::to_string(got), 20); meter.restart(); FileFormats::smbx64LevelPrepare(FileDataNew); FileFormats::WriteSMBX64LvlFileF(wpath+file, FileDataNew, FileDataNew.meta.RecentFormatVersion); got = meter.elapsed(); timesout << " WRITE -> " << got << "\n"; timesout.flush(); #ifdef GENERATE_LVLX_FILES FileFormats::WriteExtendedLvlFileF(xpath+file+"x", FileDataNew); #endif } else { cout << "NEW PARSER FAILED: Invalid file\n" << FileFormats::errorString; niout << path+file << "\r\nInfo: " << FileDataNew.meta.ERROR_info << "\r\nlinedata" << FileDataNew.meta.ERROR_linedata << "\r\nline:" << FileDataNew.meta.ERROR_linenum << "\r\n\r\n"; niout.flush(); } cout.flush(); } niout.close(); cout << "==================DEEP TEST OF SMBX64=END==============\n"; cout.flush(); } /*********************************************************************/ #endif //ENABLE_SMBX64_DEEPTEST #ifdef ENABLE_SMBX38A_DEEPTEST /**********************DEEP TEST OF SMBX38A files*********************/ { cout << "==================DEEP TEST OF SMBX38A==================\n"; std::string path = "../PGEFileLib_test_files/smbx38a/"; string opath = "../PGEFileLib_test_files/smbx38a_lvlx_diffs/"; string wpath = "../PGEFileLib_test_files/smbx38a_out/"; TheDir testDir(path); testDir.mkdir("../smbx38a_lvlx_diffs"); testDir.mkdir("../smbx38a_out"); vector<string> files = testDir.entryList(); std::ofstream niout; niout.open("invalid_new.log", std::ios::out); std::ofstream oiout; oiout.open("invalid_old.log", std::ios::out); std::ofstream diout; diout.open("differents.log", std::ios::out); std::ofstream timesout; timesout.open("times.log", std::ios::out); ElapsedTimer meter; meter.start(); for(int i=0; i<(signed)files.size(); i++) { std::string file=files[i]; PGE_FileFormats_misc::TextFileInput fileI(path+file, false); string raw_old; string raw_new; LevelData FileDataNew; LevelData FileDataOld; clock_t time_old=0; clock_t time_new=0; FileDataNew = FileFormats::CreateLevelData(); fileI.seek(0, PGE_FileFormats_misc::TextInput::begin); meter.restart(); if(FileFormats::ReadSMBX38ALvlFile(fileI, FileDataNew)) { clock_t got = meter.elapsed(); time_new=got; timesout << flString(file, 30) << " NEW -> " << flString(std::to_string(got), 20); FileFormats::smbx64CountStars( FileDataNew ); meter.restart(); FileFormats::WriteExtendedLvlFileRaw(FileDataNew, raw_new); got = meter.elapsed(); timesout << " WRITE -> " << flString(std::to_string(got), 20); FileFormats::WriteSMBX38ALvlFileF(wpath+file, FileDataNew); } else { cout << "NEW PARSER FAILED: Invalid file\n" << FileFormats::errorString; niout << path+file << "\r\nInfo: " << FileDataNew.meta.ERROR_info << "\r\nlinedata" << FileDataNew.meta.ERROR_linedata << "\r\nline:" << FileDataNew.meta.ERROR_linenum << "\r\n\r\n"; niout.flush(); } FileDataOld = FileFormats::CreateLevelData(); fileI.close(); fileI.open(path+file, false); meter.restart(); if(FileFormats::ReadSMBX38ALvlFile_OLD(fileI, FileDataOld)) { clock_t got = meter.elapsed(); time_old=got; timesout << " OLD -> " << flString(std::to_string(got), 20); if(time_old>time_new) timesout << " NEW READS FASTER"; else if(time_old<time_new) timesout << " OLD READS FASTER"; else timesout << " BOTH ARE SAME"; timesout << "\n"; FileFormats::smbx64CountStars( FileDataOld ); FileFormats::WriteExtendedLvlFileRaw(FileDataOld, raw_old); } else { cout << "OLD PARSER FAILED: Invalid file\n" << FileFormats::errorString; oiout << path+file << "\r\nInfo: " << FileDataOld.meta.ERROR_info << "\r\nlinedata" << FileDataOld.meta.ERROR_linedata << "\r\nline:" << FileDataOld.meta.ERROR_linenum << "\r\n\r\n"; oiout.flush(); } if(raw_old.empty()) continue; if(raw_new.empty()) continue; if(raw_old != raw_new) { cout << "FILES ARE DIFFERENT\n"; diout << path+file << "\r\n"; diout.flush(); FileFormats::WriteExtendedLvlFileF(opath+file+".old.lvlx", FileDataNew); FileFormats::WriteExtendedLvlFileF(opath+file+".new.lvlx", FileDataOld); } else { //Remove similar files! //QFile(opath+file+".old.lvlx").remove(); //QFile(opath+file+".new.lvlx").remove(); } cout.flush(); } oiout.close(); niout.close(); diout.close(); cout << "==================DEEP TEST OF SMBX38A=END==============\n"; cout.flush(); } /*********************************************************************/ #endif//ENABLE_SMBX38A_DEEPTEST #if defined(ENABLE_SMBX64_DEEPTEST) && defined(ENABLE_PGEX_DEEPTEST) /**********************DEEP TEST OF PGE-X files*********************/ { cout << "==================DEEP TEST OF PGE-X==================\n"; string path = "../PGEFileLib_test_files/pgex/"; string wpath = "../PGEFileLib_test_files/pgex_out/"; TheDir testDir(path); testDir.mkdir("../pgex_out"); vector<string> files = testDir.entryList(); std::ofstream niout; niout.open("invalid_pgex.log", std::ios::out); std::ofstream timesout; timesout.open("times_pgex.log", std::ios::out); ElapsedTimer meter; meter.start(); for(size_t i=0; i<files.size(); i++) { string file=files[i]; PGE_FileFormats_misc::TextFileInput fileI(path+file, false); LevelData FileDataNew; FileDataNew = FileFormats::CreateLevelData(); fileI.seek(0, PGE_FileFormats_misc::TextInput::begin); meter.restart(); if(FileFormats::ReadExtendedLvlFile(fileI, FileDataNew)) { clock_t got = meter.elapsed(); timesout << flString(file, 30) << " READ -> " << flString(std::to_string(got), 20); meter.restart(); FileFormats::smbx64CountStars( FileDataNew ); FileFormats::WriteExtendedLvlFileF(wpath+file, FileDataNew); got = meter.elapsed(); timesout << " WRITE -> " << got << "\n"; timesout.flush(); } else { cout << "NEW PARSER FAILED: Invalid file\n" << FileFormats::errorString; niout << path+file << "\r\nInfo: " << FileDataNew.meta.ERROR_info << "\r\nlinedata" << FileDataNew.meta.ERROR_linedata << "\r\nline:" << FileDataNew.meta.ERROR_linenum << "\r\n\r\n"; niout.flush(); } cout.flush(); } niout.close(); cout << "==================DEEP TEST OF PGE-X=END==============\n"; cout.flush(); } /*********************************************************************/ #endif cout << "!!!!!!!!!!!!!!!!!!!!!EVERYTHING HAS BEEN DONE!!!!!!!!!!!!!!!!!!!!!\n"; return 0; }