Merge branch 'master' of github.com:teamfisk/TacticalZ
# Conflicts: # src/Game/Game.cpp
This commit is contained in:
@@ -17,119 +17,119 @@ class EventBroker;
|
||||
|
||||
class BaseEventRelay
|
||||
{
|
||||
friend class EventBroker;
|
||||
friend class EventBroker;
|
||||
|
||||
protected:
|
||||
BaseEventRelay(std::string contextTypeName, std::string eventTypeName)
|
||||
: m_ContextTypeName(contextTypeName)
|
||||
, m_EventTypeName(eventTypeName)
|
||||
, m_Broker(nullptr)
|
||||
{ }
|
||||
~BaseEventRelay();
|
||||
BaseEventRelay(std::string contextTypeName, std::string eventTypeName)
|
||||
: m_ContextTypeName(contextTypeName)
|
||||
, m_EventTypeName(eventTypeName)
|
||||
, m_Broker(nullptr)
|
||||
{ }
|
||||
~BaseEventRelay();
|
||||
|
||||
public:
|
||||
virtual bool Receive(const std::shared_ptr<Event> event) = 0;
|
||||
virtual bool Receive(const std::shared_ptr<Event> event) = 0;
|
||||
|
||||
protected:
|
||||
std::string m_ContextTypeName;
|
||||
std::string m_EventTypeName;
|
||||
EventBroker* m_Broker;
|
||||
std::string m_ContextTypeName;
|
||||
std::string m_EventTypeName;
|
||||
EventBroker* m_Broker;
|
||||
};
|
||||
|
||||
template <typename ContextType, typename EventType>
|
||||
class EventRelay : public BaseEventRelay
|
||||
{
|
||||
public:
|
||||
typedef std::function<bool(const EventType&)> CallbackType;
|
||||
typedef std::function<bool(const EventType&)> CallbackType;
|
||||
|
||||
EventRelay()
|
||||
: m_Callback(nullptr)
|
||||
, BaseEventRelay(typeid(ContextType).name(), typeid(EventType).name())
|
||||
{ }
|
||||
EventRelay(CallbackType callback)
|
||||
: m_Callback(callback)
|
||||
, BaseEventRelay(typeid(ContextType).name(), typeid(EventType).name())
|
||||
{ }
|
||||
EventRelay()
|
||||
: m_Callback(nullptr)
|
||||
, BaseEventRelay(typeid(ContextType).name(), typeid(EventType).name())
|
||||
{ }
|
||||
EventRelay(CallbackType callback)
|
||||
: m_Callback(callback)
|
||||
, BaseEventRelay(typeid(ContextType).name(), typeid(EventType).name())
|
||||
{ }
|
||||
|
||||
protected:
|
||||
bool Receive(const std::shared_ptr<Event> event) override;
|
||||
bool Receive(const std::shared_ptr<Event> event) override;
|
||||
|
||||
private:
|
||||
CallbackType m_Callback;
|
||||
CallbackType m_Callback;
|
||||
};
|
||||
|
||||
template <typename ContextType, typename EventType>
|
||||
bool EventRelay<ContextType, EventType>::Receive(const std::shared_ptr<Event> event)
|
||||
{
|
||||
if (m_Callback != nullptr) {
|
||||
return m_Callback(*static_cast<const EventType*>(event.get()));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
if (m_Callback != nullptr) {
|
||||
return m_Callback(*static_cast<const EventType*>(event.get()));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class EventBroker
|
||||
{
|
||||
template <typename ContextType, typename EventType> friend class EventRelay;
|
||||
template <typename ContextType, typename EventType> friend class EventRelay;
|
||||
|
||||
public:
|
||||
EventBroker()
|
||||
{
|
||||
m_EventQueueRead = std::make_shared<EventQueue_t>();
|
||||
m_EventQueueWrite = std::make_shared<EventQueue_t>();
|
||||
}
|
||||
EventBroker()
|
||||
{
|
||||
m_EventQueueRead = std::make_shared<EventQueue_t>();
|
||||
m_EventQueueWrite = std::make_shared<EventQueue_t>();
|
||||
}
|
||||
|
||||
void Subscribe(BaseEventRelay &relay);
|
||||
template <typename EventType>
|
||||
void Publish(const EventType &event);
|
||||
/*
|
||||
Process all events in a given context.
|
||||
Returns: Number of events processed
|
||||
*/
|
||||
template <typename ContextType>
|
||||
int Process();
|
||||
int Process(std::string contextTypeName);
|
||||
void Swap();
|
||||
void Clear();
|
||||
void Unsubscribe(BaseEventRelay &relay);
|
||||
void Subscribe(BaseEventRelay &relay);
|
||||
template <typename EventType>
|
||||
void Publish(const EventType &event);
|
||||
/*
|
||||
Process all events in a given context.
|
||||
Returns: Number of events processed
|
||||
*/
|
||||
template <typename ContextType>
|
||||
int Process();
|
||||
int Process(std::string contextTypeName);
|
||||
void Swap();
|
||||
void Clear();
|
||||
void Unsubscribe(BaseEventRelay &relay);
|
||||
|
||||
private:
|
||||
bool m_IsProcessing = false;
|
||||
bool m_IsProcessing = false;
|
||||
|
||||
typedef std::string ContextTypeName_t; // typeid(ContextType).name()
|
||||
typedef std::string EventTypeName_t; // typeid(EventType).name()
|
||||
typedef std::string ContextTypeName_t; // typeid(ContextType).name()
|
||||
typedef std::string EventTypeName_t; // typeid(EventType).name()
|
||||
|
||||
typedef std::unordered_multimap<EventTypeName_t, BaseEventRelay*> EventRelays_t;
|
||||
typedef std::unordered_map<ContextTypeName_t, EventRelays_t> ContextRelays_t;
|
||||
ContextRelays_t m_ContextRelays;
|
||||
std::vector<BaseEventRelay*> m_RelaysToSubscribe;
|
||||
std::vector<BaseEventRelay*> m_RelaysToUnsubscribe;
|
||||
typedef std::unordered_multimap<EventTypeName_t, BaseEventRelay*> EventRelays_t;
|
||||
typedef std::unordered_map<ContextTypeName_t, EventRelays_t> ContextRelays_t;
|
||||
ContextRelays_t m_ContextRelays;
|
||||
std::vector<BaseEventRelay*> m_RelaysToSubscribe;
|
||||
std::vector<BaseEventRelay*> m_RelaysToUnsubscribe;
|
||||
|
||||
typedef std::list<std::pair<EventTypeName_t, std::shared_ptr<Event>>> EventQueue_t;
|
||||
std::shared_ptr<EventQueue_t> m_EventQueueRead;
|
||||
std::shared_ptr<EventQueue_t> m_EventQueueWrite;
|
||||
typedef std::list<std::pair<EventTypeName_t, std::shared_ptr<Event>>> EventQueue_t;
|
||||
std::shared_ptr<EventQueue_t> m_EventQueueRead;
|
||||
std::shared_ptr<EventQueue_t> m_EventQueueWrite;
|
||||
|
||||
void subscribeImmediate(BaseEventRelay& relay);
|
||||
void unsubscribeImmediate(BaseEventRelay& relay);
|
||||
void subscribeImmediate(BaseEventRelay& relay);
|
||||
void unsubscribeImmediate(BaseEventRelay& relay);
|
||||
};
|
||||
|
||||
template <typename EventType>
|
||||
void EventBroker::Publish(const EventType &event)
|
||||
{
|
||||
/*auto itpair = m_Subscribers.equal_range(typeid(EventType).name());
|
||||
for (auto it = itpair.first; it != itpair.second; ++it)
|
||||
{
|
||||
it->second->Receive(event);
|
||||
}*/
|
||||
/*auto itpair = m_Subscribers.equal_range(typeid(EventType).name());
|
||||
for (auto it = itpair.first; it != itpair.second; ++it)
|
||||
{
|
||||
it->second->Receive(event);
|
||||
}*/
|
||||
|
||||
m_EventQueueWrite->push_back(std::make_pair(typeid(EventType).name(), std::shared_ptr<EventType>(new EventType(event))));
|
||||
m_EventQueueWrite->push_back(std::make_pair(typeid(EventType).name(), std::shared_ptr<EventType>(new EventType(event))));
|
||||
}
|
||||
|
||||
template <typename ContextType>
|
||||
int EventBroker::Process()
|
||||
{
|
||||
const std::string contextTypeName = typeid(ContextType).name();
|
||||
return Process(contextTypeName);
|
||||
const std::string contextTypeName = typeid(ContextType).name();
|
||||
return Process(contextTypeName);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -10,16 +10,16 @@ class System
|
||||
friend class SystemPipeline;
|
||||
|
||||
public:
|
||||
System(const EventBroker* eventBroker, std::string componentType)
|
||||
System(EventBroker* eventBroker, std::string componentType)
|
||||
: m_EventBroker(eventBroker)
|
||||
, m_ComponentType(componentType)
|
||||
{ }
|
||||
|
||||
virtual void Update(World* world, ComponentWrapper& component, double dt) = 0;
|
||||
|
||||
private:
|
||||
const EventBroker* m_EventBroker;
|
||||
protected:
|
||||
std::string m_ComponentType;
|
||||
EventBroker* m_EventBroker;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -9,7 +9,7 @@
|
||||
class SystemPipeline
|
||||
{
|
||||
public:
|
||||
SystemPipeline(const EventBroker* eventBroker)
|
||||
SystemPipeline(EventBroker* eventBroker)
|
||||
: m_EventBroker(eventBroker)
|
||||
{ }
|
||||
~SystemPipeline()
|
||||
@@ -51,7 +51,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
const EventBroker* m_EventBroker;
|
||||
EventBroker* m_EventBroker;
|
||||
std::unordered_map<std::string, std::vector<System*>> m_Systems;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#ifndef Events_Picking_h__
|
||||
#define Events_Picking_h__
|
||||
|
||||
#include "../OpenGL.h"
|
||||
#include "../GLM.h"
|
||||
|
||||
#include "../Core/EventBroker.h"
|
||||
#include "Util/ScreenCoords.h"
|
||||
#include "FrameBuffer.h"
|
||||
#include "../Core/Entity.h"
|
||||
#include "Util/UnorderedMapVec2.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
/** Thrown Every frame, use functions to pick*/
|
||||
struct Picking : Event
|
||||
{
|
||||
public:
|
||||
Picking(FrameBuffer* pickingBuffer, GLuint* depthBuffer, glm::mat4 projectionMatrix, glm::mat4 viewMatrix, Rectangle resolution, const std::unordered_map<glm::vec2, EntityID>* pickingColorsToEntity)
|
||||
: PickingBuffer(pickingBuffer)
|
||||
, DepthBuffer(depthBuffer)
|
||||
, ProjectionMatrix(projectionMatrix)
|
||||
, ViewMatrix(viewMatrix)
|
||||
, Resolution(resolution)
|
||||
, PickingColorsToEntity(pickingColorsToEntity)
|
||||
{ }
|
||||
|
||||
|
||||
|
||||
struct PickData
|
||||
{
|
||||
//Picked Entity
|
||||
EntityID Entity;
|
||||
//World position of the "pick"
|
||||
glm::vec3 Position;
|
||||
};
|
||||
|
||||
PickData Pick(glm::vec2 screenCoord) const
|
||||
{
|
||||
PickData pickData;
|
||||
|
||||
ScreenCoords::PixelData data = ScreenCoords::ToPixelData(screenCoord, PickingBuffer, *DepthBuffer);
|
||||
|
||||
auto it = PickingColorsToEntity->find(glm::vec2(data.Color[0], data.Color[1]));
|
||||
if (it != PickingColorsToEntity->end()) {
|
||||
pickData.Entity = it->second;
|
||||
} else {
|
||||
pickData.Entity = -1;
|
||||
}
|
||||
pickData.Position = ScreenCoords::ToWorldPos(screenCoord.x, Resolution.Width - screenCoord.y, data.Depth, Resolution, ProjectionMatrix, ViewMatrix);
|
||||
|
||||
return pickData;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
FrameBuffer* PickingBuffer;
|
||||
GLuint* DepthBuffer;
|
||||
const glm::mat4 ProjectionMatrix;
|
||||
const glm::mat4 ViewMatrix;
|
||||
const Rectangle Resolution;
|
||||
const std::unordered_map<glm::vec2, EntityID>* PickingColorsToEntity;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -11,15 +11,24 @@
|
||||
#include "FrameBuffer.h"
|
||||
#include "../Core/World.h"
|
||||
|
||||
#include "../Core/EventBroker.h"
|
||||
#include "EPicking.h"
|
||||
|
||||
class Renderer : public IRenderer
|
||||
{
|
||||
public:
|
||||
Renderer(EventBroker* eventBroker)
|
||||
: m_EventBroker(eventBroker)
|
||||
{ }
|
||||
|
||||
virtual void Initialize() override;
|
||||
virtual void Update(double dt) override;
|
||||
virtual void Draw(RenderQueueCollection& rq) override;
|
||||
|
||||
private:
|
||||
//----------------------Variables----------------------//
|
||||
EventBroker* m_EventBroker;
|
||||
|
||||
Texture* m_ErrorTexture;
|
||||
Texture* m_WhiteTexture;
|
||||
float m_CameraMoveSpeed;
|
||||
@@ -31,8 +40,6 @@ private:
|
||||
Model* m_UnitQuad;
|
||||
Model* m_UnitSphere;
|
||||
|
||||
|
||||
|
||||
std::unordered_map<glm::vec2, EntityID> m_PickingColorsToEntity;
|
||||
|
||||
//----------------------Functions----------------------//
|
||||
|
||||
@@ -11,6 +11,13 @@ class ScreenCoords
|
||||
{
|
||||
public:
|
||||
ScreenCoords() = delete;
|
||||
|
||||
struct PixelData
|
||||
{
|
||||
int Color[2];
|
||||
float Depth;
|
||||
};
|
||||
|
||||
//Return world position from given screenspace coordinates and depth value in viewspace.
|
||||
static glm::vec3 ToWorldPos(glm::vec2 screenCoord, float depth, Rectangle resolution, glm::mat4 cameraProjectionMat, glm::mat4 cameraViewMat);
|
||||
static glm::vec3 ToWorldPos(float x, float y, float depth, Rectangle resolution, glm::mat4 cameraProjectionMat, glm::mat4 cameraViewMat);
|
||||
@@ -18,8 +25,8 @@ public:
|
||||
static glm::vec3 ToWorldPos(float x, float y, float depth, float screenWidth, float screenHeight, glm::mat4 cameraProjectionMat, glm::mat4 cameraViewMat);
|
||||
//Return data from the given buffers at the coordinates given in screenspace. Buffer should probably have a texture that covers the screen.
|
||||
//Data is given as R = x, B = y, and
|
||||
static glm::vec3 ToPixelData(glm::vec2 screenCoord, FrameBuffer* PickDataBuffer, GLuint DepthBuffer);
|
||||
static glm::vec3 ToPixelData(float x, float y, FrameBuffer* PickDataBuffer, GLuint DepthBuffer);
|
||||
static PixelData ToPixelData(glm::vec2 screenCoord, FrameBuffer* PickDataBuffer, GLuint DepthBuffer);
|
||||
static PixelData ToPixelData(float x, float y, FrameBuffer* PickDataBuffer, GLuint DepthBuffer);
|
||||
//Return EntityID of the clicked coordinate in given screenspace coordinates.
|
||||
//EntityID ScreenCoordsToEntityID(glm::vec2 screenCoord, float depth);
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "Core/EntityXMLFile.h"
|
||||
#include "Core/SystemPipeline.h"
|
||||
#include "RaptorCopterSystem.h"
|
||||
#include "PlayerSystem.h"
|
||||
|
||||
class Game
|
||||
{
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef PlayerSystem_h__
|
||||
#define PlayerSystem_h__
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/common.hpp>
|
||||
|
||||
#include "Common.h"
|
||||
#include "Core/System.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Core/EKeyUp.h"
|
||||
|
||||
struct KeyInput
|
||||
{
|
||||
bool Forward = false;
|
||||
bool Left = false;
|
||||
bool Back = false;
|
||||
bool Right = false;
|
||||
};
|
||||
|
||||
class PlayerSystem : public System
|
||||
{
|
||||
public:
|
||||
PlayerSystem(EventBroker* eventBroker)
|
||||
: System(eventBroker, "Player")
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &PlayerSystem::OnKeyDown);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &PlayerSystem::OnKeyUp);
|
||||
}
|
||||
|
||||
virtual void Update(World* world, ComponentWrapper& player, double dt) override;
|
||||
|
||||
private:
|
||||
float m_Speed = 5;
|
||||
glm::vec3 m_Direction;
|
||||
KeyInput input;
|
||||
|
||||
EventRelay<PlayerSystem, Events::KeyDown> m_EKeyDown;
|
||||
bool OnKeyDown(const Events::KeyDown &event);
|
||||
EventRelay<PlayerSystem, Events::KeyUp> m_EKeyUp;
|
||||
bool OnKeyUp(const Events::KeyUp &event);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -4,10 +4,12 @@
|
||||
class RaptorCopterSystem : public System
|
||||
{
|
||||
public:
|
||||
RaptorCopterSystem(const EventBroker* eventBroker)
|
||||
RaptorCopterSystem(EventBroker* eventBroker)
|
||||
: System(eventBroker, "RaptorCopter")
|
||||
{ }
|
||||
|
||||
virtual void Initialize() { }
|
||||
|
||||
virtual void Update(World* world, ComponentWrapper& raptorCopter, double dt) override
|
||||
{
|
||||
ComponentWrapper& transform = world->GetComponent(raptorCopter.EntityID, "Transform");
|
||||
|
||||
@@ -5,4 +5,5 @@
|
||||
<xs:include schemaLocation="Components/Model.xsd"/>
|
||||
<xs:include schemaLocation="Components/Test.xsd"/>
|
||||
<xs:include schemaLocation="Components/RaptorCopter.xsd"/>
|
||||
<xs:include schemaLocation="Components/Player.xsd"/>
|
||||
</xs:schema>
|
||||
@@ -0,0 +1,3 @@
|
||||
<c:Player>
|
||||
<Velocity X="0" Y="0" Z="0"/>
|
||||
</c:Player>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
|
||||
<xs:import schemaLocation="../Types.xsd" namespace="types"/>
|
||||
|
||||
<xs:element name="Player">
|
||||
<xs:complexType>
|
||||
<xs:all>
|
||||
<xs:element name="Velocity" type="t:Vector" minOccurs="0"/>
|
||||
</xs:all>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
@@ -31,6 +31,19 @@
|
||||
</c:Model>
|
||||
</Components>
|
||||
</Entity>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Player>
|
||||
<Velocity X="0" Y="0" Z="0"/>
|
||||
</c:Player>
|
||||
<c:Transform>
|
||||
<Position X="2.5"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitCube.obj</Resource>
|
||||
</c:Model>
|
||||
</Components>
|
||||
</Entity>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
@@ -48,7 +61,7 @@
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0"/>
|
||||
<Orientation X="0.0" Y="0" Z="0.0"/>
|
||||
<Orientation X="0.0" Y="0" Z="1.0"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitRaptor.obj</Resource>
|
||||
@@ -59,8 +72,8 @@
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0"/>
|
||||
<Orientation X="0" Y="0" Z="0"/>
|
||||
<Position X="-0.01" Y="0.55"/>
|
||||
<Orientation X="0" Y="0" Z="-1"/>
|
||||
</c:Transform>
|
||||
<c:RaptorCopter>
|
||||
<Speed>20</Speed>
|
||||
@@ -71,19 +84,7 @@
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0.4"/>
|
||||
<Scale X="0.1" Y="0.4" Z="0.1"/>
|
||||
</c:Transform>
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitCylinder.obj</Resource>
|
||||
<Color R="1" G="0.4" B="0.8"/>
|
||||
</c:Model>
|
||||
</Components>
|
||||
</Entity>
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0.6"/>
|
||||
<Position X="0" Y="0"/>
|
||||
<Scale X="1.7" Y="0.03" Z="0.1"/>
|
||||
<Orientation X="0" Y="0" Z="0"/>
|
||||
</c:Transform>
|
||||
@@ -96,7 +97,7 @@
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="0.6"/>
|
||||
<Position X="0" Y="0"/>
|
||||
<Scale X="1.7" Y="0.03" Z="0.1"/>
|
||||
<Orientation X="0" Y="1.57" Z="0"/>
|
||||
</c:Transform>
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
<xs:element ref="c:Model" minOccurs="0"/>
|
||||
<xs:element ref="c:Test" minOccurs="0"/>
|
||||
<xs:element ref="c:RaptorCopter" minOccurs="0"/>
|
||||
<xs:element ref="c:Player" minOccurs="0"/>
|
||||
</xs:all>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
@@ -13,7 +13,7 @@ out vec4 TextureFragment;
|
||||
|
||||
void main()
|
||||
{
|
||||
TextureFragment = vec4(PickingColor, 0, 1);
|
||||
TextureFragment = vec4(PickingColor/255, 0, 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ glm::vec3 RenderQueueFactory::AbsolutePosition(World* world, EntityID entity)
|
||||
|
||||
do {
|
||||
ComponentWrapper transform = world->GetComponent(entity, "Transform");
|
||||
position += AbsoluteOrientation(world, entity) * (glm::vec3)transform["Position"];
|
||||
position += (glm::vec3)transform["Position"];
|
||||
entity = world->GetParent(entity);
|
||||
} while (entity != 0);
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
void Renderer::Initialize()
|
||||
{
|
||||
InitializeWindow();
|
||||
|
||||
// Create default camera
|
||||
m_DefaultCamera = new ::Camera((float)m_Resolution.Width / m_Resolution.Height, glm::radians(45.f), 0.01f, 5000.f);
|
||||
m_DefaultCamera->SetPosition(glm::vec3(0, 0, 10));
|
||||
@@ -116,23 +115,6 @@ void Renderer::InputUpdate(double dt)
|
||||
|
||||
static double mousePosX, mousePosY;
|
||||
glfwGetCursorPos(m_Window, &mousePosX, &mousePosY);
|
||||
if (glfwGetMouseButton(m_Window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS) {
|
||||
glm::vec3 data = ScreenCoords::ToPixelData(mousePosX, m_Resolution.Height - mousePosY, &m_PickingBuffer, m_DepthBuffer);
|
||||
glm::vec2 color = glm::vec2(data);
|
||||
float depth = data.z;
|
||||
|
||||
glm::vec3 viewPos = ScreenCoords::ToWorldPos(mousePosX, m_Resolution.Height - mousePosY, depth, m_Resolution, m_Camera->ProjectionMatrix(), m_Camera->ViewMatrix());
|
||||
// glm::vec3 worldPos = glm::vec3(glm::inverse(m_Camera->ViewMatrix()) * glm::vec4(viewPos, 1.f));
|
||||
|
||||
//printf("R: %f, G: %f, Depth: %f\n", color.r, color.g, depth);
|
||||
//printf("view: x: %f, y: %f z: %f, Length: %f\n\n", viewPos.x, viewPos.y, viewPos.z, glm::length(viewPos));
|
||||
|
||||
if (color != glm::vec2(0, 0)) {
|
||||
EntityID pickedEntity = m_PickingColorsToEntity[color];
|
||||
printf("Picked Entity: %i", pickedEntity);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (glfwGetKey(m_Window, GLFW_KEY_SPACE) == GLFW_PRESS) {
|
||||
|
||||
@@ -158,7 +140,8 @@ void Renderer::InputUpdate(double dt)
|
||||
|
||||
void Renderer::Update(double dt)
|
||||
{
|
||||
InputUpdate(dt);
|
||||
m_EventBroker->Process<Renderer>();
|
||||
InputUpdate(dt);
|
||||
|
||||
}
|
||||
|
||||
@@ -213,10 +196,12 @@ void Renderer::DrawScene(RenderQueueCollection& rq)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
GLERROR("DrawScene Error");
|
||||
}
|
||||
|
||||
void Renderer::PickingPass(RenderQueueCollection& rq)
|
||||
{
|
||||
m_PickingColorsToEntity.clear();
|
||||
m_PickingBuffer.Bind();
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
@@ -225,7 +210,7 @@ void Renderer::PickingPass(RenderQueueCollection& rq)
|
||||
|
||||
glClearColor(0.f, 0.f, 0.f, 1.f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
int r = 30;
|
||||
int r = 1;
|
||||
int g = 0;
|
||||
//TODO: Render: Add code for more jobs than modeljobs.
|
||||
|
||||
@@ -237,8 +222,18 @@ void Renderer::PickingPass(RenderQueueCollection& rq)
|
||||
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
|
||||
|
||||
if (modelJob) {
|
||||
glm::vec2 pickColor = glm::vec2(r/255.f, g/255.f);
|
||||
m_PickingColorsToEntity[pickColor] = modelJob->Entity;
|
||||
//---------------
|
||||
//TODO: Renderer: IMPORTANT: Fixa detta så det inte loopar igenom listan varje frame.
|
||||
//---------------
|
||||
int pickColor[2] = { r, g };
|
||||
for (auto i : m_PickingColorsToEntity) {
|
||||
if(modelJob->Entity == i.second) {
|
||||
pickColor[0] = i.first.x;
|
||||
pickColor[1] = i.first.y;
|
||||
r -= 1;
|
||||
}
|
||||
}
|
||||
m_PickingColorsToEntity[glm::vec2(pickColor[0], pickColor[1])] = modelJob->Entity;
|
||||
|
||||
|
||||
//Render picking stuff
|
||||
@@ -246,19 +241,32 @@ void Renderer::PickingPass(RenderQueueCollection& rq)
|
||||
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->ModelMatrix));
|
||||
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix()));
|
||||
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix()));
|
||||
glUniform2fv(glGetUniformLocation(ShaderHandle, "PickingColor"), 1, glm::value_ptr(pickColor));
|
||||
glUniform2fv(glGetUniformLocation(ShaderHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1])));
|
||||
|
||||
glBindVertexArray(modelJob->Model->VAO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer);
|
||||
glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, 0, modelJob->StartIndex);
|
||||
r+=50;
|
||||
r += 1;
|
||||
if(r > 255) {
|
||||
r = 0;
|
||||
g+=50;
|
||||
g += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
m_PickingBuffer.Unbind();
|
||||
GLERROR("PickingPass Error");
|
||||
|
||||
//Publish pick event every frame with the pick data that can be picked by the event
|
||||
Events::Picking pickEvent = Events::Picking(
|
||||
&m_PickingBuffer,
|
||||
&m_DepthBuffer,
|
||||
m_Camera->ProjectionMatrix(),
|
||||
m_Camera->ViewMatrix(),
|
||||
m_Resolution,
|
||||
&m_PickingColorsToEntity);
|
||||
|
||||
m_EventBroker->Publish(pickEvent);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -300,7 +308,7 @@ void Renderer::InitializeTextures()
|
||||
*/
|
||||
|
||||
GenerateTexture(&m_PickingTexture, GL_CLAMP_TO_BORDER, GL_LINEAR,
|
||||
glm::vec2(m_Resolution.Width, m_Resolution.Height), GL_RG8, GL_RG, GL_FLOAT);
|
||||
glm::vec2(m_Resolution.Width, m_Resolution.Height), GL_RG8, GL_RG, GL_UNSIGNED_BYTE);
|
||||
}
|
||||
|
||||
void Renderer::GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type)
|
||||
|
||||
@@ -29,22 +29,31 @@ glm::vec3 ScreenCoords::ToWorldPos(glm::vec2 screenCoord, float depth, float scr
|
||||
return ToWorldPos(screenCoord.x, screenCoord.y, depth, screenWidth, screenHeight, cameraProjectionMat, cameraViewMat);
|
||||
}
|
||||
|
||||
glm::vec3 ScreenCoords::ToPixelData(float x, float y, FrameBuffer* PickDataBuffer, GLuint DepthBuffer)
|
||||
ScreenCoords::PixelData ScreenCoords::ToPixelData(float x, float y, FrameBuffer* PickDataBuffer, GLuint DepthBuffer)
|
||||
{
|
||||
PickDataBuffer->Bind();
|
||||
glm::vec2 pixelData;
|
||||
glReadPixels(x, y, 1, 1, GL_RG, GL_FLOAT, &pixelData);
|
||||
unsigned char pdata[2];
|
||||
glReadPixels(x, y, 1, 1, GL_RG, GL_UNSIGNED_BYTE, &pdata);
|
||||
PickDataBuffer->Unbind();
|
||||
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, DepthBuffer);
|
||||
float depthData;
|
||||
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depthData);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
|
||||
return glm::vec3(pixelData, depthData);
|
||||
PixelData p;
|
||||
p.Color[0] = (int)pdata[0];
|
||||
p.Color[1] = (int)pdata[1];
|
||||
p.Depth = depthData;
|
||||
|
||||
GLERROR("ScreenCoords::ToPixelData Error");
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
glm::vec3 ScreenCoords::ToPixelData(glm::vec2 screenCoord, FrameBuffer* PickDataBuffer, GLuint DepthBuffer)
|
||||
ScreenCoords::PixelData ScreenCoords::ToPixelData(glm::vec2 screenCoord, FrameBuffer* PickDataBuffer, GLuint DepthBuffer)
|
||||
{
|
||||
return ToPixelData(screenCoord.x, screenCoord.y, PickDataBuffer, DepthBuffer);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ file(GLOB SOURCE_FILES
|
||||
set(SOURCE_FILES
|
||||
${SOURCE_FILES}
|
||||
"Game.cpp"
|
||||
"PlayerSystem.cpp"
|
||||
)
|
||||
|
||||
set(LIBRARIES
|
||||
|
||||
+42
-39
@@ -2,39 +2,39 @@
|
||||
|
||||
Game::Game(int argc, char* argv[])
|
||||
{
|
||||
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
|
||||
ResourceManager::RegisterType<Model>("Model");
|
||||
ResourceManager::RegisterType<Texture>("Texture");
|
||||
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
|
||||
ResourceManager::RegisterType<Model>("Model");
|
||||
ResourceManager::RegisterType<Texture>("Texture");
|
||||
ResourceManager::RegisterType<EntityXMLFile>("EntityXMLFile");
|
||||
|
||||
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1));
|
||||
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1));
|
||||
|
||||
// Create the core event broker
|
||||
m_EventBroker = new EventBroker();
|
||||
// Create the core event broker
|
||||
m_EventBroker = new EventBroker();
|
||||
|
||||
m_RenderQueueFactory = new RenderQueueFactory();
|
||||
|
||||
// Create the renderer
|
||||
m_Renderer = new Renderer();
|
||||
m_Renderer->SetFullscreen(m_Config->Get<bool>("Video.Fullscreen", false));
|
||||
m_Renderer->SetVSYNC(m_Config->Get<bool>("Video.VSYNC", false));
|
||||
m_Renderer->SetResolution(Rectangle(
|
||||
0,
|
||||
0,
|
||||
m_Config->Get<int>("Video.Width", 1280),
|
||||
m_Config->Get<int>("Video.Height", 720)
|
||||
));
|
||||
m_Renderer->Initialize();
|
||||
// Create the renderer
|
||||
m_Renderer = new Renderer(m_EventBroker);
|
||||
m_Renderer->SetFullscreen(m_Config->Get<bool>("Video.Fullscreen", false));
|
||||
m_Renderer->SetVSYNC(m_Config->Get<bool>("Video.VSYNC", false));
|
||||
m_Renderer->SetResolution(Rectangle(
|
||||
0,
|
||||
0,
|
||||
m_Config->Get<int>("Video.Width", 1280),
|
||||
m_Config->Get<int>("Video.Height", 720)
|
||||
));
|
||||
m_Renderer->Initialize();
|
||||
m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get<float>("Video.FOV", 90.f)));
|
||||
|
||||
// Create input manager
|
||||
m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker);
|
||||
// Create input manager
|
||||
m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker);
|
||||
|
||||
// Create the root level GUI frame
|
||||
m_FrameStack = new GUI::Frame(m_EventBroker);
|
||||
m_FrameStack->Width = m_Renderer->Resolution().Width;
|
||||
m_FrameStack->Height = m_Renderer->Resolution().Height;
|
||||
// Create the root level GUI frame
|
||||
m_FrameStack = new GUI::Frame(m_EventBroker);
|
||||
m_FrameStack->Width = m_Renderer->Resolution().Width;
|
||||
m_FrameStack->Height = m_Renderer->Resolution().Height;
|
||||
|
||||
// Create a world
|
||||
m_World = new World();
|
||||
@@ -42,44 +42,47 @@ Game::Game(int argc, char* argv[])
|
||||
if (!mapToLoad.empty()) {
|
||||
ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World);
|
||||
}
|
||||
|
||||
|
||||
// Create system pipeline
|
||||
m_SystemPipeline = new SystemPipeline(m_EventBroker);
|
||||
m_SystemPipeline->AddSystem<RaptorCopterSystem>();
|
||||
m_SystemPipeline->AddSystem<PlayerSystem>();
|
||||
|
||||
m_LastTime = glfwGetTime();
|
||||
|
||||
|
||||
m_LastTime = glfwGetTime();
|
||||
|
||||
testIntialize();
|
||||
}
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
delete m_FrameStack;
|
||||
delete m_EventBroker;
|
||||
delete m_FrameStack;
|
||||
delete m_EventBroker;
|
||||
}
|
||||
|
||||
void Game::Tick()
|
||||
{
|
||||
double currentTime = glfwGetTime();
|
||||
double dt = currentTime - m_LastTime;
|
||||
m_LastTime = currentTime;
|
||||
double currentTime = glfwGetTime();
|
||||
double dt = currentTime - m_LastTime;
|
||||
m_LastTime = currentTime;
|
||||
|
||||
m_EventBroker->Swap();
|
||||
m_InputManager->Update(dt);
|
||||
m_Renderer->Update(dt);
|
||||
m_EventBroker->Swap();
|
||||
m_EventBroker->Swap();
|
||||
m_InputManager->Update(dt);
|
||||
m_EventBroker->Swap();
|
||||
|
||||
// Iterate through systems and update world!
|
||||
m_SystemPipeline->Update(m_World, dt);
|
||||
testTick(dt);
|
||||
m_Renderer->Update(dt);
|
||||
|
||||
m_RenderQueueFactory->Update(m_World);
|
||||
m_Renderer->Draw(m_RenderQueueFactory->RenderQueues());
|
||||
m_Renderer->Draw(m_RenderQueueFactory->RenderQueues());
|
||||
|
||||
m_EventBroker->Swap();
|
||||
m_EventBroker->Clear();
|
||||
m_EventBroker->Swap();
|
||||
m_EventBroker->Clear();
|
||||
|
||||
glfwPollEvents();
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "PlayerSystem.h"
|
||||
|
||||
|
||||
void PlayerSystem::Update(World * world, ComponentWrapper & player, double dt)
|
||||
{
|
||||
if (input.Forward) {
|
||||
m_Direction.z = -1;
|
||||
} else if (input.Back) {
|
||||
m_Direction.z = 1;
|
||||
} else {
|
||||
m_Direction.z = 0;
|
||||
}
|
||||
if (input.Left) {
|
||||
m_Direction.x = -1;
|
||||
} else if (input.Right) {
|
||||
m_Direction.x = 1;
|
||||
} else {
|
||||
m_Direction.x = 0;
|
||||
}
|
||||
m_EventBroker->Process<PlayerSystem>();
|
||||
ComponentWrapper& transform = world->GetComponent(player.EntityID, "Transform");
|
||||
(glm::vec3&)player["Velocity"] = m_Speed * float(dt) * m_Direction;
|
||||
(glm::vec3&)transform["Position"] += (glm::vec3)player["Velocity"];
|
||||
}
|
||||
|
||||
bool PlayerSystem::OnKeyDown(const Events::KeyDown & event)
|
||||
{
|
||||
if (event.KeyCode == GLFW_KEY_W) {
|
||||
input.Forward = true;
|
||||
}
|
||||
if (event.KeyCode == GLFW_KEY_A) {
|
||||
input.Left = true;
|
||||
}
|
||||
if (event.KeyCode == GLFW_KEY_S) {
|
||||
input.Back = true;
|
||||
}
|
||||
if (event.KeyCode == GLFW_KEY_D) {
|
||||
input.Right = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PlayerSystem::OnKeyUp(const Events::KeyUp & event)
|
||||
{
|
||||
if (event.KeyCode == GLFW_KEY_W) {
|
||||
input.Forward = false;
|
||||
}
|
||||
if (event.KeyCode == GLFW_KEY_A) {
|
||||
input.Left = false;
|
||||
}
|
||||
if (event.KeyCode == GLFW_KEY_S) {
|
||||
input.Back = false;
|
||||
}
|
||||
if (event.KeyCode == GLFW_KEY_D) {
|
||||
input.Right = false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user