From b34d2d3dd0c0b1219c259d86a4d4da90819bb090 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 13 Mar 2014 14:01:15 +0100 Subject: [PATCH] Optimizations --- Escape-the-Dawn.sln | 3 +++ Escape-the-Dawn/Components/Collision.h | 3 +++ Escape-the-Dawn/Escape-the-Dawn.vcxproj | 2 +- Escape-the-Dawn/GameWorld.cpp | 1 + Escape-the-Dawn/Renderer.cpp | 24 ++++++++++--------- Escape-the-Dawn/Renderer.h | 12 ++-------- Escape-the-Dawn/Systems/CollisionSystem.cpp | 7 +++++- .../Systems/LevelGenerationSystem.cpp | 6 +---- Escape-the-Dawn/Systems/SoundsSystem.cpp | 2 +- Escape-the-Dawn/World.h | 6 ++--- 10 files changed, 34 insertions(+), 32 deletions(-) diff --git a/Escape-the-Dawn.sln b/Escape-the-Dawn.sln index a932c57..ea79299 100644 --- a/Escape-the-Dawn.sln +++ b/Escape-the-Dawn.sln @@ -17,4 +17,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(Performance) = preSolution + HasPerformanceSessions = true + EndGlobalSection EndGlobal diff --git a/Escape-the-Dawn/Components/Collision.h b/Escape-the-Dawn/Components/Collision.h index b9f44a5..83450a4 100644 --- a/Escape-the-Dawn/Components/Collision.h +++ b/Escape-the-Dawn/Components/Collision.h @@ -10,7 +10,10 @@ namespace Components struct Collision : Component { + Collision() : Phantom(false), Interested(false) { } + bool Phantom; + bool Interested; std::vector CollidingEntities; }; diff --git a/Escape-the-Dawn/Escape-the-Dawn.vcxproj b/Escape-the-Dawn/Escape-the-Dawn.vcxproj index cb14255..861bdb7 100644 --- a/Escape-the-Dawn/Escape-the-Dawn.vcxproj +++ b/Escape-the-Dawn/Escape-the-Dawn.vcxproj @@ -54,6 +54,7 @@ $(SolutionDir)bin\ $(SolutionDir)obj\$(ProjectName)\ $(ProjectName)-$(Configuration) + true @@ -87,7 +88,6 @@ None - false true true OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32s.lib;glfw3.lib;%(AdditionalDependencies) diff --git a/Escape-the-Dawn/GameWorld.cpp b/Escape-the-Dawn/GameWorld.cpp index 33aa55f..2e32d6f 100644 --- a/Escape-the-Dawn/GameWorld.cpp +++ b/Escape-the-Dawn/GameWorld.cpp @@ -100,6 +100,7 @@ void GameWorld::Initialize() AddComponent(m_Player, "Input"); collision = AddComponent(m_Player, "Collision"); collision->Phantom = false; + collision->Interested = true; bounds = AddComponent(m_Player, "Bounds"); bounds->Origin = glm::vec3(transform->Position.x, transform->Position.y - 4, transform->Position.z + 3); bounds->VolumeVector = glm::vec3(4.f,0.7f,4); diff --git a/Escape-the-Dawn/Renderer.cpp b/Escape-the-Dawn/Renderer.cpp index 3b9ff21..1c68d81 100644 --- a/Escape-the-Dawn/Renderer.cpp +++ b/Escape-the-Dawn/Renderer.cpp @@ -233,12 +233,13 @@ void Renderer::DrawShadowMap() m_ShaderProgramShadows.Bind(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - for (int i = 0; i < ModelsToRender.size(); i++) + for (auto tuple : ModelsToRender) { - ModelData* modelData = ModelsToRender.at(i); - auto model = modelData->model; + Model* model; + glm::mat4 modelMatrix; + std::tie(model, modelMatrix) = tuple; - MVP = depthCamera * modelData->ModelMatrix; + MVP = depthCamera * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramShadows.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); glBindVertexArray(model->VAO); @@ -268,14 +269,15 @@ void Renderer::DrawModels(ShaderProgram &shader) glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); glm::mat4 MVP; - for (int i = 0; i < ModelsToRender.size(); i++) + for (auto tuple : ModelsToRender) { - ModelData* modelData = ModelsToRender.at(i); - auto model = modelData->model; + Model* model; + glm::mat4 modelMatrix; + std::tie(model, modelMatrix) = tuple; - MVP = cameraMatrix * modelData->ModelMatrix; + MVP = cameraMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelData->ModelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); glBindVertexArray(model->VAO); for (auto texGroup : model->TextureGroups) { @@ -298,9 +300,9 @@ void Renderer::AddTextToDraw() void Renderer::AddModelToDraw(std::shared_ptr model, glm::vec3 position, glm::quat orientation, glm::vec3 scale) { - glm::mat4 ModelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); + glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); // You can now use ModelMatrix to build the MVP matrix - ModelsToRender.push_back(new ModelData(model, ModelMatrix)); + ModelsToRender.push_back(std::make_tuple(model.get(), modelMatrix)); } void Renderer::AddPointLightToDraw( diff --git a/Escape-the-Dawn/Renderer.h b/Escape-the-Dawn/Renderer.h index ca15901..b113017 100644 --- a/Escape-the-Dawn/Renderer.h +++ b/Escape-the-Dawn/Renderer.h @@ -30,15 +30,7 @@ public: int HEIGHT, WIDTH; - struct ModelData - { - std::shared_ptr model; - glm::mat4 ModelMatrix; - ModelData(std::shared_ptr _model, glm::mat4 _modelMatrix) - : model(_model), ModelMatrix(_modelMatrix) {} - }; - - std::vector ModelsToRender; + std::list> ModelsToRender; int Lights; std::vector Light_position; std::vector Light_specular; @@ -47,7 +39,7 @@ public: std::vector Light_linearAttenuation; std::vector Light_quadraticAttenuation; std::vector Light_spotExponent; - std::vector> AABBsToRender; + std::list> AABBsToRender; Renderer(); diff --git a/Escape-the-Dawn/Systems/CollisionSystem.cpp b/Escape-the-Dawn/Systems/CollisionSystem.cpp index 097f05f..ab91656 100644 --- a/Escape-the-Dawn/Systems/CollisionSystem.cpp +++ b/Escape-the-Dawn/Systems/CollisionSystem.cpp @@ -20,6 +20,10 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID // Clear old collisions collisionComponent->CollidingEntities.clear(); + // Quit out if we're not interested in collision events + if (!collisionComponent->Interested) + return; + auto entities = m_World->GetEntities(); for (auto pair : *entities) { @@ -43,7 +47,8 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID //pair.first, pair.second; if(entity == entity2) - break; + continue; + Intersects(entity, entity2); } } diff --git a/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp b/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp index 397752d..b2cd7f3 100644 --- a/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp +++ b/Escape-the-Dawn/Systems/LevelGenerationSystem.cpp @@ -9,9 +9,6 @@ Systems::LevelGenerationSystem::LevelGenerationSystem( World* world ) void Systems::LevelGenerationSystem::SpawnObstacle() { - - - typeRandom = 0 + (rand() % 3); @@ -75,7 +72,7 @@ void Systems::LevelGenerationSystem::Update( double dt ) elapsedtime = 0; } - std::vector removethis; + std::list removethis; for(auto ent : obstacles) { @@ -85,7 +82,6 @@ void Systems::LevelGenerationSystem::Update( double dt ) if(transformComponent->Position.z > 100) { removethis.push_back(ent); - } } diff --git a/Escape-the-Dawn/Systems/SoundsSystem.cpp b/Escape-the-Dawn/Systems/SoundsSystem.cpp index 88789c7..6ab5d0a 100644 --- a/Escape-the-Dawn/Systems/SoundsSystem.cpp +++ b/Escape-the-Dawn/Systems/SoundsSystem.cpp @@ -54,7 +54,7 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par auto soundEmitter = m_World->GetComponent(entity, "SoundEmitter"); if(soundEmitter != nullptr) { - ALuint source = m_Source[soundEmitter.get()]; + ALuint source = m_Source[soundEmitter]; alSourcei(source, AL_GAIN, soundEmitter->Gain); alSourcei(source, AL_MAX_DISTANCE, soundEmitter->MaxDistance); alSourcei(source, AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance); diff --git a/Escape-the-Dawn/World.h b/Escape-the-Dawn/World.h index ce8ca37..add6ae3 100644 --- a/Escape-the-Dawn/World.h +++ b/Escape-the-Dawn/World.h @@ -60,7 +60,7 @@ public: std::shared_ptr AddComponent(EntityID entity, std::string componentType); std::shared_ptr AddComponent(EntityID entity, std::string componentType); template - std::shared_ptr GetComponent(EntityID entity, std::string componentType); + T* GetComponent(EntityID entity, std::string componentType); /*std::vector GetEntityChildren(EntityID entity);*/ @@ -117,9 +117,9 @@ std::shared_ptr World::AddComponent(EntityID entity, std::string componentTyp template -std::shared_ptr World::GetComponent(EntityID entity, std::string componentType) +T* World::GetComponent(EntityID entity, std::string componentType) { - return std::static_pointer_cast(m_EntityComponents[entity][componentType]); + return (T*)m_EntityComponents[entity][componentType].get(); } #endif // World_h__ \ No newline at end of file