Entityx usage and fixed release types

This commit is contained in:
Julian Nießner
2018-05-28 20:20:29 +02:00
parent fc1d615f91
commit 247eda236d
14 changed files with 142 additions and 19 deletions

View File

@@ -45,6 +45,7 @@ include_directories(${BOX2D_INCLUDE_DIRS})
find_package(ENTITYX REQUIRED CONFIG) find_package(ENTITYX REQUIRED CONFIG)
include_directories(${ENTITYX_INCLUDE_DIRS}) include_directories(${ENTITYX_INCLUDE_DIRS})
MESSAGE( STATUS "Entityx Lib: " ${ENTITYX_LIBRARIES} )
MESSAGE( STATUS "OpenGL Lib: " ${OPENGL_gl_LIBRARY} ) MESSAGE( STATUS "OpenGL Lib: " ${OPENGL_gl_LIBRARY} )
MESSAGE( STATUS "SDL2 Lib: " ${SDL2_LIBRARIES} ) MESSAGE( STATUS "SDL2 Lib: " ${SDL2_LIBRARIES} )
MESSAGE( STATUS "SDL2_IMG Lib: " ${SDL2_IMAGE_LIBRARIES} ) MESSAGE( STATUS "SDL2_IMG Lib: " ${SDL2_IMAGE_LIBRARIES} )

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@@ -1,10 +1,18 @@
set(ENTITYX_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}") set(ENTITYX_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}")
# Support both 32 and 64 bit builds # Support both 32 and 64 bit builds
if (${CMAKE_SIZEOF_VOID_P} MATCHES 8) if (${CMAKE_BUILD_TYPE} MATCHES "DEBUG")
else () if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
set(ENTITYX_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/build/Release/entityx.lib") else ()
set(ENTITYX_DLL "${CMAKE_CURRENT_LIST_DIR}/build/Release/entityx.dll") set(ENTITYX_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/build/Debug/entityx-d.lib")
endif () set(ENTITYX_DLL "${CMAKE_CURRENT_LIST_DIR}/build/Debug/entityx.dll")
endif ()
else (${CMAKE_BUILD_TYPE} MATCHES "DEBUG")
if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
else ()
set(ENTITYX_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/build/Release/entityx.lib")
set(ENTITYX_DLL "${CMAKE_CURRENT_LIST_DIR}/build/Release/entityx.dll")
endif ()
endif (${CMAKE_BUILD_TYPE} MATCHES "DEBUG")
string(STRIP "${ENTITYX_LIBRARIES}" ENTITYX_LIBRARIES) string(STRIP "${ENTITYX_LIBRARIES}" ENTITYX_LIBRARIES)

View File

@@ -71,7 +71,7 @@ void DarkRP2D::loop(unsigned int delta)
void DarkRP2D::update(unsigned int delta) void DarkRP2D::update(unsigned int delta)
{ {
gameScreen->update(); gameScreen->update(delta);
} }
void DarkRP2D::render(unsigned int delta) void DarkRP2D::render(unsigned int delta)
@@ -85,7 +85,7 @@ void DarkRP2D::render(unsigned int delta)
Shader bitmapShader = assets->getShader("bitmapShader"); Shader bitmapShader = assets->getShader("bitmapShader");
font->drawText(bitmapShader, "Hallo du noob", 25.0f, 25.0f, 1.f, glm::vec3(0.5f, 0.8f, 0.2f)); font->drawText(bitmapShader, "Hallo du noob", 25.0f, 25.0f, 1.f, glm::vec3(0.5f, 0.8f, 0.2f));
gameScreen->render(); gameScreen->render(delta);
#ifndef NDEBUG #ifndef NDEBUG
// 1. Show a simple window. // 1. Show a simple window.

View File

@@ -0,0 +1,16 @@
#include "MovementSystem.h"
#include "components/Direction.h"
#include "components/Position.h"
void MovementSystem::update(entityx::EntityManager &es,
entityx::EventManager &events,
entityx::TimeDelta dt)
{
for (entityx::Entity entity : es.entities_with_components<Position, Direction>())
{
entityx::ComponentHandle<Position> position = entity.component<Position>();
entityx::ComponentHandle<Direction> direction = entity.component<Direction>();
position.get()->x += direction.get()->x * dt;
position.get()->y += direction.get()->y * dt;
}
}

View File

@@ -0,0 +1,10 @@
#ifndef MOVEMENTSYSTEM_H
#define MOVEMENTSYSTEM_H
#include <entityx/entityx.h>
struct MovementSystem : public entityx::System<MovementSystem> {
void update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt) override;
};
#endif //MOVEMENTSYSTEM_H

View File

@@ -0,0 +1,31 @@
#include "RenderSystem.h"
#include "components/Renderable.h"
#include "components/Position.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
void RenderSystem::update(entityx::EntityManager &es,
entityx::EventManager &events,
entityx::TimeDelta dt)
{
for (entityx::Entity entity : es.entities_with_components<Renderable, Position>())
{
entityx::ComponentHandle<Renderable> meshContainer = entity.component<Renderable>();
entityx::ComponentHandle<Position> pos = entity.component<Position>();
glm::mat4 model(1);
float x = pos.get()->x;
float y = pos.get()->y;
model = glm::translate(model, glm::vec3(x, y, 0.f));
glm::mat4 combined;
combined = _camera->getProjectionMatrix() * _camera->getViewMatrix() * model;
//Drawing
Shader defaultShader = _assets->getShader(meshContainer.get()->_shaderName);
defaultShader.SetMatrix4("MVP", combined, true);
//Draw Mesh
_renderer->draw(*(meshContainer.get()->_mesh), defaultShader);
}
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include <entityx/entityx.h>
#include "../Renderer.h"
#include "../Camera.h"
#include "../Assets.h"
struct RenderSystem : public entityx::System<RenderSystem> {
RenderSystem(Renderer *renderer, OrtographicCamera *camera, Assets *assets) : _renderer(renderer), _camera(camera), _assets(assets) {}
void update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt) override;
Renderer *_renderer;
OrtographicCamera *_camera;
Assets *_assets;
};

View File

@@ -0,0 +1,11 @@
#ifndef DIRECTION_H
#define DIRECTION_H
struct Direction
{
Direction(float x = 0.0f, float y = 0.0f) : x(x), y(y) {}
float x, y;
};
#endif

View File

@@ -0,0 +1,12 @@
#pragma once
#include "../../Mesh.h"
#include <string>
struct Renderable
{
Renderable(Mesh *mesh, std::string shaderName) : _mesh(mesh), _shaderName(shaderName) {}
Mesh *_mesh;
std::string _shaderName;
};

View File

@@ -1,5 +1,10 @@
#include "GameScreen.h" #include "GameScreen.h"
#include <entityx/entityx.h> #include "../entities/components/Position.h"
#include "../entities/components/Direction.h"
#include "../entities/components/Renderable.h"
#include "../entities/MovementSystem.h"
#include "../entities/RenderSystem.h"
#include <SDL.h> #include <SDL.h>
#include <iostream> #include <iostream>
@@ -26,9 +31,14 @@ GameScreen::GameScreen(Renderer *renderer, Assets *assets)
camera = new OrtographicCamera(); camera = new OrtographicCamera();
//Create Entities //Create Entities
//entityx::EntityX ex; entityx::Entity charakter = ex.entities.create();
//entityx::Entity charakter = ex.entities.create(); charakter.assign<Position>();
//charakter.assign<Position>(); charakter.assign<Direction>(1.f,0.f); //apply speed right
charakter.assign<Renderable>(obj, "defaultShader");
ex.systems.add<MovementSystem>();
ex.systems.add<RenderSystem>(renderer,camera,assets);
ex.systems.configure();
} }
GameScreen::~GameScreen() GameScreen::~GameScreen()
@@ -46,16 +56,18 @@ void GameScreen::pause()
{ {
} }
void GameScreen::render() void GameScreen::render(unsigned int delta)
{ {
//Drawing double dt = ((double) delta) * 0.001; //(10^-3) milli
ex.systems.update<RenderSystem>(dt);
/*//Drawing
Shader defaultShader = assets->getShader("defaultShader"); Shader defaultShader = assets->getShader("defaultShader");
defaultShader.SetMatrix4("MVP", camera->getCombinedMatrix(), true); defaultShader.SetMatrix4("MVP", camera->getCombinedMatrix(), true);
//Draw Mesh //Draw Mesh
renderer->draw(*obj, defaultShader); renderer->draw(*obj, defaultShader);*/
} }
void GameScreen::update() void GameScreen::update(unsigned int delta)
{ {
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event) != 0) while (SDL_PollEvent(&event) != 0)
@@ -66,6 +78,8 @@ void GameScreen::update()
command->execute(*camera); command->execute(*camera);
} }
} }
double dt = ((double) delta) * 0.001; //(10^-3) milli
ex.systems.update<MovementSystem>(dt);
} }
void GameScreen::resize() void GameScreen::resize()

