MCPcopy Create free account
hub / github.com/ddnet/ddnet / AddSound

Method AddSound

src/game/editor/editor.cpp:3848–3900  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3846}
3847
3848bool CEditor::AddSound(const char *pFilename, int StorageType, void *pUser)
3849{
3850 CEditor *pEditor = (CEditor *)pUser;
3851
3852 // check if we have that sound already
3853 char aBuf[128];
3854 IStorage::StripPathAndExtension(pFilename, aBuf, sizeof(aBuf));
3855 for(const auto &pSound : pEditor->Map()->m_vpSounds)
3856 {
3857 if(!str_comp(pSound->m_aName, aBuf))
3858 {
3859 pEditor->ShowFileDialogError("Sound named '%s' was already added.", pSound->m_aName);
3860 return false;
3861 }
3862 }
3863
3864 if(pEditor->Map()->m_vpSounds.size() >= MAX_MAPSOUNDS)
3865 {
3866 pEditor->m_PopupEventType = POPEVENT_SOUND_MAX;
3867 pEditor->m_PopupEventActivated = true;
3868 return false;
3869 }
3870
3871 // load external
3872 void *pData;
3873 unsigned DataSize;
3874 if(!pEditor->Storage()->ReadFile(pFilename, StorageType, &pData, &DataSize))
3875 {
3876 pEditor->ShowFileDialogError("Failed to open sound file '%s'.", pFilename);
3877 return false;
3878 }
3879
3880 // load sound
3881 const int SoundId = pEditor->Sound()->LoadOpusFromMem(pData, DataSize, true, pFilename);
3882 if(SoundId == -1)
3883 {
3884 free(pData);
3885 pEditor->ShowFileDialogError("Failed to load sound from file '%s'.", pFilename);
3886 return false;
3887 }
3888
3889 // add sound
3890 std::shared_ptr<CEditorSound> pSound = std::make_shared<CEditorSound>(pEditor->Map());
3891 pSound->m_SoundId = SoundId;
3892 pSound->m_DataSize = DataSize;
3893 pSound->m_pData = pData;
3894 str_copy(pSound->m_aName, aBuf);
3895 pEditor->Map()->m_vpSounds.push_back(pSound);
3896
3897 pEditor->Map()->SelectSound(pSound);
3898 pEditor->OnDialogClose();
3899 return true;
3900}
3901
3902bool CEditor::ReplaceSound(const char *pFilename, int StorageType, bool CheckDuplicate)
3903{

Callers

nothing calls this directly

Calls 11

str_compFunction · 0.85
ShowFileDialogErrorMethod · 0.80
ReadFileMethod · 0.80
LoadOpusFromMemMethod · 0.80
SelectSoundMethod · 0.80
str_copyFunction · 0.50
MapMethod · 0.45
StorageMethod · 0.45
SoundMethod · 0.45
OnDialogCloseMethod · 0.45

Tested by

no test coverage detected