Some new logic, m_Channels and m_Sounds added. Sound is really low...

This commit is contained in:
Stiffly
2014-05-25 01:34:57 +02:00
parent ecbcee0fbf
commit 3a638a356c
5 changed files with 97 additions and 45 deletions
+2 -2
View File
@@ -10,10 +10,10 @@ namespace Components
struct SoundEmitter : Component
{
SoundEmitter() : Gain(1.f), MaxDistance(1.f), ReferenceDistance(1.f), Pitch(1.f), Loop(false) {}
SoundEmitter() : Gain(1.f), MaxDistance(1.f), MinDistance(1.f), Pitch(1.f), Loop(false) {}
float Gain;
float MaxDistance;
float ReferenceDistance;
float MinDistance;
float Pitch;
bool Loop;
std::string Path;
+31 -8
View File
@@ -218,7 +218,7 @@ void GameWorld::Initialize()
{
auto tank = CreateEntity();
auto listener = AddComponent<Components::Listener>(tank);
auto transform = AddComponent<Components::Transform>(tank);
transform->Position = glm::vec3(0, 5, 0);
//transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0));
@@ -309,6 +309,7 @@ void GameWorld::Initialize()
auto cameraComp = AddComponent<Components::Camera>(camera);
cameraComp->FarClip = 2000.f;
AddComponent<Components::Input>(camera);
AddComponent<Components::Listener>(tank);
//auto freeSteering = AddComponent<Components::FreeSteering>(camera);
CommitEntity(camera);
}
@@ -688,13 +689,35 @@ void GameWorld::Initialize()
}
}
auto soundEmitter = CreateEntity();
auto transform = AddComponent<Components::Transform>(soundEmitter);
transform->Position = glm::vec3(0,10,0);
transform->Scale = glm::vec3(3);
AddComponent<Components::SoundEmitter>(soundEmitter);
auto model = AddComponent<Components::Model>(soundEmitter);
model->ModelFile = "Models/PlaceHolders/PhysicsTest/PointLight.obj";
{
auto se1 = CreateEntity();
auto transform = AddComponent<Components::Transform>(se1);
transform->Position = glm::vec3(0,10,0);
transform->Scale = glm::vec3(3);
auto emitterComponent = AddComponent<Components::SoundEmitter>(se1);
emitterComponent->Path = "Sounds/WUB.mp3";
emitterComponent->MinDistance = 1.f;
emitterComponent->MaxDistance = 50.f;
emitterComponent->Gain = 1;
auto model = AddComponent<Components::Model>(se1);
model->ModelFile = "Models/PlaceHolders/PhysicsTest/PointLight.obj";
CommitEntity(se1);
}
{
auto se2 = CreateEntity();
auto transform = AddComponent<Components::Transform>(se2);
transform->Position = glm::vec3(100,10,0);
transform->Scale = glm::vec3(3);
auto emitterComponent = AddComponent<Components::SoundEmitter>(se2);
emitterComponent->Path = "Sounds/DarkHorse.mp3";
emitterComponent->Gain = 1;
emitterComponent->MinDistance = 1.f;
emitterComponent->MaxDistance = 50.f;
auto model = AddComponent<Components::Model>(se2);
model->ModelFile = "Models/PlaceHolders/PhysicsTest/PointLight.obj";
CommitEntity(se2);
}
/*for (int x = 0; x < 5; x++)
+1 -1
View File
@@ -159,7 +159,7 @@ bool Systems::InputSystem::OnBindKey(const Events::BindKey &event)
else
{
m_KeyBindings[event.KeyCode] = std::make_tuple(event.Command, event.Value);
LOG_DEBUG("Input: Bound key %c to %s", (char)event.KeyCode, event.Command.c_str());
LOG_DEBUG("Input: Bound key %i:%c to %s", event.KeyCode, (char)event.KeyCode, event.Command.c_str());
}
return true;
+54 -30
View File
@@ -19,9 +19,11 @@ void Systems::SoundSystem::Initialize()
m_FmodSystem->playSound(FMOD_CHANNEL_FREE, sound, false, 0);*/
FMOD_System_Create(&m_System);
FMOD_System_Init(m_System, 32, FMOD_INIT_NORMAL, nullptr);
FMOD_System_Set3DSettings(m_System, 10.0f, 10.f, 1.0f); //dopplerScale, distancefactor, rolloffscale
FMOD_System_GetMasterChannelGroup(m_System, &m_ChannelGruoup);
FMOD_System_Init(m_System, 1024, FMOD_INIT_NORMAL, nullptr);
FMOD_System_Set3DSettings(m_System, 10.0f, 1.f, 100.f); //dopplerScale, distancefactor, rolloffscale
//FMOD_System_GetMasterChannelGroup(m_System, &m_ChannelGruoup);
m_Playing = false;
}
void Systems::SoundSystem::RegisterComponents(ComponentFactory* cf)
@@ -60,13 +62,14 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
glm::vec3 tForward = tOri[2];
FMOD_VECTOR lForward = {tForward.x, tForward.y, tForward.z};
FMOD_System_Set3DListenerAttributes(m_System, 0, (const FMOD_VECTOR*)&lPos, (const FMOD_VECTOR*)&lVel, (const FMOD_VECTOR*)&lForward, (const FMOD_VECTOR*)&lUp);
FMOD_System_Set3DListenerAttributes(m_System, lID, (const FMOD_VECTOR*)&lPos, (const FMOD_VECTOR*)&lVel, (const FMOD_VECTOR*)&lForward, (const FMOD_VECTOR*)&lUp);
}
auto emitter = m_World->GetComponent<Components::SoundEmitter>(entity);
if(emitter)
{
FMOD_CHANNEL* channel = m_Channels[entity];
FMOD_SOUND* sound = m_Sounds[entity];
auto eTransform = m_World->GetComponent<Components::Transform>(entity);
glm::vec3 tPos = eTransform->Position;
@@ -75,53 +78,74 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
glm::vec3 tVel = eTransform->Velocity;
FMOD_VECTOR eVel = {tVel.x, tVel.y, tVel.z};
FMOD_Channel_SetMode(channel, FMOD_3D_LINEARROLLOFF);
FMOD_Channel_Set3DAttributes(channel, (const FMOD_VECTOR*)&ePos, (const FMOD_VECTOR*)&eVel);
if(!m_Playing)
{
PlaySound(m_Channels[entity], 1, false);
PlaySound(channel, sound, 1, emitter->Loop);
m_Playing = true;
}
}
}
void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
{
if(type == typeid(Components::SoundEmitter).name())
// if(type == typeid(Components::Listener).name())
// {
// m_Listeners.push_back(component->Entity);
// }
//
// if (type == typeid(Components::SoundEmitter).name())
// {
// //std::shared_ptr<Components::SoundEmitter> eComponent = std::dynamic_pointer_cast<Components::SoundEmitter>(component);
//
// FMOD_CHANNEL* channel;
// m_Channels.insert(std::pair<EntityID, FMOD_CHANNEL*>(component->Entity, channel));
//
// LoadSound("Sounds/DarkHorse.mp3", 100, 30, false, 1, 0);
// }
}
void Systems::SoundSystem::OnEntityCommit(EntityID entity)
{
//TEMP ska slängas in i OnComponentCreated
auto lComponent = m_World->GetComponent<Components::Listener>(entity);
if(lComponent)
{
m_Listeners.push_back(component->Entity);
m_Listeners.push_back(entity);
}
if (type == typeid(Components::SoundEmitter).name())
auto eCompoonent = m_World->GetComponent<Components::SoundEmitter>(entity);
if(eCompoonent)
{
FMOD_CHANNEL* channel;
m_Channels.insert(std::pair<EntityID, FMOD_CHANNEL*>(component->Entity, channel));
LoadSound("Sounds/WUB.mp3");
FMOD_SOUND* sound;
std::string path = eCompoonent->Path;
float volume = eCompoonent->Gain;
bool loop = eCompoonent->Loop;
float maxDist = eCompoonent->MaxDistance;
float minDist = eCompoonent->MinDistance;
float pitch = eCompoonent->Pitch;
if(LoadSound(sound, path, maxDist, minDist, loop, volume, pitch) != FMOD_OK)
LOG_ERROR("FMOD did not load sound file");
m_Channels.insert(std::make_pair(entity, channel));
m_Sounds.insert(std::make_pair(entity, sound));
}
}
bool Systems::SoundSystem::LoadSound(std::string filePath)
FMOD_RESULT Systems::SoundSystem::LoadSound(FMOD_SOUND* &sound, std::string filePath, float maxDist, float minDist, bool loop, float volume, float pitch)
{
bool loadedFile;
FMOD_RESULT result = FMOD_System_CreateSound(m_System, filePath.c_str(), FMOD_3D_LINEARROLLOFF, NULL, &m_Sound);
result = FMOD_Sound_Set3DMinMaxDistance(m_Sound, 1.f, 100.f);
FMOD_RESULT result = FMOD_System_CreateSound(m_System, filePath.c_str(), FMOD_3D , 0, &sound);
FMOD_Sound_Set3DMinMaxDistance(m_Sound, minDist, maxDist);
if(result != FMOD_OK)
{
loadedFile = false;
LOG_ERROR("FMOD ERROR: Could not load sound file: %s \n", filePath.c_str());
}
else
{
loadedFile = true;
}
return loadedFile;
return result;
}
void Systems::SoundSystem::PlaySound(FMOD_CHANNEL* channel, float volume, bool loop)
void Systems::SoundSystem::PlaySound(FMOD_CHANNEL* channel, FMOD_SOUND* sound, float volume, bool loop)
{
m_Playing = true;
FMOD_System_PlaySound(m_System, FMOD_CHANNEL_FREE, m_Sound, false, &channel);
FMOD_System_PlaySound(m_System, FMOD_CHANNEL_FREE, sound, false, &channel);
FMOD_Channel_SetVolume(channel, volume);
}
+9 -4
View File
@@ -26,20 +26,25 @@ public:
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override;
void OnEntityCommit(EntityID entity) override;
FMOD::System *m_FmodSystem;
private:
// Events
EventRelay<Events::PlaySound> m_EPlaySound;
bool LoadSound(std::string filePath);
void PlaySound(FMOD_CHANNEL*, float volume, bool loop);
FMOD_RESULT LoadSound(FMOD_SOUND*&, std::string, float, float, bool, float, float);
void PlaySound(FMOD_CHANNEL*, FMOD_SOUND*, float volume, bool loop);
void RemoveEmitter(EntityID ent);
std::vector<EntityID> m_Listeners;
FMOD_SYSTEM* m_System;
FMOD_CHANNELGROUP* m_ChannelGruoup;
//std::map<EntityID, FMOD_CHANNEL*> m_Channels;
std::map<EntityID, FMOD_CHANNEL*> m_Channels;
std::map<EntityID, FMOD_SOUND*> m_Sounds;
FMOD::System *m_FmodSystem;
FMOD_SOUND* m_Sound; //Temp
bool m_Playing; //TEMP
};