Added Entityx logic (Movement|Render|Destruct)

This commit is contained in:
Gulum
2017-11-01 13:30:08 +01:00
parent 70857cb258
commit 6f68ac7db5
18 changed files with 163 additions and 17 deletions

View File

@@ -1,17 +1,31 @@
#include <iostream> #include <iostream>
#include <SDL2/SDL_image.h>
#include "DarkRP2D.h" #include "DarkRP2D.h"
#include "utility/Utility.h" #include "utility/Utility.h"
#include "entities/components/Position.h"
#include "entities/MovementSystem.h"
#include "entities/RenderSystem.h"
#include "entities/DestructSystem.h"
void DarkRP2D::create(SDL_Renderer *gRenderer, InputHandler *inputHandler) { void DarkRP2D::create(SDL_Renderer *gRenderer, InputHandler *inputHandler) {
this->gRenderer = gRenderer; this->gRenderer = gRenderer;
this->inputHandler = inputHandler; this->inputHandler = inputHandler;
//Load PNG texture //Load PNG texture
gTexture = utility::loadTexture( "assets/police_officer.png", gRenderer); gTexture = utility::loadTexture( "assets/police_officer.png", gRenderer);
ex = new entityx::EntityX();
ex->systems.add<DestructSystem>();
ex->systems.add<MovementSystem>();
ex->systems.add<RenderSystem>();
ex->systems.configure();
entityx::Entity entity = ex->entities.create();
entity.assign<Position>();
entity.destroy();
} }
unsigned int ups = 0; unsigned int ups = 0;
unsigned int fps = 0;
unsigned int acc = 0; unsigned int acc = 0;
void DarkRP2D::loop(unsigned int deltaTime) { void DarkRP2D::loop(unsigned int deltaTime) {
accumulator += deltaTime; accumulator += deltaTime;
@@ -25,26 +39,30 @@ void DarkRP2D::create(SDL_Renderer *gRenderer, InputHandler *inputHandler) {
skippedFrames = 0; skippedFrames = 0;
while (accumulator >= MS_PER_UPDATE && skippedFrames <= MAX_FRAMESKIP) { while (accumulator >= MS_PER_UPDATE && skippedFrames <= MAX_FRAMESKIP) {
accumulator -= MS_PER_UPDATE; accumulator -= MS_PER_UPDATE;
update(0.0f); update(MS_PER_UPDATE/1000.0f); //Converted to Sec for normalization
ups++; ups++;
skippedFrames++; skippedFrames++;
} }
if(acc >= 1000) { if(acc >= 1000) {
std::cout << "Updates per second: " << ups << std::endl; std::cout << "Updates per second: " << ups << " Frames per Second: " << fps << std::endl;
acc -= 1000; acc -= 1000;
ups = 0; ups = 0;
fps = 0;
} }
render(0.0f); fps++;
render(MS_PER_UPDATE/1000.0f);
} }
void DarkRP2D::update(float deltaInSec) { void DarkRP2D::update(double deltaInSec) {
ex->systems.update<DestructSystem>(deltaInSec);
ex->systems.update<MovementSystem>(deltaInSec);
} }
void DarkRP2D::render(float deltaInSec) { void DarkRP2D::render(double deltaInSec) {
//Clear screen //Clear screen
SDL_RenderClear( gRenderer ); SDL_RenderClear( gRenderer );
ex->systems.update<RenderSystem>(deltaInSec);
SDL_Rect textureRect; //create a rect SDL_Rect textureRect; //create a rect
textureRect.x = 0; //controls the rect's x coordinate textureRect.x = 0; //controls the rect's x coordinate
@@ -66,5 +84,7 @@ void DarkRP2D::create(SDL_Renderer *gRenderer, InputHandler *inputHandler) {
DarkRP2D::~DarkRP2D() { DarkRP2D::~DarkRP2D() {
//Free loaded image //Free loaded image
SDL_DestroyTexture( gTexture ); SDL_DestroyTexture( gTexture );
delete ex;
gTexture = NULL; gTexture = NULL;
} }

View File

@@ -2,6 +2,8 @@
#define DARKRP2D_H #define DARKRP2D_H
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <entityx/entityx.h>
#include "input/InputHandler.h" #include "input/InputHandler.h"
class DarkRP2D { class DarkRP2D {
@@ -9,7 +11,7 @@ private:
unsigned int accumulator = 0; unsigned int accumulator = 0;
int skippedFrames = 0; int skippedFrames = 0;
//test texture //test texture
SDL_Texture* gTexture = NULL; SDL_Texture *gTexture = NULL;
InputHandler *inputHandler; InputHandler *inputHandler;
public: public:
@@ -17,14 +19,15 @@ public:
static const unsigned int MS_PER_UPDATE = 17; // 1 / UPDATES_PER_SECOND ~ 16,777777777 static const unsigned int MS_PER_UPDATE = 17; // 1 / UPDATES_PER_SECOND ~ 16,777777777
static const int MAX_FRAMESKIP = 5; static const int MAX_FRAMESKIP = 5;
SDL_Renderer* gRenderer = NULL; SDL_Renderer *gRenderer = NULL;
entityx::EntityX *ex = NULL;
DarkRP2D() {} DarkRP2D() {}
~DarkRP2D(); ~DarkRP2D();
void create(SDL_Renderer *gRenderer, InputHandler *inputHandler); void create(SDL_Renderer *gRenderer, InputHandler *inputHandler);
void loop(unsigned int deltaTime); void loop(unsigned int deltaTime);
void update(float deltaInSec); void update(double deltaInSec);
void render(float deltaInSec); void render(double deltaInSec);
void resize(int width, int height); void resize(int width, int height);
}; };

View File

@@ -130,13 +130,10 @@ void close() {
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
//Initialization flag //Initialization flag
bool success = true; bool success = true;
success = init(); success = init();
SDL_SetWindowResizable(gWindow, SDL_TRUE); SDL_SetWindowResizable(gWindow, SDL_TRUE);
//Main loop flag //Main loop flag
bool running = true; bool running = true;
//Event handler //Event handler

View File

@@ -0,0 +1,19 @@
#include "DestructSystem.h"
#include "components/Texture.h"
#include <iostream>
void DestructSystem::configure(entityx::EventManager &event_manager) {
event_manager.subscribe<entityx::EntityDestroyedEvent>(*this);
}
void DestructSystem::update(entityx::EntityManager &entities, entityx::EventManager &events, entityx::TimeDelta dt){}
void DestructSystem::receive(const entityx::EntityDestroyedEvent &destroyedEvent) {
/*entityx::ComponentHandle<Texture> texture = destroyedEvent.entity.component<Texture>();
if (texture) {
// Do stuff with texture
}*/
std::cout << "!!!!!!!!!!!!!!!!!!!Entity destroyed!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
}

View File

@@ -0,0 +1,13 @@
#ifndef DESTRUCTSYSTEM_H
#define DESTRUCTSYSTEM_H
#include <entityx/entityx.h>
class DestructSystem : public entityx::System<DestructSystem>, public entityx::Receiver<DestructSystem> {
public:
void configure(entityx::EventManager &event_manager);
void update(entityx::EntityManager &entities, entityx::EventManager &events, entityx::TimeDelta dt);
void receive(const entityx::EntityDestroyedEvent &destroyedEvent);
};
#endif //DESTRUCTSYSTEM_H

View File

@@ -0,0 +1,7 @@
#include "MovementSystem.h"
#include <iostream>
void MovementSystem::update(entityx::EntityManager &entities, entityx::EventManager &events, entityx::TimeDelta dt) {
//std::cout << "Moving delta: " << dt << std::endl;
}

View File

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

View File

@@ -0,0 +1,7 @@
#include "RenderSystem.h"
#include <iostream>
void RenderSystem::update(entityx::EntityManager &entities, entityx::EventManager &events, entityx::TimeDelta dt) {
//std::cout << "Rendering delta: " << dt << std::endl;
}

View File

@@ -0,0 +1,11 @@
#ifndef RENDERSYSTEM_H
#define RENDERSYSTEM_H
#include <entityx/entityx.h>
class RenderSystem : public entityx::System<RenderSystem> {
public:
void update(entityx::EntityManager &entities, entityx::EventManager &events, entityx::TimeDelta dt) override;
};
#endif //RENDERSYSTEM_H

View File

@@ -0,0 +1,10 @@
#ifndef POSITION_H
#define POSITION_H
struct Position {
Position(float x = 0.0f, float y = 0.0f) : x(x), y(y) {}
float x, y;
};
#endif //POSITION_H

View File

@@ -0,0 +1,11 @@
#ifndef TEXTURE_H
#define TEXTURE_H
#include <SDL2/SDL.h>
struct Texture {
Texture(SDL_Texture *gTexture) : texture(gTexture) {}
SDL_Texture *texture;
};
#endif //TEXTURE_H

View File

@@ -19,7 +19,9 @@ private:
public: public:
InputHandler(); InputHandler();
~InputHandler(); ~InputHandler();
//Called when an KeyDown or KeyUP event happened
void inputEvent(SDL_Event *e); void inputEvent(SDL_Event *e);
//Called by the main GameLoop to get the newest Command
Command* handleInput(); Command* handleInput();
}; };

View File

@@ -5,7 +5,7 @@
class TestCommand : public Command { class TestCommand : public Command {
public: public:
virtual void execute(); void execute() override;
}; };
#endif //TESTCOMMAND_H #endif //TESTCOMMAND_H

View File

@@ -0,0 +1,5 @@
#include "DownCommand.h"
void DownCommand::execute() {
}

View File

@@ -0,0 +1,12 @@
#ifndef DOWNCOMMAND_H
#define DOWNCOMMAND_H
#include "../Command.h"
class DownCommand : public Command {
public:
DownCommand();
void execute() override;
};
#endif //DOWNCOMMAND_H

View File

@@ -0,0 +1,6 @@
#ifndef LEFTCOMMAND_H
#define LEFTCOMMAND_H
#include "../Command.h"
#endif //LEFTCOMMAND_H

View File

@@ -0,0 +1,6 @@
#ifndef RIGHTCOMMAND_H
#define RIGHTCOMMAND_H
#include "../Command.h"
#endif //RIGHTCOMMAND_H

View File

@@ -0,0 +1,6 @@
#ifndef RIGHTCOMMAND_H
#define RIGHTCOMMAND_H
#include "../Command.h"
#endif //RIGHTCOMMAND_H