Refactored existing systems to register their own components and resource types.

This commit is contained in:
2014-04-13 16:41:35 +02:00
parent e4e5068727
commit 062242ab8e
14 changed files with 183 additions and 131 deletions
+13 -88
View File
@@ -24,6 +24,16 @@ Systems::SoundSystem::SoundSystem(World* world)
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
}
void Systems::SoundSystem::RegisterComponents(ComponentFactory* cf)
{
cf->Register("SoundEmitter", []() { return new Components::SoundEmitter(); });
}
void Systems::SoundSystem::RegisterResourceTypes(ResourceManager* rm)
{
rm->RegisterType("Sound", [](std::string resourceName) { return new Sound(resourceName); });
}
void Systems::SoundSystem::Update(double dt)
{
@@ -81,7 +91,7 @@ void Systems::SoundSystem::PlaySound(Components::SoundEmitter* emitter, std::str
if (m_Sources.find(emitter) == m_Sources.end())
return;
ALuint buffer = LoadFile(fileName);
ALuint buffer = *m_World->GetResourceManager()->Load<Sound>("Sound", fileName);
if (buffer == 0)
return;
ALuint source = m_Sources[emitter];
@@ -91,7 +101,7 @@ void Systems::SoundSystem::PlaySound(Components::SoundEmitter* emitter, std::str
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter)
{
ALuint buffer = LoadFile(emitter->Path);
ALuint buffer = *m_World->GetResourceManager()->Load<Sound>("Sound", emitter->Path);
ALuint source = m_Sources[emitter.get()];
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(m_Sources[emitter.get()]);
@@ -123,91 +133,6 @@ void Systems::SoundSystem::OnComponentRemoved(std::string type, Component* compo
}
}
ALuint Systems::SoundSystem::LoadFile(std::string path)
{
if (m_BufferCache.find(path) != m_BufferCache.end())
return m_BufferCache[path];
FILE* fp = NULL;
fp = fopen(path.c_str(), "rb");
if (fp == NULL)
{
LOG_ERROR("Failed to load sound file \"%s\"", path.c_str());
return 0;
}
//CHECK FOR VALID WAVE-FILE
fread(type, sizeof(char), 4, fp);
if(type[0]!='R' || type[1]!='I' || type[2]!='F' || type[3]!='F')
{
LOG_ERROR("ERROR: No RIFF in WAVE-file");
return 0;
}
fread(&size, sizeof(unsigned long), 1, fp);
fread(type, sizeof(char), 4, fp);
if(type[0]!='W' || type[1]!='A' || type[2]!='V' || type[3]!='E')
{
LOG_ERROR("ERROR: Not WAVE-file");
return 0;
}
fread(type, sizeof(char), 4, fp);
if(type[0]!='f' || type[1]!='m' || type[2]!='t' || type[3]!=' ')
{
LOG_ERROR("ERROR: No fmt in WAVE-file");
return 0;
}
//READ THE DATA FROM WAVE-FILE
fread(&chunkSize, sizeof(unsigned long), 1, fp);
fread(&formatType, sizeof(short), 1, fp);
fread(&channels, sizeof(short), 1, fp);
fread(&sampleRate, sizeof(unsigned long), 1, fp);
fread(&avgBytesPerSec, sizeof(unsigned long), 1, fp);
fread(&bytesPerSample, sizeof(short), 1, fp);
fread(&bitsPerSample, sizeof(short), 1, fp);
fread(type, sizeof(char), 4, fp);
if(type[0]!='d' || type[1]!='a' || type[2]!='t' || type[3]!='a')
{
LOG_ERROR("ERROR: WAVE-file Missing data");
return 0;
}
fread(&dataSize, sizeof(unsigned long), 1, fp);
unsigned char* buf = new unsigned char[dataSize];
fread(buf, sizeof(unsigned char), dataSize, fp);
fclose(fp);
// Create buffer
ALuint format = 0;
if(bitsPerSample == 8)
{
if(channels == 1)
format = AL_FORMAT_MONO8;
else if(channels == 2)
format = AL_FORMAT_STEREO8;
}
if(bitsPerSample == 16)
{
if (channels == 1)
format = AL_FORMAT_MONO16;
else if (channels == 2)
format = AL_FORMAT_STEREO16;
}
ALuint buffer;
alGenBuffers(1, &buffer);
alBufferData(buffer, format, buf, dataSize, sampleRate);
delete[] buf;
m_BufferCache[path] = buffer;
return buffer;
}
ALuint Systems::SoundSystem::CreateSource()
{
ALuint source;
@@ -217,4 +142,4 @@ ALuint Systems::SoundSystem::CreateSource()
alDopplerVelocity(350.f); // Defines the velocity of the sound
return source;
}
}