Merge branch 'master' into LevelSystemAndSuch

This commit is contained in:
PlatinumSkink
2015-09-28 15:06:37 +02:00
86 changed files with 6999 additions and 1135 deletions
+238 -91
View File
@@ -49,47 +49,60 @@
#include "Physics/PhysicsSystem.h"
#include "Physics/CPhysics.h"
#include "Physics/CBoxShape.h"
#include "Physics/CRectangleShape.h"
#include "Physics/ESetImpulse.h"
#include "Physics/CWaterVolume.h"
#include "Game/EGameStart.h"
#include "Sound/EPlaySound.h"
#include "GUI/Frame.h"
#include "GUI/Button.h"
#include "Game/MainMenu.h"
#include "Game/HUD.h"
namespace dd
{
class Engine
{
public:
Engine(int argc, char* argv[]) {
m_EventBroker = std::make_shared<EventBroker>();
m_EventBroker = std::make_shared<EventBroker>();
m_Renderer = std::make_shared<Renderer>();
m_Renderer->SetFullscreen(false);
//m_Renderer->SetResolution(Rectangle(0, 0, 1920, 1080));
m_Renderer->SetResolution(Rectangle(0, 0, 675, 1080));
m_Renderer = std::make_shared<Renderer>();
m_Renderer->SetFullscreen(false);
//m_Renderer->SetResolution(Rectangle(0, 0, 1920, 1080));
m_Renderer->SetResolution(Rectangle(0, 0, 675, 1080));
m_Renderer->Initialize();
m_InputManager = std::make_shared<InputManager>(m_Renderer->Window(), m_EventBroker);
m_FrameStack = new GUI::Frame(m_EventBroker.get());
m_FrameStack->Width = 675;
m_FrameStack->Height = 1080;
auto menu = new GUI::MainMenu(m_FrameStack, "MainMenu");
m_InputManager = std::make_shared<InputManager>(m_Renderer->Window(), m_EventBroker);
m_World = std::make_shared<World>(m_EventBroker);
//TODO: Move this out of engine.h
m_World->ComponentFactory.Register<Components::Transform>();
m_World->SystemFactory.Register<Systems::TransformSystem>(
[this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::TransformSystem>();
//TODO: Move this out of engine.h
m_World->ComponentFactory.Register<Components::Transform>();
m_World->SystemFactory.Register<Systems::TransformSystem>(
[this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::TransformSystem>();
m_World->ComponentFactory.Register<Components::Model>();
m_World->ComponentFactory.Register<Components::Template>();
m_World->ComponentFactory.Register<Components::CollisionSound>();
m_World->SystemFactory.Register<Systems::SoundSystem>([this]() { return new Systems::SoundSystem(m_World.get(), m_EventBroker); });
m_World->SystemFactory.Register<Systems::SoundSystem>(
[this]() { return new Systems::SoundSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::SoundSystem>();
m_World->ComponentFactory.Register<Components::Sprite>();
m_World->ComponentFactory.Register<Components::Sprite>();
m_World->ComponentFactory.Register<Components::RectangleShape>();
m_World->ComponentFactory.Register<Components::Physics>();
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>();
@@ -98,116 +111,211 @@ public:
m_World->ComponentFactory.Register<Components::PowerUp>();
m_World->ComponentFactory.Register<Components::PowerUpBrick>();
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::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->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->SystemFactory.Register<Systems::PadSystem>(
[this]() { return new Systems::PadSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::PadSystem>();
m_World->SystemFactory.Register<Systems::BallSystem>([this]() { return new Systems::BallSystem(m_World.get(), m_EventBroker); });
m_World->SystemFactory.Register<Systems::BallSystem>(
[this]() { return new Systems::BallSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::BallSystem>();
m_World->ComponentFactory.Register<Components::Model>();
m_World->ComponentFactory.Register<Components::Template>();
m_World->ComponentFactory.Register<Components::Model>();
m_World->ComponentFactory.Register<Components::Template>();
m_World->ComponentFactory.Register<Components::PointLight>();
m_World->Initialize();
m_World->ComponentFactory.Register<Components::WaterVolume>();
m_World->Initialize();
//TODO: Remove tobias light-test code.
/*{
}*/
//OctoBall
{
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(-0.f, 0.26f, -10.f);
{
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(-0.f, 0.26f, -9.f);
transform->Scale = glm::vec3(0.5f, 0.5f, 0.5f);
transform->Velocity = glm::vec3(0.0f, -10.f, 0.f);
auto model = m_World->AddComponent<Components::Model>(ent);
auto model = m_World->AddComponent<Components::Model>(ent);
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> ball = m_World->AddComponent<Components::Ball>(ent);
ball->Speed = 10.f;
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
ball->Speed = 5.f;
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
physics->Category = CollisionLayer::Type::Ball;
physics->Mask = CollisionLayer::Type::Pad | CollisionLayer::Type::Brick | CollisionLayer::Type::Wall;
physics->Calculate = true;
auto plight = m_World->AddComponent<Components::PointLight>(ent);
plight->Radius = 2.f;
m_World->CommitEntity(ent);
}
m_World->CommitEntity(ent);
}
//PointLightTest
{
auto t_Light = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(t_Light);
transform->Position = glm::vec3(2.f, 1.5f, -9.f);
transform->Position = glm::vec3(-3.f, 6.f, -5.f);
auto pl = m_World->AddComponent<Components::PointLight>(t_Light);
pl->Radius = 8.f;
pl->Radius = 20.f;
pl->Diffuse = glm::vec3(0.8f, 0.7f, 0.05f);
auto model = m_World->AddComponent<Components::Model>(t_Light);
model->ModelFile = "Core/UnitSphere.obj";
m_World->CommitEntity(t_Light);
}
{
auto t_Light = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(t_Light);
transform->Position = glm::vec3(3.f, -5.f, -5.f);
auto pl = m_World->AddComponent<Components::PointLight>(t_Light);
pl->Radius = 15.f;
pl->Diffuse = glm::vec3(0.1f, 0.5f, 0.8f);
auto model = m_World->AddComponent<Components::Model>(t_Light);
model->ModelFile = "Core/UnitSphere.obj";
m_World->CommitEntity(t_Light);
}
//Halfpipe background test model.
{
auto t_halfPipe = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(t_halfPipe);
transform->Position = glm::vec3(0.f, 0.f, -13.f);
transform->Scale = glm::vec3(5.f);
transform->Position = glm::vec3(0.f, 0.f, -15.f);
transform->Scale = glm::vec3(6.f, 6.f, 10.f);
auto model = m_World->AddComponent<Components::Model>(t_halfPipe);
model->ModelFile = "Models/Test/halfpipe/Halfpipe.obj";
model->Color = glm::vec4(1.f, 1.f, 1.f, 0.3f);
m_World->CommitEntity(t_halfPipe);
}
//Background
{
auto background = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(background);
transform->Position = glm::vec3(0.f, 0.f, -30.f);
transform->Scale = glm::vec3(2681.f / 50.f, 1080.f / 50.f, 1.f);
auto sprite = m_World->AddComponent<Components::Sprite>(background);
sprite->SpriteFile = "Textures/Background.png";
m_World->CommitEntity(background);
}
//Water test
{
auto t_waterBody = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(t_waterBody);
transform->Position = glm::vec3(0.f, -5.5f, -10.f);
transform->Scale = glm::vec3(7.f, 1.5f, 1.f);
auto water = m_World->AddComponent<Components::WaterVolume>(t_waterBody);
auto body = m_World->AddComponent<Components::RectangleShape>(t_waterBody);
m_World->CommitEntity(t_waterBody);
}
//TODO: Why does the ball not collide with these bricks?
//BottomBox
{
auto topWall = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(topWall);
transform->Position = glm::vec3(0.f, -6.f, -9.9f);
transform->Scale = glm::vec3(10.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);
}
//SideBox
// {
// auto topWall = m_World->CreateEntity();
// std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
// transform->Position = glm::vec3(3.f, -3.0f, -9.9f);
// transform->Scale = glm::vec3(0.5f, 3.f, 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);
// }
// //OtherSideBox
// {
// auto topWall = m_World->CreateEntity();
// std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
// transform->Position = glm::vec3(-4.f, -3.0f, -9.9f);
// transform->Scale = glm::vec3(0.5f, 3.0f, 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 topWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(
topWall);
transform->Position = glm::vec3(0.f, 6.f, -10.f);
transform->Scale = glm::vec3(20.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::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(
topWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
physics->Static = true;
physics->Category = CollisionLayer::Type::Wall;
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::Brick;
m_World->CommitEntity(topWall);
}
{
auto leftWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(leftWall);
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(
leftWall);
transform->Position = glm::vec3(-4.f, 1.f, -10.f);
transform->Scale = glm::vec3(0.5f, 20.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::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(
leftWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(leftWall);
physics->Static = true;
physics->Category = CollisionLayer::Type::Wall;
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::Brick;
m_World->CommitEntity(leftWall);
}
{
auto rightWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(rightWall);
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(
rightWall);
transform->Position = glm::vec3(4.f, 1.f, -10.f);
transform->Scale = glm::vec3(0.5f, 20.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::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(
rightWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(rightWall);
physics->Static = true;
physics->Category = CollisionLayer::Type::Wall;
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::Brick;
m_World->CommitEntity(rightWall);
}
@@ -216,18 +324,27 @@ public:
auto ent = m_World->CreateEntity();
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(1.6, 0.4, 0.);
ctransform->Position = glm::vec3(0.f, -3.5f, -10.f);
ctransform->Scale = glm::vec3(1.0f, 1.0f, 1.f);
auto rectangle = m_World->AddComponent<Components::RectangleShape>(ent);
auto physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
auto csprite = m_World->AddComponent<Components::Sprite>(ent);
physics->Category = CollisionLayer::Type::Pad;
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::PowerUp;
physics->Calculate = true;
auto cModel = m_World->AddComponent<Components::Model>(ent);
cModel->ModelFile = "Models/Submarine2.obj";
auto pad = m_World->AddComponent<Components::Pad>(ent);
csprite->SpriteFile = "Textures/Pad.png";
m_World->CommitEntity(ent);
}
m_LastTime = glfwGetTime();
//EVENT_SUBSCRIBE_MEMBER(m_EGameStart, &Engine::OnGameStart);
m_EGameStart = decltype(m_EGameStart)(std::bind(&Engine::OnGameStart, this, std::placeholders::_1));
m_EventBroker->Subscribe(m_EGameStart);
m_LastTime = glfwGetTime();
}
bool Running() const { return !glfwWindowShouldClose(m_Renderer->Window()); }
@@ -238,39 +355,32 @@ public:
double dt = currentTime - m_LastTime;
m_LastTime = currentTime;
ResourceManager::Update();
// Update input
m_InputManager->Update(dt);
// Swap event queues to get fresh input data in the read queue
//m_EventBroker->Swap();
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");
ResourceManager::Update();
if (m_GameIsRunning) {
m_World->Update(dt);
}
m_EventBroker->Process<GUI::Frame>();
m_FrameStack->UpdateLayered(dt);
m_RendererQueue.Clear();
m_FrameStack->DrawLayered(m_RendererQueue);
//TODO Fill up the renderQueue with models (Temp fix)
TEMPAddToRenderQueue();
if (m_GameIsRunning) {
TEMPAddToRenderQueue();
}
// Render scene
//TODO send renderqueue to draw.
m_Renderer->Draw(m_RendererQueue);
m_EventBroker->Process<Engine>();
// Swap event queues
m_EventBroker->Clear();
m_EventBroker->Swap();
glfwPollEvents();
}
@@ -281,12 +391,9 @@ public:
//TODO: Get this out of engine.h
void TEMPAddToRenderQueue()
{
if (!m_TransformSystem)
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>();
m_RendererQueue.Clear();
for (auto &pair : *m_World->GetEntities())
{
EntityID entity = pair.first;
@@ -315,8 +422,6 @@ public:
}
}
//TODO: Add LightLoadShit
auto pointLightComponent = m_World->GetComponent<Components::PointLight>(entity);
if (pointLightComponent)
{
@@ -327,6 +432,19 @@ public:
pointLightComponent->Radius);
}
auto parent = m_World->GetEntityParent(entity);
if(parent != 0) {
auto waterParticleComponent = m_World->GetComponent<Components::WaterVolume>(parent);
if (waterParticleComponent) {
//TODO: Remove hardcoded color.
//TODO: Do i even need modelMatrix?
Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity);
glm::mat4 modelMatrix = glm::translate(absoluteTransform.Position)
* glm::scale(absoluteTransform.Scale);
EnqueueWaterParticles(absoluteTransform.Position, glm::vec4(1.f, 1.f, 1.f, 1.f), modelMatrix, absoluteTransform.Position.z);
}
}
auto spriteComponent = m_World->GetComponent<Components::Sprite>(entity);
if (spriteComponent)
@@ -355,7 +473,6 @@ public:
}
}
m_RendererQueue.Sort();
}
//TODO: Get this out of engine.h
@@ -374,7 +491,6 @@ public:
job.EndIndex = texGroup.EndIndex;
job.ModelMatrix = modelMatrix;
job.Color = color;
job.fileName = fileName;
m_RendererQueue.Deferred.Add(job);
}
@@ -408,15 +524,46 @@ public:
}
void EnqueueWaterParticles(glm::vec3 position, glm::vec4 color, glm::mat4 modelMatrix, float depth)
{
WaterParticleJob job;
job.Position = position;
job.Color = color;
job.ModelMatrix = modelMatrix;
job.Depth = depth;
m_RendererQueue.Forward.Add(job);
}
private:
std::shared_ptr<ResourceManager> m_ResourceManager;
std::shared_ptr<EventBroker> m_EventBroker;
GUI::Frame* m_FrameStack = nullptr;
std::shared_ptr<Renderer> m_Renderer;
RenderQueueCollection m_RendererQueue;
std::shared_ptr<InputManager> m_InputManager;
std::shared_ptr<World> m_World;
//TODO: Redo
bool m_GameIsRunning = false;
dd::EventRelay<Engine, dd::Events::GameStart> m_EGameStart;
bool OnGameStart(const dd::Events::GameStart &event)
{
m_GameIsRunning = true;
//Todo: Move this
{
dd::Events::PlaySound e;
e.path = "Sounds/BGM/under-the-sea-instrumental.wav";
e.isAmbient = true;
m_EventBroker->Publish(e);
}
{
dd::Events::PlaySound e;
e.path = "Sounds/BGM/water-flowing.wav";
e.volume = 0.3f;
e.isAmbient = true;
m_EventBroker->Publish(e);
}
};
double m_LastTime;
};
+562
View File
@@ -0,0 +1,562 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <sstream>
#include <Sound/CCollisionSound.h>
#include "ResourceManager.h"
#include "OBJ.h"
#include "Model.h"
#include "Texture.h"
#include "EventBroker.h"
#include "RenderQueue.h"
#include "Renderer.h"
#include "InputManager.h"
//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 "Sound/SoundSystem.h"
#include "Sound/CCollisionSound.h"
#include "Game/LevelSystem.h"
#include "Game/PadSystem.h"
#include "Game/CBall.h"
#include "Game/CPad.h"
#include "Game/CBrick.h"
#include "Game/BallSystem.h"
#include "Game/Bricks/CPowerUpBrick.h"
#include "Physics/PhysicsSystem.h"
#include "Physics/CPhysics.h"
#include "Physics/CRectangleShape.h"
#include "Physics/ESetImpulse.h"
#include "Physics/CWaterVolume.h"
#include "Game/EGameStart.h"
#include "Sound/EPlaySound.h"
#include "GUI/Frame.h"
#include "GUI/Button.h"
#include "Game/MainMenu.h"
#include "Game/HUD.h"
namespace dd
{
class Engine
{
public:
Engine(int argc, char* argv[]) {
m_EventBroker = std::make_shared<EventBroker>();
m_Renderer = std::make_shared<Renderer>();
m_Renderer->SetFullscreen(false);
//m_Renderer->SetResolution(Rectangle(0, 0, 1920, 1080));
m_Renderer->SetResolution(Rectangle(0, 0, 675, 1080));
m_Renderer->Initialize();
m_FrameStack = new GUI::Frame(m_EventBroker.get());
m_FrameStack->Width = 675;
m_FrameStack->Height = 1080;
auto hud = new GUI::HUD(m_FrameStack, "HUD");
auto menu = new GUI::MainMenu(m_FrameStack, "MainMenu");
m_InputManager = std::make_shared<InputManager>(m_Renderer->Window(), m_EventBroker);
m_World = std::make_shared<World>(m_EventBroker);
//TODO: Move this out of engine.h
m_World->ComponentFactory.Register<Components::Transform>();
m_World->SystemFactory.Register<Systems::TransformSystem>(
[this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::TransformSystem>();
m_World->ComponentFactory.Register<Components::Model>();
m_World->ComponentFactory.Register<Components::Template>();
m_World->ComponentFactory.Register<Components::CollisionSound>();
m_World->SystemFactory.Register<Systems::SoundSystem>(
[this]() { return new Systems::SoundSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::SoundSystem>();
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->ComponentFactory.Register<Components::Life>();
m_World->ComponentFactory.Register<Components::PowerUp>();
m_World->ComponentFactory.Register<Components::PowerUpBrick>();
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->SystemFactory.Register<Systems::BallSystem>(
[this]() { return new Systems::BallSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::BallSystem>();
m_World->ComponentFactory.Register<Components::Model>();
m_World->ComponentFactory.Register<Components::Template>();
m_World->ComponentFactory.Register<Components::PointLight>();
m_World->ComponentFactory.Register<Components::WaterVolume>();
m_World->Initialize();
//OctoBall
{
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(-0.f, 0.26f, -9.f);
transform->Scale = glm::vec3(0.5f, 0.5f, 0.5f);
transform->Velocity = glm::vec3(0.0f, -10.f, 0.f);
auto model = m_World->AddComponent<Components::Model>(ent);
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> ball = m_World->AddComponent<Components::Ball>(ent);
ball->Speed = 5.f;
<<<<<<< HEAD
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
physics->Category = CollisionLayer::Type::Ball;
physics->Mask = CollisionLayer::Type::Pad | CollisionLayer::Type::Brick | CollisionLayer::Type::Wall;
physics->Calculate = true;
=======
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
>>>>>>> origin/master
auto plight = m_World->AddComponent<Components::PointLight>(ent);
plight->Radius = 2.f;
m_World->CommitEntity(ent);
}
//PointLightTest
{
auto t_Light = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(t_Light);
transform->Position = glm::vec3(2.f, 1.5f, -9.f);
auto pl = m_World->AddComponent<Components::PointLight>(t_Light);
pl->Radius = 8.f;
m_World->CommitEntity(t_Light);
}
//Halfpipe background test model.
{
auto t_halfPipe = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(t_halfPipe);
transform->Position = glm::vec3(0.f, 0.f, -15.f);
transform->Scale = glm::vec3(15.f);
auto model = m_World->AddComponent<Components::Model>(t_halfPipe);
model->ModelFile = "Models/Test/halfpipe/Halfpipe.obj";
model->Color = glm::vec4(1.f, 1.f, 1.f, 0.3f);
m_World->CommitEntity(t_halfPipe);
}
//Background
{
auto background = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(background);
transform->Position = glm::vec3(0.f, 0.f, -30.f);
transform->Scale = glm::vec3(2681.f / 50.f, 1080.f / 50.f, 1.f);
auto sprite = m_World->AddComponent<Components::Sprite>(background);
sprite->SpriteFile = "Textures/Background.png";
m_World->CommitEntity(background);
}
//Water test
{
auto t_waterBody = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(t_waterBody);
transform->Position = glm::vec3(0.f, -4.5f, -10.f);
transform->Scale = glm::vec3(7.f, 1.5f, 1.f);
auto water = m_World->AddComponent<Components::WaterVolume>(t_waterBody);
auto body = m_World->AddComponent<Components::RectangleShape>(t_waterBody);
m_World->CommitEntity(t_waterBody);
}
//TODO: Why does the ball not collide with these bricks?
//BottomBox
{
auto topWall = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(topWall);
transform->Position = glm::vec3(0.f, -6.f, -9.9f);
transform->Scale = glm::vec3(10.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);
}
//SideBox
// {
// auto topWall = m_World->CreateEntity();
// std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
// transform->Position = glm::vec3(3.f, -3.0f, -9.9f);
// transform->Scale = glm::vec3(0.5f, 3.f, 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);
// }
// //OtherSideBox
// {
// auto topWall = m_World->CreateEntity();
// std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
// transform->Position = glm::vec3(-4.f, -3.0f, -9.9f);
// transform->Scale = glm::vec3(0.5f, 3.0f, 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 topWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(
topWall);
transform->Position = glm::vec3(0.f, 6.f, -10.f);
transform->Scale = glm::vec3(20.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;
physics->Category = CollisionLayer::Type::Wall;
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::Brick;
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(-4.f, 1.f, -10.f);
transform->Scale = glm::vec3(0.5f, 20.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;
physics->Category = CollisionLayer::Type::Wall;
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::Brick;
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(4.f, 1.f, -10.f);
transform->Scale = glm::vec3(0.5f, 20.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;
physics->Category = CollisionLayer::Type::Wall;
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::Brick;
m_World->CommitEntity(rightWall);
}
{
auto ent = m_World->CreateEntity();
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(1.0f, 1.0f, 1.f);
auto rectangle = m_World->AddComponent<Components::RectangleShape>(ent);
auto physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
physics->Category = CollisionLayer::Type::Pad;
physics->Mask = CollisionLayer::Type::Ball | CollisionLayer::Type::PowerUp;
physics->Calculate = true;
auto cModel = m_World->AddComponent<Components::Model>(ent);
cModel->ModelFile = "Models/Submarine2.obj";
auto pad = m_World->AddComponent<Components::Pad>(ent);
m_World->CommitEntity(ent);
}
//EVENT_SUBSCRIBE_MEMBER(m_EGameStart, &Engine::OnGameStart);
m_EGameStart = decltype(m_EGameStart)(std::bind(&Engine::OnGameStart, this, std::placeholders::_1));
m_EventBroker->Subscribe(m_EGameStart);
m_LastTime = glfwGetTime();
}
bool Running() const { return !glfwWindowShouldClose(m_Renderer->Window()); }
void Tick()
{
double currentTime = glfwGetTime();
double dt = currentTime - m_LastTime;
m_LastTime = currentTime;
// Update input
m_InputManager->Update(dt);
// Swap event queues to get fresh input data in the read queue
//m_EventBroker->Swap();
ResourceManager::Update();
if (m_GameIsRunning) {
m_World->Update(dt);
}
m_EventBroker->Process<GUI::Frame>();
m_FrameStack->UpdateLayered(dt);
m_RendererQueue.Clear();
m_FrameStack->DrawLayered(m_RendererQueue);
//TODO Fill up the renderQueue with models (Temp fix)
if (m_GameIsRunning) {
TEMPAddToRenderQueue();
}
// Render scene
//TODO send renderqueue to draw.
m_Renderer->Draw(m_RendererQueue);
m_EventBroker->Process<Engine>();
// Swap event queues
m_EventBroker->Swap();
glfwPollEvents();
}
std::shared_ptr<Systems::TransformSystem> m_TransformSystem;
std::shared_ptr<Systems::LevelSystem> m_LevelSystem;
//TODO: Get this out of engine.h
void TEMPAddToRenderQueue()
{
if (!m_TransformSystem)
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>();
for (auto &pair : *m_World->GetEntities())
{
EntityID entity = pair.first;
auto templateComponent = m_World->GetComponent<Components::Template>(entity);
if (templateComponent)
continue;
auto transform = m_World->GetComponent<Components::Transform>(entity);
if (!transform)
continue;
auto modelComponent = m_World->GetComponent<Components::Model>(entity);
if (modelComponent)
{
Model* modelAsset = nullptr;
modelAsset = ResourceManager::Load<Model>(modelComponent->ModelFile);
if (modelAsset)
{
Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity);
glm::mat4 modelMatrix = glm::translate(glm::mat4(), absoluteTransform.Position)
* glm::toMat4(absoluteTransform.Orientation)
* glm::scale(absoluteTransform.Scale);
EnqueueModel(modelAsset, modelMatrix, modelComponent->Transparent, modelComponent->Color, modelComponent->ModelFile);
}
}
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 parent = m_World->GetEntityParent(entity);
if(parent != 0) {
auto waterParticleComponent = m_World->GetComponent<Components::WaterVolume>(parent);
if (waterParticleComponent) {
//TODO: Remove hardcoded color.
//TODO: Do i even need modelMatrix?
Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity);
glm::mat4 modelMatrix = glm::translate(absoluteTransform.Position)
* glm::scale(absoluteTransform.Scale);
EnqueueWaterParticles(absoluteTransform.Position, glm::vec4(1.f, 1.f, 1.f, 1.f), modelMatrix, absoluteTransform.Position.z);
}
}
auto spriteComponent = m_World->GetComponent<Components::Sprite>(entity);
if (spriteComponent)
{
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, absoluteTransform.Position.z);
}
}
}
//TODO: Get this out of engine.h
void EnqueueModel(Model* model, glm::mat4 modelMatrix, float transparent, glm::vec4 color, std::string fileName)
{
for (auto texGroup : model->TextureGroups)
{
ModelJob job;
job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0;
job.DiffuseTexture = (texGroup.Texture) ? *texGroup.Texture : 0;
job.NormalTexture = (texGroup.NormalMap) ? *texGroup.NormalMap : 0;
job.SpecularTexture = (texGroup.SpecularMap) ? *texGroup.SpecularMap : 0;
job.VAO = model->VAO;
job.ElementBuffer = model->ElementBuffer;
job.StartIndex = texGroup.StartIndex;
job.EndIndex = texGroup.EndIndex;
job.ModelMatrix = modelMatrix;
job.Color = color;
m_RendererQueue.Deferred.Add(job);
}
}
// TODO: Get this out of engine.h
void EnqueueSprite(Texture* texture, Texture* normalTexture, Texture* specularTexture, glm::mat4 modelMatrix, glm::vec4 color, float depth)
{
SpriteJob job;
job.TextureID = texture->ResourceID;
job.DiffuseTexture = *texture;
job.NormalTexture = *normalTexture;
job.SpecularTexture = *specularTexture;
job.ModelMatrix = modelMatrix;
job.Color = color;
job.Depth = depth;
m_RendererQueue.Forward.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);
}
void EnqueueWaterParticles(glm::vec3 position, glm::vec4 color, glm::mat4 modelMatrix, float depth)
{
WaterParticleJob job;
job.Position = position;
job.Color = color;
job.ModelMatrix = modelMatrix;
job.Depth = depth;
m_RendererQueue.Forward.Add(job);
}
private:
std::shared_ptr<EventBroker> m_EventBroker;
GUI::Frame* m_FrameStack = nullptr;
std::shared_ptr<Renderer> m_Renderer;
RenderQueueCollection m_RendererQueue;
std::shared_ptr<InputManager> m_InputManager;
std::shared_ptr<World> m_World;
//TODO: Redo
bool m_GameIsRunning = false;
dd::EventRelay<Engine, dd::Events::GameStart> m_EGameStart;
bool OnGameStart(const dd::Events::GameStart &event)
{
m_GameIsRunning = true;
//Todo: Move this
{
dd::Events::PlaySound e;
e.path = "Sounds/BGM/under-the-sea-instrumental.wav";
e.isAmbient = true;
m_EventBroker->Publish(e);
}
{
dd::Events::PlaySound e;
e.path = "Sounds/BGM/water-flowing.wav";
e.volume = 0.3f;
e.isAmbient = true;
m_EventBroker->Publish(e);
}
};
double m_LastTime;
};
}
+1 -1
View File
@@ -119,7 +119,7 @@ public:
template <typename ContextType>
int Process();
int Process(std::string contextTypeName);
void Clear();
void Swap();
void Unsubscribe(BaseEventRelay &relay);
private:
-3
View File
@@ -33,9 +33,6 @@ class PNG : public Image
public:
PNG(std::string path);
~PNG();
private:
};
}
+44 -8
View File
@@ -22,8 +22,9 @@
#include <cstdint>
#include <forward_list>
#include "Texture.h"
#include "Model.h"
#include "Core/Util/Rectangle.h"
#include "Core/Texture.h"
#include "Core/Model.h"
namespace dd
{
@@ -52,8 +53,8 @@ struct ModelJob : RenderJob
unsigned int ShaderID;
unsigned int TextureID;
std::string fileName;
glm::mat4 ProjectionMatrix;
glm::mat4 ViewMatrix;
glm::mat4 ModelMatrix;
GLuint DiffuseTexture;
GLuint NormalTexture;
@@ -90,6 +91,8 @@ struct SpriteJob : RenderJob
unsigned int ShaderID;
unsigned int TextureID;
glm::mat4 ProjectionMatrix;
glm::mat4 ViewMatrix;
glm::mat4 ModelMatrix;
GLuint DiffuseTexture;
GLuint NormalTexture;
@@ -115,6 +118,30 @@ struct PointLightJob : RenderJob
}
};
struct FrameJob : SpriteJob
{
Rectangle Scissor;
Rectangle Viewport;
std::string Name;
void CalculateHash() override
{
Hash = 0;
}
};
struct WaterParticleJob : RenderJob
{
glm::vec3 Position;
glm::vec4 Color;
glm::mat4 ModelMatrix;
void CalculateHash() override
{
Hash = 0;
}
};
class RenderQueue
{
public:
@@ -122,7 +149,8 @@ public:
void Add(T &job)
{
job.CalculateHash();
Jobs.push_front(std::shared_ptr<T>(new T(job)));
Jobs.push_back(std::shared_ptr<T>(new T(job)));
m_Size++;
}
void Sort()
@@ -133,19 +161,24 @@ public:
void Clear()
{
Jobs.clear();
m_Size = 0;
}
std::forward_list<std::shared_ptr<RenderJob>>::const_iterator begin()
int Size() const { return m_Size; }
std::list<std::shared_ptr<RenderJob>>::const_iterator begin()
{
return Jobs.begin();
}
std::forward_list<std::shared_ptr<RenderJob>>::const_iterator end()
std::list<std::shared_ptr<RenderJob>>::const_iterator end()
{
return Jobs.end();
}
std::forward_list<std::shared_ptr<RenderJob>> Jobs;
std::list<std::shared_ptr<RenderJob>> Jobs;
private:
int m_Size = 0;
};
struct RenderQueueCollection
@@ -153,12 +186,14 @@ struct RenderQueueCollection
RenderQueue Deferred;
RenderQueue Forward;
RenderQueue Lights;
RenderQueue GUI;
void Clear()
{
Deferred.Clear();
Forward.Clear();
Lights.Clear();
GUI.Clear();
}
void Sort()
@@ -166,6 +201,7 @@ struct RenderQueueCollection
Deferred.Sort();
Forward.Sort();
Lights.Sort();
GUI.Sort();
}
};
+17
View File
@@ -72,12 +72,15 @@ private:
ShaderProgram* m_spDeferred3;
ShaderProgram* m_spForward;
ShaderProgram* m_spScreen;
ShaderProgram* t_m_spWater;
ShaderProgram* t_m_spWater2;
GLuint m_ScreenQuad = 0;
Model* m_UnitSphere = nullptr;
Model* m_UnitQuad = nullptr;
Texture* m_StandardNormal;
Texture* m_StandardSpecular;
Texture* t_m_WhiteSphereTexture;
GLuint m_rbDepthBuffer = 0;
GLuint m_fbDeferred1 = 0;
@@ -89,11 +92,23 @@ private:
GLuint m_tLighting = 0;
GLuint m_fbDeferred3 = 0;
GLuint m_tFinal = 0;
//Water GTexture
GLuint t_m_Gwater = 0;
//Water blur texture
GLuint t_m_BWater;
GLuint t_m_BWater2;
//WAterpass Framebuffer
GLuint t_m_fbWater;
//Waterpass Framebuffer
GLuint t_m_fbWaterBlur;
GLuint t_m_fbWaterBlur2;
GLuint m_CurrentScreenBuffer = 0;
void LoadShaders();
void CreateBuffers();
GLuint CreateQuad();
GLuint CreateWaterParticleVAO(RenderQueue &particles);
static bool DepthSort(const std::shared_ptr<RenderJob> &i, const std::shared_ptr<RenderJob> &j) { return (i->Depth < j->Depth); }
@@ -101,6 +116,8 @@ private:
void DrawForward(RenderQueue &objects, RenderQueue &lights);
void DrawScene(RenderQueue &objects, ShaderProgram &program);
void DrawLightSpheres(RenderQueue &lights);
void DrawWater(RenderQueue &objects);
void DrawGUI(RenderQueue& rq);
void DebugKeys();
};
+3
View File
@@ -43,6 +43,9 @@ public:
operator GLuint() const { return m_Texture; }
unsigned int Width = 0;
unsigned int Height = 0;
private:
GLuint m_Texture = 0;
};
+8 -7
View File
@@ -44,29 +44,30 @@ struct Rectangle
int Height;
virtual int Left() const { return X; }
void SetLeft(int left)
virtual void SetLeft(int left)
{
Width += X - left;
//Width += X - left;
X = left;
}
virtual int Right() const { return X + Width; }
void SetRight(int right)
virtual void SetRight(int right)
{
Width = right - X;
}
virtual int Top() const { return Y; }
void SetTop(int top)
virtual void SetTop(int top)
{
Height += Y - top;
//Height += Y - top;
Y = top;
}
virtual int Bottom() const { return Y + Height; }
int SetBottom(int bottom)
virtual void SetBottom(int bottom)
{
Height = bottom - Y;
}
Rectangle& operator+=(const Rectangle &rhs) {
Rectangle& operator+=(const Rectangle &rhs)
{
SetLeft(std::min(Left(), rhs.Left()));
SetRight(std::max(Right(), rhs.Right()));
SetTop(std::min(Top(), rhs.Top()));
+147
View File
@@ -0,0 +1,147 @@
#ifndef GUI_BUTTON_H__
#define GUI_BUTTON_H__
#include "GUI/TextureFrame.h"
#include "GUI/EButtonEnter.h"
#include "GUI/EButtonLeave.h"
#include "GUI/EButtonPress.h"
#include "GUI/EButtonRelease.h"
#include "Core/EMouseMove.h"
#include "Core/EMousePress.h"
#include "Core/EMouseRelease.h"
namespace dd
{
namespace GUI
{
class Button : public TextureFrame
{
public:
Button(Frame* parent, std::string name)
: TextureFrame(parent, name)
{
EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &Button::OnMouseMove);
EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &Button::OnMousePress);
EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &Button::OnMouseRelease);
}
void SetTextureHover(std::string resourceName)
{
m_TextureHover = resourceName;
}
void SetTextureReleased(std::string resourceName)
{
m_TextureReleased = resourceName;
SetTexture(resourceName);
}
void SetTexturePressed(std::string resourceName)
{
m_TexturePressed = resourceName;
}
void Draw(RenderQueueCollection& rq) override
{
if (m_Texture == nullptr && !m_TextureReleased.empty()) {
SetTexture(m_TextureReleased);
}
TextureFrame::Draw(rq);
}
virtual void OnEnter() { }
virtual void OnLeave() { }
virtual void OnPress() { }
virtual void OnRelease() { }
protected:
bool m_MouseIsOver = false;
bool m_IsDown = false;
virtual bool OnMouseMove(const Events::MouseMove& event)
{
bool isOver = Rectangle::Intersects(AbsoluteRectangle(), Rectangle(event.X, event.Y, 1, 1));
if (isOver && !m_MouseIsOver) { // Enter
if (!m_IsDown) {
if (!m_TextureHover.empty()) {
SetTexture(m_TextureHover);
}
}
OnEnter();
Events::ButtonEnter e;
e.FrameName = m_Name;
EventBroker->Publish(e);
} else if (!isOver && m_MouseIsOver) { // Leave
if (!m_IsDown) {
if (!m_TextureReleased.empty()) {
SetTexture(m_TextureReleased);
}
}
OnLeave();
Events::ButtonLeave e;
e.FrameName = m_Name;
EventBroker->Publish(e);
}
m_MouseIsOver = isOver;
return true;
}
virtual bool OnMousePress(const Events::MousePress& event)
{
if (!Rectangle::Intersects(AbsoluteRectangle(), Rectangle(event.X, event.Y, 1, 1))) {
return false;
}
if (!m_TexturePressed.empty()) {
SetTexture(m_TexturePressed);
}
m_IsDown = true;
OnPress();
Events::ButtonPress e;
e.FrameName = m_Name;
e.Button = this;
EventBroker->Publish(e);
return true;
}
virtual bool OnMouseRelease(const Events::MouseRelease& event)
{
bool isOver = Rectangle::Intersects(AbsoluteRectangle(), Rectangle(event.X, event.Y, 1, 1));
if (!isOver && !m_IsDown) {
return false;
}
if (m_MouseIsOver) {
if (!m_TextureHover.empty()) {
SetTexture(m_TextureHover);
}
} else {
if (!m_TextureReleased.empty()) {
SetTexture(m_TextureReleased);
}
}
m_IsDown = false;
OnRelease();
Events::ButtonRelease e;
e.FrameName = m_Name;
e.Button = this;
EventBroker->Publish(e);
return true;
}
private:
EventRelay<Frame, Events::MouseMove> m_EMouseMove;
EventRelay<Frame, Events::MousePress> m_EMousePress;
EventRelay<Frame, Events::MouseRelease> m_EMouseRelease;
std::string m_TextureHover;
std::string m_TexturePressed;
std::string m_TextureReleased;
};
}
}
#endif
+40
View File
@@ -0,0 +1,40 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Events_ButtonEnter_h__
#define Events_ButtonEnter_h__
#include "Core/EventBroker.h"
namespace dd
{
namespace Events
{
/** Thrown on GUI button hover. */
struct ButtonEnter : Event
{
std::string FrameName;
};
}
}
#endif // Events_ButtonEnter_h__
+40
View File
@@ -0,0 +1,40 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Events_ButtonLeave_h__
#define Events_ButtonLeave_h__
#include "Core/EventBroker.h"
namespace dd
{
namespace Events
{
/** Thrown on GUI button hover. */
struct ButtonLeave : Event
{
std::string FrameName;
};
}
}
#endif // Events_ButtonLeave_h__
+43
View File
@@ -0,0 +1,43 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Events_ButtonPress_h__
#define Events_ButtonPress_h__
#include "Core/EventBroker.h"
namespace dd
{
namespace GUI { class Button; }
namespace Events
{
/** Thrown on GUI button press. */
struct ButtonPress : Event
{
std::string FrameName;
GUI::Button* Button;
};
}
}
#endif // Events_ButtonPress_h__
+43
View File
@@ -0,0 +1,43 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Events_ButtonRelease_h__
#define Events_ButtonRelease_h__
#include "Core/EventBroker.h"
namespace dd
{
namespace GUI { class Button; }
namespace Events
{
/** Thrown on GUI button release. */
struct ButtonRelease : Event
{
std::string FrameName;
GUI::Button* Button;
};
}
}
#endif // Events_ButtonRelease_h__
+45
View File
@@ -0,0 +1,45 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Events_SliderUpdate_h__
#define Events_SliderUpdate_h__
#include "Core/EventBroker.h"
namespace dd
{
namespace GUI { class Slider; }
namespace Events
{
/** Thrown when GUI slider is moved. */
struct SliderUpdate : Event
{
std::string FrameName;
GUI::Slider* Slider;
float Percentage;
float DeltaPercentage;
};
}
}
#endif // Events_SliderUpdate_h__
+264
View File
@@ -0,0 +1,264 @@
#ifndef GUI_Frame_h__
#define GUI_Frame_h__
#include <memory>
#include <map>
#include "Core/Util/Rectangle.h"
#include "Core/EventBroker.h"
#include "Core/EKeyDown.h"
#include "Core/EKeyUp.h"
#include "Core/ResourceManager.h"
#include "Core/Renderer.h"
#include "Core/RenderQueue.h"
#include "Core/Texture.h"
#include "Input/EInputCommand.h"
namespace dd
{
namespace GUI
{
class Frame : public Rectangle
{
public:
enum class Anchor
{
Left,
Right,
Top,
Bottom
};
static const int BaseWidth = 1280;
static const int BaseHeight = 720;
// Set up a base frame with an event broker
Frame(dd::EventBroker* eventBroker)
: EventBroker(eventBroker)
, BaseFrame(this)
, Rectangle() { }
// Create a frame as a child
Frame(Frame* parent, std::string name)
: m_Name(name)
{
SetParent(parent);
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Frame::OnKeyDown);
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Frame::OnKeyUp);
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Frame::OnCommand);
}
~Frame()
{
/*for (auto layer : m_Children)
{
for (auto child : layer.second)
{
delete child.second;
}
}
if (m_Parent)
{
m_Parent->RemoveChild(this);
}*/
}
Frame* Parent() const { return m_Parent; }
void SetParent(Frame* parent)
{
if (parent == nullptr) {
LOG_ERROR("Failed to parent frame \"%s\": Invalid parent", m_Name.c_str());
return;
}
m_Layer = parent->Layer() + 1;
parent->AddChild(this);
m_Parent = parent;
EventBroker = parent->EventBroker;
BaseFrame = parent->BaseFrame;
}
void AddChild(Frame* child)
{
m_Children[child->m_Layer].insert(std::make_pair(child->Name(), child));
if (m_Parent) {
m_Parent->AddChild(child);
}
}
void RemoveChild(Frame* child)
{
auto it = m_Children.find(child->m_Layer);
if (it != m_Children.end()) {
m_Children.erase(it);
}
if (m_Parent) {
m_Parent->RemoveChild(child);
}
}
std::string Name() const { return m_Name; }
void SetName(std::string val) { m_Name = val; }
int Layer() const { return m_Layer; }
bool Hidden() const
{
if (m_Parent)
return m_Parent->Hidden() || m_Hidden;
else
return m_Hidden;
}
bool Visible() const
{
return !Hidden();
}
virtual void Hide() { m_Hidden = true; }
virtual void Show() { m_Hidden = false; }
int Left() const override
{
if (m_Parent)
return m_Parent->Left() + X;
else
return X;
}
void SetLeft(int absLeft) override
{
if (m_Parent) {
X = absLeft - m_Parent->Left();
} else {
X = absLeft;
}
}
int Right() const override
{
return Left() + Width;
}
void SetRight(int absRight) override
{
if (m_Parent) {
X = absRight - Width - m_Parent->Left();
} else {
X = absRight - Width;
}
}
int Top() const override
{
if (m_Parent)
return m_Parent->Top() + Y;
else
return Y;
}
void SetTop(int absTop) override
{
if (m_Parent) {
Y = absTop - m_Parent->Top();
} else {
Y = absTop;
}
}
int Bottom() const override
{
return Top() + Height;
}
void SetBottom(int absBottom) override
{
if (m_Parent) {
Y = absBottom - Height - m_Parent->Top();
} else {
Y = absBottom - Height;
}
}
glm::vec2 Scale()
{
if (m_Parent)
return m_Parent->Scale();
else
return glm::vec2(Width, Height) / glm::vec2(BaseWidth, BaseHeight);
}
Rectangle AbsoluteRectangle()
{
int left = Left();
if (m_Parent)
left = std::max(left, m_Parent->Left());
int top = Top();
if (m_Parent)
top = std::max(top, m_Parent->Top());
int width = Right() - left;
int height = Bottom() - top;
return Rectangle(left, top, width, height);
}
void UpdateLayered(double dt)
{
// Update ourselves
this->Update(dt);
// Update children
for (auto& pairLayer : m_Children) {
auto children = pairLayer.second;
for (auto& pairChild : children) {
auto child = pairChild.second;
child->Update(dt);
}
}
}
virtual void Update(double dt) { }
void DrawLayered(RenderQueueCollection& rq)
{
if (this->Hidden())
return;
// Draw ourselves
this->Draw(rq);
// Draw children
for (auto& pairLayer : m_Children) {
auto children = pairLayer.second;
for (auto& pairChild : children) {
auto child = pairChild.second;
if (child->Hidden())
continue;
child->Draw(rq);
}
}
}
virtual void Draw(RenderQueueCollection& rq) { }
protected:
dd::EventBroker* EventBroker;
Frame* BaseFrame = nullptr;
std::string m_Name = "UIParent";
int m_Layer = 0;
bool m_Hidden = false;
Frame* m_Parent = nullptr;
typedef std::multimap<std::string, Frame*> Children_t; // name -> frame
std::map<int, Children_t> m_Children; // layer -> Children_t
virtual bool OnKeyDown(const Events::KeyDown& event) { return false; }
virtual bool OnKeyUp(const Events::KeyUp& event) { return false; }
virtual bool OnCommand(const Events::InputCommand& event) { return false; }
private:
EventRelay<Frame, Events::KeyDown> m_EKeyDown;
EventRelay<Frame, Events::KeyUp> m_EKeyUp;
EventRelay<Frame, Events::InputCommand> m_EInputCommand;
};
}
}
#endif // GUI_Frame_h__
+109
View File
@@ -0,0 +1,109 @@
#ifndef GUI_NUMBERFRAME_H__
#define GUI_NUMBERFRAME_H__
#include "GUI/TextureFrame.h"
namespace dd
{
namespace GUI
{
class NumberFrame : public Frame
{
public:
NumberFrame(Frame* parent, std::string name)
: Frame(parent, name)
{
m_NumberTextures[0] = "Textures/GUI/Numbers/N0.png";
m_NumberTextures[1] = "Textures/GUI/Numbers/N1.png";
m_NumberTextures[2] = "Textures/GUI/Numbers/N2.png";
m_NumberTextures[3] = "Textures/GUI/Numbers/N3.png";
m_NumberTextures[4] = "Textures/GUI/Numbers/N4.png";
m_NumberTextures[5] = "Textures/GUI/Numbers/N5.png";
m_NumberTextures[6] = "Textures/GUI/Numbers/N6.png";
m_NumberTextures[7] = "Textures/GUI/Numbers/N7.png";
m_NumberTextures[8] = "Textures/GUI/Numbers/N8.png";
m_NumberTextures[9] = "Textures/GUI/Numbers/N9.png";
// Create number frame if missing
for (int i = 0; i < 10; i++) {
auto frame = new GUI::TextureFrame(this, "NumberFrameDigit");
frame->SetTexture(m_NumberTextures[0]);
frame->DisableScissor();
// Position frame based on alignment
if (i > 0) {
frame->X = i * 20;
}
Width += frame->Width;
if (Height < frame->Height) {
Height = frame->Height;
}
m_NumberFrames.push_front(frame);
}
SetNumber(0);
}
int Number() const { return m_Number; }
void SetNumber(int number)
{
m_Number = number;
// Clear number textures
//for (auto& frame : m_NumberFrames) {
// frame->SetTexture("");
//}
//Width = 0;
//Height = 0;
int n = 0;
do {
// // Create number frame if missing
// if (m_NumberFrames.size() <= n) {
// auto frame = new GUI::TextureFrame(this, "NumberFrameDigit");
// frame->DisableScissor();
// m_NumberFrames.push_front(frame);
// }
// Update value
int digit = number % 10;
m_NumberFrames[n]->SetTexture(m_NumberTextures[digit]);
//Width += m_NumberFrames[n]->Width;
//if (Height < m_NumberFrames[n]->Height) {
// Height = m_NumberFrames[n]->Height;
//}
// // Position frame based on alignment
// if (n > 0) {
// if (!m_LeftAligned) {
// m_NumberFrames[n]->SetLeft(m_NumberFrames[n - 1]->Right());
// } else {
// m_NumberFrames[n]->SetRight(m_NumberFrames[n - 1]->Left());
// }
// }
number /= 10;
n++;
} while (number > 0);
}
void SetLeftAlign() { m_LeftAligned = true; }
void SetRightAlign() { m_LeftAligned = false; }
virtual void Update(double dt) override
{
}
private:
int m_Number = 0;
bool m_LeftAligned = true;
std::string m_NumberTextures[10];
std::deque<TextureFrame*> m_NumberFrames;
};
}
}
#endif
+124
View File
@@ -0,0 +1,124 @@
#ifndef GUI_SLIDER_H__
#define GUI_SLIDER_H__
#include "GUI/TextureFrame.h"
#include "GUI/Button.h"
#include "GUI/EButtonPress.h"
#include "GUI/EButtonRelease.h"
#include "GUI/ESliderUpdate.h"
#include "Core/EMouseMove.h"
namespace dd
{
namespace GUI
{
class Slider : public TextureFrame
{
public:
Slider(Frame* parent, std::string name)
: TextureFrame(parent, name)
{
m_SliderButton = new GUI::Button(this, "SliderButton");
m_SliderButton->DisableScissor();
EVENT_SUBSCRIBE_MEMBER(m_EButtonPress, &Slider::OnButtonPress);
EVENT_SUBSCRIBE_MEMBER(m_EButtonRelease, &Slider::OnButtonRelease);
EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &Slider::OnMouseMove);
}
float Percentage() const { return m_Percentage; }
void SetPercentage(const float percentage)
{
m_Percentage = percentage;
m_SliderButton->X = percentage * (Width - m_SliderButton->Width);
}
void SetTextureHover(std::string resourceName)
{
m_SliderButton->SetTextureHover(resourceName);
}
void SetTextureReleased(std::string resourceName)
{
m_SliderButton->SetTextureReleased(resourceName);
}
void SetTexturePressed(std::string resourceName)
{
m_SliderButton->SetTexturePressed(resourceName);
}
void AlignVertically()
{
m_SliderButton->Y = (Height / 2) - (m_SliderButton->Height / 2);
}
~Slider()
{
if (m_SliderButton != nullptr) {
delete m_SliderButton;
m_SliderButton = nullptr;
}
}
virtual void Update(double dt) override
{
}
private:
Button* m_SliderButton = nullptr;
bool m_IsGrabbed = false;
float m_Percentage = 0.f;
EventRelay<Frame, Events::ButtonPress> m_EButtonPress;
EventRelay<Frame, Events::ButtonRelease> m_EButtonRelease;
EventRelay<Frame, Events::MouseMove> m_EMouseMove;
virtual bool OnButtonPress(const Events::ButtonPress& event)
{
if (event.Button == m_SliderButton) {
m_IsGrabbed = true;
}
return true;
}
virtual bool OnButtonRelease(const Events::ButtonRelease& event)
{
if (event.Button == m_SliderButton) {
m_IsGrabbed = false;
}
return true;
}
virtual bool OnMouseMove(const Events::MouseMove& event)
{
if (m_IsGrabbed) {
// Set horizontal position
m_SliderButton->X += event.DeltaX;
if (m_SliderButton->Left() < Left()) {
m_SliderButton->SetLeft(Left());
} else if (m_SliderButton->Right() > Right()) {
m_SliderButton->SetRight(Right());
}
// Update percentage
float newPercentage = m_SliderButton->X / (float)(Width - m_SliderButton->Width);
Events::SliderUpdate e;
e.FrameName = Name();
e.Slider = this;
e.Percentage = newPercentage;
e.DeltaPercentage = newPercentage - m_Percentage;
EventBroker->Publish(e);
m_Percentage = newPercentage;
}
return true;
}
};
}
}
#endif
+111
View File
@@ -0,0 +1,111 @@
#ifndef GUI_TextureFrame_h__
#define GUI_TextureFrame_h__
#include "GUI/Frame.h"
#include "Core/Texture.h"
namespace dd
{
namespace GUI
{
class TextureFrame : public Frame
{
public:
TextureFrame(Frame* parent, std::string name)
: Frame(parent, name) { }
void EnableScissor() { m_ScissorEnabled = true; }
void DisableScissor() { m_ScissorEnabled = false; }
void Draw(RenderQueueCollection& rq) override
{
if (m_Texture == nullptr)
return;
// Main texture
{
FrameJob job;
job.Scissor = (m_ScissorEnabled) ? m_Parent->AbsoluteRectangle() : Rectangle();
job.Viewport = Rectangle(Left(), Top(), Width, Height);
job.TextureID = m_Texture->ResourceID;
job.DiffuseTexture = *m_Texture;
job.Color = glm::vec4(m_Color.r, m_Color.g, m_Color.b, m_Color.a * m_CurrentFade);
job.Name = Name();
rq.GUI.Add(job);
}
// Texture while fading
if (m_FadeTexture && m_CurrentFade < 1) {
FrameJob job;
job.Scissor = (m_ScissorEnabled) ? m_Parent->AbsoluteRectangle() : Rectangle();
job.Viewport = Rectangle(Left(), Top(), Width, Height);
job.TextureID = m_FadeTexture->ResourceID;
job.DiffuseTexture = *m_FadeTexture;
job.Color = glm::vec4(m_Color.r, m_Color.g, m_Color.b, m_Color.a);
job.Name = Name();
rq.GUI.Add(job);
}
}
dd::Texture* Texture() const { return m_Texture; }
void SetTexture(std::string resourceName)
{
if (resourceName.empty()) {
m_Texture = nullptr;
return;
}
m_Texture = ResourceManager::Load<dd::Texture>(resourceName);
if (m_Texture == nullptr) {
m_Texture = ResourceManager::Load<dd::Texture>("Textures/Core/ErrorTexture.png");
}
SizeToTexture();
}
void SizeToTexture()
{
if (m_Texture != nullptr) {
this->Width = m_Texture->Width;
this->Height = m_Texture->Height;
}
}
void FadeToTexture(std::string resourceName, double duration)
{
m_FadeTexture = m_Texture;
SetTexture(resourceName);
m_FadeDuration = duration;
m_CurrentFade = 0.f;
}
void Update(double dt) override
{
if (m_CurrentFade < 1) {
m_CurrentFade += dt / m_FadeDuration;
if (m_CurrentFade > 1) {
m_FadeTexture = nullptr;
m_CurrentFade = 1;
m_FadeDuration = 0;
}
}
}
glm::vec4 Color() const { return m_Color; }
void SetColor(glm::vec4 val) { m_Color = val; }
protected:
bool m_ScissorEnabled = true;
dd::Texture* m_Texture = nullptr;
dd::Texture* m_FadeTexture = nullptr;
glm::vec4 m_Color = glm::vec4(1.f, 1.f, 1.f, 1.f);
float m_FadeDuration = 0.f;
float m_CurrentFade = 1.f;
};
}
}
#endif // GUI_TextureFrame_h__
+25
View File
@@ -0,0 +1,25 @@
//
// Created by Adam on 2015-09-25.
//
#ifndef EVENTS_EGAMESTART_H__
#define EVENTS_EGAMESTART_H__
#include "Core/EventBroker.h"
namespace dd
{
namespace Events
{
struct GameStart : public Event
{
};
}
}
#endif
+53
View File
@@ -0,0 +1,53 @@
#ifndef GUI_HUD_H__
#define GUI_HUD_H__
#include "GUI/Frame.h"
#include "GUI/TextureFrame.h"
#include "GUI/NumberFrame.h"
#include "Core/EKeyUp.h"
#include "Game/EScoreEvent.h"
namespace dd
{
namespace GUI
{
class HUD : public Frame
{
public:
HUD(Frame* parent, std::string name)
: Frame(parent, name)
{
Width = parent->Width;
Height = parent->Height;
m_LevelIndicator = new GUI::TextureFrame(this, "HUDLevelIndicator");
m_LevelIndicator->SetTexture("Textures/GUI/HUD/LevelIndicatorBG.png");
m_ScoreIndicator = new GUI::TextureFrame(this, "HUDScoreIndicator");
m_ScoreIndicator->SetTexture("Textures/GUI/HUD/ScoreIndicatorBG.png");
m_ScoreIndicator->SetRight(Right());
m_ScoreNumberFrame = new GUI::NumberFrame(m_ScoreIndicator, "HUDScoreNumberFrame");
m_ScoreNumberFrame->X = 15;
m_ScoreNumberFrame->Y = 21;
EVENT_SUBSCRIBE_MEMBER(m_EScore, &HUD::OnScore);
}
private:
TextureFrame* m_LevelIndicator = nullptr;
TextureFrame* m_ScoreIndicator = nullptr;
NumberFrame* m_ScoreNumberFrame = nullptr;
EventRelay<Frame, Events::ScoreEvent> m_EScore;
bool OnScore(const Events::ScoreEvent& event)
{
m_ScoreNumberFrame->SetNumber(m_ScoreNumberFrame->Number() + event.Score);
}
};
}
}
#endif
+1 -1
View File
@@ -19,12 +19,12 @@
#include "Game/ELifeLost.h"
#include "Game/EResetBall.h"
#include "Game/EScoreEvent.h"
#include "Physics/CRectangleShape.h"
#include "Game/EMultiBall.h"
#include "Game/EGameOver.h"
#include "Game/ECreatePowerUp.h"
#include "Game/EPowerUpTaken.h"
#include "Game/Bricks/CPowerUpBrick.h"
#include "Physics/CBoxShape.h"
#include "Physics/CPhysics.h"
#include "Physics/CCircleShape.h"
#include "Physics/ESetImpulse.h"
+59
View File
@@ -0,0 +1,59 @@
#ifndef GUI_MAINMENU_H__
#define GUI_MAINMENU_H__
#include "GUI/TextureFrame.h"
#include "Game/MainMenuMain.h"
#include "Game/MainMenuOptions.h"
#include "Core/EKeyUp.h"
namespace dd
{
namespace GUI
{
class MainMenu : public TextureFrame
{
public:
MainMenu(Frame* parent, std::string name)
: TextureFrame(parent, name)
{
SetTexture("Textures/GUI/Menu/Background.png");
Width = 675;
Height = 1080;
m_LeftBanner = new GUI::TextureFrame(this, "MainMenuLeftBanner");
m_LeftBanner->SetTexture("Textures/GUI/Menu/Left.png");
m_LeftBanner->Width = 231;
m_LeftBanner->Height = 1080;
m_MainMenuMain = new GUI::MainMenuMain(this, "MainMenuMain");
m_MainMenuMain->SetLeft(m_LeftBanner->Right());
m_MainMenuMain->SetTop(60);
m_MainMenuOptions = new GUI::MainMenuOptions(this, "MainMenuOptions");
m_MainMenuOptions->SetLeft(m_LeftBanner->Right());
m_MainMenuOptions->SetTop(m_MainMenuMain->Bottom());
//EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &MainMenu::OnKeyUp);
}
private:
TextureFrame* m_LeftBanner;
MainMenuMain* m_MainMenuMain;
MainMenuOptions* m_MainMenuOptions;
//EventRelay<Frame, Events::KeyUp> m_EKeyUp;
bool OnKeyUp(const Events::KeyUp& event)
{
if (event.KeyCode == GLFW_KEY_ESCAPE) {
Show();
}
return true;
}
};
}
}
#endif
+72
View File
@@ -0,0 +1,72 @@
#ifndef GUI_MAINMENUMAIN_H__
#define GUI_MAINMENUMAIN_H__
#include "GUI/TextureFrame.h"
#include "GUI/Button.h"
#include "Game/EGameStart.h"
#include "Game/HUD.h"
namespace dd
{
namespace GUI
{
class MainMenuMain : public TextureFrame
{
public:
MainMenuMain(Frame* parent, std::string name)
: TextureFrame(parent, name)
{
Width = 409;
Height = 543;
m_Title = new GUI::TextureFrame(this, "MainMenuMainTitle");
m_Title->SetTexture("Textures/GUI/Menu/MainTitle.png");
m_SquidoutButton = new GUI::Button(this, "MainMenuMainSquidoutButton");
m_SquidoutButton->SetTop(m_Title->Bottom());
m_SquidoutButton->SetTextureReleased("Textures/GUI/Menu/MainSquidout.png");
m_SquidoutButton->SetTextureHover("Textures/GUI/Menu/MainSquidoutHover.png");
m_SquidoutButton->SetTexturePressed("Textures/GUI/Menu/MainSquidoutClick.png");
m_SquidoutButton->SizeToTexture();
m_TimeAttackButton = new GUI::Button(this, "MainMenuMainTimeAttackButton");
m_TimeAttackButton->SetTop(m_SquidoutButton->Bottom());
m_TimeAttackButton->SetTextureReleased("Textures/GUI/Menu/MainTimeAttack.png");
m_TimeAttackButton->SetTextureHover("Textures/GUI/Menu/MainTimeAttackHover.png");
m_TimeAttackButton->SetTexturePressed("Textures/GUI/Menu/MainTimeAttackClick.png");
m_TimeAttackButton->SizeToTexture();
m_OptionsButton = new GUI::Button(this, "MainMenuMainOptionsButton");
m_OptionsButton->SetTop(m_TimeAttackButton->Bottom());
m_OptionsButton->SetTextureReleased("Textures/GUI/Menu/MainOptions.png");
m_OptionsButton->SetTextureHover("Textures/GUI/Menu/MainOptionsHover.png");
m_OptionsButton->SetTexturePressed("Textures/GUI/Menu/MainOptionsClick.png");
m_OptionsButton->SizeToTexture();
EVENT_SUBSCRIBE_MEMBER(m_EButtonRelease, &MainMenuMain::OnButtonRelease);
}
private:
TextureFrame* m_Title;
Button* m_SquidoutButton;
Button* m_TimeAttackButton;
Button* m_OptionsButton;
EventRelay<Frame, Events::ButtonRelease> m_EButtonRelease;
virtual bool OnButtonRelease(const Events::ButtonRelease& event)
{
if (event.Button == m_SquidoutButton) {
Events::GameStart e;
EventBroker->Publish(e);
m_Parent->Hide();
auto hud = new GUI::HUD(BaseFrame, "HUD");
}
return true;
}
};
}
}
#endif
+82
View File
@@ -0,0 +1,82 @@
#ifndef GUI_MAINMENUOPTIONS_H__
#define GUI_MAINMENUOPTIONS_H__
#include "GUI/TextureFrame.h"
#include "GUI/Button.h"
#include "GUI/Slider.h"
#include "GUI/ESliderUpdate.h"
#include "Sound/EMasterVolume.h"
namespace dd
{
namespace GUI
{
class MainMenuOptions : public TextureFrame
{
public:
MainMenuOptions(Frame* parent, std::string name)
: TextureFrame(parent, name)
{
Width = 409;
Height = 1080;
m_Title = new GUI::TextureFrame(this, "MainMenuOptionsTitle");
m_Title->SetTexture("Textures/GUI/Menu/OptionsTitle.png");
m_TitleSFX = new GUI::TextureFrame(this, "MainMenuOptionsSFXTitle");
m_TitleSFX->SetTop(m_Title->Bottom() + 20);
m_TitleSFX->SetTexture("Textures/GUI/Menu/OptionsSFXTitle.png");
m_SFXSlider = new GUI::Slider(this, "MainMenuOptionsSFXSlider");
m_SFXSlider->X = 66;
m_SFXSlider->SetTop(m_TitleSFX->Bottom() + 10);
m_SFXSlider->SetTexture("Textures/GUI/Menu/SliderBackground.png");
m_SFXSlider->SetTextureReleased("Textures/GUI/Menu/SliderHandle.png");
m_SFXSlider->AlignVertically();
m_SFXSlider->SetPercentage(1.f);
m_TitleBGM = new GUI::TextureFrame(this, "MainMenuOptionsBGMTitle");
m_TitleBGM->SetTop(m_SFXSlider->Bottom() + 20);
m_TitleBGM->SetTexture("Textures/GUI/Menu/OptionsBGMTitle.png");
m_BGMSlider = new GUI::Slider(this, "MainMenuOptionsBGMSlider");
m_BGMSlider->X = 66;
m_BGMSlider->SetTop(m_TitleBGM->Bottom() + 10);
m_BGMSlider->SetTexture("Textures/GUI/Menu/SliderBackground.png");
m_BGMSlider->SetTextureReleased("Textures/GUI/Menu/SliderHandle.png");
m_BGMSlider->AlignVertically();
m_BGMSlider->SetPercentage(1.f);
EVENT_SUBSCRIBE_MEMBER(m_ESliderUpdate, &MainMenuOptions::OnSliderUpdate);
}
private:
TextureFrame* m_Title = nullptr;
TextureFrame* m_TitleSFX = nullptr;
Slider* m_SFXSlider = nullptr;
Slider* m_BGMSlider = nullptr;
TextureFrame* m_TitleBGM = nullptr;
EventRelay<Frame, Events::SliderUpdate> m_ESliderUpdate;
bool OnSliderUpdate(const Events::SliderUpdate& event)
{
if (event.Slider == m_SFXSlider) {
Events::MasterVolume e;
e.isAmbient = false;
e.gain = event.Percentage;
EventBroker->Publish(e);
}
if (event.Slider == m_BGMSlider) {
Events::MasterVolume e;
e.isAmbient = true;
e.gain = event.Percentage;
EventBroker->Publish(e);
}
return true;
}
};
}
}
#endif
+1 -1
View File
@@ -14,7 +14,7 @@
#include "Core/EKeyUp.h"
#include "Input/EBindKey.h"
#include "Physics/EContact.h"
#include "Physics/CBoxShape.h"
#include "Physics/CRectangleShape.h"
#include "Physics/CPhysics.h"
#include "Physics/CCircleShape.h"
#include "Physics/ESetImpulse.h"
+25 -2
View File
@@ -4,17 +4,40 @@
namespace dd
{
namespace CollisionLayer
{
enum Type
{
Pad = 1 << 0,
Ball = 1 << 1,
Brick = 1 << 2,
Water = 1 << 3,
PowerUp = 1 << 4,
Other = 1 << 5,
Wall = 1 << 6
};
}
namespace Components
{
struct Physics: public Component
{
//idk if anything should be here :)
bool Static = true;
bool Calculate = false;
float GravityScale = 0.f;
CollisionLayer::Type Category = CollisionLayer::Type::Other;
CollisionLayer::Type Mask =
CollisionLayer::Type::Pad |
CollisionLayer::Type::Ball |
CollisionLayer::Type::Brick |
CollisionLayer::Type::Water |
CollisionLayer::Type::PowerUp |
CollisionLayer::Type::Wall |
CollisionLayer::Type::Other;
};
}
}
#endif //DAYDREAM_CPHYSICS_H
+19
View File
@@ -0,0 +1,19 @@
#ifndef CWATERVOLUME_H
#define CWATERVOLUME_H
#include "Core/Component.h"
namespace dd
{
namespace Components
{
struct WaterVolume : public Component
{
};
}
}
#endif
+20 -5
View File
@@ -5,7 +5,7 @@
#include "Core/System.h"
#include "Core/World.h"
#include "Physics/CBoxShape.h"
#include "CRectangleShape.h"
#include "Physics/CPhysics.h"
#include <Box2D/Box2D.h>
#include "Core/CTransform.h"
@@ -15,6 +15,9 @@
#include "Physics/CCircleShape.h"
#include "Core/EventBroker.h"
#include "Game/CPad.h"
#include "Physics/CWaterVolume.h"
#include "Game/CBall.h"
#include "Rendering/CSprite.h"
namespace dd
@@ -64,6 +67,15 @@ private:
void CreateBody(EntityID entity);
b2ParticleSystem *m_ParticleSystem;
b2ParticleGroup* t_watergroup;
std::unordered_map<EntityID, const b2ParticleHandle*> m_EntitiesToParticleHandle;
std::unordered_map<const b2ParticleHandle*, EntityID> m_ParticleHandleToEntities;
void InitializeWater();
void SyncWater(); //TODO: Probably remove this
void CreateParticleGroup(EntityID entity);
struct Impulse
{
@@ -102,16 +114,19 @@ private:
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
EntityID entityA = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
EntityID entityB = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
EntityID entityB = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()];
auto physicsComponentA = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityA);
auto physicsComponentB = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityB);
if (physicsComponentA != nullptr || physicsComponentB != nullptr) {
// Turn of collisions
contact->SetEnabled(false);
if (physicsComponentA != nullptr && physicsComponentB != nullptr) {
if (physicsComponentA->Calculate || physicsComponentB->Calculate) {
// Turn of collisions
contact->SetEnabled(false);
}
}
}
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{
+149
View File
@@ -0,0 +1,149 @@
#ifndef DAYDREAM_PHYSICSSYSTEM_H
#define DAYDREAM_PHYSICSSYSTEM_H
#include <unordered_map>
#include "Core/System.h"
#include "Core/World.h"
#include "CRectangleShape.h"
#include "Physics/CPhysics.h"
#include <Box2D/Box2D.h>
#include "Core/CTransform.h"
#include "Transform/TransformSystem.h"
#include "Physics/EContact.h"
#include "Physics/ESetImpulse.h"
#include "Physics/CCircleShape.h"
#include "Core/EventBroker.h"
#include "Game/CPad.h"
<<<<<<< HEAD
#include "Game/CBall.h"
=======
#include "Physics/CWaterVolume.h"
#include "Rendering/CSprite.h"
>>>>>>> origin/master
namespace dd
{
namespace Systems
{
class PhysicsSystem : public System
{
friend class ContractListener;
public:
PhysicsSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
: System(world, eventBroker) {}
~PhysicsSystem();
EventRelay<PhysicsSystem, Events::SetImpulse> m_SetImpulse;
bool SetImpulse(const Events::SetImpulse &event);
void RegisterComponents(ComponentFactory* cf) override;
void Initialize() override;
// Called once per system every tick
void Update(double dt) override;
// Called once for every entity in the world every tick
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
// Called when components are committed to an entity
void OnEntityCommit(EntityID entity) override;
// Called when an entity is removed
void OnEntityRemoved(EntityID entity) override;
private:
b2Vec2 m_Gravity;
b2World* m_PhysicsWorld;
float m_TimeStep;
float m_Accumulator;
int m_VelocityIterations, m_PositionIterations;
std::unordered_map<EntityID, b2Body*> m_EntitiesToBodies;
std::unordered_map<b2Body*, EntityID> m_BodiesToEntities;
void CreateBody(EntityID entity);
b2ParticleSystem *m_ParticleSystem;
b2ParticleGroup* t_watergroup;
std::unordered_map<EntityID, const b2ParticleHandle*> m_EntitiesToParticleHandle;
std::unordered_map<const b2ParticleHandle*, EntityID> m_ParticleHandleToEntities;
void InitializeWater();
void SyncWater(); //TODO: Probably remove this
void CreateParticleGroup(EntityID entity);
struct Impulse
{
b2Body* Body;
b2Vec2 Impulse;
b2Vec2 Point;
};
std::list<Impulse> m_Impulses;
class ContactListener : public b2ContactListener
{
public:
ContactListener(PhysicsSystem* physicsSystem)
: m_PhysicsSystem(physicsSystem) { }
void BeginContact(b2Contact* contact)
{
b2WorldManifold worldManifold;
contact->GetWorldManifold(&worldManifold);
Events::Contact e;
e.Entity1 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
e.Entity2 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()];
e.Normal = glm::normalize(glm::vec2(contact->GetManifold()->localNormal.x, contact->GetManifold()->localNormal.y));
e.SignificantNormal = glm::normalize((glm::abs(e.Normal.x) > glm::abs(e.Normal.y)) ? glm::vec2(e.Normal.x, 0) : glm::vec2(0, e.Normal.y));
m_PhysicsSystem->EventBroker->Publish(e);
}
void EndContact(b2Contact* contact)
{
}
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
EntityID entityA = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
EntityID entityB = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()];
auto physicsComponentA = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityA);
auto physicsComponentB = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityB);
if (physicsComponentA != nullptr && physicsComponentB != nullptr) {
if (physicsComponentA->Calculate || physicsComponentB->Calculate) {
// Turn of collisions
contact->SetEnabled(false);
}
}
}
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{
}
private:
PhysicsSystem* m_PhysicsSystem;
};
ContactListener* m_ContactListener;
};
}
}
#endif //DAYDREAM_PHYSICSSYSTEM_H
+27
View File
@@ -0,0 +1,27 @@
//
// Created by Adam on 2015-09-24.
//
#ifndef EVENTS_EMASTERVOLUME_H__
#define EVENTS_EMASTERVOLUME_H__
#include "Core/EventBroker.h"
namespace dd
{
namespace Events
{
struct MasterVolume : public Event
{
float gain = 1;
//To determine what channel group to apply the change to.
bool isAmbient = false;
};
}
}
#endif
+1 -1
View File
@@ -14,7 +14,7 @@ struct PlaySound : Event
std::string path;
float volume = 1.f;
float pitch = 1.f;
bool loop = false;
bool isAmbient = false;
};
}
+6 -2
View File
@@ -10,6 +10,7 @@
#include "Sound.h"
#include "Sound/EPlaySound.h"
#include "Sound/EStopSound.h"
#include "Sound/EMasterVolume.h"
#include "Physics/EContact.h"
#include "Game/CBall.h"
#include "Game/CBrick.h"
@@ -38,17 +39,20 @@ private:
dd::EventRelay<SoundSystem, dd::Events::PlaySound> m_EPlaySFX;
dd::EventRelay<SoundSystem, dd::Events::Contact> m_EContact;
dd::EventRelay<SoundSystem, dd::Events::StopSound> m_EStopSound;
dd::EventRelay<SoundSystem, dd::Events::MasterVolume> m_EMasterVolume;
bool OnPlaySound(const dd::Events::PlaySound &event);
bool OnContact(const dd::Events::Contact &event);
bool OnStopSound(const dd::Events::StopSound &event);
bool OnMasterVolume(const dd::Events::MasterVolume &event);
ALuint CreateSource();
std::map<ALuint, Sound*> m_SourcesToBuffers;
std::map<ALuint, Sound*> m_BGMSourcesToBuffers;
std::map<ALuint, Sound*> m_SFXSourcesToBuffers;
ALCdevice* m_Device;
//Temp
float m_BGMMasterVolume, m_SFXMasterVolume;
};
}