char* CCShaderCache::GetShaderFile(std::string filename) { int SizeOut; char* pData; filename = GetBaseAppPath() + filename; pData = (char*)FileManager::GetFileManager()->Get(filename, &SizeOut, false); return pData; }
byte * LoadFileIntoMemoryBasic(string fileName, unsigned int *length, bool bUseSavePath, bool bAddBasePath) { *length = 0; if (bAddBasePath) { if (bUseSavePath) { fileName = GetSavePath() + fileName; } else { fileName = GetBaseAppPath() + fileName; } } FILE *fp = fopen(fileName.c_str(), "rb"); if (!fp) { //file not found return NULL; } fseek(fp, 0, SEEK_END); *length = ftell(fp); fseek(fp, 0, SEEK_SET); byte *pData = new byte[(*length) +1]; if (!pData) { fclose(fp); *length = UINT_MAX; //signal a mem error return NULL; } pData[*length] = 0; fread(pData, *length, 1, fp); fclose(fp); //we add an extra null at the end to be nice, when loading text files this can be useful return pData; }
void TextScanner::AppendToFile( string fileName, bool bAddBasePath /*= true*/ ) { if (m_lines.empty()) return; if (bAddBasePath) { fileName = GetBaseAppPath()+fileName; } FILE *fp = NULL; if (GetPlatformID() == PLATFORM_ID_LINUX) { fp = fopen(fileName.c_str(), "a+"); } else { fp = fopen(fileName.c_str(), "ab"); if (!fp) { fp = fopen(fileName.c_str(), "wb"); } } if (!fp) { //Uhh.... bad idea, could create infinite loop //LogError("Unable to create/append to %s", text); return; } string temp; for (uint32 i=0; i < m_lines.size(); i++) { temp = m_lines[i]+"\r\n"; fwrite(temp.c_str(), temp.size(), 1, fp); } fclose(fp); // }
void AudioManagerSDL::Preload( string fName, bool bLooping /*= false*/, bool bIsMusic /*= false*/, bool bAddBasePath /*= true*/, bool bForceStreaming ) { if (bIsMusic) return;//we don't preload music that way SoundObject *pObject = GetSoundObjectByFileName( fName.c_str()); if (!pObject) { //create it pObject = new SoundObject; pObject->m_fileName = fName; assert(! (GetFileExtension(fName) == "mp3") && "SDL mixer doesn't support mp3 for non music playback though"); string basePath; if (bAddBasePath) { basePath = GetBaseAppPath(); } pObject->m_pSound = Mix_LoadWAV( (basePath+fName).c_str()); if (!pObject->m_pSound) { LogMsg("Error loading %s (%s)", (basePath+fName).c_str(), Mix_GetError()); delete pObject; return; } #ifdef _DEBUG LogMsg("Caching out %s", fName.c_str()); #endif pObject->m_bIsLooping = bLooping; m_soundList.push_back(pObject); } }
AudioHandle AudioManagerSDL::Play( string fName, bool bLooping /*= false*/, bool bIsMusic, bool bAddBasePath, bool bForceStreaming) { #ifdef _DEBUG //LogMsg("********** AudioSDL: Thinking of playing %s, music=%d", fName.c_str(), int(bIsMusic)); #endif if (!GetSoundEnabled() && !bIsMusic) return AUDIO_HANDLE_BLANK; if (!GetMusicEnabled() && bIsMusic) { m_bLastMusicLooping = bLooping; m_lastMusicFileName = fName; return AUDIO_HANDLE_BLANK; } if (bIsMusic && m_bLastMusicLooping == bLooping && m_lastMusicFileName == fName && m_bLastMusicLooping && IsPlaying((AudioHandle) m_pMusicChannel)) { return (AudioHandle) m_pMusicChannel; } int loops = 0; if (bLooping) loops = -1; if (bIsMusic) { string basePath; if (bAddBasePath) { basePath = GetBaseAppPath(); } m_lastMusicFileName = fName; StopMusic(); m_pMusicChannel = Mix_LoadMUS( (basePath+fName).c_str()); if (!m_pMusicChannel && !bAddBasePath) { LogError("Couldn't load %s, trying again with full path", (basePath+fName).c_str()); basePath = GetBaseAppPath(); //try again with the basepath added.. the SDL sound system on webos seems to require it m_pMusicChannel = Mix_LoadMUS((basePath+fName).c_str()); } if (!m_pMusicChannel) { LogError("Unable to load music file %s. Missing?", (basePath+fName).c_str()); return AUDIO_HANDLE_BLANK; } m_lastMusicID = (AudioHandle) m_pMusicChannel; m_bLastMusicLooping = bLooping; SetMusicVol(m_musicVol); int ret = Mix_PlayMusic(m_pMusicChannel, loops); if (ret == -1) { LogError("Unable to play music file %s.", (GetBaseAppPath()+fName).c_str()); } return (AudioHandle) m_pMusicChannel; } //non music SoundObject *pObject = GetSoundObjectByFileName(fName); if (!pObject) { //create it Preload(fName, bLooping, bIsMusic, bAddBasePath, bForceStreaming); pObject = GetSoundObjectByFileName(fName); if (!pObject) { LogError("Unable to cache sound %s", fName.c_str()); return AUDIO_HANDLE_BLANK; } } #ifdef _DEBUG //LogMsg("AudioSDL: Playing sfx %s", fName.c_str()); #endif //play it int channel = Mix_PlayChannel(-1, pObject->m_pSound, loops); if (channel == -1) { pObject->m_pLastChannelToUse = AUDIO_HANDLE_BLANK; return AUDIO_HANDLE_BLANK; } pObject->m_pLastChannelToUse = channel + C_CHANNEL_OFFSET_SO_ZERO_ISNT_USED; //need this because sometimes it's set to nothing by default?? SetVol(pObject->m_pLastChannelToUse, 1.0f); return (AudioHandle)pObject->m_pLastChannelToUse ; }