Merge remote-tracking branch 'origin/master' into Physics

Conflicts:
	include/Core/Engine.h
	src/game/Physics/PhysicsSystem.cpp
This commit is contained in:
viktorljung
2015-09-14 15:37:38 +01:00
53 changed files with 807 additions and 92 deletions
+170 -35
View File
@@ -30,16 +30,22 @@
//TODO: Remove includes that are only here for the temporary draw solution.
#include "World.h"
#include "CTransform.h"
#include "Core/EventBroker.h"
#include "Rendering/CModel.h"
#include "Rendering/CSprite.h"
#include "CTemplate.h"
#include "Rendering/CPointLight.h"
#include "Transform/TransformSystem.h"
#include "Game/LevelSystem.h"
#include "Game/PadSystem.h"
#include "Game/CBall.h"
#include "Game/CBrick.h"
#include "Game/CPad.h"
#include "Physics/PhysicsSystem.h"
#include "Physics/CPhysics.h"
#include "Physics/CBoxShape.h"
#include "Physics/ESetImpulse.h"
namespace dd
{
@@ -67,40 +73,123 @@ public:
// m_World->ComponentFactory.Register<Components::Model>();
// m_World->ComponentFactory.Register<Components::Template>();
m_World->ComponentFactory.Register<Components::Sprite>();
m_World->ComponentFactory.Register<Components::RectangleShape>();
m_World->ComponentFactory.Register<Components::Physics>();
m_World->ComponentFactory.Register<Components::Ball>();
m_World->ComponentFactory.Register<Components::Brick>();
m_World->ComponentFactory.Register<Components::Pad>();
m_World->SystemFactory.Register<Systems::PhysicsSystem>(
[this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::PhysicsSystem>();
m_World->SystemFactory.Register<Systems::LevelSystem>([this]() { return new Systems::LevelSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::LevelSystem>();
m_World->SystemFactory.Register<Systems::PadSystem>([this]() { return new Systems::PadSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::PadSystem>();
m_World->ComponentFactory.Register<Components::Model>();
m_World->ComponentFactory.Register<Components::Template>();
m_World->ComponentFactory.Register<Components::PointLight>();
m_World->Initialize();
//TODO: Remove tobias light-test code.
{
auto t_BrickWall = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(t_BrickWall);
transform->Position = glm::vec3(0.f, 0.f, -10.f);
auto sprite = m_World->AddComponent<Components::Sprite>(t_BrickWall);
//TODO: Rename SpriteFile to DiffuseTexture or similar.
sprite->SpriteFile = "Textures/Test/Brick_Diffuse.png";
sprite->NormalTexture = "Textures/Test/Brick_Normal.png";
sprite->SpecularTexture = "Textures/Test/Brick_Specular.png";
}
{
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(0.f, 0.f, -10.f);
transform->Position = glm::vec3(0.5f, 0.f, -10.f);
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
sprite->SpriteFile = "Textures/Ball.png";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> ball = m_World->AddComponent<Components::Ball>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
auto plight = m_World->AddComponent<Components::PointLight>(ent);
plight->Radius = 2.f;
m_World->CommitEntity(ent);
Events::SetImpulse e;
e.Entity = ent;
e.Impulse = glm::vec2(0.f, -7.f);
e.Point = glm::vec2(0.5f, 0.f);
m_EventBroker->Publish(e);
}
{
auto topWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
transform->Position = glm::vec3(0.f, 5.f, -10.f);
transform->Scale = glm::vec3(15.f, 0.5f, 1.f);
{
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(topWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(topWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
physics->Static = true;
m_World->CommitEntity(topWall);
}
{
auto leftWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(leftWall);
transform->Position = glm::vec3(-9.f, 1.f, -10.f);
transform->Scale = glm::vec3(0.5f, 10.f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(leftWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(leftWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(leftWall);
physics->Static = true;
m_World->CommitEntity(leftWall);
}
{
auto rightWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(rightWall);
transform->Position = glm::vec3(9.f, 1.f, -10.f);
transform->Scale = glm::vec3(0.5f, 10.f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(rightWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(rightWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(rightWall);
physics->Static = true;
m_World->CommitEntity(rightWall);
}
/*{
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(0.f, -2.f, -10.f);
transform->Position = glm::vec3(0.f, -3.f, -10.f);
transform->Scale = glm::vec3(8.f, 0.5f, 1.f);
transform->Orientation = glm::rotate(transform->Orientation, glm::radians(25.f), glm::vec3(0, 0, -1));
@@ -113,28 +202,22 @@ public:
physics->Static = true;
m_World->CommitEntity(ent);
}
}*/
{
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(-2.f, -1.f, -10.f);
transform->Scale = glm::vec3(0.5f, 8.5f, 1.f);
//transform->Orientation = glm::rotate(transform->Orientation, glm::radians(25.f), glm::vec3(0, 0, -1));
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = true;
m_World->SetProperty(ent, "Name", "Pad");
auto ctransform = m_World->AddComponent<Components::Transform>(ent);
ctransform->Position = glm::vec3(0.f, -5.f, -10.f);
ctransform->Scale = glm::vec3(3.2, 0.8, 0.);
auto rectangle = m_World->AddComponent<Components::RectangleShape>(ent);
auto physics = m_World->AddComponent<Components::Physics>(ent);
auto csprite = m_World->AddComponent<Components::Sprite>(ent);
auto pad = m_World->AddComponent<Components::Pad>(ent);
csprite->SpriteFile = "Textures/Pad.png";
m_World->CommitEntity(ent);
}
m_LastTime = glfwGetTime();
}
@@ -153,6 +236,19 @@ public:
m_World->Update(dt);
//
// if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) {
// ResourceManager::Reload("Shaders/Deferred/3/Fragment.glsl");
// }
//
//TODO Fill up the renderQueue with models (Temp fix)
// TEMPAddToRenderQueue();
// Render scene
//TODO send renderqueue to draw.
// m_Renderer->Draw(m_RendererQueue);
if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) {
ResourceManager::Reload("Shaders/Deferred/3/Fragment.glsl");
}
@@ -171,6 +267,7 @@ public:
}
std::shared_ptr<Systems::TransformSystem> m_TransformSystem;
std::shared_ptr<Systems::LevelSystem> m_LevelSystem;
//TODO: Get this out of engine.h
void TEMPAddToRenderQueue()
@@ -209,19 +306,43 @@ public:
}
}
//TODO: Add LightLoadShit
auto pointLightComponent = m_World->GetComponent<Components::PointLight>(entity);
if (pointLightComponent)
{
Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity);
EnqueuePointLight(absoluteTransform.Position,
pointLightComponent->Diffuse,
pointLightComponent->Specular,
pointLightComponent->Radius);
}
auto spriteComponent = m_World->GetComponent<Components::Sprite>(entity);
if (spriteComponent)
{
auto textureAsset = ResourceManager::Load<Texture>(spriteComponent->SpriteFile);
if (textureAsset)
{
Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity);
glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(absoluteTransform.Orientation).z, glm::vec3(0, 0, -1));
glm::mat4 modelMatrix = glm::translate(absoluteTransform.Position)
* glm::toMat4(orientation2D)
* glm::scale(absoluteTransform.Scale);
EnqueueSprite(textureAsset, modelMatrix, spriteComponent->Color);
std::string normal = spriteComponent->NormalTexture;
std::string spec = spriteComponent->SpecularTexture;
if (normal.empty()) {
normal = "Textures/Core/NeutralNormalMap.png";
}
if (spec.empty()) {
spec = "Textures/Core/NeutralSpecularMap.png";
}
auto texturediff = ResourceManager::Load<Texture>(spriteComponent->SpriteFile);
auto texturenorm = ResourceManager::Load<Texture>(normal);
auto texturespec = ResourceManager::Load<Texture>(spec);
Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity);
glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(absoluteTransform.Orientation).z, glm::vec3(0, 0, -1));
glm::mat4 modelMatrix = glm::translate(absoluteTransform.Position)
* glm::toMat4(orientation2D)
* glm::scale(absoluteTransform.Scale);
EnqueueSprite(texturediff, texturenorm, texturespec, modelMatrix, spriteComponent->Color);
}
}
@@ -250,15 +371,29 @@ public:
}
// TODO: Get this out of engine.h
void EnqueueSprite(Texture* texture, glm::mat4 modelMatrix, glm::vec4 color)
void EnqueueSprite(Texture* texture, Texture* normalTexture, Texture* specularTexture, glm::mat4 modelMatrix, glm::vec4 color)
{
SpriteJob job;
job.TextureID = texture->ResourceID;
job.Texture = *texture;
job.DiffuseTexture = *texture;
job.NormalTexture = *normalTexture;
job.SpecularTexture = *specularTexture;
job.ModelMatrix = modelMatrix;
job.Color = color;
m_RendererQueue.Forward.Add(job);
m_RendererQueue.Deferred.Add(job);
}
void EnqueuePointLight(glm::vec3 position, glm::vec3 diffuseColor, glm::vec3 specularColor, float radius)
{
PointLightJob job;
job.Position = position;
job.DiffuseColor = diffuseColor;
job.SpecularColor = specularColor;
job.Radius = radius;
m_RendererQueue.Lights.Add(job);
}
private:
+2 -33
View File
@@ -34,54 +34,23 @@ class InputController
public:
InputController(std::shared_ptr<dd::EventBroker> eventBroker)
: EventBroker(eventBroker)
, InGame(true)
{ Initialize(); }
virtual void Initialize()
{
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &InputController::_OnCommand);
EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &InputController::_OnMouseMove);
EVENT_SUBSCRIBE_MEMBER(m_EGameStart, &InputController::OnGameStart);
EVENT_SUBSCRIBE_MEMBER(m_EGameOver, &InputController::OnGameOver);
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &InputController::OnCommand);
EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &InputController::OnMouseMove);
}
virtual bool OnCommand(const Events::InputCommand &event) { return false; }
virtual bool OnMouseMove(const Events::MouseMove &event) { return false; }
virtual bool OnGameStart(const Events::GameStart &event)
{
InGame = true;
return true;
}
virtual bool OnGameOver(const Events::GameOver &event)
{
InGame = false;
return true;
}
protected:
std::shared_ptr<dd::EventBroker> EventBroker;
bool InGame;
private:
EventRelay<EventContext, Events::InputCommand> m_EInputCommand;
EventRelay<EventContext, Events::MouseMove> m_EMouseMove;
EventRelay<EventContext, Events::GameStart> m_EGameStart;
EventRelay<EventContext, Events::GameOver> m_EGameOver;
virtual bool _OnCommand(const Events::InputCommand &event)
{
if (InGame || event.Value == 0)
return OnCommand(event);
else
return false;
}
virtual bool _OnMouseMove(const Events::MouseMove &event)
{
if (InGame)
return OnMouseMove(event);
else
return false;
}
};
}
Regular → Executable
+3 -1
View File
@@ -89,7 +89,9 @@ struct SpriteJob : RenderJob
unsigned int TextureID;
glm::mat4 ModelMatrix;
GLuint Texture;
GLuint DiffuseTexture;
GLuint NormalTexture;
GLuint SpecularTexture;
glm::vec4 Color;
void CalculateHash() override
+1
View File
@@ -75,6 +75,7 @@ private:
GLuint m_ScreenQuad = 0;
Model* m_UnitSphere = nullptr;
Model* m_UnitQuad = nullptr;
GLuint m_rbDepthBuffer = 0;
GLuint m_fbDeferred1 = 0;
+25
View File
@@ -0,0 +1,25 @@
//
// Created by Adniklastrator on 2015-09-10.
//
#ifndef DAYDREAM_CBALL_H
#define DAYDREAM_CBALL_H
#include "Core/Component.h"
namespace dd
{
namespace Components
{
struct Ball : Component
{
};
}
}
#endif //DAYDREAM_CBALL_H
+26
View File
@@ -0,0 +1,26 @@
//
// Created by Adniklastrator on 2015-09-09.
//
#ifndef DAYDREAM_CBRICK_H
#define DAYDREAM_CBRICK_H
#include "Core/Component.h"
namespace dd
{
namespace Components
{
struct Brick : Component
{
int Points = 10;
int Hits = 1;
};
}
}
#endif //DAYDREAM_CBRICK_H
+28
View File
@@ -0,0 +1,28 @@
//
// Created by Adniklastrator on 2015-09-10.
//
#ifndef DAYDREAM_CPAD_H
#define DAYDREAM_CPAD_H
#include "Core/Component.h"
#include "Core/EKeyDown.h"
#include "Core/EventBroker.h"
namespace dd
{
namespace Components
{
struct Pad : Component
{
int Lives;
int Points;
};
}
}
#endif //DAYDREAM_CPAD_H
+21
View File
@@ -0,0 +1,21 @@
//
// Created by Adniklastrator on 2015-09-09.
//
#ifndef DAYDREAM_ESTAGECLEARED_H
#define DAYDREAM_ESTAGECLEARED_H
#include "Core/EventBroker.h"
namespace dd
{
namespace Events
{
struct StageCleared : Event
{
};
}
}
#endif //DAYDREAM_ESTAGECLEARED_H
+79
View File
@@ -0,0 +1,79 @@
//
// Created by Adniklastrator on 2015-09-09.
//
#ifndef DAYDREAM_LEVELSYSTEM_H
#define DAYDREAM_LEVELSYSTEM_H
#include "Core/System.h"
#include "Core/CTransform.h"
#include "Rendering/CSprite.h"
#include "Game/CBrick.h"
#include "Physics/EContact.h"
#include "Core/EventBroker.h"
#include "Core/World.h"
#include "Game/CBall.h"
#include "Game/EStageCleared.h"
#include "Physics/CBoxShape.h"
#include "Physics/CPhysics.h"
#include <fstream>
#include <iostream>
#include <intrin.h>
namespace dd
{
namespace Systems
{
// Me trying things out.
class Level
{
public:
int levelRows;
int levelLines;
int levelSpaceBetweenBricks;
int levelSpaceToEdge;
int bricks[];
};
class LevelSystem : public System
{
public:
LevelSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
: System(world, eventBroker)
{ }
void Initialize() override;
void CreateBasicLevel(int, int, glm::vec2, int);
void SaveLevel(int, int, glm::vec2, int); // Shouldn't be here, but I'm experimenting.
void LoadLevel(char[20]);
void CreateBrick(int, int, glm::vec2, int, int);
void ProcessCollision();
void OnEntityRemoved(EntityID entity);
void EndLevel();
void Update(double dt);
private:
dd::EventRelay<LevelSystem, dd::Events::Contact> m_EContact;
bool OnContact(const dd::Events::Contact &event);
bool m_Initialized = false;
int numberOfBricks;
int tRows = 7;
int tLines = 8;
glm::vec2 tSpaceBetweenBricks = glm::vec2(2, 0.5);
int tSpaceToEdge = 0;
};
}
}
#endif //DAYDREAM_LEVELSYSTEM_H
+82
View File
@@ -0,0 +1,82 @@
//
// Created by Adniklastrator on 2015-09-10.
//
#ifndef DAYDREAM_PADSYSTEM_H
#define DAYDREAM_PADSYSTEM_H
#include "Core/InputController.h"
#include "Core/System.h"
#include "Core/EventBroker.h"
#include "Core/EKeyDown.h"
#include "Core/EKeyUp.h"
#include "Input/EBindKey.h"
#include "Physics/EContact.h"
#include "Core/CTransform.h"
#include "Physics/CBoxShape.h"
#include "Physics/CPhysics.h"
#include "Rendering/CSprite.h"
#include "Game/CBall.h"
#include "Core/Component.h"
#include "Physics/ESetImpulse.h"
#include "Physics/CCircleShape.h"
namespace dd
{
namespace Systems
{
class PadSystem : public System
{
public:
PadSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
: System(world, eventBroker)
{ }
void Initialize() override;
void Update(double dt) override;
private:
dd::EventRelay<PadSystem, dd::Events::KeyDown> m_EKeyDown;
dd::EventRelay<PadSystem, dd::Events::KeyUp> m_EKeyUp;
dd::EventRelay<PadSystem, dd::Events::Contact> m_EContact;
bool OnKeyDown(const dd::Events::KeyDown &event);
bool OnKeyUp(const dd::Events::KeyUp &event);
bool OnContact(const dd::Events::Contact &event);
class PadSteeringInputController;
std::array<std::shared_ptr<PadSteeringInputController>, 4> m_PadInputControllers;
EntityID ent = 0;
Components::Transform* transform;
glm::vec3 acceleration = glm::vec3(0.f, 0.f, 0.f);
bool left = false, right = false;
};
class PadSystem::PadSteeringInputController : InputController<PadSystem>
{
public:
PadSteeringInputController(std::shared_ptr<dd::EventBroker> eventBroker)
: InputController(eventBroker)
{
}
protected:
virtual bool OnCommand(const Events::InputCommand &event);
};
}
}
#endif //DAYDREAM_PADSYSTEM_H
+5 -5
View File
@@ -7,11 +7,11 @@ namespace dd
namespace Components
{
struct Physics: public Component
{
//idk if anything should be here :)
bool Static = true;
};
struct Physics: public Component
{
//idk if anything should be here :)
bool Static = true;
};
}
-2
View File
@@ -80,8 +80,6 @@ private:
e.Entity1 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
e.Entity2 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()];
std::cout << e.Entity1 << " " << e.Entity2 << std::endl;
m_PhysicsSystem->EventBroker->Publish(e);
}
void EndContact(b2Contact* contact)
Regular → Executable
+2
View File
@@ -34,6 +34,8 @@ struct Sprite : Component
Sprite() : Color(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)) { }
std::string SpriteFile;
std::string NormalTexture;
std::string SpecularTexture;
glm::vec4 Color;
};
+1 -1
View File
@@ -39,7 +39,7 @@ public:
TransformSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
: System(world, eventBroker)
{ }
//void Update(double dt) override;
void Update(double dt) override;
Components::Transform AbsoluteTransform(EntityID entity);
glm::vec3 AbsolutePosition(EntityID entity);