diff options
Diffstat (limited to 'src/libjin/Game')
-rw-r--r-- | src/libjin/Game/je_entity.cpp | 11 | ||||
-rw-r--r-- | src/libjin/Game/je_entity.h | 75 | ||||
-rw-r--r-- | src/libjin/Game/je_game.cpp | 79 | ||||
-rw-r--r-- | src/libjin/Game/je_game.h | 88 | ||||
-rw-r--r-- | src/libjin/Game/je_scene.cpp | 0 | ||||
-rw-r--r-- | src/libjin/Game/je_scene.h | 73 |
6 files changed, 326 insertions, 0 deletions
diff --git a/src/libjin/Game/je_entity.cpp b/src/libjin/Game/je_entity.cpp new file mode 100644 index 0000000..1396518 --- /dev/null +++ b/src/libjin/Game/je_entity.cpp @@ -0,0 +1,11 @@ +#include "je_entity.h" + +namespace JinEngine +{ + namespace Game + { + + + + } // namespace Game +} // namespace JinEngine
\ No newline at end of file diff --git a/src/libjin/Game/je_entity.h b/src/libjin/Game/je_entity.h new file mode 100644 index 0000000..4a252da --- /dev/null +++ b/src/libjin/Game/je_entity.h @@ -0,0 +1,75 @@ +#ifndef __JE_GAME_OBJECT_H +#define __JE_GAME_OBJECT_H + +#include "../core/je_configuration.h" +#if defined(jin_game) + +#include <list> +#include <map> +#include <set> + +#include "../common/je_object.h" +#include "../common/je_types.h" + +namespace JinEngine +{ + namespace Game + { + + /// + /// Game object base class. + /// + class Entity : public Object + { + public: + + /// + /// + /// + virtual ~Entity(); + + /// + /// + /// + void lifecycle(); + + /// + /// + /// + void setVisible(bool isVisible); + + /// + /// + /// + void setActive(bool isActive); + + protected: + virtual void onAlive(); + virtual void onUpdate(float dt); + virtual void onDraw(); + virtual void onDie(); + + uint32 layer; // layer where entity belongs + uint32 index; // render index in layer + uint64 tag; // tag of entity, 64 now + bool mIsVisible; // if the entity is visible or not + bool mIsActive; // if the entity is joined into the logic + + }; + + /// + /// Entity list. For quickly adding and removing entities. + /// + typedef std::list<Entity*> EntityList; + + /// + /// Entity set. For searching and keeps entities unique and sorted. + /// + typedef std::set<Entity*> EntitySet; + + } // namespace Game +} // namespace JinEngine + +#endif // jin_game + +#endif
\ No newline at end of file diff --git a/src/libjin/Game/je_game.cpp b/src/libjin/Game/je_game.cpp new file mode 100644 index 0000000..6eb1d3f --- /dev/null +++ b/src/libjin/Game/je_game.cpp @@ -0,0 +1,79 @@ +#include "../core/je_configuration.h" +#if defined(jin_game) + +#include <iostream> + +#include "../time/je_timer.h" +#include "../input/je_event.h" +#include "../graphics/je_window.h" +#include "../math/je_math.h" + +#include "je_game.h" + +namespace JinEngine +{ + namespace Core + { + + using namespace JinEngine::Graphics; + using namespace JinEngine::Input; + using namespace JinEngine::Time; + using namespace JinEngine::Math; + + Game::Game() :_running(true) {}; + + /* default game loop */ + void Game::run() + { + if (_onLoad != nullptr) + _onLoad(); + Window* wnd = Window::get(); + const int FPS = wnd ? wnd->getFPS() : 60; + const int MS_PER_UPDATE = 1000.0f / FPS; + _running = true; + Event e; + int current = getMilliSecond(); + int previous = current; + int dt = 0; + while (_running) + { + while (JinEngine::Input::pollEvent(&e)) + { + if (_onEvent != nullptr) + _onEvent(&e); + if (!_running) + goto quitloop; + } + previous = current; + current = getMilliSecond(); + dt = current - previous; + if (_onUpdate != nullptr) + _onUpdate(dt); + if (_onDraw != nullptr) + _onDraw(); + wnd->swapBuffers(); + sleep(1); + } + quitloop:; + } + + bool Game::initSystem(const SettingBase* setting) + { + if (setting == nullptr) + return false; + Game::Setting* s = (Game::Setting*) setting; + _onEvent = s->eventHandler; + _onUpdate = s->updater; + _onDraw = s->drawer; + _onLoad = s->loader; + return true; + } + + void Game::quitSystem() + { + } + + } // namespace Core +} // namespace JinEngine + +#endif // jin_game
\ No newline at end of file diff --git a/src/libjin/Game/je_game.h b/src/libjin/Game/je_game.h new file mode 100644 index 0000000..78c3385 --- /dev/null +++ b/src/libjin/Game/je_game.h @@ -0,0 +1,88 @@ +#ifndef __JE_CORE_GAME_H +#define __JE_CORE_GAME_H + +#include "../core/je_configuration.h" +#if defined(jin_game) + +#include "../common/je_subsystem.hpp" +#include "../utils/je_macros.h" +#include "../input/je_Event.h" + +#include "SDL2/SDL.h" + +namespace JinEngine +{ + namespace Core + { + + /// + /// Game class. + /// + class Game : public Subsystem<Game> + { + public: + + typedef void(*onLoad)(); + typedef void(*onEvent)(JinEngine::Input::Event* e); + typedef void(*onUpdate)(int dt); + typedef void(*onDraw)(); + + /// + /// Game setting. + /// + struct Setting : SettingBase + { + onEvent eventHandler; + onUpdate updater; + onDraw drawer; + onLoad loader; + }; + + /// + /// Main game loop. + /// + void run(); + + /// + /// Stop game. + /// + inline void stop() + { + _running = false; + }; + + /// + /// Return if game is running. + /// + /// @return True if game is running, otherwise return false. + /// + inline bool running() + { + return _running; + }; + + private: + + Game(); + ~Game() {}; + + singleton(Game); + + onEvent _onEvent; + onUpdate _onUpdate; + onDraw _onDraw; + onLoad _onLoad; + + bool _running; + + bool initSystem(const SettingBase* setting); + void quitSystem(); + + }; + + } // namespace Core +} // namespace JinEngine + +#endif // jin_game + +#endif // __JE_CORE_GAME_H
\ No newline at end of file diff --git a/src/libjin/Game/je_scene.cpp b/src/libjin/Game/je_scene.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/libjin/Game/je_scene.cpp diff --git a/src/libjin/Game/je_scene.h b/src/libjin/Game/je_scene.h new file mode 100644 index 0000000..f510a1f --- /dev/null +++ b/src/libjin/Game/je_scene.h @@ -0,0 +1,73 @@ +#ifndef __JE_GAME_SCENE_H +#define __JE_GAME_SCENE_H + +#include "../core/je_configuration.h" +#if defined(jin_game) + +#include <map> +#include <list> + +#include "je_entity.h" + +namespace JinEngine +{ + namespace Game + { + + /// + /// Handle all entities. + /// + class Scene + { + public: + /// + /// + /// + void addEntity(Entity* entity); + + /// + /// + /// + EntityList& getEntitiesByTag(uint64 tag); + + /// + /// + /// + EntityList& getEntitiesByLayer(uint32 layer); + + /// + /// + /// + void setEntitiesActiveByTag(uint64 tag); + + /// + /// + /// + void setEntitiesActiveByLayer(uint32 layer); + + /// + /// + /// + void removeEntitiesByLayer(uint32 layer); + + /// + /// + /// + void removeEntitiesByTag(uint64 tag); + + protected: + // all entities + EntitySet entities; + // all entities grouped by layer, render order + std::map<uint32, EntityList> layers; + // all entities grouped by tag + std::map<uint64, EntityList> tags; + + }; + + } // namespace Game +} // namespace JinEngine + +#endif // jin_game + +#endif
\ No newline at end of file |