Added Entityx logic (Movement|Render|Destruct)
This commit is contained in:
@@ -1,17 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <SDL2/SDL_image.h>
|
||||
|
||||
#include "DarkRP2D.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) {
|
||||
this->gRenderer = gRenderer;
|
||||
this->inputHandler = inputHandler;
|
||||
//Load PNG texture
|
||||
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 fps = 0;
|
||||
unsigned int acc = 0;
|
||||
void DarkRP2D::loop(unsigned int deltaTime) {
|
||||
accumulator += deltaTime;
|
||||
@@ -25,26 +39,30 @@ void DarkRP2D::create(SDL_Renderer *gRenderer, InputHandler *inputHandler) {
|
||||
skippedFrames = 0;
|
||||
while (accumulator >= MS_PER_UPDATE && skippedFrames <= MAX_FRAMESKIP) {
|
||||
accumulator -= MS_PER_UPDATE;
|
||||
update(0.0f);
|
||||
update(MS_PER_UPDATE/1000.0f); //Converted to Sec for normalization
|
||||
ups++;
|
||||
skippedFrames++;
|
||||
}
|
||||
|
||||
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;
|
||||
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
|
||||
SDL_RenderClear( gRenderer );
|
||||
ex->systems.update<RenderSystem>(deltaInSec);
|
||||
|
||||
SDL_Rect textureRect; //create a rect
|
||||
textureRect.x = 0; //controls the rect's x coordinate
|
||||
@@ -66,5 +84,7 @@ void DarkRP2D::create(SDL_Renderer *gRenderer, InputHandler *inputHandler) {
|
||||
DarkRP2D::~DarkRP2D() {
|
||||
//Free loaded image
|
||||
SDL_DestroyTexture( gTexture );
|
||||
delete ex;
|
||||
|
||||
gTexture = NULL;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#define DARKRP2D_H
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <entityx/entityx.h>
|
||||
|
||||
#include "input/InputHandler.h"
|
||||
|
||||
class DarkRP2D {
|
||||
@@ -18,13 +20,14 @@ public:
|
||||
static const int MAX_FRAMESKIP = 5;
|
||||
|
||||
SDL_Renderer *gRenderer = NULL;
|
||||
entityx::EntityX *ex = NULL;
|
||||
|
||||
DarkRP2D() {}
|
||||
~DarkRP2D();
|
||||
void create(SDL_Renderer *gRenderer, InputHandler *inputHandler);
|
||||
void loop(unsigned int deltaTime);
|
||||
void update(float deltaInSec);
|
||||
void render(float deltaInSec);
|
||||
void update(double deltaInSec);
|
||||
void render(double deltaInSec);
|
||||
void resize(int width, int height);
|
||||
};
|
||||
|
||||
|
||||
@@ -132,11 +132,8 @@ void close() {
|
||||
int main(int argc, char *argv[]) {
|
||||
//Initialization flag
|
||||
bool success = true;
|
||||
|
||||
success = init();
|
||||
|
||||
SDL_SetWindowResizable(gWindow, SDL_TRUE);
|
||||
|
||||
//Main loop flag
|
||||
bool running = true;
|
||||
//Event handler
|
||||
|
||||
19
src/entities/DestructSystem.cpp
Normal file
19
src/entities/DestructSystem.cpp
Normal 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;
|
||||
}
|
||||
13
src/entities/DestructSystem.h
Normal file
13
src/entities/DestructSystem.h
Normal 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
|
||||
7
src/entities/MovementSystem.cpp
Normal file
7
src/entities/MovementSystem.cpp
Normal 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;
|
||||
}
|
||||
11
src/entities/MovementSystem.h
Normal file
11
src/entities/MovementSystem.h
Normal 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
|
||||
7
src/entities/RenderSystem.cpp
Normal file
7
src/entities/RenderSystem.cpp
Normal 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;
|
||||
}
|
||||
11
src/entities/RenderSystem.h
Normal file
11
src/entities/RenderSystem.h
Normal 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
|
||||
10
src/entities/components/Position.h
Normal file
10
src/entities/components/Position.h
Normal 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
|
||||
11
src/entities/components/Texture.h
Normal file
11
src/entities/components/Texture.h
Normal 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
|
||||
@@ -19,7 +19,9 @@ private:
|
||||
public:
|
||||
InputHandler();
|
||||
~InputHandler();
|
||||
//Called when an KeyDown or KeyUP event happened
|
||||
void inputEvent(SDL_Event *e);
|
||||
//Called by the main GameLoop to get the newest Command
|
||||
Command* handleInput();
|
||||
};
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
class TestCommand : public Command {
|
||||
public:
|
||||
virtual void execute();
|
||||
void execute() override;
|
||||
};
|
||||
|
||||
#endif //TESTCOMMAND_H
|
||||
|
||||
5
src/input/commands/moveCommands/DownCommand.cpp
Normal file
5
src/input/commands/moveCommands/DownCommand.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "DownCommand.h"
|
||||
|
||||
void DownCommand::execute() {
|
||||
|
||||
}
|
||||
12
src/input/commands/moveCommands/DownCommand.h
Normal file
12
src/input/commands/moveCommands/DownCommand.h
Normal 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
|
||||
6
src/input/commands/moveCommands/LeftCommand.h
Normal file
6
src/input/commands/moveCommands/LeftCommand.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef LEFTCOMMAND_H
|
||||
#define LEFTCOMMAND_H
|
||||
|
||||
#include "../Command.h"
|
||||
|
||||
#endif //LEFTCOMMAND_H
|
||||
6
src/input/commands/moveCommands/RightCommand.h
Normal file
6
src/input/commands/moveCommands/RightCommand.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef RIGHTCOMMAND_H
|
||||
#define RIGHTCOMMAND_H
|
||||
|
||||
#include "../Command.h"
|
||||
|
||||
#endif //RIGHTCOMMAND_H
|
||||
6
src/input/commands/moveCommands/UpCommand.h
Normal file
6
src/input/commands/moveCommands/UpCommand.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef RIGHTCOMMAND_H
|
||||
#define RIGHTCOMMAND_H
|
||||
|
||||
#include "../Command.h"
|
||||
|
||||
#endif //RIGHTCOMMAND_H
|
||||
Reference in New Issue
Block a user