Improved some code (smart pointers)

Former-commit-id: 67729603eb41ce06c9b3857311146b7cd6faa4e9
This commit is contained in:
Lynix 2014-07-24 10:33:26 +02:00
parent bd0f38092e
commit 4a1d7bc503
2 changed files with 7 additions and 7 deletions

View File

@ -7,6 +7,7 @@
#include <Nazara/Audio/OpenAL.hpp> #include <Nazara/Audio/OpenAL.hpp>
#include <Nazara/Audio/SoundStream.hpp> #include <Nazara/Audio/SoundStream.hpp>
#include <Nazara/Core/Thread.hpp> #include <Nazara/Core/Thread.hpp>
#include <memory>
#include <vector> #include <vector>
#include <Nazara/Audio/Debug.hpp> #include <Nazara/Audio/Debug.hpp>
@ -18,8 +19,8 @@ bool NzMusicParams::IsValid() const
struct NzMusicImpl struct NzMusicImpl
{ {
ALenum audioFormat; ALenum audioFormat;
std::unique_ptr<NzSoundStream> stream;
std::vector<nzInt16> chunkSamples; std::vector<nzInt16> chunkSamples;
NzSoundStream* stream;
NzThread thread; NzThread thread;
bool loop = false; bool loop = false;
bool paused = false; bool paused = false;
@ -50,7 +51,7 @@ bool NzMusic::Create(NzSoundStream* soundStream)
m_impl->sampleRate = soundStream->GetSampleRate(); m_impl->sampleRate = soundStream->GetSampleRate();
m_impl->audioFormat = NzOpenAL::AudioFormat[format]; m_impl->audioFormat = NzOpenAL::AudioFormat[format];
m_impl->chunkSamples.resize(format * m_impl->sampleRate); // Une seconde de samples m_impl->chunkSamples.resize(format * m_impl->sampleRate); // Une seconde de samples
m_impl->stream = soundStream; m_impl->stream.reset(soundStream);
return true; return true;
} }
@ -61,7 +62,6 @@ void NzMusic::Destroy()
{ {
Stop(); Stop();
delete m_impl->stream;
delete m_impl; delete m_impl;
m_impl = nullptr; m_impl = nullptr;
} }

View File

@ -8,6 +8,7 @@
#include <Nazara/Audio/OpenAL.hpp> #include <Nazara/Audio/OpenAL.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <cstring> #include <cstring>
#include <memory>
#include <stdexcept> #include <stdexcept>
#include <Nazara/Audio/Debug.hpp> #include <Nazara/Audio/Debug.hpp>
@ -23,7 +24,7 @@ struct NzSoundBufferImpl
ALuint buffer; ALuint buffer;
nzAudioFormat format; nzAudioFormat format;
nzUInt32 duration; nzUInt32 duration;
nzInt16* samples; std::unique_ptr<nzInt16[]> samples;
unsigned int sampleCount; unsigned int sampleCount;
unsigned int sampleRate; unsigned int sampleRate;
}; };
@ -104,7 +105,7 @@ bool NzSoundBuffer::Create(nzAudioFormat format, unsigned int sampleCount, unsig
m_impl->format = format; m_impl->format = format;
m_impl->sampleCount = sampleCount; m_impl->sampleCount = sampleCount;
m_impl->sampleRate = sampleRate; m_impl->sampleRate = sampleRate;
m_impl->samples = new nzInt16[sampleCount]; m_impl->samples.reset(new nzInt16[sampleCount]);
std::memcpy(&m_impl->samples[0], samples, sampleCount*sizeof(nzInt16)); std::memcpy(&m_impl->samples[0], samples, sampleCount*sizeof(nzInt16));
NotifyCreated(); NotifyCreated();
@ -117,7 +118,6 @@ void NzSoundBuffer::Destroy()
{ {
NotifyDestroy(); NotifyDestroy();
delete[] m_impl->samples;
delete m_impl; delete m_impl;
m_impl = nullptr; m_impl = nullptr;
} }
@ -159,7 +159,7 @@ const nzInt16* NzSoundBuffer::GetSamples() const
} }
#endif #endif
return m_impl->samples; return m_impl->samples.get();
} }
unsigned int NzSoundBuffer::GetSampleCount() const unsigned int NzSoundBuffer::GetSampleCount() const