diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 281dcd6..ab544c7 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -118,24 +118,8 @@ void GameWorld::Update(double dt) void GameWorld::RegisterComponents() { - m_ComponentFactory.Register("Input", []() { return new Components::Input(); }); - m_ComponentFactory.Register("ParticleEmitter", []() { return new Components::ParticleEmitter(); }); - m_ComponentFactory.Register("SoundEmitter", []() { return new Components::SoundEmitter(); }); - m_ComponentFactory.Register("Template", []() { return new Components::Template(); }); - m_ComponentFactory.Register("Transform", []() { return new Components::Transform(); }); - m_ComponentFactory.Register("FreeSteering", []() { return new Components::FreeSteering(); }); - - m_ComponentFactory.Register("Physics", []() { return new Components::Physics(); }); - m_ComponentFactory.Register("CompoundShape", []() { return new Components::CompoundShape(); }); - m_ComponentFactory.Register("SphereShape", []() { return new Components::SphereShape(); }); - m_ComponentFactory.Register("BoxShape", []() { return new Components::BoxShape(); }); - - m_ComponentFactory.Register("HingeConstraint", []() { return new Components::HingeConstraint(); }); - m_ComponentFactory.Register("BallSocketConstraint", []() { return new Components::BallSocketConstraint(); }); - m_ComponentFactory.Register("SliderConstraint", []() { return new Components::SliderConstraint(); }); - - m_ComponentFactory.Register("Vehicle", []() { return new Components::Vehicle(); }); - m_ComponentFactory.Register("Wheel", []() { return new Components::Wheel(); }); + m_ComponentFactory.Register("Transform", []() { return new Components::Transform(); }); + m_ComponentFactory.Register("Template", []() { return new Components::Template(); }); } void GameWorld::RegisterSystems() @@ -148,12 +132,8 @@ void GameWorld::RegisterSystems() //m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); }); m_SystemFactory.Register("FreeSteeringSystem", [this]() { return new Systems::FreeSteeringSystem(this); }); m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); }); - m_SystemFactory.Register("PhysicsSystem", [this]() { return new Systems::PhysicsSystem(this); }); - m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_Renderer); }); - - } void GameWorld::AddSystems() @@ -166,10 +146,6 @@ void GameWorld::AddSystems() //AddSystem("PlayerSystem"); AddSystem("FreeSteeringSystem"); AddSystem("SoundSystem"); - AddSystem("PhysicsSystem"); - AddSystem("RenderSystem"); - - } \ No newline at end of file diff --git a/src/Sound.cpp b/src/Sound.cpp new file mode 100644 index 0000000..98257dd --- /dev/null +++ b/src/Sound.cpp @@ -0,0 +1,96 @@ +#include "PrecompiledHeader.h" +#include "Sound.h" + +Sound::Sound(std::string path) +{ + m_Buffer = 0; + m_Buffer = LoadFile(path); +} + +ALuint Sound::LoadFile(std::string path) +{ + char type[4]; + unsigned long size, chunkSize; + short formatType, channels; + unsigned long sampleRate, avgBytesPerSec; + short bytesPerSample, bitsPerSample; + unsigned long dataSize; + + 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; + + return buffer; +} diff --git a/src/Sound.h b/src/Sound.h new file mode 100644 index 0000000..236768e --- /dev/null +++ b/src/Sound.h @@ -0,0 +1,22 @@ +#ifndef Sound_h__ +#define Sound_h__ + +#include +#include + +#include "ResourceManager.h" + +class Sound : public Resource +{ +public: + Sound(std::string path); + + ALuint LoadFile(std::string path); + + operator ALuint() const { return m_Buffer; } + +private: + ALuint m_Buffer; +}; + +#endif // Sound_h__ diff --git a/src/System.h b/src/System.h index c3de4cf..0cd3cc7 100755 --- a/src/System.h +++ b/src/System.h @@ -14,8 +14,8 @@ public: System(World* world) : m_World(world) { } virtual ~System() { } - virtual void RegisterComponents(ComponentFactory* cf) { } // TODO: Make abstract - virtual void RegisterResourceTypes(ResourceManager* rm) { } // TODO: Make abstract + virtual void RegisterComponents(ComponentFactory* cf) { } + virtual void RegisterResourceTypes(ResourceManager* rm) { } virtual void Initialize() { } diff --git a/src/Systems/FreeSteeringSystem.cpp b/src/Systems/FreeSteeringSystem.cpp index 341f01c..407200b 100755 --- a/src/Systems/FreeSteeringSystem.cpp +++ b/src/Systems/FreeSteeringSystem.cpp @@ -2,9 +2,9 @@ #include "FreeSteeringSystem.h" #include "World.h" -Systems::FreeSteeringSystem::FreeSteeringSystem(World* world) : System(world) +void Systems::FreeSteeringSystem::RegisterComponents(ComponentFactory* cf) { - + cf->Register("FreeSteering", []() { return new Components::FreeSteering(); }); } void Systems::FreeSteeringSystem::Update(double dt) @@ -68,4 +68,4 @@ void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS } } -} \ No newline at end of file +} diff --git a/src/Systems/FreeSteeringSystem.h b/src/Systems/FreeSteeringSystem.h index cf589fd..11b86ae 100755 --- a/src/Systems/FreeSteeringSystem.h +++ b/src/Systems/FreeSteeringSystem.h @@ -10,7 +10,9 @@ namespace Systems class FreeSteeringSystem : public System { public: - FreeSteeringSystem(World* world); + FreeSteeringSystem(World* world) + : System(world) { } + void RegisterComponents(ComponentFactory* cf) override; void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; diff --git a/src/Systems/InputSystem.cpp b/src/Systems/InputSystem.cpp index 6ff84ff..b64bc03 100755 --- a/src/Systems/InputSystem.cpp +++ b/src/Systems/InputSystem.cpp @@ -2,6 +2,11 @@ #include "InputSystem.h" #include "World.h" +void Systems::InputSystem::RegisterComponents(ComponentFactory* cf) +{ + cf->Register("Input", []() { return new Components::Input(); }); +} + void Systems::InputSystem::Update(double dt) { m_LastKeyState = m_CurrentKeyState; diff --git a/src/Systems/InputSystem.h b/src/Systems/InputSystem.h index 99cb01f..abceb77 100755 --- a/src/Systems/InputSystem.h +++ b/src/Systems/InputSystem.h @@ -15,6 +15,7 @@ class InputSystem : public System public: InputSystem(World* world, std::shared_ptr renderer) : System(world), m_Renderer(renderer) { } + void RegisterComponents(ComponentFactory* cf) override; void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index da32e25..cf58ea9 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -16,6 +16,22 @@ Systems::PhysicsSystem::PhysicsSystem(World* world) : System(world) } +void Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) +{ + cf->Register("Physics", []() { return new Components::Physics(); }); + + cf->Register("CompoundShape", []() { return new Components::CompoundShape(); }); + cf->Register("SphereShape", []() { return new Components::SphereShape(); }); + cf->Register("BoxShape", []() { return new Components::BoxShape(); }); + + cf->Register("HingeConstraint", []() { return new Components::HingeConstraint(); }); + cf->Register("BallSocketConstraint", []() { return new Components::BallSocketConstraint(); }); + cf->Register("SliderConstraint", []() { return new Components::SliderConstraint(); }); + + cf->Register("Vehicle", []() { return new Components::Vehicle(); }); + cf->Register("Wheel", []() { return new Components::Wheel(); }); +} + void Systems::PhysicsSystem::Update(double dt) { // Update entity transform in physics world @@ -354,4 +370,3 @@ void Systems::PhysicsSystem::TearDownPhysicsState(EntityID entity, EntityID pare m_PhysicsData.erase(entity); } - diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index b59a5c6..4697bc2 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -26,6 +26,8 @@ class PhysicsSystem : public System { public: PhysicsSystem(World* world); + void RegisterComponents(ComponentFactory* cf) override; + void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void OnComponentCreated(std::string type, std::shared_ptr component) override; diff --git a/src/Systems/SoundSystem.cpp b/src/Systems/SoundSystem.cpp index 45efa62..ffb9b06 100755 --- a/src/Systems/SoundSystem.cpp +++ b/src/Systems/SoundSystem.cpp @@ -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", 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 emitter) { - ALuint buffer = LoadFile(emitter->Path); + ALuint buffer = *m_World->GetResourceManager()->Load("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; -} \ No newline at end of file +} diff --git a/src/Systems/SoundSystem.h b/src/Systems/SoundSystem.h index a70e693..85ee4fa 100755 --- a/src/Systems/SoundSystem.h +++ b/src/Systems/SoundSystem.h @@ -1,12 +1,13 @@ #ifndef SoundEmitter_h__ #define SoundEmitter_h__ +#include +#include +#include #include "System.h" #include "Components/Transform.h" #include "Components/SoundEmitter.h" -#include -#include -#include +#include "Sound.h" namespace Systems { @@ -15,6 +16,8 @@ class SoundSystem : public System { public: SoundSystem(World* world); + void RegisterComponents(ComponentFactory* cf) override; + void RegisterResourceTypes(ResourceManager* rm) override; void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; @@ -29,13 +32,12 @@ private: ALuint CreateSource(); //File-info - char type[4]; - unsigned long size, chunkSize; - short formatType, channels; - unsigned long sampleRate, avgBytesPerSec; - short bytesPerSample, bitsPerSample; - unsigned long dataSize; - + //char type[4]; + //unsigned long size, chunkSize; + //short formatType, channels; + //unsigned long sampleRate, avgBytesPerSec; + //short bytesPerSample, bitsPerSample; + //unsigned long dataSize; std::map m_Sources; std::map m_BufferCache; // string = fileName diff --git a/vs12/Returngeance/Returngeance.vcxproj b/vs12/Returngeance/Returngeance.vcxproj index 6d4e3b3..cea16eb 100755 --- a/vs12/Returngeance/Returngeance.vcxproj +++ b/vs12/Returngeance/Returngeance.vcxproj @@ -100,6 +100,7 @@ + @@ -148,6 +149,7 @@ + diff --git a/vs12/Returngeance/Returngeance.vcxproj.filters b/vs12/Returngeance/Returngeance.vcxproj.filters index 35dc81f..26bcf35 100755 --- a/vs12/Returngeance/Returngeance.vcxproj.filters +++ b/vs12/Returngeance/Returngeance.vcxproj.filters @@ -49,6 +49,7 @@ Input\Systems + @@ -238,6 +239,9 @@ Rendering\Components + + Audio +