SOUNDEMITTER COMPLETE!?!?!?

This commit is contained in:
Adam
2014-03-10 02:36:02 +01:00
parent 0bdabe3855
commit b35ad2732b
4 changed files with 49 additions and 28 deletions
+10 -2
View File
@@ -57,7 +57,7 @@ void GameWorld::Initialize()
//AddSystem("CollisionSystem");
//AddSystem("ParticleSystem");
AddSystem("PlayerSystem");
//AddSystem("SoundSystem");
AddSystem("SoundSystem");
AddSystem("RenderSystem");
std::shared_ptr<Components::Transform> transform;
@@ -70,6 +70,14 @@ void GameWorld::Initialize()
transform->Position = glm::vec3(0.f, 0.f, 0.f);
model = AddComponent<Components::Model>(ent, "Model");
model->ModelFile = "ship.obj";
auto soundEmitter = AddComponent<Components::SoundEmitter>(ent, "SoundEmitter");
soundEmitter->Gain = 1;
soundEmitter->MaxDistance = 10;
soundEmitter->Loop = true;
soundEmitter->ReferenceDistance = 0.1;
soundEmitter->Pitch = 1;
GetSystem<Systems::SoundSystem>("SoundSystem")->PlaySound(soundEmitter, "hallelujah.wav");
ent = CreateEntity();
SetProperty(ent, "Name", std::string("Camera"));
@@ -90,7 +98,7 @@ void GameWorld::RegisterSystems()
//m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); });
//m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); });
m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); });
//m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); });
m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); });
m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_Renderer); });
}
Binary file not shown.
+3 -3
View File
@@ -26,7 +26,7 @@ public:
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override;
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName);
void LoadFile(const char* fileName);
ALuint LoadFile(std::string fileName);
ALuint CreateSource();
private:
@@ -39,10 +39,10 @@ private:
DWORD sampleRate, avgBytesPerSec;
short bytesPerSample, bitsPerSample;
DWORD dataSize;
unsigned char* buf;
std::map<Component*, ALuint> source;
std::map<std::string, ALuint> buffer; // string = fileName
std::map<std::string, ALuint> m_BufferCache; // string = fileName
};
}
+36 -23
View File
@@ -71,6 +71,8 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName)
{
ALuint buffer = LoadFile(fileName);
alSourcei(source[emitter.get()], AL_BUFFER, buffer);
alSourcePlay(source[emitter.get()]);
}
@@ -80,24 +82,33 @@ void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<
source[component.get()] = CreateSource();
}
void Systems::SoundSystem::LoadFile(const char* fileName)
ALuint Systems::SoundSystem::LoadFile(std::string fileName)
{
if (m_BufferCache.find(fileName) != m_BufferCache.end())
return m_BufferCache[fileName];
FILE *fp = NULL;
fp = fopen(strcat("Sounds/", fileName), "rb");
fp = fopen(strcat("Sounds/", fileName.c_str()), "rb");
//CHECK FOR VALID WAVE-FILE
fread(type, sizeof(char), 4, fp);
if(type[0]!='R' || type[1]!='I' || type[2]!='F' || type[3]!='F')
return LOG_ERROR("ERROR: No RIFF in WAVE-file");
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(DWORD), 1, fp);
fread(type, sizeof(char), 4, fp);
if(type[0]!='W' || type[1]!='A' || type[2]!='V' || type[3]!='E')
return LOG_ERROR("ERROR: Not WAVE-file");
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]!=' ')
return LOG_ERROR("ERROR: No fmt in WAVE-file");
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(DWORD), 1, fp);
@@ -110,26 +121,19 @@ void Systems::SoundSystem::LoadFile(const char* fileName)
fread(type, sizeof(char), 4, fp);
if(type[0]!='d' || type[1]!='a' || type[2]!='t' || type[3]!='a')
return LOG_ERROR("ERROR: WAVE-file Missing data");
{
LOG_ERROR("ERROR: WAVE-file Missing data");
return 0;
}
fread(&dataSize, sizeof(DWORD), 1, fp);
buf = new unsigned char[dataSize];
unsigned char* buf = new unsigned char[dataSize];
fread(buf, sizeof(BYTE), dataSize, fp);
fclose(fp);
}
ALuint Systems::SoundSystem::CreateSource()
{
ALuint source;
ALuint buffer;
ALuint frequency = sampleRate;
// Create buffer
ALuint format = 0;
alGenBuffers(1, &buffer);
alGenBuffers(1, &source);
if(bitsPerSample == 8)
{
if(channels == 1)
@@ -137,7 +141,6 @@ ALuint Systems::SoundSystem::CreateSource()
else if(channels == 2)
format = AL_FORMAT_STEREO8;
}
if(bitsPerSample == 16)
{
if (channels == 1)
@@ -145,8 +148,18 @@ ALuint Systems::SoundSystem::CreateSource()
else if (channels == 2)
format = AL_FORMAT_STEREO16;
}
ALuint buffer;
alGenBuffers(1, &buffer);
alBufferData(buffer, format, buf, dataSize, sampleRate);
alBufferData(buffer, format, buf, dataSize, frequency);
m_BufferCache[fileName] = buffer;
return buffer;
}
ALuint Systems::SoundSystem::CreateSource()
{
ALuint source;
alGenBuffers(1, &source);
return source;
}