Refactored existing systems to register their own components and resource types.
This commit is contained in:
+2
-26
@@ -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");
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
#ifndef Sound_h__
|
||||
#define Sound_h__
|
||||
|
||||
#include <AL/al.h>
|
||||
#include <AL/alc.h>
|
||||
|
||||
#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__
|
||||
+2
-2
@@ -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() { }
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -15,6 +15,7 @@ class InputSystem : public System
|
||||
public:
|
||||
InputSystem(World* world, std::shared_ptr<Renderer> 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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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> component) override;
|
||||
|
||||
+13
-88
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+12
-10
@@ -1,12 +1,13 @@
|
||||
#ifndef SoundEmitter_h__
|
||||
#define SoundEmitter_h__
|
||||
#include <AL/al.h>
|
||||
#include <AL/alc.h>
|
||||
#include <vector>
|
||||
|
||||
#include "System.h"
|
||||
#include "Components/Transform.h"
|
||||
#include "Components/SoundEmitter.h"
|
||||
#include <AL/al.h>
|
||||
#include <AL/alc.h>
|
||||
#include <vector>
|
||||
#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<Component*, ALuint> m_Sources;
|
||||
std::map<std::string, ALuint> m_BufferCache; // string = fileName
|
||||
|
||||
@@ -100,6 +100,7 @@
|
||||
<ClCompile Include="..\..\src\ResourceManager.cpp" />
|
||||
<ClCompile Include="..\..\src\ShaderProgram.cpp" />
|
||||
<ClCompile Include="..\..\src\Skybox.cpp" />
|
||||
<ClCompile Include="..\..\src\Sound.cpp" />
|
||||
<ClCompile Include="..\..\src\Systems\DebugSystem.cpp" />
|
||||
<ClCompile Include="..\..\src\Systems\FreeSteeringSystem.cpp" />
|
||||
<ClCompile Include="..\..\src\Systems\InputSystem.cpp" />
|
||||
@@ -148,6 +149,7 @@
|
||||
<ClInclude Include="..\..\src\ResourceManager.h" />
|
||||
<ClInclude Include="..\..\src\ShaderProgram.h" />
|
||||
<ClInclude Include="..\..\src\Skybox.h" />
|
||||
<ClInclude Include="..\..\src\Sound.h" />
|
||||
<ClInclude Include="..\..\src\System.h" />
|
||||
<ClInclude Include="..\..\src\Systems\DebugSystem.h" />
|
||||
<ClInclude Include="..\..\src\Systems\FreeSteeringSystem.h" />
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
<Filter>Input\Systems</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\ResourceManager.cpp" />
|
||||
<ClCompile Include="..\..\src\Sound.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Filter Include="Util">
|
||||
@@ -238,6 +239,9 @@
|
||||
<Filter>Rendering\Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ResourceManager.h" />
|
||||
<ClInclude Include="..\..\src\Sound.h">
|
||||
<Filter>Audio</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\src\Shaders\AABB.frag.glsl">
|
||||
|
||||
Reference in New Issue
Block a user