diff --git a/src/DarkRP2D.cpp b/src/DarkRP2D.cpp index 2a6bfe0..00bcba7 100644 --- a/src/DarkRP2D.cpp +++ b/src/DarkRP2D.cpp @@ -1,17 +1,31 @@ #include -#include #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(); + ex->systems.add(); + ex->systems.add(); + ex->systems.configure(); + + entityx::Entity entity = ex->entities.create(); + entity.assign(); + 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(deltaInSec); + ex->systems.update(deltaInSec); } - void DarkRP2D::render(float deltaInSec) { + void DarkRP2D::render(double deltaInSec) { //Clear screen SDL_RenderClear( gRenderer ); + ex->systems.update(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; } diff --git a/src/DarkRP2D.h b/src/DarkRP2D.h index aed1ce0..6435c6e 100644 --- a/src/DarkRP2D.h +++ b/src/DarkRP2D.h @@ -2,6 +2,8 @@ #define DARKRP2D_H #include +#include + #include "input/InputHandler.h" class DarkRP2D { @@ -9,7 +11,7 @@ private: unsigned int accumulator = 0; int skippedFrames = 0; //test texture - SDL_Texture* gTexture = NULL; + SDL_Texture *gTexture = NULL; InputHandler *inputHandler; public: @@ -17,14 +19,15 @@ public: static const unsigned int MS_PER_UPDATE = 17; // 1 / UPDATES_PER_SECOND ~ 16,777777777 static const int MAX_FRAMESKIP = 5; - SDL_Renderer* gRenderer = NULL; + 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); }; diff --git a/src/DesktopLauncher.cpp b/src/DesktopLauncher.cpp index 100f9a1..329c19d 100644 --- a/src/DesktopLauncher.cpp +++ b/src/DesktopLauncher.cpp @@ -130,13 +130,10 @@ void close() { } int main(int argc, char *argv[]) { - //Initialization flag - bool success = true; - + //Initialization flag + bool success = true; success = init(); - SDL_SetWindowResizable(gWindow, SDL_TRUE); - //Main loop flag bool running = true; //Event handler diff --git a/src/entities/DestructSystem.cpp b/src/entities/DestructSystem.cpp new file mode 100644 index 0000000..86b591d --- /dev/null +++ b/src/entities/DestructSystem.cpp @@ -0,0 +1,19 @@ +#include "DestructSystem.h" +#include "components/Texture.h" + +#include + +void DestructSystem::configure(entityx::EventManager &event_manager) { + event_manager.subscribe(*this); +} + +void DestructSystem::update(entityx::EntityManager &entities, entityx::EventManager &events, entityx::TimeDelta dt){} + + +void DestructSystem::receive(const entityx::EntityDestroyedEvent &destroyedEvent) { + /*entityx::ComponentHandle texture = destroyedEvent.entity.component(); + if (texture) { + // Do stuff with texture + }*/ + std::cout << "!!!!!!!!!!!!!!!!!!!Entity destroyed!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; +} diff --git a/src/entities/DestructSystem.h b/src/entities/DestructSystem.h new file mode 100644 index 0000000..05da5f1 --- /dev/null +++ b/src/entities/DestructSystem.h @@ -0,0 +1,13 @@ +#ifndef DESTRUCTSYSTEM_H +#define DESTRUCTSYSTEM_H + +#include + +class DestructSystem : public entityx::System, public entityx::Receiver { +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 diff --git a/src/entities/MovementSystem.cpp b/src/entities/MovementSystem.cpp new file mode 100644 index 0000000..21e0967 --- /dev/null +++ b/src/entities/MovementSystem.cpp @@ -0,0 +1,7 @@ +#include "MovementSystem.h" + +#include + +void MovementSystem::update(entityx::EntityManager &entities, entityx::EventManager &events, entityx::TimeDelta dt) { + //std::cout << "Moving delta: " << dt << std::endl; +} diff --git a/src/entities/MovementSystem.h b/src/entities/MovementSystem.h new file mode 100644 index 0000000..d9e4086 --- /dev/null +++ b/src/entities/MovementSystem.h @@ -0,0 +1,11 @@ +#ifndef MOVEMENTSYSTEM_H +#define MOVEMENTSYSTEM_H + +#include + +class MovementSystem : public entityx::System { +public: + void update(entityx::EntityManager &entities, entityx::EventManager &events, entityx::TimeDelta dt) override; +}; + +#endif //MOVEMENTSYSTEM_H diff --git a/src/entities/RenderSystem.cpp b/src/entities/RenderSystem.cpp new file mode 100644 index 0000000..7bad130 --- /dev/null +++ b/src/entities/RenderSystem.cpp @@ -0,0 +1,7 @@ +#include "RenderSystem.h" + +#include + +void RenderSystem::update(entityx::EntityManager &entities, entityx::EventManager &events, entityx::TimeDelta dt) { + //std::cout << "Rendering delta: " << dt << std::endl; +} diff --git a/src/entities/RenderSystem.h b/src/entities/RenderSystem.h new file mode 100644 index 0000000..a7325e6 --- /dev/null +++ b/src/entities/RenderSystem.h @@ -0,0 +1,11 @@ +#ifndef RENDERSYSTEM_H +#define RENDERSYSTEM_H + +#include + +class RenderSystem : public entityx::System { +public: + void update(entityx::EntityManager &entities, entityx::EventManager &events, entityx::TimeDelta dt) override; +}; + +#endif //RENDERSYSTEM_H diff --git a/src/entities/components/Position.h b/src/entities/components/Position.h new file mode 100644 index 0000000..3a23154 --- /dev/null +++ b/src/entities/components/Position.h @@ -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 diff --git a/src/entities/components/Texture.h b/src/entities/components/Texture.h new file mode 100644 index 0000000..f5f19b8 --- /dev/null +++ b/src/entities/components/Texture.h @@ -0,0 +1,11 @@ +#ifndef TEXTURE_H +#define TEXTURE_H + +#include + +struct Texture { + Texture(SDL_Texture *gTexture) : texture(gTexture) {} + SDL_Texture *texture; +}; + +#endif //TEXTURE_H diff --git a/src/input/InputHandler.h b/src/input/InputHandler.h index 91245bb..74fa497 100644 --- a/src/input/InputHandler.h +++ b/src/input/InputHandler.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(); }; diff --git a/src/input/commands/TestCommand.h b/src/input/commands/TestCommand.h index ba39959..8b680fe 100644 --- a/src/input/commands/TestCommand.h +++ b/src/input/commands/TestCommand.h @@ -5,7 +5,7 @@ class TestCommand : public Command { public: - virtual void execute(); + void execute() override; }; #endif //TESTCOMMAND_H diff --git a/src/input/commands/moveCommands/DownCommand.cpp b/src/input/commands/moveCommands/DownCommand.cpp new file mode 100644 index 0000000..95340a1 --- /dev/null +++ b/src/input/commands/moveCommands/DownCommand.cpp @@ -0,0 +1,5 @@ +#include "DownCommand.h" + +void DownCommand::execute() { + +} diff --git a/src/input/commands/moveCommands/DownCommand.h b/src/input/commands/moveCommands/DownCommand.h new file mode 100644 index 0000000..fa832d2 --- /dev/null +++ b/src/input/commands/moveCommands/DownCommand.h @@ -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 diff --git a/src/input/commands/moveCommands/LeftCommand.h b/src/input/commands/moveCommands/LeftCommand.h new file mode 100644 index 0000000..9cfd822 --- /dev/null +++ b/src/input/commands/moveCommands/LeftCommand.h @@ -0,0 +1,6 @@ +#ifndef LEFTCOMMAND_H +#define LEFTCOMMAND_H + +#include "../Command.h" + +#endif //LEFTCOMMAND_H diff --git a/src/input/commands/moveCommands/RightCommand.h b/src/input/commands/moveCommands/RightCommand.h new file mode 100644 index 0000000..5835d94 --- /dev/null +++ b/src/input/commands/moveCommands/RightCommand.h @@ -0,0 +1,6 @@ +#ifndef RIGHTCOMMAND_H +#define RIGHTCOMMAND_H + +#include "../Command.h" + +#endif //RIGHTCOMMAND_H diff --git a/src/input/commands/moveCommands/UpCommand.h b/src/input/commands/moveCommands/UpCommand.h new file mode 100644 index 0000000..5835d94 --- /dev/null +++ b/src/input/commands/moveCommands/UpCommand.h @@ -0,0 +1,6 @@ +#ifndef RIGHTCOMMAND_H +#define RIGHTCOMMAND_H + +#include "../Command.h" + +#endif //RIGHTCOMMAND_H