diff options
-rw-r--r-- | src/components/Component.hpp | 9 | ||||
-rw-r--r-- | src/components/Name.hpp | 37 | ||||
-rw-r--r-- | src/components/Player.hpp | 16 | ||||
-rw-r--r-- | src/components/Position.hpp | 44 | ||||
-rw-r--r-- | src/components/Render.hpp | 52 | ||||
-rw-r--r-- | src/components/Script.hpp | 57 | ||||
-rw-r--r-- | src/components/Velocity.hpp | 42 | ||||
-rw-r--r-- | src/engine.cpp | 54 | ||||
-rw-r--r-- | src/engine.hpp | 40 | ||||
-rw-r--r-- | src/gamerun.hpp | 68 | ||||
-rw-r--r-- | src/input.hpp | 82 | ||||
-rw-r--r-- | src/main.cpp | 37 | ||||
-rw-r--r-- | src/player.cpp | 57 | ||||
-rw-r--r-- | src/player.hpp | 77 | ||||
-rw-r--r-- | src/render.cpp | 7 | ||||
-rw-r--r-- | src/render.hpp | 39 | ||||
-rw-r--r-- | src/script.cpp | 10 | ||||
-rw-r--r-- | src/script.hpp | 136 | ||||
-rw-r--r-- | src/shader.hpp | 162 |
19 files changed, 523 insertions, 503 deletions
diff --git a/src/components/Component.hpp b/src/components/Component.hpp index 98095ba..5a062bd 100644 --- a/src/components/Component.hpp +++ b/src/components/Component.hpp @@ -20,13 +20,12 @@ #include <sol/sol.hpp> -template <typename T> +template<typename T> class Component { - public: - Component(){}; - +public: virtual T FromLua(sol::object) = 0; }; -#endif//COMPONENT_HPP_ +#endif // COMPONENT_HPP_ + diff --git a/src/components/Name.hpp b/src/components/Name.hpp index c472cbb..94b7531 100644 --- a/src/components/Name.hpp +++ b/src/components/Name.hpp @@ -15,29 +15,30 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef NAME_HPP_ -#define NAME_HPP_ +#ifndef COMPONENT_NAME_HPP_ +#define COMPONENT_NAME_HPP_ -#include "components/Component.hpp" +#include "Component.hpp" #include <string> struct Name : Component<Name>, entityx::Component<Name> { - public: - std::string name; - Name(std::string _name) : name(_name) {} - Name(void): name() {}; - - Name FromLua(sol::object ref) - { - if (ref.get_type() == sol::type::string) { - this->name = ref.as<std::string>(); - } else { - throw std::string("Name component not formatted properly"); - } - return *this; - } +public: + std::string name; + Name(std::string _name = std::string()) : + name(_name) {} + + Name FromLua(sol::object ref) + { + if (ref.get_type() == sol::type::string) + this->name = ref.as<std::string>(); + else + throw std::string("Name component not formatted properly"); + + return *this; + } }; -#endif//NAME_HPP_ +#endif // COMPONENT_NAME_HPP_ + diff --git a/src/components/Player.hpp b/src/components/Player.hpp index 6fb6311..5c1e870 100644 --- a/src/components/Player.hpp +++ b/src/components/Player.hpp @@ -18,17 +18,19 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef PLAYER_HPP_ -#define PLAYER_HPP_ +#ifndef COMPONENT_PLAYER_HPP_ +#define COMPONENT_PLAYER_HPP_ #include "Component.hpp" struct Player : Component<Player>, entityx::Component<Player> { - Player FromLua([[maybe_unused]] sol::object ref) - { - return *this; - } +public: + Player FromLua([[maybe_unused]] sol::object ref) + { + return *this; + } }; -#endif // PLAYER_HPP_ +#endif // COMPONENT_PLAYER_HPP_ + diff --git a/src/components/Position.hpp b/src/components/Position.hpp index 8a6bd74..c801998 100644 --- a/src/components/Position.hpp +++ b/src/components/Position.hpp @@ -16,31 +16,33 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef POSITION_HPP_ -#define POSITION_HPP_ +#ifndef COMPONENT_POSITION_HPP_ +#define COMPONENT_POSITION_HPP_ -#include "components/Component.hpp" +#include "Component.hpp" struct Position : Component<Position>, entityx::Component<Position> { - public: - double x, y; - Position(double _x, double _y): x(_x), y(_y) {} - Position(void): x(0), y(0) {} - - Position FromLua(sol::object ref) - { - if (ref.get_type() == sol::type::table) { - sol::table tab = ref; - if (tab["x"] != nullptr) - this->x = tab["x"]; - if (tab["y"] != nullptr) - this->y = tab["y"]; - } else { - throw std::string("Position table not formatted properly"); - } - return *this; +public: + double x, y; + + Position(double _x = 0, double _y = 0) : + x(_x), y(_y) {} + + Position FromLua(sol::object ref) + { + if (ref.get_type() == sol::type::table) { + sol::table tab = ref; + if (tab["x"] != nullptr) + this->x = tab["x"]; + if (tab["y"] != nullptr) + this->y = tab["y"]; + } else { + throw std::string("Position table not formatted properly"); } + return *this; + } }; -#endif//POSITION_HPP_ +#endif // COMPONENT_POSITION_HPP_ + diff --git a/src/components/Render.hpp b/src/components/Render.hpp index 5a9b1a4..451f2d1 100644 --- a/src/components/Render.hpp +++ b/src/components/Render.hpp @@ -15,38 +15,38 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef RENDERC_HPP_ -#define RENDERC_HPP_ +#ifndef COMPONENT_RENDER_HPP_ +#define COMPONENT_RENDER_HPP_ -#include <components/Component.hpp> +#include "Component.hpp" struct Render : Component<Render>, entityx::Component<Render> { - public: - std::string texture; - bool visible; +public: + std::string texture; + bool visible; - Render(std::string _file): texture(_file), visible(true) {} - Render(): texture(), visible(false) {} + Render(std::string _file) : + texture(_file), visible(true) {} + Render(void) : + texture(), visible(false) {} - Render FromLua(sol::object ref) - { - if (ref.get_type() == sol::type::table) { - sol::table tab = ref; - if (tab["visible"].get_type() == sol::type::boolean) { - this->visible = tab["visible"]; - } - if (tab["texture"].get_type() == sol::type::string) { - this->texture = tab["texture"]; - } - } else { - throw std::string( - "Render component table formatted incorrectly" - ); - } - return *this; + Render FromLua(sol::object ref) + { + if (ref.get_type() == sol::type::table) { + sol::table tab = ref; + if (tab["visible"].get_type() == sol::type::boolean) + this->visible = tab["visible"]; + if (tab["texture"].get_type() == sol::type::string) + this->texture = tab["texture"]; + } else { + throw std::string( + "Render component table formatted incorrectly" + ); } - + return *this; + } }; -#endif//RENDERC_HPP_ +#endif // COMPONENT_RENDER_HPP_ + diff --git a/src/components/Script.hpp b/src/components/Script.hpp index 7570802..66addc8 100644 --- a/src/components/Script.hpp +++ b/src/components/Script.hpp @@ -16,37 +16,38 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef SCRIPT_COMPONENT_HPP_ -#define SCRIPT_COMPONENT_HPP_ +#ifndef COMPONENT_SCRIPT_HPP_ +#define COMPONENT_SCRIPT_HPP_ -#include <components/Component.hpp> +#include "Component.hpp" struct Scripted : Component<Scripted>, entityx::Component<Scripted> { - public: - sol::table caller; - Scripted() {} - Scripted(sol::table call): caller(call) {} - - - ~Scripted() - {} - - void cleanup() - { - caller = sol::nil; - } - - Scripted FromLua(sol::object) - { - return *this; - } - - void exec() { - if (caller["Idle"] == sol::type::function) - caller["Idle"](caller); // Call idle function and pass itself - // in or to fulfill the 'self' param - } +public: + sol::table caller; + + Scripted(void) {} + Scripted(sol::table call) : + caller(call) {} + + ~Scripted() {} + + void cleanup(void) + { + caller = sol::nil; + } + + Scripted FromLua([[maybe_unused]] sol::object ref) + { + return *this; + } + + void exec(void) { + if (caller["Idle"] == sol::type::function) + caller["Idle"](caller); // Call idle function and pass itself + // in or to fulfill the 'self' param + } }; -#endif//SCRIPT_COMPONENT_HPP_ +#endif // COMPONENT_SCRIPT_HPP_ + diff --git a/src/components/Velocity.hpp b/src/components/Velocity.hpp index c2b85e8..29c0e5c 100644 --- a/src/components/Velocity.hpp +++ b/src/components/Velocity.hpp @@ -17,31 +17,33 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef VELOCITY_HPP_ -#define VELOCITY_HPP_ +#ifndef COMPONENT_VELOCITY_HPP_ +#define COMPONENT_VELOCITY_HPP_ -#include <components/Component.hpp> +#include "Component.hpp" struct Velocity : Component<Velocity>, entityx::Component<Velocity> { - public: - double x, y; - Velocity(): x(0), y(0) {} - Velocity(double _x, double _y): x(_x), y(_y) {} +public: + double x, y; - Velocity FromLua(sol::object ref) - { - if (ref.get_type() == sol::type::table) { - sol::table tab = ref; - if (tab["x"] != nullptr) - this->x = tab["x"]; - if (tab["y"] != nullptr) - this->y = tab["y"]; - } else { - throw std::string("Velocity table not formatted properly"); - } - return *this; + Velocity(double _x = 0, double _y = 0) : + x(_x), y(_y) {} + + Velocity FromLua(sol::object ref) + { + if (ref.get_type() == sol::type::table) { + sol::table tab = ref; + if (tab["x"] != nullptr) + this->x = tab["x"]; + if (tab["y"] != nullptr) + this->y = tab["y"]; + } else { + throw std::string("Velocity table not formatted properly"); } + return *this; + } }; -#endif//VELOCITY_HPP_ +#endif // COMPONENT_VELOCITY_HPP_ + diff --git a/src/engine.cpp b/src/engine.cpp index 26dcd0a..f235651 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -32,30 +32,28 @@ int Engine::init(void) { - systems.add<GameRunSystem>(); - systems.add<InputSystem>(); - systems.add<PlayerSystem>(); + systems.add<GameRunSystem>(); + systems.add<InputSystem>(); + systems.add<PlayerSystem>(); systems.add<RenderSystem>(); systems.add<ScriptSystem>(); + systems.configure(); - systems.configure(); + systems.system<ScriptSystem>()->init(); - systems.system<ScriptSystem>()->init(); - - return 0; + return 0; } void Engine::logicLoop(void) { - using namespace std::chrono_literals; + using namespace std::chrono_literals; namespace cr = std::chrono; typedef std::chrono::high_resolution_clock mc; entityx::TimeDelta dt = 0; /**< Elapsed milliseconds since each loop */ double elapsed = 0; - while (shouldRun()) { - + while (shouldRun()) { auto start = mc::now(); /*********************** @@ -68,7 +66,7 @@ void Engine::logicLoop(void) p.y += (v.y * dt/1000.0); }); - systems.update<InputSystem>(dt); + systems.update<InputSystem>(dt); /******************* * LOGIC UPDATES * @@ -88,10 +86,10 @@ void Engine::logicLoop(void) auto diff = end - start; auto micros = cr::duration_cast<cr::microseconds>(diff); auto msc = micros.count(); - dt = static_cast<double>(msc)/1000.0; + dt = static_cast<double>(msc) / 1000.0; elapsed += dt; - std::this_thread::yield(); - } + std::this_thread::yield(); + } // Remove all Lua references from entities entities.each<Scripted>([](entityx::Entity, Scripted &f){ f.cleanup(); }); @@ -99,29 +97,29 @@ void Engine::logicLoop(void) void Engine::renderLoop(void) { - while (shouldRun()) { - systems.update<RenderSystem>(0); - std::this_thread::yield(); - } + while (shouldRun()) { + systems.update<RenderSystem>(0); + std::this_thread::yield(); + } } void Engine::run(void) { - // Start logic thread - logicThread = std::thread([this](void) { - logicLoop(); - }); + // Start logic thread + logicThread = std::thread([this](void) { + logicLoop(); + }); - // Keep render loop on main thread - renderLoop(); + // Keep render loop on main thread + renderLoop(); - // Done, bring logic thread back - logicThread.join(); + // Done, bring logic thread back + logicThread.join(); } bool Engine::shouldRun(void) { - auto grs = systems.system<GameRunSystem>(); - return grs ? grs->shouldRun() : true; + auto grs = systems.system<GameRunSystem>(); + return grs ? grs->shouldRun() : true; } diff --git a/src/engine.hpp b/src/engine.hpp index fb14093..26b1c45 100644 --- a/src/engine.hpp +++ b/src/engine.hpp @@ -32,33 +32,33 @@ class Engine { private: - entityx::EventManager events; - entityx::EntityManager entities; - entityx::SystemManager systems; + entityx::EventManager events; + entityx::EntityManager entities; + entityx::SystemManager systems; - std::thread logicThread; + std::thread logicThread; - void logicLoop(void); - void renderLoop(void); + void logicLoop(void); + void renderLoop(void); - bool shouldRun(void); + bool shouldRun(void); public: - Engine(void) : - entities(events), - systems(entities, events) {} + Engine(void) : + entities(events), + systems(entities, events) {} - /** - * Initializes the game engine. - * @return Zero on success, non-zero otherwise - */ - int init(void); + /** + * Initializes the game engine. + * @return Zero on success, non-zero otherwise + */ + int init(void); - /** - * Runs the game engine. - * Function returns when game is told to end/exit. - */ - void run(void); + /** + * Runs the game engine. + * Function returns when game is told to end/exit. + */ + void run(void); }; #endif // ENGINE_HPP_ diff --git a/src/gamerun.hpp b/src/gamerun.hpp index efe6ed9..ceb0716 100644 --- a/src/gamerun.hpp +++ b/src/gamerun.hpp @@ -18,8 +18,8 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -#ifndef GAMERUN_HPP_ -#define GAMERUN_HPP_ +#ifndef SYSTEM_GAMERUN_HPP_ +#define SYSTEM_GAMERUN_HPP_ #include "input.hpp" @@ -33,44 +33,48 @@ * Listens for a key event to tell the game to exit. */ class GameRunSystem : public entityx::System<GameRunSystem>, - public entityx::Receiver<GameRunSystem> { + public entityx::Receiver<GameRunSystem> +{ private: - std::atomic_bool shouldRunFlag; + std::atomic_bool shouldRunFlag; public: - /** - * Configures the system to wait for the exit event. - */ - void configure([[maybe_unused]] entityx::EntityManager& entities, - entityx::EventManager& events) { - shouldRunFlag.store(true); + /** + * Configures the system to wait for the exit event. + */ + void configure([[maybe_unused]] entityx::EntityManager& entities, + entityx::EventManager& events) + { + shouldRunFlag.store(true); - events.subscribe<KeyUpEvent>(*this); + events.subscribe<KeyUpEvent>(*this); - std::cout << "Press escape to exit." << std::endl; - } + std::cout << "Press escape to exit." << std::endl; + } - // Unused - void update([[maybe_unused]] entityx::EntityManager& entities, - [[maybe_unused]] entityx::EventManager& events, - [[maybe_unused]] entityx::TimeDelta dt) final {} + // Unused + void update([[maybe_unused]] entityx::EntityManager& entities, + [[maybe_unused]] entityx::EventManager& events, + [[maybe_unused]] entityx::TimeDelta dt) final {} - /** - * Receiver for key release events, used to listen for game exit key. - */ - void receive(const KeyUpEvent& kue) { - if (kue.sym == SDLK_ESCAPE) - shouldRunFlag.store(false); - } + /** + * Receiver for key release events, used to listen for game exit key. + */ + void receive(const KeyUpEvent& kue) + { + if (kue.sym == SDLK_ESCAPE) + shouldRunFlag.store(false); + } - /** - * Checks if the game exit key has been pressed. - * @return True if the game should exit - */ - inline bool shouldRun(void) const { - return shouldRunFlag.load(); - } + /** + * Checks if the game exit key has been pressed. + * @return True if the game should exit + */ + bool shouldRun(void) const + { + return shouldRunFlag.load(); + } }; -#endif // GAMERUN_HPP_ +#endif // SYSTEM_GAMERUN_HPP_ diff --git a/src/input.hpp b/src/input.hpp index 39d4045..fa92c39 100644 --- a/src/input.hpp +++ b/src/input.hpp @@ -18,8 +18,8 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -#ifndef INPUT_HPP_ -#define INPUT_HPP_ +#ifndef SYSTEM_INPUT_HPP_ +#define SYSTEM_INPUT_HPP_ #include <entityx/entityx.h> #include <SDL2/SDL.h> @@ -28,12 +28,13 @@ * @class KeyUpEvent * Stores info regarding key releases. */ -struct KeyUpEvent { - KeyUpEvent(const SDL_Keysym& keysym) : - sym(keysym.sym), mod(keysym.mod) {} +struct KeyUpEvent +{ + SDL_Keycode sym; + Uint16 mod; - SDL_Keycode sym; - Uint16 mod; + KeyUpEvent(const SDL_Keysym& keysym) : + sym(keysym.sym), mod(keysym.mod) {} }; /** @@ -41,48 +42,49 @@ struct KeyUpEvent { * Stores info regarding key presses. */ struct KeyDownEvent { - KeyDownEvent(const SDL_Keysym& keysym) : - sym(keysym.sym), mod(keysym.mod) {} + SDL_Keycode sym; + Uint16 mod; - SDL_Keycode sym; - Uint16 mod; + KeyDownEvent(const SDL_Keysym& keysym) : + sym(keysym.sym), mod(keysym.mod) {} }; /** * @class InputSystem * Listens for user input from SDL, and emits input events accordingly. */ -class InputSystem : public entityx::System<InputSystem> { +class InputSystem : public entityx::System<InputSystem> +{ public: - /** - * Prepares the system for running. - */ - void configure([[maybe_unused]] entityx::EntityManager& entities, - [[maybe_unused]] entityx::EventManager& events) final { - } + /** + * Prepares the system for running. + */ + void configure([[maybe_unused]] entityx::EntityManager& entities, + [[maybe_unused]] entityx::EventManager& events) final {} - /** - * Updates the system by checking for SDL events. - */ - void update([[maybe_unused]] entityx::EntityManager& entities, - [[maybe_unused]] entityx::EventManager& events, - [[maybe_unused]] entityx::TimeDelta dt) final { - for (SDL_Event event; SDL_PollEvent(&event);) { - switch (event.type) { - case SDL_KEYUP: - if (auto key = event.key; key.repeat == 0) - events.emit<KeyUpEvent>(key.keysym); - break; - case SDL_KEYDOWN: - if (auto key = event.key; key.repeat == 0) - events.emit<KeyDownEvent>(key.keysym); - break; - default: - break; - } - } - } + /** + * Updates the system by checking for SDL events. + */ + void update([[maybe_unused]] entityx::EntityManager& entities, + [[maybe_unused]] entityx::EventManager& events, + [[maybe_unused]] entityx::TimeDelta dt) final + { + for (SDL_Event event; SDL_PollEvent(&event);) { + switch (event.type) { + case SDL_KEYUP: + if (auto key = event.key; key.repeat == 0) + events.emit<KeyUpEvent>(key.keysym); + break; + case SDL_KEYDOWN: + if (auto key = event.key; key.repeat == 0) + events.emit<KeyDownEvent>(key.keysym); + break; + default: + break; + } + } + } }; -#endif // INPUT_HPP_ +#endif // SYSTEM_INPUT_HPP_ diff --git a/src/main.cpp b/src/main.cpp index c56fd79..7876f8a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,23 +26,22 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[]) { - // Initialize SDL - if (SDL_Init(0) != 0) { - std::cerr << "SDL failed to initialize: " << SDL_GetError() - << std::endl; - return -1; - } else { - atexit(SDL_Quit); - } - - //LuaTest(); - - // Create the engine - Engine engine; - engine.init(); - - // Go go go! - engine.run(); - - return 0; + // Initialize SDL + if (SDL_Init(0) != 0) { + std::cerr << "SDL failed to initialize: " << SDL_GetError() + << std::endl; + return -1; + } else { + atexit(SDL_Quit); + } + + // Create the engine + Engine engine; + engine.init(); + + // Go go go! + engine.run(); + + return 0; } + diff --git a/src/player.cpp b/src/player.cpp index c72a4e4..7d1653e 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -23,53 +23,54 @@ #include "components/Velocity.hpp" void PlayerSystem::configure([[maybe_unused]] entityx::EntityManager& entities, - entityx::EventManager& events) + entityx::EventManager& events) { - events.subscribe<entityx::ComponentAddedEvent<Player>>(*this); - events.subscribe<entityx::ComponentRemovedEvent<Player>>(*this); - events.subscribe<KeyUpEvent>(*this); - events.subscribe<KeyDownEvent>(*this); + events.subscribe<entityx::ComponentAddedEvent<Player>>(*this); + events.subscribe<entityx::ComponentRemovedEvent<Player>>(*this); + events.subscribe<KeyUpEvent>(*this); + events.subscribe<KeyDownEvent>(*this); } void PlayerSystem::update([[maybe_unused]] entityx::EntityManager& entites, - [[maybe_unused]] entityx::EventManager& events, - [[maybe_unused]] entityx::TimeDelta dt) + [[maybe_unused]] entityx::EventManager& events, + [[maybe_unused]] entityx::TimeDelta dt) { } void PlayerSystem::receive(const entityx::ComponentAddedEvent<Player>& cae) { - player = cae.entity; + player = cae.entity; } void PlayerSystem::receive(const entityx::ComponentRemovedEvent<Player>& cre) { - if (player == cre.entity) - player.invalidate(); + if (player == cre.entity) + player.invalidate(); } void PlayerSystem::receive(const KeyDownEvent& kue) { - if (player.valid()) { - if (kue.sym == SDLK_a) { - if (auto vel = player.component<Velocity>(); vel) - vel->x -= GROUND_VELOCITY; - } else if (kue.sym == SDLK_d) { - if (auto vel = player.component<Velocity>(); vel) - vel->x += GROUND_VELOCITY; - } - } + if (player.valid()) { + if (kue.sym == SDLK_a) { + if (auto vel = player.component<Velocity>(); vel) + vel->x += GROUND_VELOCITY; + } else if (kue.sym == SDLK_d) { + if (auto vel = player.component<Velocity>(); vel) + vel->x -= GROUND_VELOCITY; + } + } } void PlayerSystem::receive(const KeyUpEvent& kue) { - if (player.valid()) { - if (kue.sym == SDLK_a) { - if (auto vel = player.component<Velocity>(); vel) - vel->x += GROUND_VELOCITY; - } else if (kue.sym == SDLK_d) { - if (auto vel = player.component<Velocity>(); vel) - vel->x -= GROUND_VELOCITY; - } - } + if (player.valid()) { + if (kue.sym == SDLK_a) { + if (auto vel = player.component<Velocity>(); vel) + vel->x -= GROUND_VELOCITY; + } else if (kue.sym == SDLK_d) { + if (auto vel = player.component<Velocity>(); vel) + vel->x += GROUND_VELOCITY; + } + } } + diff --git a/src/player.hpp b/src/player.hpp index 4469745..b1821c5 100644 --- a/src/player.hpp +++ b/src/player.hpp @@ -18,8 +18,8 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef PLAYERSYSTEM_HPP_ -#define PLAYERSYSTEM_HPP_ +#ifndef SYSTEM_PLAYER_HPP_ +#define SYSTEM_PLAYER_HPP_ #include <SDL2/SDL.h> #include <entityx/entityx.h> @@ -32,49 +32,50 @@ * Controls player entity movement. */ class PlayerSystem : public entityx::System<PlayerSystem>, - public entityx::Receiver<PlayerSystem> + public entityx::Receiver<PlayerSystem> { - private: - /** - * Defines player's horizontal movement velocity. - */ - constexpr static double GROUND_VELOCITY = 100; +private: + /** + * Defines player's horizontal movement velocity. + */ + constexpr static double GROUND_VELOCITY = 100; - entityx::Entity player; + entityx::Entity player; - public: - /** - * Prepares the system for running. - */ - void configure([[maybe_unused]] entityx::EntityManager& entities, - entityx::EventManager& events) final; +public: + /** + * Prepares the system for running. + */ + void configure(entityx::EntityManager& entities, + entityx::EventManager& events) final; - /** - * Updates the scripting system. - */ - void update([[maybe_unused]] entityx::EntityManager& entites, - [[maybe_unused]] entityx::EventManager& events, - [[maybe_unused]] entityx::TimeDelta dt) final; + /** + * Updates the scripting system. + */ + void update(entityx::EntityManager& entites, + entityx::EventManager& events, + entityx::TimeDelta dt) final; - /** - * Captures the player entity. - */ - void receive(const entityx::ComponentAddedEvent<Player>& cae); + /** + * Captures the player entity. + */ + void receive(const entityx::ComponentAddedEvent<Player>& cae); - /** - * Invalidates the system's player entity (assume player is gone). - */ - void receive(const entityx::ComponentRemovedEvent<Player>& cre); + /** + * Invalidates the system's player entity (assume player is gone). + */ + void receive(const entityx::ComponentRemovedEvent<Player>& cre); - /** - * Applies velocity based on key press. - */ - void receive(const KeyDownEvent& kue); + /** + * Applies velocity based on key press. + */ + void receive(const KeyDownEvent& kue); - /** - * Removes applied velocity - */ - void receive(const KeyUpEvent& kue); + /** + * Removes applied velocity + */ + void receive(const KeyUpEvent& kue); }; -#endif // PLAYERSYSTEM_HPP_ +#endif // SYSTEM_PLAYER_HPP_ + diff --git a/src/render.cpp b/src/render.cpp index 7c047f6..2771535 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -22,8 +22,8 @@ #include <components/Render.hpp> #include <components/Position.hpp> -void RenderSystem::configure([[maybe_unused]]entityx::EntityManager& entities, - [[maybe_unused]]entityx::EventManager& events) +void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities, + [[maybe_unused]] entityx::EventManager& events) { init(); } @@ -80,7 +80,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities, *************/ entities.each<Render, Position>( - [this, a](entityx::Entity, Render &r, Position &p){ + [this, a](entityx::Entity, Render &r, Position &p) { if (!r.visible) return; @@ -174,3 +174,4 @@ int RenderSystem::init(void) return 0; } + diff --git a/src/render.hpp b/src/render.hpp index 21869a3..6362d63 100644 --- a/src/render.hpp +++ b/src/render.hpp @@ -19,12 +19,12 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef RENDER_HPP_ -#define RENDER_HPP_ +#ifndef SYSTEM_RENDER_HPP_ +#define SYSTEM_RENDER_HPP_ -#include <entityx/entityx.h> +#include "shader.hpp" -#include <shader.hpp> +#include <entityx/entityx.h> #include <SDL2/SDL.h> @@ -37,37 +37,37 @@ class RenderSystem : public entityx::System<RenderSystem> { private: - constexpr static const char *title = "gamedev2"; - constexpr static int width = 640; - constexpr static int height = 480; + constexpr static const char *title = "gamedev2"; + constexpr static int width = 640; + constexpr static int height = 480; - std::unique_ptr<SDL_Window, void (*)(SDL_Window *)> window; - SDL_GLContext context; + std::unique_ptr<SDL_Window, void (*)(SDL_Window *)> window; + SDL_GLContext context; Shader worldShader; + public: - RenderSystem(void): - window(nullptr, SDL_DestroyWindow) - {} + RenderSystem(void) : + window(nullptr, SDL_DestroyWindow) {} ~RenderSystem(void) { - SDL_GL_DeleteContext(context); + SDL_GL_DeleteContext(context); SDL_Quit(); } /** * Prepares the system for running. */ - void configure([[maybe_unused]]entityx::EntityManager& entities, - [[maybe_unused]]entityx::EventManager& events) final; + void configure(entityx::EntityManager& entities, + entityx::EventManager& events) final; /** * Updates the render system. */ - void update([[maybe_unused]] entityx::EntityManager& entities, - [[maybe_unused]] entityx::EventManager& events, - [[maybe_unused]] entityx::TimeDelta dt) final; + void update(entityx::EntityManager& entities, + entityx::EventManager& events, + entityx::TimeDelta dt) final; /** * Initializes the rendering system @@ -76,4 +76,5 @@ public: int init(void); }; -#endif//RENDER_HPP_ +#endif // SYSTEM_RENDER_HPP_ + diff --git a/src/script.cpp b/src/script.cpp index ffefb1d..80ac538 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -24,14 +24,15 @@ * SYSTEM SPECIFIC * *********************/ -void ScriptSystem::configure([[maybe_unused]]entityx::EntityManager& entities, - [[maybe_unused]]entityx::EventManager& events) +void ScriptSystem::configure(entityx::EntityManager& entities, + entityx::EventManager& events) { this->manager = &entities; this->events = &events; events.subscribe<EntitySpawnEvent>(*this); + // Init after systems.configure() in engine.cpp //init(); } @@ -39,7 +40,6 @@ void ScriptSystem::update([[maybe_unused]] entityx::EntityManager& entites, [[maybe_unused]] entityx::EventManager& events, [[maybe_unused]] entityx::TimeDelta dt) { - } @@ -81,9 +81,8 @@ void ScriptSystem::doFile(void) #include <components/Script.hpp> #include <components/Velocity.hpp> -void ScriptSystem::scriptExport() +void ScriptSystem::scriptExport(void) { - std::function<sol::table(sol::table)> func = [this](sol::table t){ return spawn(t);}; @@ -174,3 +173,4 @@ sol::table ScriptSystem::spawn(sol::object param) return *toRet; } + diff --git a/src/script.hpp b/src/script.hpp index be02542..84c698c 100644 --- a/src/script.hpp +++ b/src/script.hpp @@ -18,85 +18,85 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef SCRIPT_HPP_ -#define SCRIPT_HPP_ +#ifndef SYSTEM_SCRIPT_HPP_ +#define SYSTEM_SCRIPT_HPP_ #include <entityx/entityx.h> #include <sol/sol.hpp> -struct EntitySpawnEvent { - EntitySpawnEvent (sol::object ref) - : ref(ref) {} - +struct EntitySpawnEvent +{ sol::object ref; + + EntitySpawnEvent(sol::object _ref) : + ref(_ref) {} }; /** * @class ScriptSystem * Handles all the game's scripting requests */ -class ScriptSystem : public entityx::System<ScriptSystem> - , public entityx::Receiver<ScriptSystem> +class ScriptSystem : public entityx::System<ScriptSystem>, + public entityx::Receiver<ScriptSystem> { - private: - /** - * The script systems internal lua state that handles all - * interactions between C and Lua - */ - sol::state lua; - - entityx::EventManager* events; - entityx::EntityManager* manager; - - public: - ScriptSystem(void) - {} - - ~ScriptSystem(void) - {} - - /** - * Prepares the system for running. - */ - void configure([[maybe_unused]]entityx::EntityManager& entities, - [[maybe_unused]]entityx::EventManager& events) final; - - /** - * Updates the scripting system. - */ - void update([[maybe_unused]] entityx::EntityManager& entites, - [[maybe_unused]] entityx::EventManager& events, - [[maybe_unused]] entityx::TimeDelta dt) final; - - /** - * Receives all entity spawning events and manages the - * script counterpart. - */ - void receive (const EntitySpawnEvent &toSpawn); - - /** - * Initializes the lua states and libraries. - * @return Zero on success, non-zero on error - */ - int init(void); - - /** - * Run the initialization file. - */ - void doFile(void); - - /** - * The function called by lua scripts in order to spawn an entity. - * @param param The table that must be passed in by Lua. This is a - * sol2 object instead of a sol2 table because this allows us to handle - * errors easier instead of letting sol2 do the error handling. - */ - sol::table spawn(sol::object param); - - /** - * Contains all calls that export components/functions to lua. - */ - void scriptExport(); +private: + /** + * The script systems internal lua state that handles all + * interactions between C and Lua + */ + sol::state lua; + + entityx::EventManager* events; + entityx::EntityManager* manager; + +public: + ScriptSystem(void) {} + + ~ScriptSystem(void) {} + + /** + * Prepares the system for running. + */ + void configure(entityx::EntityManager& entities, + entityx::EventManager& events) final; + + /** + * Updates the scripting system. + */ + void update(entityx::EntityManager& entites, + entityx::EventManager& events, + entityx::TimeDelta dt) final; + + /** + * Receives all entity spawning events and manages the + * script counterpart. + */ + void receive(const EntitySpawnEvent &toSpawn); + + /** + * Initializes the lua states and libraries. + * @return Zero on success, non-zero on error + */ + int init(void); + + /** + * Run the initialization file. + */ + void doFile(void); + + /** + * The function called by lua scripts in order to spawn an entity. + * @param param The table that must be passed in by Lua. This is a + * sol2 object instead of a sol2 table because this allows us to handle + * errors easier instead of letting sol2 do the error handling. + */ + sol::table spawn(sol::object param); + + /** + * Contains all calls that export components/functions to lua. + */ + void scriptExport(void); }; -#endif//SCRIPT_HPP_ +#endif // SYSTEM_SCRIPT_HPP_ + diff --git a/src/shader.hpp b/src/shader.hpp index e67a4a0..8691ab0 100644 --- a/src/shader.hpp +++ b/src/shader.hpp @@ -17,8 +17,8 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef SHADER_HPP -#define SHADER_HPP +#ifndef SYSTEM_SHADER_HPP_ +#define SYSTEM_SHADER_HPP_ #include <string> #include <unordered_map> @@ -27,81 +27,87 @@ class Shader { - private: - /** - * Reads the contents of a shader file and returns a c++ string - * object representing the contents - * @param The file path - * @return The shader contents - */ - std::string readShader(std::string); - - /** - * Creates a shader from a filename and a shader type - * @param The file path containing the shader data - * @param What type of shader to create - * @return Memory address of shader location - */ - GLuint createShader(std::string, GLenum); - - GLuint program; /**< GPU Memory address of shader program */ - - /** - * Name of shader attributes and their corresponding memory - * locations in which the data exists - */ - std::unordered_map<std::string, GLuint> attributes; - - /** - * Name of shader uniforms in and their corresponding memory - * locations in which the data exists - */ - std::unordered_map<std::string, GLuint> uniforms; - public: - Shader(): program(-1) {} - /** - * Given the file paths of two shaders, create a shader program. - * @param v The file path of the vertex shader file. - * @param f The file path of the fragment shader file. - * @return The GPU Memory location of the shader program - */ - GLuint createProgram(std::string v, std::string f); - - /** - * Finds and binds an attribute to the current shader if possible - * @param The attribute to bind in the shader - * @return The memory address of the new attribute, or -1 if the - * attribute doesn't exist in the shader - */ - GLint addAttribute(std::string); - /** - * Finds and binds a uniform to the current shader if possible - * @param The uniform to bind in the shader - * @return The memory address of the new uniform, or -1 if the - * uniform doesn't exist in the shader - */ - GLint addUniform(std::string); - - /** - * Finds the GPU memory address of the given attribute in the shader - * program - * @param The attribute to find - * @return The attribute memory location, or -1 if it doesn't exist - */ - GLint getAttribute(std::string); - /** - * Finds the GPU memory address of the given uniform in the shader - * program - * @param The uniform to find - * @return The uniform memory location, or -1 if it doesn't exist - */ - GLint getUniform(std::string); - - /** - * Gets the memory address of the program stored in this object - * @return The GPU memory address of the shader program stored - */ - GLuint getProgram(); +private: + /** + * Reads the contents of a shader file and returns a c++ string + * object representing the contents + * @param The file path + * @return The shader contents + */ + std::string readShader(std::string); + + /** + * Creates a shader from a filename and a shader type + * @param The file path containing the shader data + * @param What type of shader to create + * @return Memory address of shader location + */ + GLuint createShader(std::string, GLenum); + + GLuint program; /**< GPU Memory address of shader program */ + + /** + * Name of shader attributes and their corresponding memory + * locations in which the data exists + */ + std::unordered_map<std::string, GLuint> attributes; + + /** + * Name of shader uniforms in and their corresponding memory + * locations in which the data exists + */ + std::unordered_map<std::string, GLuint> uniforms; + +public: + Shader(void) : + program(-1) {} + + /** + * Given the file paths of two shaders, create a shader program. + * @param v The file path of the vertex shader file. + * @param f The file path of the fragment shader file. + * @return The GPU Memory location of the shader program + */ + GLuint createProgram(std::string v, std::string f); + + /** + * Finds and binds an attribute to the current shader if possible + * @param The attribute to bind in the shader + * @return The memory address of the new attribute, or -1 if the + * attribute doesn't exist in the shader + */ + GLint addAttribute(std::string); + + /** + * Finds and binds a uniform to the current shader if possible + * @param The uniform to bind in the shader + * @return The memory address of the new uniform, or -1 if the + * uniform doesn't exist in the shader + */ + GLint addUniform(std::string); + + /** + * Finds the GPU memory address of the given attribute in the shader + * program + * @param The attribute to find + * @return The attribute memory location, or -1 if it doesn't exist + */ + GLint getAttribute(std::string); + + /** + * Finds the GPU memory address of the given uniform in the shader + * program + * @param The uniform to find + * @return The uniform memory location, or -1 if it doesn't exist + */ + GLint getUniform(std::string); + + /** + * Gets the memory address of the program stored in this object + * @return The GPU memory address of the shader program stored + */ + GLuint getProgram(); }; -#endif // SHADER_HPP +#endif // SYSTEM_SHADER_HPP_ + |