~ moved OnRegisterSceneNode to CObject as this function should barely change

+ support for sounds added

Signed-off-by: Alexis Maiquez <almamu@almamu.com>
This commit is contained in:
Alexis Maiquez 2019-09-09 10:13:06 +02:00
parent e285b313e0
commit f7be32eed3
8 changed files with 135 additions and 9 deletions

View File

@ -50,6 +50,8 @@ add_executable(
src/WallpaperEngine/Render/Objects/CImage.h src/WallpaperEngine/Render/Objects/CImage.h
src/WallpaperEngine/Render/Objects/CImage.cpp src/WallpaperEngine/Render/Objects/CImage.cpp
src/WallpaperEngine/Render/Objects/CSound.h
src/WallpaperEngine/Render/Objects/CSound.cpp
src/WallpaperEngine/FileSystem/FileSystem.cpp src/WallpaperEngine/FileSystem/FileSystem.cpp
src/WallpaperEngine/FileSystem/FileSystem.h src/WallpaperEngine/FileSystem/FileSystem.h

View File

@ -22,4 +22,12 @@ CObject::~CObject()
CScene* CObject::getScene () CScene* CObject::getScene ()
{ {
return this->m_scene; return this->m_scene;
}
void CObject::OnRegisterSceneNode ()
{
if (this->m_object->isVisible () == true)
SceneManager->registerNodeForRendering (this);
ISceneNode::OnRegisterSceneNode ();
} }

View File

@ -23,6 +23,8 @@ namespace WallpaperEngine::Render
CObject (CScene* scene, std::string type, Core::CObject *object); CObject (CScene* scene, std::string type, Core::CObject *object);
~CObject (); ~CObject ();
void OnRegisterSceneNode () override;
CScene* getScene (); CScene* getScene ();
private: private:

View File

@ -1,5 +1,9 @@
#include "WallpaperEngine/Core/Objects/CImage.h" #include "WallpaperEngine/Core/Objects/CImage.h"
#include "WallpaperEngine/Core/Objects/CSound.h"
#include "WallpaperEngine/Render/Objects/CImage.h" #include "WallpaperEngine/Render/Objects/CImage.h"
#include "WallpaperEngine/Render/Objects/CSound.h"
#include "CScene.h" #include "CScene.h"
using namespace WallpaperEngine; using namespace WallpaperEngine;
@ -28,6 +32,10 @@ CScene::CScene (Core::CProject* project, Irrlicht::CContext* context) :
{ {
new Objects::CImage (this, (*cur)->As <Core::Objects::CImage> ()); new Objects::CImage (this, (*cur)->As <Core::Objects::CImage> ());
} }
else if ((*cur)->Is <Core::Objects::CSound> () == true)
{
new Objects::CSound (this, (*cur)->As <Core::Objects::CSound> ());
}
} }
this->m_nextId = ++highestId; this->m_nextId = ++highestId;

View File

@ -181,14 +181,6 @@ const irr::core::aabbox3d<irr::f32>& CImage::getBoundingBox() const
return this->m_boundingBox; return this->m_boundingBox;
} }
void CImage::OnRegisterSceneNode ()
{
if (this->m_image->isVisible () == true)
SceneManager->registerNodeForRendering (this);
ISceneNode::OnRegisterSceneNode ();
}
void CImage::OnSetConstants (irr::video::IMaterialRendererServices *services, int32_t userData) void CImage::OnSetConstants (irr::video::IMaterialRendererServices *services, int32_t userData)
{ {
irr::f32 g_Texture0 = 0; irr::f32 g_Texture0 = 0;

View File

@ -21,7 +21,6 @@ namespace WallpaperEngine::Render::Objects
void render () override; void render () override;
const irr::core::aabbox3d<irr::f32>& getBoundingBox() const override; const irr::core::aabbox3d<irr::f32>& getBoundingBox() const override;
void OnRegisterSceneNode () override;
protected: protected:
static const std::string Type; static const std::string Type;

View File

@ -0,0 +1,78 @@
#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 ()
{
std::vector<std::string>::const_iterator cur = this->m_sound->getSounds ()->begin ();
std::vector<std::string>::const_iterator 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 ()
{
std::vector<Mix_Music*>::const_iterator mixcur = this->m_sdl.begin ();
std::vector<Mix_Music*>::const_iterator 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";

View File

@ -0,0 +1,37 @@
#pragma once
#include <SDL_rwops.h>
#include <SDL_mixer.h>
#include "WallpaperEngine/Core/Objects/CSound.h"
#include "WallpaperEngine/Render/CObject.h"
using namespace WallpaperEngine;
namespace WallpaperEngine::Render::Objects
{
class CSound : public CObject
{
public:
CSound (CScene* scene, Core::Objects::CSound* sound);
void render () override;
const irr::core::aabbox3d<irr::f32>& getBoundingBox() const override;
protected:
static const std::string Type;
void load ();
void play ();
private:
std::vector<std::string> m_filenames;
std::vector <Mix_Music*> m_sdl;
std::vector <SDL_RWops*> m_bufferReader;
std::vector <void*> m_soundBuffer;
Core::Objects::CSound* m_sound;
irr::core::aabbox3d<irr::f32> m_boundingBox;
};
}