Revert "Indentation style changed to Horstmann"

This reverts commit f2259f712d.

Conflicts:

	src/GameWorld.cpp
	src/Systems/PhysicsSystem.cpp
This commit is contained in:
2014-04-12 01:48:15 +02:00
parent d7e65ff118
commit 3470a80f40
61 changed files with 697 additions and 354 deletions
+22 -11
View File
@@ -13,42 +13,53 @@ void Systems::FreeSteeringSystem::Update(double dt)
}
void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{ auto steering = m_World->GetComponent<Components::FreeSteering>(entity, "FreeSteering");
{
auto steering = m_World->GetComponent<Components::FreeSteering>(entity, "FreeSteering");
auto input = m_World->GetComponent<Components::Input>(entity, "Input");
if (steering && input)
{ auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
{
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
glm::vec3 Camera_Right = glm::vec3(glm::vec4(1, 0, 0, 0) * transform->Orientation);
glm::vec3 Camera_Forward = glm::vec3(glm::vec4(0, 0, 1, 0) * transform->Orientation);
float speed = steering->Speed;
if (input->KeyState[GLFW_KEY_LEFT_SHIFT])
{ speed *= 4.0f;
{
speed *= 4.0f;
}
if (input->KeyState[GLFW_KEY_LEFT_ALT])
{ speed /= 4.0f;
{
speed /= 4.0f;
}
if (input->KeyState[GLFW_KEY_A])
{ transform->Position -= Camera_Right * (float)dt * speed;
{
transform->Position -= Camera_Right * (float)dt * speed;
}
else if (input->KeyState[GLFW_KEY_D])
{ transform->Position += Camera_Right * (float)dt * speed;
{
transform->Position += Camera_Right * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_W])
{ transform->Position -= Camera_Forward * (float)dt * speed;
{
transform->Position -= Camera_Forward * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_S])
{ transform->Position += Camera_Forward * (float)dt * speed;
{
transform->Position += Camera_Forward * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_SPACE])
{ transform->Position += glm::vec3(0, 1, 0) * (float)dt * speed;
{
transform->Position += glm::vec3(0, 1, 0) * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_LEFT_CONTROL])
{ transform->Position -= glm::vec3(0, 1, 0) * (float)dt * speed;
{
transform->Position -= glm::vec3(0, 1, 0) * (float)dt * speed;
}
if (input->MouseState[GLFW_MOUSE_BUTTON_LEFT])
{ // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS // spelling tobias :3
{
// TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS // spelling tobias :3
//---------------------------------------------------------------------
transform->Orientation = glm::angleAxis<float>(input->dY / 300.f, glm::vec3(1, 0, 0)) * transform->Orientation;
+20 -10
View File
@@ -3,17 +3,20 @@
#include "World.h"
void Systems::InputSystem::Update(double dt)
{ m_LastKeyState = m_CurrentKeyState;
{
m_LastKeyState = m_CurrentKeyState;
m_LastMouseState = m_CurrentMouseState;
// Keyboard input
for (int i = 0; i <= GLFW_KEY_LAST; ++i)
{ m_CurrentKeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i);
{
m_CurrentKeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i);
}
// Mouse buttons
for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i)
{ m_CurrentMouseState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i);
{
m_CurrentMouseState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i);
}
// Cursor position
@@ -26,36 +29,43 @@ void Systems::InputSystem::Update(double dt)
// Lock mouse while holding LMB
if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT])
{ m_LastMouseX = m_Renderer->WIDTH / 2.f; // xpos;
{
m_LastMouseX = m_Renderer->WIDTH / 2.f; // xpos;
m_LastMouseY = m_Renderer->HEIGHT / 2.f; // ypos;
glfwSetCursorPos(m_Renderer->GetWindow(), m_LastMouseX, m_LastMouseY);
}
// Hide/show cursor with LMB
if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && !m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT])
{ glfwSetInputMode(m_Renderer->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
{
glfwSetInputMode(m_Renderer->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
if (!m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT])
{ glfwSetInputMode(m_Renderer->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
{
glfwSetInputMode(m_Renderer->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
#ifdef DEBUG
// Wireframe
if (m_CurrentKeyState[GLFW_KEY_F1] && !m_LastKeyState[GLFW_KEY_F1])
{ m_Renderer->DrawWireframe(!m_Renderer->DrawWireframe());
{
m_Renderer->DrawWireframe(!m_Renderer->DrawWireframe());
}
// Normals
if (m_CurrentKeyState[GLFW_KEY_F2] && !m_LastKeyState[GLFW_KEY_F2])
{ m_Renderer->DrawNormals(!m_Renderer->DrawNormals());
{
m_Renderer->DrawNormals(!m_Renderer->DrawNormals());
}
// Bounds
if (m_CurrentKeyState[GLFW_KEY_F3] && !m_LastKeyState[GLFW_KEY_F3])
{ m_Renderer->DrawBounds(!m_Renderer->DrawBounds());
{
m_Renderer->DrawBounds(!m_Renderer->DrawBounds());
}
#endif
}
void Systems::InputSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{ auto input = m_World->GetComponent<Components::Input>(entity, "Input");
{
auto input = m_World->GetComponent<Components::Input>(entity, "Input");
if (input == nullptr)
return;
+66 -33
View File
@@ -5,7 +5,8 @@
Systems::PhysicsSystem::PhysicsSystem(World* world) : System(world)
{ m_Broadphase = new btDbvtBroadphase();
{
m_Broadphase = new btDbvtBroadphase();
m_CollisionConfiguration = new btDefaultCollisionConfiguration();
m_Dispatcher = new btCollisionDispatcher(m_CollisionConfiguration);
m_Solver = new btSequentialImpulseConstraintSolver();
@@ -16,16 +17,19 @@ Systems::PhysicsSystem::PhysicsSystem(World* world) : System(world)
}
void Systems::PhysicsSystem::Update(double dt)
{ // Update entity transform in physics world
{
// Update entity transform in physics world
for (auto pair : *m_World->GetEntities())
{ EntityID entity = pair.first;
{
EntityID entity = pair.first;
EntityID parent = pair.second;
if (parent != 0)
continue;
if (m_PhysicsData.find(entity) != m_PhysicsData.end())
{ PhysicsData* physicsData = &m_PhysicsData[entity];
{
PhysicsData* physicsData = &m_PhysicsData[entity];
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
btTransform transform;
@@ -47,7 +51,8 @@ void Systems::PhysicsSystem::Update(double dt)
}
void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{ auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
if (!transformComponent)
return;
@@ -60,8 +65,10 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p
auto wheelComponent = m_World->GetComponent<Components::Wheel>(entity, "Wheel");
if (physicsComponent || sphereShapeComponent || boxShapeComponent || meshShapeComponent || staticMeshShapeComponent)
{ if (m_PhysicsData.find(entity) == m_PhysicsData.end())
{ SetUpPhysicsState(entity, parent);
{
if (m_PhysicsData.find(entity) == m_PhysicsData.end())
{
SetUpPhysicsState(entity, parent);
}
if (parent != 0)
@@ -80,8 +87,10 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p
transformComponent->Orientation.w = transform.getRotation().w();
}
else
{ if (m_PhysicsData.find(entity) != m_PhysicsData.end())
{ TearDownPhysicsState(entity, parent);
{
if (m_PhysicsData.find(entity) != m_PhysicsData.end())
{
TearDownPhysicsState(entity, parent);
}
}
@@ -90,12 +99,15 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p
auto hingeComponent = m_World->GetComponent<Components::HingeConstraint>(entity, "HingeConstraint");
if (ballSocketComponent)
{ EntityID entityA = ballSocketComponent->EntityA;
{
EntityID entityA = ballSocketComponent->EntityA;
EntityID entityB = ballSocketComponent->EntityB;
if (m_Constraints.find(std::make_pair(entityA, entityB)) == m_Constraints.end())
{ if (m_PhysicsData.find(entityA) != m_PhysicsData.end() && m_PhysicsData.find(entityB) != m_PhysicsData.end())
{ btVector3 pivotA = btVector3(ballSocketComponent->PivotA.x, ballSocketComponent->PivotA.y, ballSocketComponent->PivotA.z);
{
if (m_PhysicsData.find(entityA) != m_PhysicsData.end() && m_PhysicsData.find(entityB) != m_PhysicsData.end())
{
btVector3 pivotA = btVector3(ballSocketComponent->PivotA.x, ballSocketComponent->PivotA.y, ballSocketComponent->PivotA.z);
btVector3 pivotB = btVector3(ballSocketComponent->PivotB.x, ballSocketComponent->PivotB.y, ballSocketComponent->PivotB.z);
m_Constraints[std::make_pair(entityA, entityB)] = new btPoint2PointConstraint(*m_PhysicsData[entityA].RigidBody, *m_PhysicsData[entityB].RigidBody, pivotA, pivotB);
@@ -105,12 +117,15 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p
}
}
else if (sliderComponent)
{ EntityID entityA = sliderComponent->EntityA;
{
EntityID entityA = sliderComponent->EntityA;
EntityID entityB = sliderComponent->EntityB;
if (m_Constraints.find(std::make_pair(entityA, entityB)) == m_Constraints.end())
{ if (m_PhysicsData.find(entityA) != m_PhysicsData.end() && m_PhysicsData.find(entityB) != m_PhysicsData.end())
{ btTransform transformA;
{
if (m_PhysicsData.find(entityA) != m_PhysicsData.end() && m_PhysicsData.find(entityB) != m_PhysicsData.end())
{
btTransform transformA;
m_PhysicsData[entityA].MotionState->getWorldTransform(transformA);
btTransform transformB;
m_PhysicsData[entityB].MotionState->getWorldTransform(transformB);
@@ -121,12 +136,15 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p
}
}
else if (hingeComponent)
{ EntityID entityA = hingeComponent->EntityA;
{
EntityID entityA = hingeComponent->EntityA;
EntityID entityB = hingeComponent->EntityB;
if (m_Constraints.find(std::make_pair(entityA, entityB)) == m_Constraints.end())
{ if (m_PhysicsData.find(entityA) != m_PhysicsData.end() && m_PhysicsData.find(entityB) != m_PhysicsData.end())
{ btVector3 PivotA = btVector3(hingeComponent->PivotA.x, hingeComponent->PivotA.y, hingeComponent->PivotA.z);
{
if (m_PhysicsData.find(entityA) != m_PhysicsData.end() && m_PhysicsData.find(entityB) != m_PhysicsData.end())
{
btVector3 PivotA = btVector3(hingeComponent->PivotA.x, hingeComponent->PivotA.y, hingeComponent->PivotA.z);
btVector3 PivotB = btVector3(hingeComponent->PivotB.x, hingeComponent->PivotB.y, hingeComponent->PivotB.z);
btVector3 AxisA = btVector3(hingeComponent->AxisA.x, hingeComponent->AxisA.y, hingeComponent->AxisA.z);
@@ -214,9 +232,11 @@ void Systems::PhysicsSystem::OnComponentRemoved(std::string type, Component* com
}
void Systems::PhysicsSystem::SetUpPhysicsState(EntityID entity, EntityID parent)
{ auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
if (!transformComponent)
{ LOG_WARNING("Physics component missing transform component on entity %i", entity);
{
LOG_WARNING("Physics component missing transform component on entity %i", entity);
return;
}
@@ -228,7 +248,8 @@ void Systems::PhysicsSystem::SetUpPhysicsState(EntityID entity, EntityID parent)
auto staticMeshShapeComponent = m_World->GetComponent<Components::StaticMeshShape>(entity, "StaticMeshShape");
if (compoundShapeComponent && (sphereShapeComponent || boxShapeComponent || meshShapeComponent || staticMeshShapeComponent))
{ LOG_WARNING("Entity %i has both compound shape and normal shape! Normal shapes must be children to entity with compound shape.", entity);
{
LOG_WARNING("Entity %i has both compound shape and normal shape! Normal shapes must be children to entity with compound shape.", entity);
}
PhysicsData* physicsData = &m_PhysicsData[entity];
@@ -238,7 +259,8 @@ void Systems::PhysicsSystem::SetUpPhysicsState(EntityID entity, EntityID parent)
// Set-up compound shape
if (compoundShapeComponent)
{ btCompoundShape* compoundShape = new btCompoundShape();
{
btCompoundShape* compoundShape = new btCompoundShape();
physicsData->CollisionShape = compoundShape;
btTransform transform;
@@ -247,7 +269,8 @@ void Systems::PhysicsSystem::SetUpPhysicsState(EntityID entity, EntityID parent)
btVector3 inertia;
if (physicsComponent->Mass != 0)
{ physicsData->CollisionShape->calculateLocalInertia(physicsComponent->Mass, inertia);
{
physicsData->CollisionShape->calculateLocalInertia(physicsComponent->Mass, inertia);
}
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(physicsComponent->Mass, physicsData->MotionState, physicsData->CollisionShape, inertia);
@@ -258,26 +281,32 @@ void Systems::PhysicsSystem::SetUpPhysicsState(EntityID entity, EntityID parent)
// Set-up normal shapes
else if (boxShapeComponent)
{ physicsData->CollisionShape = new btBoxShape(btVector3(boxShapeComponent->Width, boxShapeComponent->Height, boxShapeComponent->Depth));
{
physicsData->CollisionShape = new btBoxShape(btVector3(boxShapeComponent->Width, boxShapeComponent->Height, boxShapeComponent->Depth));
}
else if (sphereShapeComponent)
{ physicsData->CollisionShape = new btSphereShape(sphereShapeComponent->Radius);
{
physicsData->CollisionShape = new btSphereShape(sphereShapeComponent->Radius);
}
else if (meshShapeComponent)
{ // TODO: Collision mesh things go here
{
// TODO: Collision mesh things go here
//new btConvexTriangleMeshShape()
}
if (boxShapeComponent || sphereShapeComponent || meshShapeComponent || staticMeshShapeComponent)
{ btTransform transform;
{
btTransform transform;
transform.setFromOpenGLMatrix(glm::value_ptr(glm::translate(glm::mat4(), transformComponent->Position) * glm::toMat4(transformComponent->Orientation)));
// If there's a local physics component
if (physicsComponent)
{ physicsData->MotionState = new btDefaultMotionState(transform);
{
physicsData->MotionState = new btDefaultMotionState(transform);
btVector3 inertia;
if (physicsComponent->Mass != 0)
{ physicsData->CollisionShape->calculateLocalInertia(physicsComponent->Mass, inertia);
{
physicsData->CollisionShape->calculateLocalInertia(physicsComponent->Mass, inertia);
}
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(physicsComponent->Mass, physicsData->MotionState, physicsData->CollisionShape, inertia);
@@ -286,16 +315,19 @@ void Systems::PhysicsSystem::SetUpPhysicsState(EntityID entity, EntityID parent)
m_DynamicsWorld->addRigidBody(physicsData->RigidBody);
}
else
{ // Otherwise, find our base parent and attach to compound shape
{
// Otherwise, find our base parent and attach to compound shape
EntityID baseParent = m_World->GetEntityBaseParent(entity);
auto basePhysicsComponent = m_World->GetComponent<Components::Physics>(baseParent, "Physics");
if (!basePhysicsComponent)
{ LOG_WARNING("Failed to attach orphan collision shape on entity %i: missing physics component on base parent entity %i", entity, baseParent);
{
LOG_WARNING("Failed to attach orphan collision shape on entity %i: missing physics component on base parent entity %i", entity, baseParent);
return;
}
auto baseCompoundShapeComponent = m_World->GetComponent<Components::CompoundShape>(baseParent, "CompoundShape");
if (!baseCompoundShapeComponent)
{ LOG_WARNING("Failed to attach orphan collision shape on entity %i: missing compound shape on base parent entity %i", entity, baseParent);
{
LOG_WARNING("Failed to attach orphan collision shape on entity %i: missing compound shape on base parent entity %i", entity, baseParent);
return;
}
PhysicsData* basePhysicsData = &m_PhysicsData.at(baseParent);
@@ -313,7 +345,8 @@ void Systems::PhysicsSystem::SetUpPhysicsState(EntityID entity, EntityID parent)
}
void Systems::PhysicsSystem::TearDownPhysicsState(EntityID entity, EntityID parent)
{ PhysicsData* physicsData = &m_PhysicsData[entity];
{
PhysicsData* physicsData = &m_PhysicsData[entity];
delete physicsData->RigidBody;
delete physicsData->MotionState;
+2 -1
View File
@@ -43,7 +43,8 @@ private:
struct PhysicsData
{ btRigidBody* RigidBody;
{
btRigidBody* RigidBody;
btMotionState* MotionState;
btCollisionShape* CollisionShape;
};
+18 -9
View File
@@ -3,21 +3,26 @@
#include "World.h"
void Systems::RenderSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
{ if(type == "Model")
{ auto modelComponent = std::static_pointer_cast<Components::Model>(component);
{
if(type == "Model")
{
auto modelComponent = std::static_pointer_cast<Components::Model>(component);
}
}
void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{ auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
if (transformComponent == nullptr)
return;
// Draw models
auto modelComponent = m_World->GetComponent<Components::Model>(entity, "Model");
if (modelComponent != nullptr)
{ if (m_CachedModels.find(modelComponent->ModelFile) == m_CachedModels.end())
{ m_CachedModels[modelComponent->ModelFile] = std::make_shared<Model>(OBJ(modelComponent->ModelFile));
{
if (m_CachedModels.find(modelComponent->ModelFile) == m_CachedModels.end())
{
m_CachedModels[modelComponent->ModelFile] = std::make_shared<Model>(OBJ(modelComponent->ModelFile));
}
auto model = m_CachedModels[modelComponent->ModelFile];
@@ -32,7 +37,8 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
auto collision = m_World->GetComponent<Components::Collision>(entity, "Collision");
auto bounds = m_World->GetComponent<Components::Bounds>(entity, "Bounds");
if (bounds != nullptr)
{ glm::vec3 origin = m_TransformSystem->AbsolutePosition(entity) + (transformComponent->Scale * bounds->Origin);
{
glm::vec3 origin = m_TransformSystem->AbsolutePosition(entity) + (transformComponent->Scale * bounds->Origin);
glm::vec3 volumeVector = transformComponent->Scale * bounds->VolumeVector;
m_Renderer->AddAABBToDraw(origin, volumeVector, (collision != nullptr && collision->CollidingEntities.size() > 0));
}
@@ -40,7 +46,8 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
auto pointLightComponent = m_World->GetComponent<Components::PointLight>(entity, "PointLight");
if (pointLightComponent != nullptr)
{ glm::vec3 position = m_TransformSystem->AbsolutePosition(entity);
{
glm::vec3 position = m_TransformSystem->AbsolutePosition(entity);
m_Renderer->AddPointLightToDraw(
position,
pointLightComponent->Specular,
@@ -53,7 +60,8 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
auto cameraComponent = m_World->GetComponent<Components::Camera>(entity, "Camera");
if (cameraComponent != nullptr)
{ m_Renderer->GetCamera()->Position(transformComponent->Position);
{
m_Renderer->GetCamera()->Position(transformComponent->Position);
m_Renderer->GetCamera()->Orientation(transformComponent->Orientation);
m_Renderer->GetCamera()->FOV(cameraComponent->FOV);
@@ -63,7 +71,8 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
}
void Systems::RenderSystem::Initialize()
{ m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>("TransformSystem");
{
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>("TransformSystem");
}
+46 -23
View File
@@ -4,15 +4,18 @@
Systems::SoundSystem::SoundSystem(World* world)
: System(world)
{ //initialize OpenAL
{
//initialize OpenAL
ALCdevice* Device = alcOpenDevice(NULL);
ALCcontext* context;
if(Device)
{ context = alcCreateContext(Device, NULL);
{
context = alcCreateContext(Device, NULL);
alcMakeContextCurrent(context);
}
else
{ LOG_ERROR("OMG OPEN AL FAIL");
{
LOG_ERROR("OMG OPEN AL FAIL");
}
alGetError();
@@ -27,13 +30,15 @@ void Systems::SoundSystem::Update(double dt)
}
void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{ auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
if (transformComponent == nullptr)
return;
auto entityName = m_World->GetProperty<std::string>(entity, "Name");
if (entityName == "Camera")
{ glm::vec3 playerPos = transformComponent->Position;
{
glm::vec3 playerPos = transformComponent->Position;
ALfloat listenerPos[3] = { playerPos.x, playerPos.y, -playerPos.z };
glm::vec3 playerVel = transformComponent->Velocity;
@@ -52,7 +57,8 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
auto soundEmitter = m_World->GetComponent<Components::SoundEmitter>(entity, "SoundEmitter");
if(soundEmitter != nullptr)
{ ALuint source = m_Sources[soundEmitter];
{
ALuint source = m_Sources[soundEmitter];
alSourcef(source, AL_GAIN, soundEmitter->Gain);
//alSourcef(source, AL_MAX_DISTANCE, soundEmitter->MaxDistance);
alSourcef(source, AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance);
@@ -71,7 +77,8 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
}
void Systems::SoundSystem::PlaySound(Components::SoundEmitter* emitter, std::string fileName)
{ if (m_Sources.find(emitter) == m_Sources.end())
{
if (m_Sources.find(emitter) == m_Sources.end())
return;
ALuint buffer = LoadFile(fileName);
@@ -83,61 +90,73 @@ 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 = LoadFile(emitter->Path);
ALuint source = m_Sources[emitter.get()];
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(m_Sources[emitter.get()]);
}
void Systems::SoundSystem::StopSound(std::shared_ptr<Components::SoundEmitter> emitter)
{ alSourceStop(m_Sources[emitter.get()]);
{
alSourceStop(m_Sources[emitter.get()]);
}
void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
{ if(type == "SoundEmitter")
{ ALuint source = CreateSource();
{
if(type == "SoundEmitter")
{
ALuint source = CreateSource();
m_Sources[component.get()] = source;
}
}
void Systems::SoundSystem::OnComponentRemoved(std::string type, Component* component)
{ if(type == "SoundEmitter")
{ if (m_Sources.find(component) != m_Sources.end())
{ ALuint source = m_Sources[component];
{
if(type == "SoundEmitter")
{
if (m_Sources.find(component) != m_Sources.end())
{
ALuint source = m_Sources[component];
alDeleteSources(1, &source);
}
}
}
ALuint Systems::SoundSystem::LoadFile(std::string path)
{ if (m_BufferCache.find(path) != m_BufferCache.end())
{
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());
{
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");
{
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");
{
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");
{
LOG_ERROR("ERROR: No fmt in WAVE-file");
return 0;
}
@@ -152,7 +171,8 @@ ALuint Systems::SoundSystem::LoadFile(std::string path)
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");
{
LOG_ERROR("ERROR: WAVE-file Missing data");
return 0;
}
@@ -165,13 +185,15 @@ ALuint Systems::SoundSystem::LoadFile(std::string path)
// Create buffer
ALuint format = 0;
if(bitsPerSample == 8)
{ if(channels == 1)
{
if(channels == 1)
format = AL_FORMAT_MONO8;
else if(channels == 2)
format = AL_FORMAT_STEREO8;
}
if(bitsPerSample == 16)
{ if (channels == 1)
{
if (channels == 1)
format = AL_FORMAT_MONO16;
else if (channels == 2)
format = AL_FORMAT_STEREO16;
@@ -187,7 +209,8 @@ ALuint Systems::SoundSystem::LoadFile(std::string path)
}
ALuint Systems::SoundSystem::CreateSource()
{ ALuint source;
{
ALuint source;
alGenSources((ALuint)1, &source);
alDopplerFactor(1); // Numbers greater than 1 will increase Doppler effect, numbers lower than 1 will decrease the Doppler effect
+15 -12
View File
@@ -14,11 +14,13 @@
//}
glm::vec3 Systems::TransformSystem::AbsolutePosition(EntityID entity)
{ glm::vec3 absPosition;
{
glm::vec3 absPosition;
glm::quat accumulativeOrientation;
do
{ auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
{
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
//absPosition += transform->Position;
entity = m_World->GetEntityParent(entity);
auto transform2 = m_World->GetComponent<Components::Transform>(entity, "Transform");
@@ -26,34 +28,35 @@ glm::vec3 Systems::TransformSystem::AbsolutePosition(EntityID entity)
absPosition += transform2->Orientation * transform->Position;
else
absPosition += transform->Position;
}
while (entity != 0);
} while (entity != 0);
return absPosition * accumulativeOrientation;
}
glm::quat Systems::TransformSystem::AbsoluteOrientation(EntityID entity)
{ glm::quat absOrientation;
{
glm::quat absOrientation;
do
{ auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
{
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
absOrientation *= transform->Orientation;
entity = m_World->GetEntityParent(entity);
}
while (entity != 0);
} while (entity != 0);
return absOrientation;
}
glm::vec3 Systems::TransformSystem::AbsoluteScale(EntityID entity)
{ glm::vec3 absScale(1);
{
glm::vec3 absScale(1);
do
{ auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
{
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
absScale *= transform->Scale;
entity = m_World->GetEntityParent(entity);
}
while (entity != 0);
} while (entity != 0);
return absScale;
}