diff options
Diffstat (limited to 'src/libjin/game')
-rw-r--r-- | src/libjin/game/je_application.cpp | 81 | ||||
-rw-r--r-- | src/libjin/game/je_application.h | 88 | ||||
-rw-r--r-- | src/libjin/game/je_entity.cpp | 0 | ||||
-rw-r--r-- | src/libjin/game/je_entity.h | 27 | ||||
-rw-r--r-- | src/libjin/game/je_gameobject.cpp | 11 | ||||
-rw-r--r-- | src/libjin/game/je_gameobject.h | 83 | ||||
-rw-r--r-- | src/libjin/game/je_scene.cpp | 0 | ||||
-rw-r--r-- | src/libjin/game/je_scene.h | 73 |
8 files changed, 363 insertions, 0 deletions
diff --git a/src/libjin/game/je_application.cpp b/src/libjin/game/je_application.cpp new file mode 100644 index 0000000..723a809 --- /dev/null +++ b/src/libjin/game/je_application.cpp @@ -0,0 +1,81 @@ +#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 "../graphics/je_gl.h" +#include "../math/je_math.h" + +#include "je_application.h" + +using namespace JinEngine::Graphics; +using namespace JinEngine::Input; +using namespace JinEngine::Time; +using namespace JinEngine::Math; + +namespace JinEngine +{ + namespace Game + { + + Application::Application() :_running(true) {}; + + /* default game loop */ + void Application::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); + glClear(GL_COLOR_BUFFER_BIT); + if (_onDraw != nullptr) + _onDraw(); + wnd->swapBuffers(); + sleep(1); + } + quitloop:; + } + + bool Application::initSystem(const SettingBase* setting) + { + if (setting == nullptr) + return false; + Application::Setting* s = (Application::Setting*) setting; + _onEvent = s->eventHandler; + _onUpdate = s->updater; + _onDraw = s->drawer; + _onLoad = s->loader; + return true; + } + + void Application::quitSystem() + { + } + + } // namespace Core +} // namespace JinEngine + +#endif // jin_game
\ No newline at end of file diff --git a/src/libjin/game/je_application.h b/src/libjin/game/je_application.h new file mode 100644 index 0000000..094de6a --- /dev/null +++ b/src/libjin/game/je_application.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 Game + { + + /// + /// Game class. + /// + class Application : public Subsystem<Application> + { + 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: + + Application(); + ~Application() {}; + + singleton(Application); + + 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_entity.cpp b/src/libjin/game/je_entity.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/libjin/game/je_entity.cpp diff --git a/src/libjin/game/je_entity.h b/src/libjin/game/je_entity.h new file mode 100644 index 0000000..80c6ff3 --- /dev/null +++ b/src/libjin/game/je_entity.h @@ -0,0 +1,27 @@ +#ifndef __JE_ENTITY_H__ +#define __JE_ENTITY_H__ + +#include "je_gameobject.h" + +namespace JinEngine +{ + namespace Game + { +/* + /// + /// + /// + class Entity : public GameObject + { + public: + Entity(); + + private: + + + }; +*/ + } // namespace Game +} // namespace JinEngine + +#endif
\ No newline at end of file diff --git a/src/libjin/game/je_gameobject.cpp b/src/libjin/game/je_gameobject.cpp new file mode 100644 index 0000000..1396518 --- /dev/null +++ b/src/libjin/game/je_gameobject.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_gameobject.h b/src/libjin/game/je_gameobject.h new file mode 100644 index 0000000..7c6ec2b --- /dev/null +++ b/src/libjin/game/je_gameobject.h @@ -0,0 +1,83 @@ +#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" +#include "../graphics/je_sprite.h" + +namespace JinEngine +{ + namespace Game + { + /* + /// + /// Game object base class. + /// + class GameObject : public Object + { + public: + + /// + /// + /// + virtual ~GameObject(); + + /// + /// + /// + void lifecycle(); + + /// + /// + /// + void setVisible(bool isVisible); + + /// + /// + /// + void setActive(bool isActive); + + /// + /// + /// + void setOrder(uint32 order); + + protected: + virtual void onAlive(); + virtual void onUpdate(float dt); + virtual void onDraw(); + virtual void onDestroy(); + + uint32 mLayer; // layer where entity belongs + uint32 mOrder; // render index in layer + uint32 mTag; // tag of entity, support 32 tags now + bool mIsVisible; // if the entity is visible or not + bool mIsActive; // if the entity is joined into the logic + + Math::Transform mTransform; + + }; + + /// + /// Entity list. For quickly adding and removing entities. + /// + typedef std::list<GameObject*> EntityList; + + /// + /// Entity set. For searching and keeps entities unique and sorted. + /// + typedef std::set<GameObject*> EntitySet; + */ + } // namespace Game +} // namespace JinEngine + +#endif // jin_game + +#endif
\ 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..811d1a8 --- /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(uint32 tag); + + /// + /// + /// + EntityList& getEntitiesByLayer(uint32 layer); + + /// + /// + /// + void setEntitiesActiveByTag(uint32 tag); + + /// + /// + /// + void setEntitiesActiveByLayer(uint32 layer); + + /// + /// + /// + void removeEntitiesByLayer(uint32 layer); + + /// + /// + /// + void removeEntitiesByTag(uint32 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<uint32, EntityList> tags; + + }; + + } // namespace Game +} // namespace JinEngine + +#endif // jin_game + +#endif
\ No newline at end of file |