Merge branch 'master' of github.com:sippeangelo/Escape-the-Dawn
Conflicts: Escape-the-Dawn/AABB.h Escape-the-Dawn/Components/Bounds.h Escape-the-Dawn/Escape-the-Dawn.vcxproj Escape-the-Dawn/Escape-the-Dawn.vcxproj.filters
This commit is contained in:
@@ -4,6 +4,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Escape-the-Dawn", "Escape-the-Dawn\Escape-the-Dawn.vcxproj", "{FE405CFA-8FCE-4867-92CF-797E73B36482}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Tests", "Tests\Tests.vcxproj", "{B9A8F4AA-BE85-45EA-84DD-652D13DD22BE}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{A9755CAA-62DC-442D-B82C-F548546CFD38} = {A9755CAA-62DC-442D-B82C-F548546CFD38}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Executable", "Executable\Executable.vcxproj", "{A9755CAA-62DC-442D-B82C-F548546CFD38}"
|
||||
EndProject
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef Component_h__
|
||||
#define Component_h__
|
||||
|
||||
#include "ComponentFactory.h"
|
||||
#include "Factory.h"
|
||||
#include "Entity.h"
|
||||
|
||||
struct Component
|
||||
@@ -9,4 +9,6 @@ struct Component
|
||||
EntityID Entity;
|
||||
};
|
||||
|
||||
//&static ComponentFactory componentFactory;
|
||||
|
||||
#endif // Component_h__
|
||||
@@ -1,51 +0,0 @@
|
||||
#ifndef ComponentFactory_h__
|
||||
#define ComponentFactory_h__
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
|
||||
struct Component;
|
||||
|
||||
#define REGISTER_COMPONENT(NAME, TYPE) \
|
||||
static ComponentRegistrar<TYPE> registrar(NAME);
|
||||
|
||||
template <typename T>
|
||||
class ComponentRegistrar
|
||||
{
|
||||
public:
|
||||
ComponentRegistrar(std::string name)
|
||||
{
|
||||
ComponentFactory::Instance()->Register(name, [](void) -> std::shared_ptr<Component> { return std::shared_ptr<Component>(new T()); });
|
||||
}
|
||||
};
|
||||
|
||||
class ComponentFactory
|
||||
{
|
||||
public:
|
||||
static ComponentFactory* Instance()
|
||||
{
|
||||
static ComponentFactory factory;
|
||||
return &factory;
|
||||
}
|
||||
|
||||
void Register(std::string name, std::function<std::shared_ptr<Component>(void)> factoryFunction)
|
||||
{
|
||||
m_FactoryFunctions[name] = factoryFunction;
|
||||
}
|
||||
|
||||
std::shared_ptr<Component> Create(std::string name)
|
||||
{
|
||||
auto it = m_FactoryFunctions.find(name);
|
||||
if (it != m_FactoryFunctions.end())
|
||||
return it->second();
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, std::function<std::shared_ptr<Component>(void)>> m_FactoryFunctions;
|
||||
};
|
||||
|
||||
#endif // ComponentFactory_h__
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef Bounds_h__
|
||||
#define Bounds_h__
|
||||
#ifndef Components_Bounds_h__
|
||||
#define Components_Bounds_h__
|
||||
|
||||
#include "Component.h"
|
||||
#include <glm/common.hpp>
|
||||
@@ -14,7 +14,6 @@ namespace Components
|
||||
glm::vec3 origin;
|
||||
glm::vec3 volumeVector; //The vector that defines the volume of the BB, it goes from one corner to the opposite one
|
||||
};
|
||||
REGISTER_COMPONENT("Bounds", Bounds);
|
||||
|
||||
}
|
||||
#endif // !Bounds_h__
|
||||
#endif // !Components_Bounds_h__
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef Camera_h__
|
||||
#define Camera_h__
|
||||
#ifndef Components_Camera_h__
|
||||
#define Components_Camera_h__
|
||||
|
||||
#include "Component.h"
|
||||
|
||||
@@ -12,7 +12,6 @@ namespace Components
|
||||
int NearClip;
|
||||
int FarClip;
|
||||
};
|
||||
REGISTER_COMPONENT("Camera", Camera);
|
||||
|
||||
}
|
||||
#endif // !Camera_h__
|
||||
#endif // !Components_Camera_h__
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef Collision_h__
|
||||
#define Collision_h__
|
||||
#ifndef Components_Collision_h__
|
||||
#define Components_Collision_h__
|
||||
|
||||
#include "Entity.h"
|
||||
#include "Component.h"
|
||||
#include <vector>
|
||||
|
||||
@@ -10,9 +11,8 @@ namespace Components
|
||||
struct Collision : Component
|
||||
{
|
||||
bool Phantom;
|
||||
std::vector<int> CollidingEntities;
|
||||
std::vector<EntityID> CollidingEntities;
|
||||
};
|
||||
REGISTER_COMPONENT("Collision", Collision);
|
||||
|
||||
}
|
||||
#endif // !Collision_h__
|
||||
#endif // !Components_Collision_h__
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef DirectionalLight_h__
|
||||
#define DirectionalLight_h__
|
||||
#ifndef Components_DirectionalLight_h__
|
||||
#define Components_DirectionalLight_h__
|
||||
|
||||
#include "Component.h"
|
||||
#include "Color.h"
|
||||
@@ -12,9 +12,8 @@ namespace Components
|
||||
float Intensity;
|
||||
float MaxRange;
|
||||
float SpecularIntensity;
|
||||
Color color;
|
||||
Color Color;
|
||||
};
|
||||
REGISTER_COMPONENT("DirectionalLight", DirectionalLight);
|
||||
|
||||
}
|
||||
#endif // !DirectionalLight_h__
|
||||
#endif // !Components_DirectionalLight_h__
|
||||
@@ -1,19 +1,24 @@
|
||||
#ifndef Input_h__
|
||||
#define Input_h__
|
||||
#ifndef Components_Input_h__
|
||||
#define Components_Input_h__
|
||||
|
||||
#include <array>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "Component.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
namespace Components
|
||||
{
|
||||
|
||||
struct Input : Component
|
||||
{
|
||||
int KeyState[GLFW_KEY_LAST];
|
||||
float MouseState[2];
|
||||
std::array<int, GLFW_KEY_LAST> KeyState;
|
||||
std::array<int, GLFW_KEY_LAST> LastKeyState;
|
||||
std::array<int, GLFW_MOUSE_BUTTON_LAST> MouseState;
|
||||
std::array<int, GLFW_MOUSE_BUTTON_LAST> LastMouseState;
|
||||
float dX, dY;
|
||||
};
|
||||
REGISTER_COMPONENT("Input", Input);
|
||||
|
||||
}
|
||||
#endif // !Input_h__
|
||||
|
||||
#endif // !Components_Input_h__
|
||||
@@ -1,5 +1,7 @@
|
||||
#ifndef Model_h__
|
||||
#define Model_h__
|
||||
#ifndef Components_Model_h__
|
||||
#define Components_Model_h__
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "Component.h"
|
||||
#include "Color.h"
|
||||
@@ -9,10 +11,9 @@ namespace Components
|
||||
|
||||
struct Model : Component
|
||||
{
|
||||
std::string ModelName;
|
||||
Color color;
|
||||
std::string ModelFile;
|
||||
Color Color;
|
||||
};
|
||||
REGISTER_COMPONENT("Model", Model);
|
||||
|
||||
}
|
||||
#endif // !Model_h__
|
||||
#endif // !Components_Model_h__
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef ParticleEmitter_h__
|
||||
#define ParticleEmitter_h__
|
||||
#ifndef Components_ParticleEmitter_h__
|
||||
#define Components_ParticleEmitter_h__
|
||||
|
||||
#include "Component.h"
|
||||
#include "Color.h"
|
||||
@@ -20,7 +20,6 @@ namespace Components
|
||||
std::vector<float[3]> VelocitySpectrum;
|
||||
std::vector<float[3]> AngularVelocitySpectrum;
|
||||
};
|
||||
REGISTER_COMPONENT("ParticleEmitter", ParticleEmitter);
|
||||
|
||||
}
|
||||
#endif // !ParticleEmitter_h__
|
||||
#endif // !Components_ParticleEmitter_h__
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef PointLight_h__
|
||||
#define PointLight_h__
|
||||
#ifndef Components_PointLight_h__
|
||||
#define Components_PointLight_h__
|
||||
|
||||
#include "Component.h"
|
||||
#include "Color.h"
|
||||
@@ -12,9 +12,8 @@ namespace Components
|
||||
float Intensity;
|
||||
float MaxRange;
|
||||
float SpecularIntensity;
|
||||
Color color;
|
||||
Color Color;
|
||||
};
|
||||
REGISTER_COMPONENT("PointLight", PointLight);
|
||||
|
||||
}
|
||||
#endif // !PointLight_h__
|
||||
#endif // !Components_PointLight_h__
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef PowerUp_h__
|
||||
#define PowerUp_h__
|
||||
#ifndef Components_PowerUp_h__
|
||||
#define Components_PowerUp_h__
|
||||
|
||||
#include "Component.h"
|
||||
|
||||
@@ -8,9 +8,8 @@ namespace Components
|
||||
|
||||
struct PowerUp : Component
|
||||
{
|
||||
int Speed;
|
||||
float Speed;
|
||||
};
|
||||
REGISTER_COMPONENT("PowerUp", PowerUp);
|
||||
|
||||
}
|
||||
#endif // !PowerUp_h__
|
||||
#endif // !Components_PowerUp_h__
|
||||
@@ -1,5 +1,7 @@
|
||||
#ifndef SoundEmitter_h__
|
||||
#define SoundEmitter_h__
|
||||
#ifndef Components_SoundEmitter_h__
|
||||
#define Components_SoundEmitter_h__
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "Component.h"
|
||||
|
||||
@@ -12,7 +14,6 @@ namespace Components
|
||||
float Volume;
|
||||
float MaxRange;
|
||||
};
|
||||
REGISTER_COMPONENT("SoundEmitter", SoundEmitter);
|
||||
|
||||
}
|
||||
#endif // !SoundEmitter_h__
|
||||
#endif // !Components_SoundEmitter_h__
|
||||
@@ -1,5 +1,7 @@
|
||||
#ifndef Sprite_h__
|
||||
#define Sprite_h__
|
||||
#ifndef Components_Sprite_h__
|
||||
#define Components_Sprite_h__
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "Component.h"
|
||||
#include "Color.h"
|
||||
@@ -10,9 +12,8 @@ namespace Components
|
||||
struct Sprite : Component
|
||||
{
|
||||
std::string SpriteFile;
|
||||
Color color;
|
||||
Color Color;
|
||||
};
|
||||
REGISTER_COMPONENT("Sprite", Sprite);
|
||||
|
||||
}
|
||||
#endif // !Sprite_h__
|
||||
#endif // !Components_Sprite_h__
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef Stat_h__
|
||||
#define Stat_h__
|
||||
#ifndef Components_Stat_h__
|
||||
#define Components_Stat_h__
|
||||
|
||||
#include "Component.h"
|
||||
|
||||
@@ -8,10 +8,9 @@ namespace Components
|
||||
|
||||
struct Stat : Component
|
||||
{
|
||||
int Health;
|
||||
float Health;
|
||||
bool Destroyable;
|
||||
};
|
||||
REGISTER_COMPONENT("Stat", Stat);
|
||||
|
||||
}
|
||||
#endif // !Stat_h__
|
||||
#endif // !Components_Stat_h__
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef Template_h__
|
||||
#define Template_h__
|
||||
#ifndef Components_Template_h__
|
||||
#define Components_Template_h__
|
||||
|
||||
#include "Component.h"
|
||||
|
||||
@@ -7,7 +7,6 @@ namespace Components
|
||||
{
|
||||
|
||||
struct Template : Component { };
|
||||
REGISTER_COMPONENT("Template", Template);
|
||||
|
||||
}
|
||||
#endif // !Template_h__
|
||||
#endif // !Components_Template_h__
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef Components_Transform_h__
|
||||
#define Components_Transform_h__
|
||||
|
||||
#include "Component.h"
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
|
||||
namespace Components
|
||||
{
|
||||
|
||||
struct Transform : Component
|
||||
{
|
||||
glm::vec3 Position;
|
||||
glm::quat Orientation;
|
||||
glm::vec3 Velocity;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // Components_Transform_h__
|
||||
@@ -1,19 +0,0 @@
|
||||
#ifndef Transform_h__
|
||||
#define Transform_h__
|
||||
|
||||
#include "Component.h"
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
|
||||
namespace Components
|
||||
{
|
||||
|
||||
struct Transform : Component
|
||||
{
|
||||
float Position[3];
|
||||
glm::fquat qtrn; //Quaternion with floats
|
||||
float Velocity[3];
|
||||
};
|
||||
REGISTER_COMPONENT("Transform", Transform);
|
||||
|
||||
}
|
||||
#endif // Transform_h__
|
||||
+14
-35
@@ -11,53 +11,32 @@
|
||||
|
||||
#include "World.h"
|
||||
|
||||
|
||||
|
||||
class Engine
|
||||
{
|
||||
public:
|
||||
Engine(int argc, char* argv[])
|
||||
{
|
||||
// Initialize GLFW
|
||||
if (!glfwInit()) {
|
||||
LOG_ERROR("GLFW: Initialization failed");
|
||||
exit(EXIT_FAILURE);
|
||||
renderer.Initialize();
|
||||
m_LastTime = glfwGetTime();
|
||||
}
|
||||
|
||||
// Create a window
|
||||
m_Window = glfwCreateWindow(1280, 720, "OpenGL", nullptr, nullptr);
|
||||
if (!m_Window) {
|
||||
LOG_ERROR("GLFW: Failed to create window");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
glfwMakeContextCurrent(m_Window);
|
||||
|
||||
// GL version info
|
||||
glGetIntegerv(GL_MAJOR_VERSION, &glVersion[0]);
|
||||
glGetIntegerv(GL_MINOR_VERSION, &glVersion[1]);
|
||||
glVendor = (GLchar*)glGetString(GL_VENDOR);
|
||||
std::stringstream ss;
|
||||
ss << glVendor << " OpenGL " << glVersion[0] << "." << glVersion[1];
|
||||
#ifdef DEBUG
|
||||
ss << " DEBUG";
|
||||
#endif
|
||||
LOG_INFO(ss.str().c_str());
|
||||
glfwSetWindowTitle(m_Window, ss.str().c_str());
|
||||
|
||||
// Initialize GLEW
|
||||
if (glewInit() != GLEW_OK) {
|
||||
LOG_ERROR("GLEW: Initialization failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
bool Running() const { return !glfwWindowShouldClose(m_Window); }
|
||||
bool Running() const { return !glfwWindowShouldClose(renderer.GetWindow()); }
|
||||
|
||||
void Tick()
|
||||
{
|
||||
double currentTime = glfwGetTime();
|
||||
double dt = currentTime - m_LastTime;
|
||||
m_LastTime = currentTime;
|
||||
|
||||
//World.Update(dt);
|
||||
renderer.Draw(dt);
|
||||
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
private:
|
||||
GLFWwindow* m_Window;
|
||||
GLint glVersion[2];
|
||||
GLchar* glVendor;
|
||||
Renderer renderer;
|
||||
double m_LastTime;
|
||||
};
|
||||
@@ -98,9 +98,9 @@
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Engine.h" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="Model.cpp" />
|
||||
<ClCompile Include="Renderer\Renderer.cpp" />
|
||||
<ClCompile Include="Renderer.cpp" />
|
||||
<ClCompile Include="ShaderProgram.cpp" />
|
||||
<ClCompile Include="Systems\CollisionSystem.cpp" />
|
||||
<ClCompile Include="World.cpp" />
|
||||
<ClCompile Include="Systems\InputSystem.cpp" />
|
||||
@@ -113,9 +113,9 @@
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Color.h" />
|
||||
<ClInclude Include="Component.h" />
|
||||
<ClInclude Include="ComponentFactory.h" />
|
||||
<ClInclude Include="Components\Bounds.h" />
|
||||
<ClInclude Include="Components\Camera.h" />
|
||||
<ClInclude Include="Factory.h" />
|
||||
<ClInclude Include="Components\Bounds.h" />
|
||||
<ClInclude Include="Components\Collision.h" />
|
||||
<ClInclude Include="Components\DirectionalLight.h" />
|
||||
<ClInclude Include="Components\Input.h" />
|
||||
@@ -132,7 +132,7 @@
|
||||
<ClInclude Include="logging.h" />
|
||||
<ClInclude Include="glerror.h" />
|
||||
<ClInclude Include="Model.h" />
|
||||
<ClInclude Include="Renderer\Renderer.h" />
|
||||
<ClInclude Include="Renderer.h" />
|
||||
<ClInclude Include="ShaderProgram.h" />
|
||||
<ClInclude Include="Systems\InputSystem.h" />
|
||||
<ClInclude Include="System.h" />
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="Renderer\Renderer.cpp" />
|
||||
<ClCompile Include="World.cpp" />
|
||||
<ClCompile Include="Systems\CollisionSystem.cpp">
|
||||
<Filter>Systems</Filter>
|
||||
@@ -27,14 +25,12 @@
|
||||
</ClCompile>
|
||||
<ClCompile Include="Model.cpp" />
|
||||
<ClCompile Include="Engine.h" />
|
||||
<ClCompile Include="Renderer.cpp" />
|
||||
<ClCompile Include="ShaderProgram.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Component.h" />
|
||||
<ClInclude Include="ComponentFactory.h" />
|
||||
<ClInclude Include="Entity.h" />
|
||||
<ClInclude Include="logging.h" />
|
||||
<ClInclude Include="glerror.h" />
|
||||
<ClInclude Include="Renderer\Renderer.h" />
|
||||
<ClInclude Include="System.h" />
|
||||
<ClInclude Include="World.h" />
|
||||
<ClInclude Include="Systems\CollisionSystem.h">
|
||||
@@ -63,18 +59,6 @@
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ShaderProgram.h" />
|
||||
<ClInclude Include="Components\Bounds.h">
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Components\Camera.h">
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Components\Collision.h">
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Components\DirectionalLight.h">
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Components\Input.h">
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
@@ -103,6 +87,26 @@
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Color.h" />
|
||||
<ClInclude Include="Factory.h" />
|
||||
<ClInclude Include="Components\DirectionalLight.h">
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Components\Collision.h">
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Components\Camera.h">
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Components\Bounds.h">
|
||||
<Filter>Components</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="glerror.h">
|
||||
<Filter>Util</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="logging.h">
|
||||
<Filter>Util</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Renderer.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Shaders\Fragment.glsl">
|
||||
@@ -122,5 +126,8 @@
|
||||
<Filter Include="Shaders">
|
||||
<UniqueIdentifier>{54f1eb6f-dc00-4e97-a3ef-ac255d755e2e}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Util">
|
||||
<UniqueIdentifier>{d985d7a7-c041-46fe-ae03-aa40295a7aad}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef ComponentFactory_h__
|
||||
#define ComponentFactory_h__
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
|
||||
template <typename T>
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
void Register(std::string name, std::function<T(void)> factoryFunction)
|
||||
{
|
||||
m_FactoryFunctions[name] = factoryFunction;
|
||||
}
|
||||
|
||||
T Create(std::string name)
|
||||
{
|
||||
auto it = m_FactoryFunctions.find(name);
|
||||
if (it != m_FactoryFunctions.end()) {
|
||||
return it->second();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, std::function<T(void)>> m_FactoryFunctions;
|
||||
};
|
||||
|
||||
struct Component;
|
||||
class ComponentFactory : public Factory<Component*> { };
|
||||
|
||||
#endif // ComponentFactory_h__
|
||||
@@ -1,64 +1,77 @@
|
||||
#include "Renderer/Renderer.h"
|
||||
#include "Renderer.h"
|
||||
|
||||
Renderer::Renderer()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Renderer::Draw()
|
||||
void Renderer::Initialize()
|
||||
{
|
||||
// Initialize GLFW
|
||||
if (!glfwInit()) {
|
||||
LOG_ERROR("GLFW: Initialization failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Create a window
|
||||
m_Window = glfwCreateWindow(1280, 720, "OpenGL", nullptr, nullptr);
|
||||
if (!m_Window) {
|
||||
LOG_ERROR("GLFW: Failed to create window");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
glfwMakeContextCurrent(m_Window);
|
||||
|
||||
// GL version info
|
||||
glGetIntegerv(GL_MAJOR_VERSION, &m_glVersion[0]);
|
||||
glGetIntegerv(GL_MINOR_VERSION, &m_glVersion[1]);
|
||||
m_glVendor = (GLchar*)glGetString(GL_VENDOR);
|
||||
std::stringstream ss;
|
||||
ss << m_glVendor << " OpenGL " << m_glVersion[0] << "." << m_glVersion[1];
|
||||
#ifdef DEBUG
|
||||
ss << " DEBUG";
|
||||
#endif
|
||||
LOG_INFO(ss.str().c_str());
|
||||
glfwSetWindowTitle(m_Window, ss.str().c_str());
|
||||
|
||||
// Initialize GLEW
|
||||
if (glewInit() != GLEW_OK) {
|
||||
LOG_ERROR("GLEW: Initialization failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
LoadContent();
|
||||
}
|
||||
|
||||
void Renderer::Draw(double _dt)
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
|
||||
|
||||
//m_ShaderProgram->Bind();
|
||||
|
||||
glfwSwapBuffers(m_Window);
|
||||
}
|
||||
|
||||
void Renderer::DrawText()
|
||||
{
|
||||
//DrawShitInTextForm
|
||||
}
|
||||
|
||||
void Renderer::AddTextToDraw()
|
||||
{
|
||||
//Add to draw shit vector
|
||||
}
|
||||
|
||||
void Renderer::AddModelToDraw(Model* _model)
|
||||
{
|
||||
ModelsToRender.push_back(_model);
|
||||
}
|
||||
|
||||
//Fixa med shaders, lägga in alla verts osv.
|
||||
|
||||
|
||||
GLuint Renderer::CompileShader(GLenum shaderType, std::string fileName)
|
||||
void Renderer::LoadContent()
|
||||
{
|
||||
std::string shaderFile;
|
||||
std::ifstream in(fileName, std::ios::in);
|
||||
|
||||
if(!in)
|
||||
{
|
||||
LOG_ERROR("Error: Failed to open shader file %s", fileName);
|
||||
return 0;
|
||||
}
|
||||
in.seekg(0, std::ios::end);
|
||||
shaderFile.resize((int)in.tellg());
|
||||
in.seekg(0, std::ios::beg);
|
||||
in.read(&shaderFile[0], shaderFile.size());
|
||||
in.close();
|
||||
|
||||
GLuint shader = glCreateShader(shaderType); //GL_VERTEX_SHADER, GL_FRAGMENT_SHADER
|
||||
if(GLERROR("glCreateShader"))
|
||||
return 0;
|
||||
|
||||
const GLchar* shaderFiles = shaderFile.c_str();
|
||||
const GLint length = shaderFile.length();
|
||||
glShaderSource(shader, 1, &shaderFiles, &length);
|
||||
if(GLERROR("glShaderSource"))
|
||||
return 0;
|
||||
|
||||
glCompileShader(shader);
|
||||
GLint compileStatus;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
|
||||
if(compileStatus != GL_TRUE)
|
||||
{
|
||||
GLsizei infoLogLength;
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||
GLchar* infolog = new GLchar[infoLogLength];
|
||||
glGetShaderInfoLog(shader, infoLogLength, &infoLogLength, infolog);
|
||||
std::cerr << infolog << std::endl;
|
||||
delete[] infolog;
|
||||
}
|
||||
|
||||
if(GLERROR("glCompileShader"))
|
||||
return 0;
|
||||
|
||||
return shader;
|
||||
m_ShaderProgram->AddShader(std::unique_ptr<Shader>(new VertexShader("Shaders/Vertex.glsl")));
|
||||
m_ShaderProgram->AddShader(std::unique_ptr<Shader>(new FragmentShader("Shaders/Fragment.glsl")));
|
||||
m_ShaderProgram->Link();
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifndef Renderer_h__
|
||||
#define Renderer_h__
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -16,19 +19,33 @@
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "glerror.h"
|
||||
|
||||
#include "ShaderProgram.h"
|
||||
#include "Model.h"
|
||||
|
||||
class Renderer
|
||||
{
|
||||
public:
|
||||
GLuint shaderProgram;
|
||||
GLuint VAO;
|
||||
std::shared_ptr<ShaderProgram> m_ShaderProgram;
|
||||
|
||||
std::vector<Model*> ModelsToRender;
|
||||
|
||||
Renderer();
|
||||
|
||||
void Draw();
|
||||
void Initialize();
|
||||
void Draw(double dt);
|
||||
void DrawText();
|
||||
|
||||
GLuint CompileShader(GLenum, std::string);
|
||||
void AddModelToDraw(Model*);
|
||||
void AddTextToDraw();
|
||||
|
||||
void LoadContent();
|
||||
|
||||
GLFWwindow* GetWindow() const { return m_Window; }
|
||||
|
||||
private:
|
||||
GLFWwindow* m_Window;
|
||||
GLint m_glVersion[2];
|
||||
GLchar* m_glVendor;
|
||||
};
|
||||
|
||||
#endif // Renderer_h__
|
||||
@@ -1,38 +0,0 @@
|
||||
#include "Renderer.h"
|
||||
|
||||
Renderer::Renderer()
|
||||
{
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
}
|
||||
|
||||
void Renderer::Draw()
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
|
||||
|
||||
m_ShaderProgram->Bind();
|
||||
}
|
||||
|
||||
void Renderer::DrawText()
|
||||
{
|
||||
//DrawShitInTextForm
|
||||
}
|
||||
|
||||
void Renderer::AddTextToDraw()
|
||||
{
|
||||
//Add to draw shit vector
|
||||
}
|
||||
|
||||
void Renderer::AddObjectToDraw()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//Fixa med shaders, lägga in alla verts osv.
|
||||
|
||||
void Renderer::LoadContent()
|
||||
{
|
||||
m_ShaderProgram->AddShader(std::unique_ptr<Shader>(new VertexShader("Shaders/Vertex.glsl")));
|
||||
m_ShaderProgram->AddShader(std::unique_ptr<Shader>(new FragmentShader("Shaders/Fragment.glsl")));
|
||||
m_ShaderProgram->Link();
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <GL/glew.h>
|
||||
#define GLFW_INCLUDE_GLU
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glext.h>
|
||||
#define GLM_FORCE_RADIANS
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/constants.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "glerror.h"
|
||||
#include "ShaderProgram.h"
|
||||
|
||||
class Renderer
|
||||
{
|
||||
public:
|
||||
std::shared_ptr<ShaderProgram> m_ShaderProgram;
|
||||
GLuint VAO;
|
||||
|
||||
std::vector<std::vector<float>> Vertices;
|
||||
std::vector<std::vector<float>> Normals;
|
||||
std::vector<std::vector<float>> TextureCoords;
|
||||
|
||||
Renderer();
|
||||
|
||||
void Draw();
|
||||
void DrawText();
|
||||
|
||||
void AddObjectToDraw();
|
||||
void AddTextToDraw();
|
||||
|
||||
void LoadContent();
|
||||
GLuint CompileShader(GLenum, std::string);
|
||||
|
||||
};
|
||||
@@ -105,7 +105,9 @@ ShaderProgram::~ShaderProgram()
|
||||
void ShaderProgram::AddShader(std::unique_ptr<Shader> shader)
|
||||
{
|
||||
if (!shader->IsCompiled()) {
|
||||
shader->Compile();
|
||||
if (shader->Compile() == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
m_Shaders.push_back(std::move(shader));
|
||||
|
||||
@@ -11,7 +11,10 @@ public:
|
||||
System(World* world) : m_World(world) { }
|
||||
virtual ~System() { }
|
||||
|
||||
virtual void Update(double dt, EntityID entity, EntityID parent) = 0;
|
||||
// Called once per system every tick
|
||||
virtual void Update(double dt) { }
|
||||
// Called once for every entity in the world every tick
|
||||
virtual void Update(double dt, EntityID entity, EntityID parent) { }
|
||||
|
||||
protected:
|
||||
World* m_World;
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
#include "System.h"
|
||||
#include "logging.h"
|
||||
|
||||
#include "Components/TransformComponent.h"
|
||||
|
||||
#include "Components/Transform.h"
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "InputSystem.h"
|
||||
#include "World.h"
|
||||
|
||||
void Systems::InputSystem::Update(double dt)
|
||||
{
|
||||
m_LastKeyState = m_CurrentKeyState;
|
||||
m_LastMouseState = m_CurrentMouseState;
|
||||
|
||||
// Keyboard input
|
||||
for (int i = 0; i <= GLFW_KEY_LAST; ++i) {
|
||||
m_CurrentKeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i);
|
||||
}
|
||||
|
||||
// Mouse buttons
|
||||
for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) {
|
||||
m_CurrentKeyState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i);
|
||||
}
|
||||
|
||||
// Cursor position
|
||||
double xpos, ypos;
|
||||
glfwGetCursorPos(m_Renderer->GetWindow(), &xpos, &ypos);
|
||||
m_CurrentMouseDeltaX = xpos - m_LastMouseX;
|
||||
m_CurrentMouseDeltaY = ypos - m_LastMouseY;
|
||||
m_LastMouseX = xpos;
|
||||
m_LastMouseY = ypos;
|
||||
}
|
||||
|
||||
void Systems::InputSystem::Update(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
auto input = m_World->GetComponent<Components::Input>(entity, "Input");
|
||||
if (input == nullptr)
|
||||
return;
|
||||
|
||||
input->KeyState = m_CurrentKeyState;
|
||||
input->LastKeyState = m_LastKeyState;
|
||||
input->MouseState = m_CurrentMouseState;
|
||||
input->LastMouseState = m_LastMouseState;
|
||||
input->dX = m_CurrentMouseDeltaX;
|
||||
input->dY = m_CurrentMouseDeltaY;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef InputSystem_h__
|
||||
#define InputSystem_h__
|
||||
|
||||
#include <array>
|
||||
|
||||
#include "System.h"
|
||||
#include "Renderer.h"
|
||||
#include "Components/Input.h"
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
|
||||
class InputSystem : public System
|
||||
{
|
||||
public:
|
||||
InputSystem(World* world, Renderer* renderer)
|
||||
: System(world), m_Renderer(renderer) { }
|
||||
|
||||
void Update(double dt) override;
|
||||
void Update(double dt, EntityID entity, EntityID parent) override;
|
||||
|
||||
private:
|
||||
Renderer* m_Renderer;
|
||||
|
||||
std::array<int, GLFW_KEY_LAST> m_CurrentKeyState;
|
||||
std::array<int, GLFW_KEY_LAST> m_LastKeyState;
|
||||
std::array<int, GLFW_MOUSE_BUTTON_LAST> m_CurrentMouseState;
|
||||
std::array<int, GLFW_MOUSE_BUTTON_LAST> m_LastMouseState;
|
||||
float m_CurrentMouseDeltaX, m_CurrentMouseDeltaY;
|
||||
float m_LastMouseX, m_LastMouseY;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // InputSystem_h__
|
||||
|
||||
@@ -32,6 +32,7 @@ void World::RecursiveUpdate(std::shared_ptr<System> system, double dt, EntityID
|
||||
void World::Update(double dt)
|
||||
{
|
||||
for (auto system : m_Systems) {
|
||||
system->Update(dt);
|
||||
RecursiveUpdate(system, dt, 0);
|
||||
}
|
||||
}
|
||||
@@ -84,4 +85,19 @@ World::World()
|
||||
m_LastEntityID = 0;
|
||||
|
||||
m_Systems.push_back(std::make_shared<Systems::Collision>(this));
|
||||
|
||||
RegisterComponents();
|
||||
RegisterSystems();
|
||||
}
|
||||
|
||||
void World::RegisterComponents()
|
||||
{
|
||||
m_ComponentFactory.Register("Transform", []() { return new Components::Transform(); });
|
||||
m_ComponentFactory.Register("Input", []() { return new Components::Input(); });
|
||||
m_ComponentFactory.Register("DirectionalLight", []() { return new Components::DirectionalLight(); });
|
||||
}
|
||||
|
||||
void World::RegisterSystems()
|
||||
{
|
||||
|
||||
}
|
||||
+23
-4
@@ -6,9 +6,15 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "Renderer.h"
|
||||
|
||||
#include "Entity.h"
|
||||
#include "Component.h"
|
||||
#include "ComponentFactory.h"
|
||||
#include "Factory.h"
|
||||
#include "Components/Transform.h"
|
||||
#include "Components/Input.h"
|
||||
#include "Components/DirectionalLight.h"
|
||||
|
||||
#include "System.h"
|
||||
#include "Systems/CollisionSystem.h"
|
||||
|
||||
@@ -18,6 +24,9 @@ public:
|
||||
World();
|
||||
~World();
|
||||
|
||||
virtual void RegisterComponents();
|
||||
virtual void RegisterSystems();
|
||||
|
||||
EntityID CreateEntity(EntityID parent = 0);
|
||||
|
||||
void RemoveEntity(EntityID entity);
|
||||
@@ -29,12 +38,22 @@ public:
|
||||
template <class T>
|
||||
std::shared_ptr<T> AddComponent(EntityID entity, std::string componentType)
|
||||
{
|
||||
std::shared_ptr<T> component = std::static_pointer_cast<T>(ComponentFactory::Instance()->Create(componentType));
|
||||
std::shared_ptr<T> component = std::shared_ptr<T>(static_cast<T*>(m_ComponentFactory.Create(componentType)));
|
||||
if (component == nullptr) {
|
||||
LOG_ERROR("Failed to attach invalid component \"%s\" to entity #%i", componentType.c_str(), entity);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
m_ComponentsOfType[componentType].push_back(component);
|
||||
m_EntityComponents[entity][componentType] = component;
|
||||
return component;
|
||||
}
|
||||
|
||||
std::shared_ptr<Component> AddComponent(EntityID entity, std::string componentType)
|
||||
{
|
||||
return AddComponent<Component>(entity, componentType);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
std::shared_ptr<T> GetComponent(EntityID entity, std::string componentType)
|
||||
{
|
||||
@@ -45,9 +64,8 @@ public:
|
||||
|
||||
void AddSystem(System* system);
|
||||
|
||||
// Recursively update through the scene graph
|
||||
void Update(double dt);
|
||||
|
||||
// Recursively update through the scene graph
|
||||
void RecursiveUpdate(std::shared_ptr<System> system, double dt, EntityID parentEntity);
|
||||
|
||||
private:
|
||||
@@ -56,6 +74,7 @@ private:
|
||||
// A bottom to top tree. A map of child entities to parent entities.
|
||||
std::unordered_map<EntityID, EntityID> m_EntityParents;
|
||||
|
||||
ComponentFactory m_ComponentFactory;
|
||||
std::unordered_map<std::string, std::vector<std::shared_ptr<Component>>> m_ComponentsOfType;
|
||||
std::unordered_map<EntityID, std::map<std::string, std::shared_ptr<Component>>> m_EntityComponents;
|
||||
std::vector<std::shared_ptr<System>> m_Systems;
|
||||
|
||||
@@ -63,6 +63,13 @@
|
||||
</Link>
|
||||
<PostBuildEvent />
|
||||
<PostBuildEvent />
|
||||
<PreBuildEvent />
|
||||
<PostBuildEvent>
|
||||
<Command>$(OutDir)$(TargetName)$(TargetExt) --log_level=test_suite</Command>
|
||||
</PostBuildEvent>
|
||||
<PostBuildEvent>
|
||||
<Message>Running tests</Message>
|
||||
</PostBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
@@ -78,6 +85,13 @@
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalDependencies>boost_unit_test_framework-vc110-1_55.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
<PreBuildEvent />
|
||||
<PostBuildEvent>
|
||||
<Command>$(OutDir)$(TargetName)$(TargetExt) --log_level=test_suite</Command>
|
||||
</PostBuildEvent>
|
||||
<PostBuildEvent>
|
||||
<Message>Running tests</Message>
|
||||
</PostBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp" />
|
||||
|
||||
+12
-1
@@ -1,5 +1,7 @@
|
||||
#include "logging.h"
|
||||
|
||||
#include "World.h"
|
||||
#include "Components/TransformComponent.h"
|
||||
#include "Components/Transform.h"
|
||||
|
||||
#define BOOST_TEST_MODULE World
|
||||
#define BOOST_TEST_DYN_LINK
|
||||
@@ -51,3 +53,12 @@ BOOST_AUTO_TEST_CASE(Recycling)
|
||||
EntityID ent12 = world.CreateEntity(); // 1
|
||||
BOOST_REQUIRE(ent11 == ent12);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ComponentCreation)
|
||||
{
|
||||
World world;
|
||||
|
||||
EntityID ent = world.CreateEntity();
|
||||
BOOST_CHECK(world.AddComponent(ent, "Transform") != nullptr);
|
||||
BOOST_CHECK(world.AddComponent(ent, "Input") != nullptr);
|
||||
}
|
||||
Reference in New Issue
Block a user