View File

@@ -8,6 +8,8 @@
#include "../Camera.h" #include "../Camera.h"
#include "../input/InputHandler.h" #include "../input/InputHandler.h"
#include <entityx/entityx.h>
class GameScreen : public Screen class GameScreen : public Screen
{ {
public: public:
@@ -16,8 +18,8 @@ class GameScreen : public Screen
virtual void hide() override; virtual void hide() override;
virtual void pause() override; virtual void pause() override;
virtual void render() override; virtual void render(unsigned int delta) override;
virtual void update() override; virtual void update(unsigned int delta) override;
virtual void resize() override; virtual void resize() override;
virtual void resume() override; virtual void resume() override;
virtual void show() override; virtual void show() override;
@@ -31,6 +33,9 @@ class GameScreen : public Screen
InputHandler *inputHandler; InputHandler *inputHandler;
OrtographicCamera *camera; OrtographicCamera *camera;
Mesh *obj; Mesh *obj;
//Part of the class
entityx::EntityX ex;
}; };
#endif #endif

View File

@@ -12,8 +12,8 @@ class Screen
/* Called when this screen is no longer the current screen for a Game. */ /* Called when this screen is no longer the current screen for a Game. */
virtual void hide() = 0; virtual void hide() = 0;
virtual void pause() = 0; virtual void pause() = 0;
virtual void render() = 0; virtual void render(unsigned int delta) = 0;
virtual void update() = 0; virtual void update(unsigned int delta) = 0;
virtual void resize() = 0; virtual void resize() = 0;
virtual void resume() = 0; virtual void resume() = 0;
virtual void show() = 0; virtual void show() = 0;