linux-wallpaperengine/src/WallpaperEngine/Render/Objects/CSound.cpp
Alexis Maiquez d03edb7c1e ~ cleaned up main function and moved initialization functions to a more proper place
~ check for SDL initialization before playing audio to prevent error messages when there is not really any error
~ changed a missed iterator to auto on CSound

Signed-off-by: Alexis Maiquez <almamu@almamu.com>
2019-09-10 12:55:45 +02:00

89 lines
2.2 KiB
C++

#include <SDL.h>
#include <SDL_rwops.h>
#include <SDL_mixer.h>
#include "CSound.h"
using namespace WallpaperEngine::Render::Objects;
CSound::CSound (CScene* scene, Core::Objects::CSound* sound) :
CObject (scene, Type, sound),
m_sound (sound)
{
this->setAutomaticCulling (irr::scene::EAC_OFF);
this->m_boundingBox = irr::core::aabbox3d<irr::f32>(0, 0, 0, 0, 0, 0);
this->load ();
this->play ();
}
void CSound::load ()
{
if (SDL_WasInit (SDL_INIT_AUDIO) != SDL_INIT_AUDIO)
{
return;
}
auto cur = this->m_sound->getSounds ().begin ();
auto end = this->m_sound->getSounds ().end ();
for (; cur != end; cur ++)
{
SDL_RWops* sdlRwops = nullptr;
Mix_Music* music = nullptr;
irr::io::IReadFile* readfile = this->getScene ()->getContext ()->getDevice ()->getFileSystem ()->createAndOpenFile ((*cur).c_str ());
int filesize = readfile->getSize ();
char* filebuffer = new char [filesize];
readfile->read (filebuffer, filesize);
sdlRwops = SDL_RWFromConstMem (filebuffer, filesize);
music = Mix_LoadMUS_RW (sdlRwops);
readfile->drop ();
if (music == nullptr)
{
this->getScene ()->getContext ()->getDevice ()->getLogger ()->log (
"cannot load audio", Mix_GetError (), irr::ELL_ERROR
);
continue;
}
this->m_bufferReader.push_back (sdlRwops);
this->m_soundBuffer.push_back (filebuffer);
this->m_sdl.push_back (music);
}
}
void CSound::play ()
{
if (SDL_WasInit (SDL_INIT_AUDIO) != SDL_INIT_AUDIO)
{
return;
}
auto mixcur = this->m_sdl.begin ();
auto mixend = this->m_sdl.end ();
for (; mixcur != mixend; mixcur ++)
{
if (Mix_PlayMusic ((*mixcur), -1) == -1)
{
this->getScene ()->getContext ()->getDevice ()->getLogger ()->log (
"cannot play audio", Mix_GetError (), irr::ELL_ERROR
);
}
}
}
void CSound::render ()
{
}
const irr::core::aabbox3d<irr::f32>& CSound::getBoundingBox() const
{
return this->m_boundingBox;
}
const std::string CSound::Type = "sound";