From 066e5987c515dfc34537d73ca9d2a81ddd1f9e1b Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 21 Oct 2018 13:37:27 +0800 Subject: =?UTF-8?q?*=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/Audio/SDL/je_sdl_audio.h | 83 +++++- src/libjin/Audio/SDL/je_sdl_source.cpp | 3 +- src/libjin/Audio/SDL/je_sdl_source.h | 186 ++++++++++++- src/libjin/Audio/je_audio_manager.h | 34 ++- src/libjin/Audio/je_source.h | 77 +++++- src/libjin/Common/je_array.hpp | 77 ++++-- src/libjin/Common/je_object.cpp | 32 --- src/libjin/Common/je_object.h | 49 ---- src/libjin/Common/je_singleton.hpp | 49 +++- src/libjin/Common/je_subsystem.hpp | 44 ++- src/libjin/Common/je_types.h | 24 +- src/libjin/Common/je_utf8.cpp | 2 +- src/libjin/Common/je_utf8.h | 27 +- src/libjin/Filesystem/je_asset_database.cpp | 35 ++- src/libjin/Filesystem/je_asset_database.h | 71 ++++- src/libjin/Filesystem/je_buffer.h | 156 +++++++++-- src/libjin/Game/je_entity.h | 51 ++++ src/libjin/Game/je_game.h | 33 ++- src/libjin/Game/je_scene.h | 68 +++++ src/libjin/Graphics/Font/je_decoder.h | 6 + src/libjin/Graphics/Font/je_font.h | 1 + src/libjin/Graphics/Font/je_text.cpp | 8 +- src/libjin/Graphics/Font/je_text.h | 94 ++++++- src/libjin/Graphics/Font/je_texture_font.h | 77 +++++- src/libjin/Graphics/Font/je_ttf.h | 156 ++++++++++- src/libjin/Graphics/Shader/je_jsl_compiler.cpp | 11 + src/libjin/Graphics/Shader/je_jsl_compiler.h | 27 ++ src/libjin/Graphics/Shader/je_shader.cpp | 12 +- src/libjin/Graphics/animation/je_animation.cpp | 0 src/libjin/Graphics/animation/je_animation.h | 17 ++ src/libjin/Graphics/animation/je_clip.cpp | 0 src/libjin/Graphics/animation/je_clip.h | 17 ++ src/libjin/Graphics/je_bitmap.h | 2 + src/libjin/Graphics/je_canvas.h | 1 + src/libjin/Graphics/je_color.h | 8 +- src/libjin/Graphics/je_drawable.cpp | 78 +++--- src/libjin/Graphics/je_drawable.h | 23 +- src/libjin/Graphics/je_gl.h | 10 + src/libjin/Graphics/je_image.cpp | 8 +- src/libjin/Graphics/je_mesh.cpp | 2 +- src/libjin/Graphics/je_mesh.h | 3 + src/libjin/Graphics/je_shapes.h | 9 +- src/libjin/Graphics/je_sprite.cpp | 11 + src/libjin/Graphics/je_sprite.h | 16 +- src/libjin/Graphics/je_sprite_batch.cpp | 0 src/libjin/Graphics/je_sprite_batch.h | 17 ++ src/libjin/Graphics/je_window.cpp | 10 +- src/libjin/Graphics/je_window.h | 1 + src/libjin/Graphics/particle/je_particle.cpp | 0 src/libjin/Graphics/particle/je_particle.h | 20 ++ src/libjin/Graphics/particle/je_particle_batch.cpp | 0 src/libjin/Graphics/particle/je_particle_batch.h | 0 .../Graphics/particle/je_particle_emitter.cpp | 0 src/libjin/Graphics/particle/je_particle_emitter.h | 0 .../Graphics/particle/je_particle_system.cpp | 1 + src/libjin/Graphics/particle/je_particle_system.h | 0 src/libjin/Input/je_event.h | 18 +- src/libjin/Input/je_joypad.h | 8 +- src/libjin/Input/je_keyboard.h | 3 + src/libjin/Input/je_mouse.h | 18 ++ src/libjin/Math/README.md | 1 + src/libjin/Math/je_math.h | 6 +- src/libjin/Math/je_matrix.h | 194 ++++++------- src/libjin/Math/je_quad.h | 8 +- src/libjin/Math/je_random.h | 14 + src/libjin/Net/je_net_manager.h | 26 +- src/libjin/Net/je_socket.h | 84 +++++- src/libjin/Thread/je_thread.cpp | 301 --------------------- src/libjin/Thread/je_thread.h | 166 ------------ src/libjin/Time/je_timer.h | 44 ++- src/libjin/Utils/je_log.h | 2 +- src/libjin/ai/je_behavior_tree.cpp | 0 src/libjin/ai/je_behavior_tree.h | 20 ++ src/libjin/ai/je_state_machine.cpp | 0 src/libjin/ai/je_state_machine.h | 20 ++ src/libjin/core/README.md | 1 + src/libjin/core/je_configuration.h | 8 + src/libjin/core/je_types.h | 24 -- src/libjin/core/je_version.h | 26 ++ src/libjin/jin.h | 2 +- src/libjin/multithread/je_thread.cpp | 301 +++++++++++++++++++++ src/libjin/multithread/je_thread.h | 167 ++++++++++++ 82 files changed, 2313 insertions(+), 896 deletions(-) delete mode 100644 src/libjin/Common/je_object.cpp delete mode 100644 src/libjin/Common/je_object.h create mode 100644 src/libjin/Game/je_entity.h create mode 100644 src/libjin/Game/je_scene.h create mode 100644 src/libjin/Graphics/Shader/je_jsl_compiler.cpp create mode 100644 src/libjin/Graphics/Shader/je_jsl_compiler.h create mode 100644 src/libjin/Graphics/animation/je_animation.cpp create mode 100644 src/libjin/Graphics/animation/je_animation.h create mode 100644 src/libjin/Graphics/animation/je_clip.cpp create mode 100644 src/libjin/Graphics/animation/je_clip.h create mode 100644 src/libjin/Graphics/je_sprite_batch.cpp create mode 100644 src/libjin/Graphics/je_sprite_batch.h create mode 100644 src/libjin/Graphics/particle/je_particle.cpp create mode 100644 src/libjin/Graphics/particle/je_particle.h create mode 100644 src/libjin/Graphics/particle/je_particle_batch.cpp create mode 100644 src/libjin/Graphics/particle/je_particle_batch.h create mode 100644 src/libjin/Graphics/particle/je_particle_emitter.cpp create mode 100644 src/libjin/Graphics/particle/je_particle_emitter.h create mode 100644 src/libjin/Graphics/particle/je_particle_system.cpp create mode 100644 src/libjin/Graphics/particle/je_particle_system.h create mode 100644 src/libjin/Math/README.md create mode 100644 src/libjin/Math/je_random.h delete mode 100644 src/libjin/Thread/je_thread.cpp delete mode 100644 src/libjin/Thread/je_thread.h create mode 100644 src/libjin/ai/je_behavior_tree.cpp create mode 100644 src/libjin/ai/je_behavior_tree.h create mode 100644 src/libjin/ai/je_state_machine.cpp create mode 100644 src/libjin/ai/je_state_machine.h create mode 100644 src/libjin/core/README.md delete mode 100644 src/libjin/core/je_types.h create mode 100644 src/libjin/core/je_version.h create mode 100644 src/libjin/multithread/je_thread.cpp create mode 100644 src/libjin/multithread/je_thread.h (limited to 'src/libjin') diff --git a/src/libjin/Audio/SDL/je_sdl_audio.h b/src/libjin/Audio/SDL/je_sdl_audio.h index 01da7b3..b13dc10 100644 --- a/src/libjin/Audio/SDL/je_sdl_audio.h +++ b/src/libjin/Audio/SDL/je_sdl_audio.h @@ -18,9 +18,15 @@ namespace JinEngine #define SDLAUDIO_BYTEDEPTH (SDLAUDIO_BITDEPTH >> 3) #define SDLAUDIO_CHANNELS 2 + /// + /// Audio system SDL implementation. + /// class SDLAudio : public AudioManager { public: + /// + /// SDL audio setting. + /// struct Setting : SettingBase { public: @@ -28,29 +34,95 @@ namespace JinEngine int samples; // 缓冲区的sample数,<=samplerate }; - /* IAudio interface */ + /// + /// Play all sources whose state is playing. + /// void play() override; + + /// + /// Stop and remove all sources from the queue. + /// void stop() override; + + /// + /// Pause audio. + /// void pause() override; + + /// + /// Resume audio. + /// void resume() override; + + /// + /// Set global audio volume. + /// void setVolume(float volume) override; - /* process functions*/ + + /// + /// Process all commands in the queue. + /// void processCommands(); + + /// + /// Process all sources. + /// + /// @param buffer Source buffer. + /// @param len Source length. + /// void processSources(void* buffer, size_t len); + + /// + /// Process audio buffer. + /// + /// @param buffer Audio stream buffer. + /// @param len Length of stream buffer. + /// void processBuffer(void* buffer, size_t len); + + /// + /// Goon process. + /// + /// @return True if sucessful, otherwise return false. + /// bool goOnProcess(); - /* thread-safe */ + + /// + /// Lock audio device. + /// void lock(); + + /// + /// Unlock audio device. + /// void unlock(); private: singleton(SDLAudio); + + /// + /// SDL audio constructor. + /// SDLAudio() {}; + + /// + /// SDL audio destructor. + /// ~SDLAudio() {}; - /* subsystem interface */ + + /// + /// Initialize audio system. + /// + /// @param setting Audio setting. + /// bool initSystem(const SettingBase* setting) override; + + /// + /// Quit audio system. + /// void quitSystem() override; + // Audio device id. unsigned int audioDevice; }; @@ -59,4 +131,5 @@ namespace JinEngine } // namespace JinEngine #endif // LIBJIN_MODULES_AUDIO && LIBJIN_AUDIO_SDLAUDIO -#endif // __JE_AUDIO_SDL_H + +#endif // __JE_AUDIO_SDL_H \ No newline at end of file diff --git a/src/libjin/Audio/SDL/je_sdl_source.cpp b/src/libjin/Audio/SDL/je_sdl_source.cpp index 210d058..8f4a2dc 100644 --- a/src/libjin/Audio/SDL/je_sdl_source.cpp +++ b/src/libjin/Audio/SDL/je_sdl_source.cpp @@ -233,10 +233,9 @@ namespace JinEngine ActionFloat(SetVolume, clamp(volume, 0.0f, 1.0f)); } - bool SDLSource::setLoop(bool loop) + void SDLSource::setLoop(bool loop) { ActionBool(SetLoop, loop); - return false; } void SDLSource::setRate(float rate) diff --git a/src/libjin/Audio/SDL/je_sdl_source.h b/src/libjin/Audio/SDL/je_sdl_source.h index b32ecce..fe6abc3 100644 --- a/src/libjin/Audio/SDL/je_sdl_source.h +++ b/src/libjin/Audio/SDL/je_sdl_source.h @@ -16,39 +16,154 @@ namespace JinEngine { typedef struct SDLSourceCommand; + class SDLSourceManager; + /// + /// Audio source SDL implementation. + /// class SDLSource : public Source { public: + /// + /// Create source from raw source data file. + /// + /// @param file Audio source file. + /// @return Return source if create successful, otherwise return null. + /// static SDLSource* createSource(const char* file); + + /// + /// Create source from raw source data. + /// + /// @param mem Source data. + /// @param size Source data size. + /// @return Return source if create successful, otherwise return null. + /// static SDLSource* createSource(void* mem, size_t size); + /// + /// Source destructor. + /// ~SDLSource(); - /* ISource interface */ + + /// + /// Play source. + /// void play() override; + + /// + /// Stop source. + /// void stop() override; + + /// + /// Pause source. + /// void pause() override; + + /// + /// Resume source. + /// void resume() override; + + /// + /// Rewind source. + /// void rewind() override; + + /// + /// Return if the source is stopped. + /// + /// @return True if the source is stopped, otherwise return false. + /// bool isStopped() const override; + + /// + /// Return if the source is paused. + /// + /// @return True if the source is paused(, otherwise return false. + /// bool isPaused() const override; + + /// + /// Set pitch. + /// + /// @param pitch Pitch of source. + /// void setPitch(float pitch) override; + + /// + /// Set volume. + /// + /// @param volume Volume of source. + /// void setVolume(float volume) override; - bool setLoop(bool loop) override; + + /// + /// Set source loop. + /// + /// @param loop Looping or not. + /// + void setLoop(bool loop) override; + + /// + /// Set source rate. + /// + /// @param rate Rate of source. + /// void setRate(float rate) override; - /* handle and process anduio clip */ + + /// + /// Handle a specific command. + /// + /// @param manager Audio manager. + /// @param cmd Source commad. + /// inline void handle(SDLSourceManager* manager, SDLSourceCommand* cmd); + + /// + /// Process decoded source data. + /// + /// @param buffer Source data. + /// @param size Data size. + /// inline void process(void* buffer, size_t size); protected: + /// + /// Source constructor. + /// SDLSource(); - /* decode raw audio data */ + + /// + /// Decode wav file. + /// + /// @param mem Wav file data. + /// @param size Wav data size. + /// void decode_wav(void* mem, int size); + + /// + /// Decode ogg file. + /// + /// @param mem ogg file data. + /// @param size ogg data size. + /// void decode_ogg(void* mem, int size); - /* check state */ - inline bool is(int state) const { return (status.state & state) == state; } + /// + /// Check source state. + /// + /// @param state State to be checked. + /// @return True if state is given state, otherwise return false. + /// + inline bool is(int state) const + { + return (status.state & state) == state; + } + + // Source data. struct{ const void* data; // 音频数据 int length; // data字节长度 @@ -58,7 +173,7 @@ namespace JinEngine int samples; // sample数 = size / (bitdepth / 8) unsigned char channels; // channel数1(mono)或2(stereo) } raw; - /* Procedure controller variable */ + // Procedure controller variable. struct{ int pos; // 当前播放的sample int pitch; // pitch @@ -69,22 +184,68 @@ namespace JinEngine }; + /// + /// Source manager. + /// class SDLSourceManager { public: + /// + /// Get manager singleton. + /// + /// @return Singleton of SDL source manager. + /// static SDLSourceManager* get(); - /* Process function */ + /// + /// Process commands. + /// void processCommands(); + + /// + /// Process sources. + /// + /// @param buffer Source data. + /// @param size Size of source data. + /// void processSources(void* buffer, size_t size); - /* control flow */ + + /// + /// Clear source queue. + /// + /// This function will stop all sources. + /// void removeAllSource(); + + /// + /// Remove specific source. + /// + /// @param source SDL audio source. + /// void removeSource(SDLSource* source); + + /// + /// Push specific source into queue. + /// + /// @param source SDL audio source. + /// void pushSource(SDLSource* source); + + /// + /// Get command from queue. + /// + /// @return Command at first place. + /// SDLSourceCommand* getCommand(); + + /// + /// Push command. + /// + /// @param cmd Spcific command. + /// void pushCommand(SDLSourceCommand* cmd); - private : + private: std::queue commands; std::stack commandsPool; std::vector sources; // processing sources @@ -94,7 +255,7 @@ namespace JinEngine class SourceException : public std::exception { - const char * what() const throw () + const char* what() const throw () { return "Load Source Exception"; } @@ -104,4 +265,5 @@ namespace JinEngine } // namespace JinEngine #endif // LIBJIN_MODULES_AUDIO && LIBJIN_AUDIO_SDLAUDIO -#endif // __JE_SOURCE_SDL_H + +#endif // __JE_SOURCE_SDL_H \ No newline at end of file diff --git a/src/libjin/Audio/je_audio_manager.h b/src/libjin/Audio/je_audio_manager.h index 86716ef..10df08c 100644 --- a/src/libjin/Audio/je_audio_manager.h +++ b/src/libjin/Audio/je_audio_manager.h @@ -16,10 +16,16 @@ namespace JinEngine class Source; + /// + /// Audio manager. + /// template class AudioManager : public Subsystem { public: + /// + /// Audio state. + /// enum State { PLAY , @@ -27,19 +33,45 @@ namespace JinEngine PAUSE, }; + /// + /// Play all sources whose state is playing. + /// virtual void play() = 0; + + /// + /// Stop and remove all sources from the queue. + /// virtual void stop() = 0; + + /// + /// Pause audio. + /// virtual void pause() = 0; + + /// + /// Resume audio. + /// virtual void resume() = 0; + + /// + /// Set global audio volume. + /// virtual void setVolume(float volume) = 0; protected: singleton(AudioManager); + /// + /// AudioManager constructor. + /// AudioManager() : volume(1) , state(State::PLAY) {}; + + /// + /// AudioManager destructor. + /// virtual ~AudioManager() {}; float volume; @@ -52,4 +84,4 @@ namespace JinEngine #endif // LIBJIN_MODULES_AUDIO -#endif // __JE_AUDIO_H +#endif // __JE_AUDIO_H \ No newline at end of file diff --git a/src/libjin/Audio/je_source.h b/src/libjin/Audio/je_source.h index 9ef0b46..ac87c07 100644 --- a/src/libjin/Audio/je_source.h +++ b/src/libjin/Audio/je_source.h @@ -10,6 +10,9 @@ namespace JinEngine namespace Audio { + /// + /// Audio source encoding type. + /// enum SourceType { INVALID = 0, @@ -17,25 +20,90 @@ namespace JinEngine OGG, }; + /// + /// Audio source. + /// class Source { public: + /// + /// Source constructor. + /// Source() {}; + + /// + /// Source destructor. + /// virtual ~Source() {}; - /* interface */ - virtual void play() = 0; + + /// + /// Start playing source. + /// + virtual void play() = 0; + + /// + /// Stop playing source. + /// virtual void stop() = 0; + + /// + /// Pause source. + /// virtual void pause() = 0; + + /// + /// Resume source. + /// virtual void resume() = 0; + + /// + /// Rewind source. + /// virtual void rewind() = 0; + + /// + /// Whether the source is playing or not. + /// virtual bool isStopped() const = 0; + + /// + /// Whether the source is paused or not. + /// virtual bool isPaused() const = 0; + + /// + /// Set source pitch. + /// + /// @param pitch Pitch of source. + /// virtual void setPitch(float pitch) = 0; + + /// + /// Set volume of source. + /// + /// @param volume Volume of source. + /// virtual void setVolume(float volume) = 0; - virtual bool setLoop(bool loop) = 0; + + /// + /// Set source loop. + /// + /// @param loop Looping or not. + /// + virtual void setLoop(bool loop) = 0; + + /// + /// Set source rate. + /// + /// @param rate Rate of source. + /// virtual void setRate(float rate) = 0; protected: + + /// + /// Get type of source data. + /// static SourceType getType(const void* mem, int size); }; @@ -44,4 +112,5 @@ namespace JinEngine } // namespace JinEngine #endif // LIBJIN_MODULES_AUDIO -#endif // __JE_AUDIO_SOURCE_H + +#endif // __JE_AUDIO_SOURCE_H \ No newline at end of file diff --git a/src/libjin/Common/je_array.hpp b/src/libjin/Common/je_array.hpp index 473bae8..361f1f0 100644 --- a/src/libjin/Common/je_array.hpp +++ b/src/libjin/Common/je_array.hpp @@ -3,77 +3,114 @@ namespace JinEngine { - /* 自动释放堆内存的在栈上创建的动态数组 */ + + /// + /// A array created on heap. + /// template class Array { public: - Array() : length(0), data(nullptr) {} + /// + /// Array constructor. + /// + Array() + : length(0) + , data(nullptr) + { + } + /// + /// Array constructor. + /// + /// @param l Length of array. + /// Array(int l) { length = l; data = new T[l]; } + /// + /// Array destructor. + /// ~Array() { delete[] data; length = 0; } + /// + /// Get address of data. + /// + /// @return Address of data. + /// T* operator &() { return data; } + /// + /// Get specific element of array. + /// + /// @return Element of array. + /// T& operator[](int index) { return data[index]; } - /* 绑定内存 */ - void bind(T* d, int len) + /// + /// Bind data with given data. + /// + /// @param data Data pointer. + /// @param length Length of data. + /// + void bind(T* data, int length) { if (data != nullptr) delete data; - data = d; - length = len; + this->data = data; + this->length = length; } - void add(T v) + /// + /// Add an element. + /// + /// @param v Value of element. + /// + void add(T value) { int len = length + 1; T* d = new T[len]; memcpy(d, data, size()); - d[length] = v; + d[length] = value; bind(d, len); } + /// + /// Get size of data in byte. + /// + /// @return Size of data in byte. + /// int size() { return sizeof(T) * length; } + /// + /// Get length of data. + /// + /// @return Count of data. + /// int count() { return length; } private: - /// - /// http://blog.jobbole.com/106923/ - /// new 堆内存创建对象过程 - /// 1. 调用 new 分配内存 - /// 2. 调用构造函数 - /// - /// new, delete方法用于分配和释放内存,不负责构造和析构 - /// + // Disable new and delete. void* operator new(size_t t); - - /// - /// Disable delete. - /// void operator delete(void* ptr); T * data; diff --git a/src/libjin/Common/je_object.cpp b/src/libjin/Common/je_object.cpp deleted file mode 100644 index 69e4a56..0000000 --- a/src/libjin/Common/je_object.cpp +++ /dev/null @@ -1,32 +0,0 @@ -// LOVE -#include "je_object.h" - -namespace JinEngine -{ - - Object::Object() - : count(1) - { - } - - Object::~Object() - { - } - - int Object::getReferenceCount() const - { - return count; - } - - void Object::retain() - { - ++count; - } - - void Object::release() - { - if (--count <= 0) - delete this; - } - -} // love diff --git a/src/libjin/Common/je_object.h b/src/libjin/Common/je_object.h deleted file mode 100644 index ca17586..0000000 --- a/src/libjin/Common/je_object.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef __JE_COMMON_OBJECT_H -#define __JE_COMMON_OBJECT_H - -namespace JinEngine -{ - - class Object - { - private: - - // The reference count. - int count; - - public: - - /** - * Constructor. Sets reference count to one. - **/ - Object(); - - /** - * Destructor. - **/ - virtual ~Object() = 0; - - /** - * Gets the reference count of this Object. - * @returns The reference count. - **/ - int getReferenceCount() const; - - /** - * Retains the Object, i.e. increases the - * reference count by one. - **/ - void retain(); - - /** - * Releases one reference to the Object, i.e. decrements the - * reference count by one, and potentially deletes the Object - * if there are no more references. - **/ - void release(); - - }; // Object - -} // namespace JinEngine - -#endif \ No newline at end of file diff --git a/src/libjin/Common/je_singleton.hpp b/src/libjin/Common/je_singleton.hpp index f37f3af..e981e7a 100644 --- a/src/libjin/Common/je_singleton.hpp +++ b/src/libjin/Common/je_singleton.hpp @@ -4,33 +4,76 @@ namespace JinEngine { + /// + /// Singleton base class. + /// template class Singleton { public: + /// + /// Get singleton. + /// + /// @param Singleton instance of class. + /// static T* get() { if (_instance == nullptr) _instance = new T; return _instance; } + + /// + /// Destroy instance of singleton. + /// static void destroy() { delete _instance; _instance = nullptr; } + protected: + /// + /// Singleton constructor. + /// Singleton() {}; + + /// + /// Singleton destructor. + /// virtual ~Singleton() {}; + + /// + /// Singleton instance. + /// static T* _instance; + private: - Singleton(const Singleton&); - Singleton& operator = (const Singleton&); + /// + /// Singleton copy constructor. + /// + /// @param singleton Singleton of class. + /// + Singleton(const Singleton& singleton); + + /// + /// Singleton assignment. + /// + /// @param singleton Singleton of class. + /// + Singleton& operator = (const Singleton& singleton); + }; + /// + /// Singleton instance. + /// template T* Singleton::_instance = nullptr; -#define singleton(T) friend Singleton + /// + /// Singleton notation. + /// + #define singleton(T) friend Singleton } // namespace JinEngine diff --git a/src/libjin/Common/je_subsystem.hpp b/src/libjin/Common/je_subsystem.hpp index 4966a9d..d8e106d 100644 --- a/src/libjin/Common/je_subsystem.hpp +++ b/src/libjin/Common/je_subsystem.hpp @@ -8,22 +8,40 @@ namespace JinEngine { + /// + /// Subsystem class. + /// template class Subsystem : public Singleton { public: - struct Setting {}; + /// + /// Subsystem setting. + /// + struct Setting + { + }; + typedef Setting SettingBase; + /// + /// Initialize subsystem. + /// + /// @param setting Subsystem setting. + /// @return True if initialize sucessful, otherwise return false. + /// bool init(const SettingBase* setting = nullptr) { static bool success = initSystem(setting); return success; } + /// + /// Quit subsystem. + /// void quit() { - /*call only once*/ + // Call only once. static char __dummy__ = (quitSystem(), 1); Singleton::destroy(); } @@ -31,11 +49,27 @@ namespace JinEngine protected: singleton(System); + /// + /// Subsystem constructor. + /// Subsystem() {}; - virtual ~Subsystem() {}; - /*onlyonce*/ virtual bool initSystem(const Setting* setting) = 0; - /*onlyonce*/ virtual void quitSystem() = 0; + /// + /// Subsystem destructor. + /// + virtual ~Subsystem() + { + }; + + /// + /// Initializer callback. + /// + virtual bool initSystem(const Setting* setting) = 0; + + /// + /// Quit subsystem callback. + /// + virtual void quitSystem() = 0; }; diff --git a/src/libjin/Common/je_types.h b/src/libjin/Common/je_types.h index 7e7ad21..446c413 100644 --- a/src/libjin/Common/je_types.h +++ b/src/libjin/Common/je_types.h @@ -4,20 +4,20 @@ namespace JinEngine { - namespace common - { - typedef int8_t int8; - typedef uint8_t uint8; - typedef uint8 byte; - typedef int16_t int16; - typedef uint16_t uint16; - typedef int32_t int32; - typedef uint32_t uint32; - typedef int64_t int64; - typedef uint64_t uint64; + typedef int8_t int8; ///< Signed integer with a size of 8 bits. Supports values from -128 to 127 + typedef uint8_t uint8; ///< Unsigned integer with a size of 8 bits. Supports values from 0 to 255. + typedef uint8 byte; ///< Unsigned integer with 8 bits (1 byte). Supports 256 values from 0 to 255. + typedef int16_t int16; ///< Signed integer with a size of 16 bits. Supports values from -32768 to 32767 + typedef uint16_t uint16; ///< Unsigned integer with a size of 16 bits. Supports values from 0 to 65535. + typedef int32_t int32; ///< Signed integer with a size of 32 bits. Supports values from -2147483648 to 2147483647. + typedef uint32_t uint32; ///< Unsigned integer with a size of 32 bits. Supports values from 0 to 4294967295, (2^32 - 1). + typedef int64_t int64; ///< Signed integer with a size of 64 bits. Supports values from -(2^63) to (2^63 - 1). + typedef uint64_t uint64; ///< Unsigned integer with a size of 64 bits, Supports values from 0 to (2^64 - 1). + + typedef uint32_t uint; + typedef int32_t sint; - } } #endif \ No newline at end of file diff --git a/src/libjin/Common/je_utf8.cpp b/src/libjin/Common/je_utf8.cpp index b2c1d31..012bf5e 100644 --- a/src/libjin/Common/je_utf8.cpp +++ b/src/libjin/Common/je_utf8.cpp @@ -39,4 +39,4 @@ namespace JinEngine } // namespace JinEngine -#endif // LIBJIN_OS == LIBJIN_WINDOWS +#endif // LIBJIN_OS == LIBJIN_WINDOWS \ No newline at end of file diff --git a/src/libjin/Common/je_utf8.h b/src/libjin/Common/je_utf8.h index 9fdff10..72a11af 100644 --- a/src/libjin/Common/je_utf8.h +++ b/src/libjin/Common/je_utf8.h @@ -10,22 +10,25 @@ namespace JinEngine { - /** - * Convert the wide string to a UTF-8 encoded string. - * @param wstr The wide-char string. - * @return A UTF-8 string. - **/ + /// + /// Convert the wide string to a UTF-8 encoded string. + /// + /// @param wstr The wide-char string. + /// @return A UTF-8 string. + /// std::string to_utf8(LPCWSTR wstr); - /** - * Replace all occurences of 'find' with 'replace' in a string. - * @param str The string to modify. - * @param find The character to match. - * @param replace The character to replace matches. - **/ + /// + /// Replace all occurences of 'find' with 'replace' in a string. + /// + /// @param str The string to modify. + /// @param find The character to match. + /// @param replace The character to replace matches. + /// void replace_char(std::string & str, char find, char replace); } // namespace JinEngine #endif // LIBJIN_OS == LIBJIN_WINDOWS -#endif // __JE_COMMON_UTF8_H + +#endif // __JE_COMMON_UTF8_H \ No newline at end of file diff --git a/src/libjin/Filesystem/je_asset_database.cpp b/src/libjin/Filesystem/je_asset_database.cpp index c945d6a..edc3661 100644 --- a/src/libjin/Filesystem/je_asset_database.cpp +++ b/src/libjin/Filesystem/je_asset_database.cpp @@ -9,21 +9,21 @@ namespace JinEngine namespace Filesystem { - AssetDatabase* AssetDatabase::fs = 0; + AssetDatabase* AssetDatabase::mAssetDatabase = 0; AssetDatabase::AssetDatabase() { - S = smtnewshared(); + mSmt = smtnewshared(); } AssetDatabase* AssetDatabase::get() { - return fs ? fs : (fs = new AssetDatabase()); + return mAssetDatabase ? mAssetDatabase : (mAssetDatabase = new AssetDatabase()); } void AssetDatabase::mount(const char * path) { - int err = smtmount(S, path); + int err = smtmount(mSmt, path); if (err) { printf("%s mounted path %s", smterrstr(err), path); @@ -31,37 +31,44 @@ namespace JinEngine } } - int AssetDatabase::read(const char* path, Buffer* buffer) + bool AssetDatabase::read(const char* path, Buffer& buffer) { - buffer->data = smtread(S, path, &buffer->size); - if (buffer->data == 0) - return 0; - return 1; + size_t size; + byte* data = (byte*)smtread(mSmt, path, &size); + if (data == nullptr) + return false; + buffer.bind(data, size); + return true; } + Buffer* read(const char* path) + { + + } + void* AssetDatabase::read(const char* path, unsigned int* len) { - return smtread(S, path, len); + return smtread(mSmt, path, len); } const char* AssetDatabase::getFull(const char* path) { - return smtfullpath(S, path); + return smtfullpath(mSmt, path); } bool AssetDatabase::isDir(const char* path) { - return smtisdir(S, path); + return smtisdir(mSmt, path); } bool AssetDatabase::isFile(const char* path) { - return smtisreg(S, path); + return smtisreg(mSmt, path); } bool AssetDatabase::exists(const char* path) { - return smtexists(S, path) == 0; + return smtexists(mSmt, path) == 0; } } // namespace Filesystem diff --git a/src/libjin/Filesystem/je_asset_database.h b/src/libjin/Filesystem/je_asset_database.h index b8f96f6..81cce64 100644 --- a/src/libjin/Filesystem/je_asset_database.h +++ b/src/libjin/Filesystem/je_asset_database.h @@ -8,25 +8,84 @@ namespace JinEngine { namespace Filesystem { - /* 资源管理 */ - class AssetDatabase + + /// + /// Assets managment. + /// + class AssetDatabase { public: + /// + /// Get asset database singleton. + /// + /// @param Singleton of asset database. + /// static AssetDatabase* get(); + /// + /// Asset database constructor. + /// AssetDatabase(); + /// + /// Check if the path is directory. + /// + /// @param path Path under asset folder. + /// @return True if the given path is directory, otherwise return false. + /// bool isDir(const char* path); + + /// + /// Check if the path is file. + /// + /// @param path Path under asset folder. + /// @return True if the given path is file, otherwise return false. + /// bool isFile(const char* path); + + /// + /// Check if the path exists. + /// @param path Given path. + /// @return True if path exists, otherwise return false. + /// bool exists(const char* path); - int read(const char* path, Buffer* buffer); - void* read(const char* path, unsigned int* len); + + /// + /// Read file into a buffer. + /// + /// @param path Path of file. + /// @param buffer Buffer to fill. + /// @return True if read sucessful, otherwise return false. + /// + bool read(const char* path, Buffer& buffer); + + /// + /// Read file and return data content. + /// + /// @param path Path of file. + /// @param length Length of data. + /// @return Data if read sucessful, otherwise return null. + /// + void* read(const char* path, unsigned int* length); + + /// + /// Set asset root folder. + /// + /// @param root Root folder of assets. + /// void mount(const char* root); + + /// + /// Get full path of asset. + /// + /// @param path Path of asset. + /// @return Full path of asset. + /// const char* getFull(const char* path); private: - static AssetDatabase* fs; - smtShared* S; + static AssetDatabase* mAssetDatabase; + smtShared* mSmt; }; diff --git a/src/libjin/Filesystem/je_buffer.h b/src/libjin/Filesystem/je_buffer.h index 11faf79..18a381d 100644 --- a/src/libjin/Filesystem/je_buffer.h +++ b/src/libjin/Filesystem/je_buffer.h @@ -4,61 +4,161 @@ #include #include +#include "../common/je_types.h" + namespace JinEngine { namespace Filesystem { - /** - * 在堆上分配指定空间的buffer - */ + /// + /// Data buffer allocated on heap. + /// class Buffer { public: - Buffer() : data(0), size(0) {} + /// + /// Buffer constructor. + /// + Buffer() + : mData(0) + , mSize(0) + { + } + + /// + /// Copy constructor. + /// + /// @param src Buffer source. + /// Buffer(const Buffer& src) { - delete[] data; - size = src.size; - data = new char[size]; - memcpy(data, src.data, size); + delete[] mData; + mSize = src.mSize; + mData = new byte[mSize]; + memcpy(mData, src.mData, mSize); } - Buffer(void* d, int s) + + /// + /// Buffer constructor. + /// + /// @param data Buffer data. + /// @param size Size of buffer. + /// + Buffer(void* data, int size) { - data = new char[size]; - memcpy(data, d, size); - size = s; + mSize = size; + mData = new byte[mSize]; + memcpy(mData, data, mSize); } - Buffer(size_t s) + + /// + /// Buffer constructor. + /// + /// @param size Size of data. + /// + Buffer(size_t size) { - data = new char[s]; - memset(data, 0, s); - size = s; + mData = new byte[size]; + memset(mData, 0, size); + mSize = size; } + + /// + /// Buffer destructor. + /// ~Buffer() { - delete[] data; - size = 0; + delete[] mData; + mSize = 0; } + + /// + /// Set buffer data. + /// + /// @param data Buffer data. + /// @param size Size of data. + /// + void set(byte* data, size_t size) + { + if (data == nullptr) + return; + if (mSize != size) + { + delete mData; + mData = new byte[size]; + } + mSize = size; + memcpy(mData, data, size); + } + + /// + /// Bind buffer data. + /// + /// @param data Buffer data. + /// @param size Size of buffer. + /// + void bind(byte* data, size_t size) + { + if (mData != nullptr) + delete mData; + mSize = size; + mData = data; + } + + /// + /// Buffer assignment. + /// + /// @param buffer Buffer to copy. + /// void operator = (const Buffer& buffer) { - delete[] data; - size = buffer.size; - data = new char[size]; - memcpy(data, buffer.data, size); + delete[] mData; + mSize = buffer.mSize; + mData = new byte[mSize]; + memcpy(mData, buffer.mData, mSize); } + /// + /// Get data addresss. + /// + /// @return Data address. + /// + const byte* operator &() + { + return mData; + } + + /// + /// Get data size. + /// + /// @return Size of data. + /// + const inline size_t size() + { + return mSize; + } + + /// + /// Clear data. + /// void clear() { - if (data == nullptr) + if (mData == nullptr) return; - free(data); - data = nullptr; + delete mData; + mData = nullptr; + mSize = 0; } - void* data; - unsigned int size; - + private: + byte* mData; + size_t mSize; + + // diasble new and delete + void* operator new(size_t t); + void operator delete(void*); + }; } // namespace Filesystem diff --git a/src/libjin/Game/je_entity.h b/src/libjin/Game/je_entity.h new file mode 100644 index 0000000..29fe8be --- /dev/null +++ b/src/libjin/Game/je_entity.h @@ -0,0 +1,51 @@ +#ifndef __JE_GAME_OBJECT_H +#define __JE_GAME_OBJECT_H + +#include +#include + +#include "../common/je_types.h" + +namespace JinEngine +{ + namespace Game + { + + /// + /// Game object base class. + /// + class Entity + { + public: + virtual ~Entity(); + + void lifecycle(); + + 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 + bool mIsVisible; + bool mIsActive; + + }; + + /// + /// Entity list. For quickly adding and removing entities. + /// + typedef std::list EntityList; + + /// + /// Entity set. For searching and keeps entities unique and sorted. + /// + typedef std::set EntitySet; + + } // namespace Game +} // namespace JinEngine + +#endif \ No newline at end of file diff --git a/src/libjin/Game/je_game.h b/src/libjin/Game/je_game.h index 8fe4b51..6f7ad6d 100644 --- a/src/libjin/Game/je_game.h +++ b/src/libjin/Game/je_game.h @@ -12,6 +12,9 @@ namespace JinEngine namespace Core { + /// + /// Game class. + /// class Game : public Subsystem { public: @@ -21,6 +24,9 @@ namespace JinEngine typedef void(*onUpdate)(int dt); typedef void(*onDraw)(); + /// + /// Game setting. + /// struct Setting : SettingBase { onEvent eventHandler; @@ -29,12 +35,31 @@ namespace JinEngine onLoad loader; }; + /// + /// Main game loop. + /// void run(); - inline void stop() { _running = false; }; - inline bool running() { return _running; }; + + /// + /// 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() {}; @@ -55,4 +80,4 @@ namespace JinEngine } // namespace Core } // namespace JinEngine -#endif // __JE_CORE_GAME_H +#endif // __JE_CORE_GAME_H \ No newline at end of file diff --git a/src/libjin/Game/je_scene.h b/src/libjin/Game/je_scene.h new file mode 100644 index 0000000..388b047 --- /dev/null +++ b/src/libjin/Game/je_scene.h @@ -0,0 +1,68 @@ +#ifndef __JE_GAME_SCENE_H +#define __JE_GAME_SCENE_H + +#include +#include + +#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 layers; + // all entities grouped by tag + std::map tags; + + }; + + } +} + +#endif \ No newline at end of file diff --git a/src/libjin/Graphics/Font/je_decoder.h b/src/libjin/Graphics/Font/je_decoder.h index 162f22e..36cbda7 100644 --- a/src/libjin/Graphics/Font/je_decoder.h +++ b/src/libjin/Graphics/Font/je_decoder.h @@ -16,6 +16,7 @@ namespace JinEngine class Decoder { public: + /// /// Decode a code unit. /// @@ -32,6 +33,7 @@ namespace JinEngine /// @return Next code unit location. /// virtual const void* next(const void* data) const = 0; + }; /// @@ -40,6 +42,7 @@ namespace JinEngine class Utf8 : public Decoder { public: + /// /// Decode a code unit. /// @@ -56,6 +59,7 @@ namespace JinEngine /// @return Next code unit location. /// const void* next(const void* data) const override; + }; /// @@ -64,6 +68,7 @@ namespace JinEngine class Ascii : public Decoder { public: + /// /// Decode a code unit. /// @@ -80,6 +85,7 @@ namespace JinEngine /// @return Next code unit location. /// const void* next(const void* data) const override; + }; } // namespace Graphics diff --git a/src/libjin/Graphics/Font/je_font.h b/src/libjin/Graphics/Font/je_font.h index ec60c21..75dd4c5 100644 --- a/src/libjin/Graphics/Font/je_font.h +++ b/src/libjin/Graphics/Font/je_font.h @@ -89,6 +89,7 @@ namespace JinEngine inline unsigned getFontSize() { return mFontSize; }; protected: + unsigned mFontSize; }; diff --git a/src/libjin/Graphics/Font/je_text.cpp b/src/libjin/Graphics/Font/je_text.cpp index 70a1c36..75dfc7b 100644 --- a/src/libjin/Graphics/Font/je_text.cpp +++ b/src/libjin/Graphics/Font/je_text.cpp @@ -20,8 +20,8 @@ namespace JinEngine { switch (encode) { - case Encode::UTF8: decoder = new Utf8(); break; - case Encode::ASCII: decoder = new Ascii(); break; + case Encode::UTF8: decoder = new Utf8(); break; + case Encode::ASCII: decoder = new Ascii(); break; } } @@ -33,8 +33,8 @@ namespace JinEngine { switch (encode) { - case Encode::UTF8: decoder = new Utf8(); break; - case Encode::ASCII: decoder = new Ascii(); break; + case Encode::UTF8: decoder = new Utf8(); break; + case Encode::ASCII: decoder = new Ascii(); break; } } diff --git a/src/libjin/Graphics/Font/je_text.h b/src/libjin/Graphics/Font/je_text.h index af40993..7436875 100644 --- a/src/libjin/Graphics/Font/je_text.h +++ b/src/libjin/Graphics/Font/je_text.h @@ -31,44 +31,134 @@ namespace JinEngine class Text { public: + /// + /// + /// Text(Encode encode, const void* data); + + /// + /// + /// Text(Encode encode, const void* data, unsigned int length); + + /// + /// + /// ~Text(); + /// + /// + /// const Content& getContent() const; + + /// + /// + /// const Content& operator*() const; private: + /// + /// + /// class Iterator { public: + + /// + /// + /// Iterator(const Iterator& itor); + + /// + /// + /// Iterator(const Encode& encode, const void* data, unsigned int length); + + /// + /// + /// ~Iterator(); + /// + /// + /// Codepoint get(); + //Iterator begin(); //Iterator end(); + + /// + /// + /// void toBegin(); + + /// + /// + /// void toEnd(); + + /// + /// + /// Codepoint operator *(); - /* prefix ++ */ + + /// + /// + /// Iterator& operator ++(); - /* postfix ++ */ + + /// + /// + /// Iterator operator ++(int); + + /// + /// + /// bool operator !=(const Iterator& itor); + + /// + /// + /// bool operator ==(const Iterator& itor); private: + + /// + /// + /// void operator = (const Iterator&); + /// + /// + /// const Encode encode; + + /// + /// + /// const Decoder* decoder; + + /// + /// + /// const void* p; + + /// + /// + /// const void* const data; + + /// + /// + /// unsigned int length; + }; + /// + /// + /// Content content; }; diff --git a/src/libjin/Graphics/Font/je_texture_font.h b/src/libjin/Graphics/Font/je_texture_font.h index 9805cda..43d92c8 100644 --- a/src/libjin/Graphics/Font/je_texture_font.h +++ b/src/libjin/Graphics/Font/je_texture_font.h @@ -25,36 +25,111 @@ namespace JinEngine , public Drawable { public: + + /// + /// + /// static TextureFont* createTextureFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh); + + /// + /// + /// static TextureFont* createTextureFont(const Bitmap* bitmap, const Text& text, int cellw, int cellh); + + /// + /// + /// static TextureFont* createTextureFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh); + + /// + /// + /// static TextureFont* createTextureFont(const Bitmap* bitmap, const Text& text, Color mask, int cellh); - + + /// + /// + /// ~TextureFont(); + /// + /// + /// Page* typeset(const Text& text, int lineheight, int spacing = 0) override; + + /// + /// + /// Page* typeset(const Content& text, int lineheight, int spacing = 0) override ; + /// + /// + /// void print(const Page* page, int x, int y) override; + + /// + /// + /// void print(const Content& text, int x, int y, int linehgiht, int spacing = 0) override; + + /// + /// + /// void print(const Text& text, int x, int y, int lineheight, int spacing = 0)override; private: + + /// + /// + /// struct TextureGlyph { float x, y, w, h; }; + /// + /// + /// TextureFont(const Bitmap* bitmap, const Content& codepoints, int cellw, int cellh); + + /// + /// + /// TextureFont(const Bitmap* bitmap, const Content& codepoints, Color mask, int cellh); + /// + /// + /// int getCharWidth(int c); + + /// + /// + /// int getCharHeight(int c); + + /// + /// + /// int getTextWidth(const Content& text, int spacing = 0); + + /// + /// + /// int getTextHeight(const Content& text, int lineheight); + + /// + /// + /// void getTextBox(const Content& text, int* w, int* h, int lineheight, int spacing = 0); + + /// + /// + /// const TextureGlyph* findGlyph(Codepoint codepoint) const; + + /// + /// + /// std::map glyphs; }; diff --git a/src/libjin/Graphics/Font/je_ttf.h b/src/libjin/Graphics/Font/je_ttf.h index e4314fe..9acb07e 100644 --- a/src/libjin/Graphics/Font/je_ttf.h +++ b/src/libjin/Graphics/Font/je_ttf.h @@ -34,32 +34,81 @@ namespace JinEngine class TTFData { public: + + /// + /// + /// static TTFData* createTTFData(const unsigned char* data, unsigned int size); + /// + /// + /// ~TTFData(); + /// + /// + /// TTF* createTTF(unsigned ttfsize); + /// + /// + /// void pushTTFsize(unsigned ttfsize); + + /// + /// + /// void popTTFsize(); + /// + /// + /// Channel* getCodepointBitmapAlpha(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const; + + /// + /// + /// Color* getCodepointBitmap(unsigned int codepoint, int* width, int* height, int* xoff, int* yoff) const; + /// + /// + /// void getVMetrics(int* baseline, int* descent); + + /// + /// + /// void getHMetrics(unsigned int codepoint, int* advanceWidth, int* leftSideBearing); private: + + /// + /// + /// static const unsigned int FONT_SIZE = 12; + /// + /// + /// TTFData(const unsigned char* data, unsigned int size); + /// + /// + /// stbtt_fontinfo info; + + /// + /// + /// struct { unsigned char* data; unsigned int size; } raw; + + /// + /// + /// std::vector scales; }; @@ -69,18 +118,43 @@ namespace JinEngine public: //static TTF* createTTF(TTFData* ttfData, unsigned ttfSzie); + /// + /// + /// Page* typeset(const Text& text, int lineheight, int spacing = 0) override; + + /// + /// + /// Page* typeset(const Content& text, int lineheight, int spacing = 0) override; + /// + /// + /// void print(const Text& text, int x, int y, int lineheight, int spacing = 0) override; + + /// + /// + /// void print(const Content& text, int x, int y, int lineheight, int spacing = 0) override; + + /// + /// + /// void print(const Page* page, int x, int y) override; + /// + /// + /// ~TTF(); private: + friend class TTFData; + /// + /// + /// struct TTFGlyph { GLuint atlas; @@ -94,34 +168,114 @@ namespace JinEngine unsigned int width, height; }; + /// + /// + /// static const int TEXTURE_SIZE_LEVELS_COUNT = 7; + + /// + /// + /// static const int TEXTURE_SIZE_LEVEL_MAX = TEXTURE_SIZE_LEVELS_COUNT - 1; + + /// + /// + /// static const int TEXTURE_WIDTHS[TEXTURE_SIZE_LEVELS_COUNT]; + + /// + /// + /// static const int TEXTURE_HEIGHTS[TEXTURE_SIZE_LEVELS_COUNT]; + /// + /// + /// TTF(TTFData* ttf, Codepoint ttfSize); + /// + /// + /// void estimateSize(); + + /// + /// + /// GLuint createAtlas(); + + /// + /// + /// TTFGlyph& bakeGlyph(Codepoint character); + + /// + /// + /// TTFGlyph& findGlyph(Codepoint character); + /// + /// + /// int getCharWidth(int c); + + /// + /// + /// int getCharHeight(int c); + /// + /// + /// int getTextWidth(const Content& text, int spacing = 0); + + /// + /// + /// int getTextHeight(const Content& text, int lineheight); + + /// + /// + /// void getTextBox(const Content& text, int* w, int* h, int lineheight, int spacing = 0); + /// + /// + /// int textureWidth; + + /// + /// + /// int textureHeight; + + /// + /// + /// std::vector atlases; + + /// + /// + /// std::map glyphs; + + /// + /// + /// TTFData* ttf; + + /// + /// + /// int baseline; + + /// + /// + /// int descent; - /* cursor helped render to texture */ + /// + /// + /// Math::Vector2 cursor; }; diff --git a/src/libjin/Graphics/Shader/je_jsl_compiler.cpp b/src/libjin/Graphics/Shader/je_jsl_compiler.cpp new file mode 100644 index 0000000..490caea --- /dev/null +++ b/src/libjin/Graphics/Shader/je_jsl_compiler.cpp @@ -0,0 +1,11 @@ +#include "je_jsl_compiler.h" + +namespace JinEngine +{ + namespace Graphics + { + + + + } // namespace Graphics +} // namespace JinEngine \ No newline at end of file diff --git a/src/libjin/Graphics/Shader/je_jsl_compiler.h b/src/libjin/Graphics/Shader/je_jsl_compiler.h new file mode 100644 index 0000000..b530466 --- /dev/null +++ b/src/libjin/Graphics/Shader/je_jsl_compiler.h @@ -0,0 +1,27 @@ +#ifndef __JE_JSL_COMPILER_H +#define __JE_JSL_COMPILER_H + +#include "../../common/je_singleton.hpp" + +namespace JinEngine +{ + namespace Graphics + { + + /// + /// Compile JSL into GLSL. + /// + class JSLCompiler : public Singleton + { + public: + + + private: + singleton(JSLCompiler); + + }; + + } +} + +#endif \ No newline at end of file diff --git a/src/libjin/Graphics/Shader/je_shader.cpp b/src/libjin/Graphics/Shader/je_shader.cpp index 19485b9..6066864 100644 --- a/src/libjin/Graphics/Shader/je_shader.cpp +++ b/src/libjin/Graphics/Shader/je_shader.cpp @@ -77,6 +77,8 @@ namespace JinEngine { if (CurrentShader == this) unuse(); + // delete shader program + glDeleteShader(mPID); } bool Shader::compile(const string& program) @@ -96,21 +98,23 @@ namespace JinEngine int start = loc_VERTEX_SHADER + strlen("#VERTEX_SHADER"); string vertex_shader = program.substr(start, loc_END_VERTEX_SHADER - start); Buffer vbuffer = Buffer(vertex_shader.length() + BASE_VERTEX_SHADER_SIZE); - formatVertexShader((char*)vbuffer.data, vertex_shader.c_str()); + formatVertexShader((char*)&vbuffer, vertex_shader.c_str()); start = loc_FRAGMENT_SHADER + strlen("#FRAGMENT_SHADER"); string fragment_shader = program.substr(start, loc_END_FRAGMENT_SHADER - start); Buffer fbuffer = Buffer(fragment_shader.length() + BASE_FRAGMENT_SHADER_SIZE); - formatFragmentShader((char*)fbuffer.data, fragment_shader.c_str()); + formatFragmentShader((char*)&fbuffer, fragment_shader.c_str()); // compile GLint success; GLuint vshader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vshader, 1, (const GLchar**)&vbuffer.data, NULL); + const byte* _data = &vbuffer; + glShaderSource(vshader, 1, (const GLchar**)&_data, NULL); glCompileShader(vshader); glGetShaderiv(vshader, GL_COMPILE_STATUS, &success); if (success == GL_FALSE) return false; GLuint fshader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fshader, 1, (const GLchar**)&fbuffer.data, NULL); + _data = &fbuffer; + glShaderSource(fshader, 1, (const GLchar**)&_data, NULL); glCompileShader(fshader); glGetShaderiv(fshader, GL_COMPILE_STATUS, &success); if (success == GL_FALSE) diff --git a/src/libjin/Graphics/animation/je_animation.cpp b/src/libjin/Graphics/animation/je_animation.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/Graphics/animation/je_animation.h b/src/libjin/Graphics/animation/je_animation.h new file mode 100644 index 0000000..c006f83 --- /dev/null +++ b/src/libjin/Graphics/animation/je_animation.h @@ -0,0 +1,17 @@ +#ifndef __JE_ANIMATION_H +#define __JE_ANIMATION_H + +namespace JinEngine +{ + namespace Graphics + { + + class Animation + { + + }; + + } // namespace Graphics +} // namespace JinEngine + +#endif \ No newline at end of file diff --git a/src/libjin/Graphics/animation/je_clip.cpp b/src/libjin/Graphics/animation/je_clip.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/Graphics/animation/je_clip.h b/src/libjin/Graphics/animation/je_clip.h new file mode 100644 index 0000000..35a35b3 --- /dev/null +++ b/src/libjin/Graphics/animation/je_clip.h @@ -0,0 +1,17 @@ +#ifndef __JE_CLIP_H +#define __JE_CLIP_H + +namespace JinEngine +{ + namespace Graphics + { + + class Clip + { + + }; + + } // namespace Graphics +} // namespace JinEngine + +#endif \ No newline at end of file diff --git a/src/libjin/Graphics/je_bitmap.h b/src/libjin/Graphics/je_bitmap.h index 8adc326..d9d0981 100644 --- a/src/libjin/Graphics/je_bitmap.h +++ b/src/libjin/Graphics/je_bitmap.h @@ -4,7 +4,9 @@ #if LIBJIN_MODULES_RENDER #include "../3rdparty/GLee/GLee.h" +#include "../common/je_types.h" #include "../math/je_vector2.hpp" + #include "je_color.h" namespace JinEngine diff --git a/src/libjin/Graphics/je_canvas.h b/src/libjin/Graphics/je_canvas.h index 321cd32..9839899 100644 --- a/src/libjin/Graphics/je_canvas.h +++ b/src/libjin/Graphics/je_canvas.h @@ -64,4 +64,5 @@ namespace JinEngine } // namespace JinEngine #endif // LIBJIN_MODULES_RENDER + #endif // __JE_CANVAS_H \ No newline at end of file diff --git a/src/libjin/Graphics/je_color.h b/src/libjin/Graphics/je_color.h index a3c7582..80c1e4d 100644 --- a/src/libjin/Graphics/je_color.h +++ b/src/libjin/Graphics/je_color.h @@ -27,8 +27,14 @@ namespace JinEngine static const Color MAGENTA; static const Color YELLOW; + /// + /// + /// Color() { r = g = b = a = 0; }; - + + /// + /// + /// Color(unsigned char _r , unsigned char _g , unsigned char _b diff --git a/src/libjin/Graphics/je_drawable.cpp b/src/libjin/Graphics/je_drawable.cpp index 00ff739..af9e4d8 100644 --- a/src/libjin/Graphics/je_drawable.cpp +++ b/src/libjin/Graphics/je_drawable.cpp @@ -14,42 +14,42 @@ namespace JinEngine { Drawable::Drawable(int w, int h) - : texture(0) - , size(w, h) - , anchor(0, 0) + : mTexture(0) + , mSize(w, h) + , mOrigin(0, 0) { - vertex_coords[0] = 0; vertex_coords[1] = 0; - vertex_coords[2] = 0; vertex_coords[3] = h; - vertex_coords[4] = w; vertex_coords[5] = h; - vertex_coords[6] = w; vertex_coords[7] = 0; - - texture_coords[0] = 0; texture_coords[1] = 0; - texture_coords[2] = 0; texture_coords[3] = 1; - texture_coords[4] = 1; texture_coords[5] = 1; - texture_coords[6] = 1; texture_coords[7] = 0; + mVertexCoords[0] = 0; mVertexCoords[1] = 0; + mVertexCoords[2] = 0; mVertexCoords[3] = h; + mVertexCoords[4] = w; mVertexCoords[5] = h; + mVertexCoords[6] = w; mVertexCoords[7] = 0; + + mTextureCoords[0] = 0; mTextureCoords[1] = 0; + mTextureCoords[2] = 0; mTextureCoords[3] = 1; + mTextureCoords[4] = 1; mTextureCoords[5] = 1; + mTextureCoords[6] = 1; mTextureCoords[7] = 0; } Drawable::Drawable(const Bitmap* bitmap) - : texture(0) - , anchor(0, 0) + : mTexture(0) + , mOrigin(0, 0) { - unsigned int w = size.w = bitmap->getWidth(); - unsigned int h = size.h = bitmap->getHeight(); + uint32 w = mSize.w = bitmap->getWidth(); + uint32 h = mSize.h = bitmap->getHeight(); - vertex_coords[0] = 0; vertex_coords[1] = 0; - vertex_coords[2] = 0; vertex_coords[3] = h; - vertex_coords[4] = w; vertex_coords[5] = h; - vertex_coords[6] = w; vertex_coords[7] = 0; + mVertexCoords[0] = 0; mVertexCoords[1] = 0; + mVertexCoords[2] = 0; mVertexCoords[3] = h; + mVertexCoords[4] = w; mVertexCoords[5] = h; + mVertexCoords[6] = w; mVertexCoords[7] = 0; - texture_coords[0] = 0; texture_coords[1] = 0; - texture_coords[2] = 0; texture_coords[3] = 1; - texture_coords[4] = 1; texture_coords[5] = 1; - texture_coords[6] = 1; texture_coords[7] = 0; + mTextureCoords[0] = 0; mTextureCoords[1] = 0; + mTextureCoords[2] = 0; mTextureCoords[3] = 1; + mTextureCoords[4] = 1; mTextureCoords[5] = 1; + mTextureCoords[6] = 1; mTextureCoords[7] = 0; const Color* pixels = bitmap->getPixels(); - texture = gl.genTexture(); - gl.bindTexture(texture); + mTexture = gl.genTexture(); + gl.bindTexture(mTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); gl.texImage(GL_RGBA8, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); @@ -58,26 +58,26 @@ namespace JinEngine Drawable::~Drawable() { - glDeleteTextures(1, &texture); + glDeleteTextures(1, &mTexture); } - void Drawable::setAnchor(int x, int y) + void Drawable::setOrigin(int x, int y) { - anchor.x = x; - anchor.y = y; + mOrigin.x = x; + mOrigin.y = y; } void Drawable::draw(int x, int y, float sx, float sy, float r) { - gl.ModelMatrix.setTransformation(x, y, r, sx, sy, anchor.x, anchor.y); + gl.ModelMatrix.setTransformation(x, y, r, sx, sy, mOrigin.x, mOrigin.y); Shader* shader = Shader::getCurrentShader(); shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix); shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix); - shader->bindVertexPointer(2, GL_FLOAT, 0, vertex_coords); - shader->bindUVPointer(2, GL_FLOAT, 0, texture_coords); + shader->bindVertexPointer(2, GL_FLOAT, 0, mVertexCoords); + shader->bindUVPointer(2, GL_FLOAT, 0, mTextureCoords); - gl.bindTexture(texture); + gl.bindTexture(mTexture); gl.drawArrays(GL_QUADS, 0, 4); gl.bindTexture(0); } @@ -90,10 +90,10 @@ namespace JinEngine slice.w, slice.h, slice.w, 0 }; - float slx = slice.x / size.w; - float sly = slice.y / size.h; - float slw = slice.w / size.w; - float slh = slice.h / size.h; + float slx = slice.x / mSize.w; + float sly = slice.y / mSize.h; + float slw = slice.w / mSize.w; + float slh = slice.h / mSize.h; float texCoords[8] = { slx, sly, slx, sly + slh, @@ -109,7 +109,7 @@ namespace JinEngine shader->bindVertexPointer(2, GL_FLOAT, 0, vertCoords); shader->bindUVPointer(2, GL_FLOAT, 0, texCoords); - gl.bindTexture(texture); + gl.bindTexture(mTexture); gl.drawArrays(GL_QUADS, 0, 4); gl.bindTexture(0); } diff --git a/src/libjin/Graphics/je_drawable.h b/src/libjin/Graphics/je_drawable.h index 2547373..1b739cb 100644 --- a/src/libjin/Graphics/je_drawable.h +++ b/src/libjin/Graphics/je_drawable.h @@ -37,7 +37,7 @@ namespace JinEngine /// /// /// - void setAnchor(int x, int y); + void setOrigin(int x, int y); /// /// @@ -52,17 +52,17 @@ namespace JinEngine /// /// /// - inline int getWidth() const { return size.w; } + inline int getWidth() const { return mSize.w; } /// /// /// - inline int getHeight() const { return size.h; } + inline int getHeight() const { return mSize.h; } /// /// /// - inline GLuint getTexture() const { return texture; } + inline GLuint getTexture() const { return mTexture; } /// /// @@ -72,14 +72,12 @@ namespace JinEngine protected: static const int DRAWABLE_V_SIZE = 8; - GLuint texture; - GLuint vbo; - /* TODO: vertex buffer object */ - /* GLuint vbo; */ - JinEngine::Math::Vector2 size; - JinEngine::Math::Vector2 anchor; - float vertex_coords[DRAWABLE_V_SIZE]; - float texture_coords[DRAWABLE_V_SIZE]; + GLuint mTexture; + GLuint mVBO; + JinEngine::Math::Vector2 mSize; + JinEngine::Math::Vector2 mOrigin; + float mVertexCoords[DRAWABLE_V_SIZE]; + float mTextureCoords[DRAWABLE_V_SIZE]; }; @@ -87,4 +85,5 @@ namespace JinEngine } // namespace JinEngine #endif // LIBJIN_MODULES_RENDER + #endif // __JE_DRAWABLE \ No newline at end of file diff --git a/src/libjin/Graphics/je_gl.h b/src/libjin/Graphics/je_gl.h index 703fdc7..846b90a 100644 --- a/src/libjin/Graphics/je_gl.h +++ b/src/libjin/Graphics/je_gl.h @@ -13,9 +13,19 @@ namespace JinEngine class OpenGL : public ogl2d::OpenGL { public: + /// + /// + /// Math::Matrix ProjectionMatrix; + + /// + /// + /// Math::Matrix ModelMatrix; + /// + /// + /// OpenGL() : ogl2d::OpenGL() { } diff --git a/src/libjin/Graphics/je_image.cpp b/src/libjin/Graphics/je_image.cpp index a905943..f800423 100644 --- a/src/libjin/Graphics/je_image.cpp +++ b/src/libjin/Graphics/je_image.cpp @@ -15,7 +15,7 @@ namespace JinEngine if (imgData == nullptr) return nullptr; int w, h; - void* data = stbi_load_from_memory((unsigned char *)imgData, size, &w, &h, NULL, STBI_rgb_alpha); + void* data = stbi_load_from_memory((uint8*)imgData, size, &w, &h, NULL, STBI_rgb_alpha); if (data == nullptr) return nullptr; Image* image = new Image(); @@ -29,8 +29,8 @@ namespace JinEngine { AssetDatabase* fs = AssetDatabase::get(); Buffer buffer; - fs->read(path, &buffer); - return createImage(buffer.data, buffer.size); + fs->read(path, buffer); + return createImage(&buffer, buffer.size()); } Image::Image() @@ -43,4 +43,4 @@ namespace JinEngine } } // namespace Graphics -} // namespace JinEngine \ No newline at end of file +} // namespace JinEngine \ No newline at end of file diff --git a/src/libjin/Graphics/je_mesh.cpp b/src/libjin/Graphics/je_mesh.cpp index 4ef5a6e..dd2d61c 100644 --- a/src/libjin/Graphics/je_mesh.cpp +++ b/src/libjin/Graphics/je_mesh.cpp @@ -5,7 +5,7 @@ namespace JinEngine namespace Graphics { - + } // namespace Graphics } // namespace JinEngine \ No newline at end of file diff --git a/src/libjin/Graphics/je_mesh.h b/src/libjin/Graphics/je_mesh.h index 854b701..ed22d91 100644 --- a/src/libjin/Graphics/je_mesh.h +++ b/src/libjin/Graphics/je_mesh.h @@ -6,6 +6,9 @@ namespace JinEngine namespace Graphics { + /// + /// + /// class Mesh { public: diff --git a/src/libjin/Graphics/je_shapes.h b/src/libjin/Graphics/je_shapes.h index ea010be..2221526 100644 --- a/src/libjin/Graphics/je_shapes.h +++ b/src/libjin/Graphics/je_shapes.h @@ -1,7 +1,7 @@ #ifndef __JE_GEOMETRY_H #define __JE_GEOMETRY_H #include "../core/je_configuration.h" -#if LIBJIN_MODULES_RENDER +#if defined(jin_graphics) #include "je_color.h" #include "je_canvas.h" @@ -18,10 +18,6 @@ namespace JinEngine LINE }RenderMode; - /** - * TODO: - * drawPixels(int n, points) - */ extern void line(int x1, int y1, int x2, int y2); extern void rect(RenderMode mode, int x, int y, int w, int h); extern void triangle(RenderMode mode, int x1, int y1, int x2, int y2, int x3, int y3); @@ -33,5 +29,6 @@ namespace JinEngine } // namespace Graphics } // namespace JinEngine -#endif // LIBJIN_MODULES_RENDER +#endif // jin_graphics + #endif // __JE_GEOMETRY_H \ No newline at end of file diff --git a/src/libjin/Graphics/je_sprite.cpp b/src/libjin/Graphics/je_sprite.cpp index e69de29..3ac976a 100644 --- a/src/libjin/Graphics/je_sprite.cpp +++ b/src/libjin/Graphics/je_sprite.cpp @@ -0,0 +1,11 @@ +#include "je_sprite.h" + +namespace JinEngine +{ + namespace Graphics + { + + + + } // namespace Graphics +} // namespace JinEngine \ No newline at end of file diff --git a/src/libjin/Graphics/je_sprite.h b/src/libjin/Graphics/je_sprite.h index 9c3f7a2..23824a7 100644 --- a/src/libjin/Graphics/je_sprite.h +++ b/src/libjin/Graphics/je_sprite.h @@ -1,6 +1,10 @@ #ifndef __JE_SPRITE_H #define __JE_SPRITE_H +#include "je_color.h" +#include "../common/je_types.h" +#include "../math/je_vector2.hpp" + namespace JinEngine { namespace Graphics @@ -11,10 +15,16 @@ namespace JinEngine class Sprite { public: - + + private: + Math::Vector2 mPosition; + Math::Vector2 mOrigin; + Math::Vector2 mScale; + Color mColor; + }; - } -} + } // namespace Graphics +} // namespace JinEngine #endif \ No newline at end of file diff --git a/src/libjin/Graphics/je_sprite_batch.cpp b/src/libjin/Graphics/je_sprite_batch.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/Graphics/je_sprite_batch.h b/src/libjin/Graphics/je_sprite_batch.h new file mode 100644 index 0000000..85a7951 --- /dev/null +++ b/src/libjin/Graphics/je_sprite_batch.h @@ -0,0 +1,17 @@ +#ifndef __JE_SPRITE_BATCH_H +#define __JE_SPRITE_BATCH_H + +namespace JinEngine +{ + namespace Graphics + { + + class SpriteBatch + { + + }; + + } // namespace Graphics +} // namespace JinEngine + +#endif \ No newline at end of file diff --git a/src/libjin/Graphics/je_window.cpp b/src/libjin/Graphics/je_window.cpp index bdcb00a..163aa36 100644 --- a/src/libjin/Graphics/je_window.cpp +++ b/src/libjin/Graphics/je_window.cpp @@ -70,15 +70,15 @@ namespace JinEngine return false; SDL_GL_SetSwapInterval(vsync ? 1 : 0); SDL_GL_MakeCurrent(wnd, ctx); - /* default configuration */ + // default configuration gl.setClearColor(0, 0, 0, 0xff); gl.pushColor(0xff, 0xff, 0xff, 0xff); gl.enable(GL_BLEND); gl.enable(GL_TEXTURE_2D); gl.setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - /* avoid white screen blink on windows */ + // avoid white screen blink on windows swapBuffers(); - /* bind to default canvas */ + // bind to default canvas Canvas::unbind(); Shader::unuse(); return true; @@ -86,10 +86,10 @@ namespace JinEngine void Window::quitSystem() { - /* disable opengl */ + // disable opengl gl.disable(GL_BLEND); gl.disable(GL_TEXTURE_2D); - /* close window */ + // close window SDL_DestroyWindow(wnd); SDL_Quit(); } diff --git a/src/libjin/Graphics/je_window.h b/src/libjin/Graphics/je_window.h index f7d265f..0969a36 100644 --- a/src/libjin/Graphics/je_window.h +++ b/src/libjin/Graphics/je_window.h @@ -59,6 +59,7 @@ namespace JinEngine void swapBuffers(); private: + // declare a singleton singleton(Window); diff --git a/src/libjin/Graphics/particle/je_particle.cpp b/src/libjin/Graphics/particle/je_particle.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/Graphics/particle/je_particle.h b/src/libjin/Graphics/particle/je_particle.h new file mode 100644 index 0000000..ba4dd18 --- /dev/null +++ b/src/libjin/Graphics/particle/je_particle.h @@ -0,0 +1,20 @@ +#ifndef __JE_PARTICLE_H +#define __JE_PARTICLE_H + +namespace JinEngine +{ + namespace Graphics + { + + /// + /// Single particle. + /// + class Particle + { + + }; + + } // namespace Graphics +} // namespace JinEngine + +#endif \ No newline at end of file diff --git a/src/libjin/Graphics/particle/je_particle_batch.cpp b/src/libjin/Graphics/particle/je_particle_batch.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/Graphics/particle/je_particle_batch.h b/src/libjin/Graphics/particle/je_particle_batch.h new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/Graphics/particle/je_particle_emitter.cpp b/src/libjin/Graphics/particle/je_particle_emitter.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/Graphics/particle/je_particle_emitter.h b/src/libjin/Graphics/particle/je_particle_emitter.h new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/Graphics/particle/je_particle_system.cpp b/src/libjin/Graphics/particle/je_particle_system.cpp new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/src/libjin/Graphics/particle/je_particle_system.cpp @@ -0,0 +1 @@ +#pragma once diff --git a/src/libjin/Graphics/particle/je_particle_system.h b/src/libjin/Graphics/particle/je_particle_system.h new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/Input/je_event.h b/src/libjin/Input/je_event.h index ca5dab7..2e87dc9 100644 --- a/src/libjin/Input/je_event.h +++ b/src/libjin/Input/je_event.h @@ -14,6 +14,9 @@ namespace JinEngine typedef SDL_Keycode Key; typedef SDL_MouseWheelEvent Wheel; + /// + /// + /// enum EventType { QUIT = SDL_QUIT, /* keyboard events */ @@ -39,6 +42,9 @@ namespace JinEngine WINDOW_EVENT = SDL_WINDOWEVENT, }; + /// + /// + /// enum WindowEvent { WINDOW_SHOWN = SDL_WINDOWEVENT_SHOWN , WINDOW_HIDDEN = SDL_WINDOWEVENT_HIDDEN , @@ -58,16 +64,25 @@ namespace JinEngine WINDOW_HIT_TEST = SDL_WINDOWEVENT_HIT_TEST , }; + /// + /// + /// inline int pollEvent(Event* e) { return SDL_PollEvent(e); } + /// + /// + /// inline const char* getKeyName(Key key) { return SDL_GetKeyName(key); } + /// + /// + /// inline const char* getButtonName(int button) { switch (button) @@ -102,4 +117,5 @@ namespace JinEngine } // namespace JinEngine #endif // LIBJIN_MODULES_INPUT -#endif \ No newline at end of file + +#endif // __JE_EVENT_H \ No newline at end of file diff --git a/src/libjin/Input/je_joypad.h b/src/libjin/Input/je_joypad.h index 49cd2d8..74173af 100644 --- a/src/libjin/Input/je_joypad.h +++ b/src/libjin/Input/je_joypad.h @@ -8,6 +8,9 @@ namespace JinEngine namespace Input { + /// + /// + /// inline const char* getJoyButtonName(int button) { switch (button) @@ -31,6 +34,9 @@ namespace JinEngine } } + /// + /// + /// inline const char* getJoyAxisName(int axis) { switch (axis) @@ -47,4 +53,4 @@ namespace JinEngine } // namespace Input } // namespace JinEngine -#endif \ No newline at end of file +#endif // __JE_JOYPAD_H \ No newline at end of file diff --git a/src/libjin/Input/je_keyboard.h b/src/libjin/Input/je_keyboard.h index 85b6267..61da361 100644 --- a/src/libjin/Input/je_keyboard.h +++ b/src/libjin/Input/je_keyboard.h @@ -6,6 +6,9 @@ namespace JinEngine namespace Input { + /// + /// + /// class Keyboard { diff --git a/src/libjin/Input/je_mouse.h b/src/libjin/Input/je_mouse.h index 6f01993..bd25e9c 100644 --- a/src/libjin/Input/je_mouse.h +++ b/src/libjin/Input/je_mouse.h @@ -10,16 +10,33 @@ namespace JinEngine namespace Input { + /// + /// + /// class Mouse : public Singleton { public: + /// + /// + /// void getState(int* x, int* y); + + /// + /// + /// void setVisible(bool visible); private: singleton(Mouse); + /// + /// + /// Mouse() {}; + + /// + /// + /// ~Mouse() {}; }; @@ -28,4 +45,5 @@ namespace JinEngine } // namespace JinEngine #endif // LIBJIN_MODULES_INPUT + #endif // __JE_MOUSE_H \ No newline at end of file diff --git a/src/libjin/Math/README.md b/src/libjin/Math/README.md new file mode 100644 index 0000000..a096332 --- /dev/null +++ b/src/libjin/Math/README.md @@ -0,0 +1 @@ +此文件夹下命名空间为JinEngine,用于存放模块间共用的内容 \ No newline at end of file diff --git a/src/libjin/Math/je_math.h b/src/libjin/Math/je_math.h index adbe2f4..1f8e0b3 100644 --- a/src/libjin/Math/je_math.h +++ b/src/libjin/Math/je_math.h @@ -11,10 +11,10 @@ namespace JinEngine { #ifdef min - # undef min + #undef min #endif // min #ifdef max - # undef max + #undef max #endif // max template @@ -74,4 +74,4 @@ namespace JinEngine } // namespace Math } // namespace JinEngine -#endif \ No newline at end of file +#endif // __JE_UTILS_MATH_H \ No newline at end of file diff --git a/src/libjin/Math/je_matrix.h b/src/libjin/Math/je_matrix.h index 1ab9371..3592d18 100644 --- a/src/libjin/Math/je_matrix.h +++ b/src/libjin/Math/je_matrix.h @@ -12,143 +12,143 @@ namespace JinEngine float x, y; float s, t; }; - /** - * This class is the basis for all transformations in LOVE. Althought not - * really needed for 2D, it contains 4x4 elements to be compatible with - * OpenGL without conversions. - * 不需要转置的矩阵 - * https://blog.csdn.net/candycat1992/article/details/8830894 - **/ + /// + /// This class is the basis for all transformations in LOVE. Althought not + /// really needed for 2D, it contains 4x4 elements to be compatible with + /// OpenGL without conversions. + /// 不需要转置的矩阵 + /// https://blog.csdn.net/candycat1992/article/details/8830894 + /// class Matrix { private: - /** - * | e0 e4 e8 e12 | - * | e1 e5 e9 e13 | - * | e2 e6 e10 e14 | - * | e3 e7 e11 e15 | - **/ + /// + /// | e0 e4 e8 e12 | + /// | e1 e5 e9 e13 | + /// | e2 e6 e10 e14 | + /// | e3 e7 e11 e15 | + /// float e[16]; public: static const Matrix Identity; - /** - * Creates a new identity matrix. - **/ + /// + /// Creates a new identity matrix. + /// Matrix(); - /** - * Destructor. - **/ + /// + /// Destructor. + /// ~Matrix(); void setOrtho(float _left, float _right, float _bottom, float _top, float _near, float _far); - /** - * Multiplies this Matrix with another Matrix, changing neither. - * @param m The Matrix to multiply with this Matrix. - * @return The combined matrix. - **/ + /// + /// Multiplies this Matrix with another Matrix, changing neither. + /// @param m The Matrix to multiply with this Matrix. + /// @return The combined matrix. + /// Matrix operator * (const Matrix & m) const; - /** - * Multiplies a Matrix into this Matrix. - * @param m The Matrix to combine into this Matrix. - **/ + /// + /// Multiplies a Matrix into this Matrix. + /// @param m The Matrix to combine into this Matrix. + /// void operator *= (const Matrix & m); - /** - * Gets a pointer to the 16 array elements. - * @return The array elements. - **/ - const float * getElements() const; + /// + /// Gets a pointer to the 16 array elements. + /// @return The array elements. + /// + const float* getElements() const; - /** - * Resets this Matrix to the identity matrix. - **/ + /// + /// Resets this Matrix to the identity matrix. + /// void setIdentity(); - /** - * Resets this Matrix to a translation. - * @param x Translation along x-axis. - * @param y Translation along y-axis. - **/ + /// + /// Resets this Matrix to a translation. + /// @param x Translation along x-axis. + /// @param y Translation along y-axis. + /// void setTranslation(float x, float y); - /** - * Resets this Matrix to a rotation. - * @param r The angle in radians. - **/ + /// + /// Resets this Matrix to a rotation. + /// @param r The angle in radians. + /// void setRotation(float r); - /** - * Resets this Matrix to a scale transformation. - * @param sx Scale factor along the x-axis. - * @param sy Scale factor along the y-axis. - **/ + /// + /// Resets this Matrix to a scale transformation. + /// @param sx Scale factor along the x-axis. + /// @param sy Scale factor along the y-axis. + /// void setScale(float sx, float sy); - /** - * Resets this Matrix to a shear transformation. - * @param kx Shear along x-axis. - * @param ky Shear along y-axis. - **/ + /// + /// Resets this Matrix to a shear transformation. + /// @param kx Shear along x-axis. + /// @param ky Shear along y-axis. + /// void setShear(float kx, float ky); - /** - * Creates a transformation with a certain position, orientation, scale - * and offset. Perfect for Drawables -- what a coincidence! - * - * @param x The translation along the x-axis. - * @param y The translation along the y-axis. - * @param angle The rotation (rad) around the center with offset (ox,oy). - * @param sx Scale along x-axis. - * @param sy Scale along y-axis. - * @param ox The offset for rotation along the x-axis. - * @param oy The offset for rotation along the y-axis. - * @param kx Shear along x-axis - * @param ky Shear along y-axis - **/ + /// + /// Creates a transformation with a certain position, orientation, scale + /// and offset. Perfect for Drawables -- what a coincidence! + /// + /// @param x The translation along the x-axis. + /// @param y The translation along the y-axis. + /// @param angle The rotation (rad) around the center with offset (ox,oy). + /// @param sx Scale along x-axis. + /// @param sy Scale along y-axis. + /// @param ox The offset for rotation along the x-axis. + /// @param oy The offset for rotation along the y-axis. + /// @param kx Shear along x-axis + /// @param ky Shear along y-axis + /// void setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy); - /** - * Multiplies this Matrix with a translation. - * @param x Translation along x-axis. - * @param y Translation along y-axis. - **/ + /// + /// Multiplies this Matrix with a translation. + /// @param x Translation along x-axis. + /// @param y Translation along y-axis. + /// void translate(float x, float y); - /** - * Multiplies this Matrix with a rotation. - * @param r Angle in radians. - **/ + /// + /// Multiplies this Matrix with a rotation. + /// @param r Angle in radians. + /// void rotate(float r); - /** - * Multiplies this Matrix with a scale transformation. - * @param sx Scale factor along the x-axis. - * @param sy Scale factor along the y-axis. - **/ + /// + /// Multiplies this Matrix with a scale transformation. + /// @param sx Scale factor along the x-axis. + /// @param sy Scale factor along the y-axis. + /// void scale(float sx, float sy); - /** - * Multiplies this Matrix with a shear transformation. - * @param kx Shear along the x-axis. - * @param ky Shear along the y-axis. - **/ + /// + /// Multiplies this Matrix with a shear transformation. + /// @param kx Shear along the x-axis. + /// @param ky Shear along the y-axis. + /// void shear(float kx, float ky); - /** - * Transforms an array of vertices by this Matrix. The sources and - * destination arrays may be the same. - * - * @param dst Storage for the transformed vertices. - * @param src The source vertices. - * @param size The number of vertices. - **/ + /// + /// Transforms an array of vertices by this Matrix. The sources and + /// destination arrays may be the same. + /// + /// @param dst Storage for the transformed vertices. + /// @param src The source vertices. + /// @param size The number of vertices. + /// void transform(vertex * dst, const vertex * src, int size) const; }; @@ -156,4 +156,4 @@ namespace JinEngine } // namespace Math } // namespace JinEngine -#endif \ No newline at end of file +#endif // __JE_MATRIX_H \ No newline at end of file diff --git a/src/libjin/Math/je_quad.h b/src/libjin/Math/je_quad.h index 4121eab..2a66fa1 100644 --- a/src/libjin/Math/je_quad.h +++ b/src/libjin/Math/je_quad.h @@ -6,12 +6,18 @@ namespace JinEngine namespace Math { + /// + /// + /// struct Quad { + /// + /// + /// float x, y, w, h; }; } // namespace Math } // namespace JinEngine -#endif \ No newline at end of file +#endif // __JE_QUAD_H \ No newline at end of file diff --git a/src/libjin/Math/je_random.h b/src/libjin/Math/je_random.h new file mode 100644 index 0000000..eb59341 --- /dev/null +++ b/src/libjin/Math/je_random.h @@ -0,0 +1,14 @@ +#ifndef __JE_RANDOM_H +#define __JE_RANDOM_H + +namespace JinEngine +{ + namespace Math + { + + + + } +} + +#endif \ No newline at end of file diff --git a/src/libjin/Net/je_net_manager.h b/src/libjin/Net/je_net_manager.h index 31c4d04..05c3fe3 100644 --- a/src/libjin/Net/je_net_manager.h +++ b/src/libjin/Net/je_net_manager.h @@ -12,15 +12,32 @@ namespace JinEngine namespace Net { + /// + /// + /// class NetManager : public Subsystem { - public: - protected: - singleton(NetManager); + singleton(NetManager); + + /// + /// + /// NetManager() {}; + + /// + /// + /// ~NetManager() {}; + + /// + /// + /// bool initSystem(const SettingBase* setting) override; + + /// + /// + /// void quitSystem() override; }; @@ -29,4 +46,5 @@ namespace JinEngine } // namespace JinEngine #endif // LIBJIN_MODULES_NET -#endif // __JE_NET_H + +#endif // __JE_NET_H \ No newline at end of file diff --git a/src/libjin/Net/je_socket.h b/src/libjin/Net/je_socket.h index 5a31683..cfe904f 100644 --- a/src/libjin/Net/je_socket.h +++ b/src/libjin/Net/je_socket.h @@ -10,12 +10,18 @@ namespace JinEngine namespace Net { + /// + /// + /// enum SocketType { TCP, UDP }; + /// + /// + /// struct SocketInformation { unsigned int address; @@ -23,34 +29,109 @@ namespace JinEngine SocketType type; }; + /// + /// + /// class Socket { public: + + /// + /// + /// Socket() {}; + + /// + /// + /// Socket(const Socket& socket); + + /// + /// + /// Socket(const SocketInformation& socketInformation); + + /// + /// + /// Socket(SocketType type, unsigned short port); + + /// + /// + /// Socket(SocketType type, unsigned int address, unsigned short port); + + /// + /// + /// Socket(SocketType type, const char* address, unsigned short port); + + /// + /// + /// ~Socket(); + + /// + /// + /// void configureBlocking(bool bocking); + + /// + /// + /// Socket* accept(); + + /// + /// + /// int receive(char* buffer, int size); + + /// + /// + /// int send(char* buffer, int size); + + /// + /// + /// void sendTo(char* buffer, int size, unsigned int address, unsigned int port); + + /// + /// + /// int receiveFrom(char* buffer, int size, unsigned int address, unsigned int port); + + /// + /// + /// void close(); protected: #if LIBJIN_NET_TEKCOS + + /// + /// + /// Socket(const tk_TCPsocket& tcpHandle); + + /// + /// + /// Socket(const tk_UDPsocket& udpHandle); + + /// + /// + /// union { tk_TCPsocket tcpHandle; tk_UDPsocket udpHandle; } handle; #endif + + /// + /// + /// SocketType type; }; @@ -59,4 +140,5 @@ namespace JinEngine } // namespace JinEngine #endif // LIBJIN_MODULES_NET -#endif // __JE_NET_SOCKET_H + +#endif // __JE_NET_SOCKET_H \ No newline at end of file diff --git a/src/libjin/Thread/je_thread.cpp b/src/libjin/Thread/je_thread.cpp deleted file mode 100644 index 3c18657..0000000 --- a/src/libjin/Thread/je_thread.cpp +++ /dev/null @@ -1,301 +0,0 @@ -#include "../core/je_configuration.h" -#if LIBJIN_MODULES_THREAD - -#include "je_thread.h" - -namespace JinEngine -{ - namespace MultiThread - { - - class Mutex - { - public: - Mutex(); - ~Mutex(); - - void lock(); - void unlock(); - private: - #if LIBJIN_THREAD_SDL - SDL_mutex* mutex; - #endif - friend class Conditional; - }; - - // 条件变量线程间signal wait - class Conditional - { - public: - Conditional(); - ~Conditional(); - void signal(); - void broadcast(); - bool wait(Mutex* mutex, int timeout = -1); - private: - #if LIBJIN_THREAD_SDL - SDL_cond* cond; - #endif - }; - - class Lock - { - public: - Lock(Mutex* m) : mutex(m) { - mutex->lock(); - } - - Lock(Mutex& m) : mutex(&m) { - mutex->lock(); - } - - ~Lock() { - mutex->unlock(); - } - private: - Mutex* mutex; - - Lock(Lock&) {} - - }; - - ////////////////////////////////////////////////////////////////////// - - Mutex::Mutex() - { - #if LIBJIN_THREAD_SDL - mutex = SDL_CreateMutex(); - #endif - } - - Mutex::~Mutex() - { - #if LIBJIN_THREAD_SDL - SDL_DestroyMutex(mutex); - #endif - } - - void Mutex::lock() - { - #if LIBJIN_THREAD_SDL - SDL_LockMutex(mutex); - #endif - } - - void Mutex::unlock() - { - #if LIBJIN_THREAD_SDL - SDL_UnlockMutex(mutex); - #endif - } - - ////////////////////////////////////////////////////////////////////// - - Conditional::Conditional() - { - #if LIBJIN_THREAD_SDL - cond = SDL_CreateCond(); - #endif - } - - Conditional::~Conditional() - { - #if LIBJIN_THREAD_SDL - SDL_DestroyCond(cond); - #endif - } - - void Conditional::signal() - { - #if LIBJIN_THREAD_SDL - SDL_CondSignal(cond); - #endif - } - - void Conditional::broadcast() - { - #if LIBJIN_THREAD_SDL - SDL_CondBroadcast(cond); - #endif - } - - bool Conditional::wait(Mutex* mutex, int timeout) - { - #if LIBJIN_THREAD_SDL - if (timeout < 0) - return !SDL_CondWait(cond, mutex->mutex); - else - return (SDL_CondWaitTimeout(cond, mutex->mutex, timeout) == 0); - #endif - } - - ////////////////////////////////////////////////////////////////////// - - Thread::ThreadData::ThreadData(Mutex* m, Conditional* c) - : mutex(m) - , condition(c) - , share() - { - } - - Thread::ThreadData::~ThreadData() - { - } - - void Thread::ThreadData::set(int slot, Variant value) - { - Lock l(mutex); - share[slot] = value; - } - - Thread::Variant Thread::ThreadData::get(int slot) - { - Lock l(mutex); - return share[slot]; - } - - bool Thread::ThreadData::exist(int slot) - { - Lock l(mutex); - return share.count(slot) == 1; - } - - void Thread::ThreadData::remove(int slot) - { - Lock l(mutex); - if (exist(slot)) - { - share.erase(slot); - } - } - - ////////////////////////////////////////////////////////////////////// - - Thread::Thread(const std::string tname, ThreadRunner runner) - : name(tname) - , running(false) - , threadRunner(runner) - { - mutex = new Mutex(); - condition = new Conditional(); - common = new Thread::ThreadData(mutex, condition); - } - - Thread::~Thread() - { - #if LIBJIN_THREAD_SDL - #endif - } - - const char* Thread::getName() - { - Lock l(mutex); - return name.c_str(); - }; - - bool Thread::isRunning() - { - Lock l(mutex); - return running; - }; - - bool Thread::start(void* p) - { - Lock l(mutex); - if (running) - return false; - if (handle) - { - #if LIBJIN_THREAD_SDL - SDL_WaitThread(handle, nullptr); - #endif - } - #if LIBJIN_THREAD_SDL - handle = SDL_CreateThread(threadRunner, name.c_str(), p); - #elif LIBJIN_THREAD_CPP - handle = new std::thread(); - #endif - return (running = (handle != nullptr)); - } - - void Thread::wait() - { - { - Lock l(mutex); - if (!handle) - return; - } - #if LIBJIN_THREAD_SDL - SDL_WaitThread(handle, nullptr); - #endif - Lock l(mutex); - running = false; - handle = nullptr; - } - - void Thread::lock() - { - if (mutex != nullptr) - mutex->lock(); - } - - void Thread::unlock() - { - if (mutex != nullptr) - mutex->unlock(); - } - - void Thread::send(int slot, const Variant& value) - { - lock(); - common->set(slot, value); - unlock(); - condition->broadcast(); - } - - bool Thread::receive(int slot) - { - return common->exist(slot); - } - - Thread::Variant Thread::fetch(int slot) - { - Thread::Variant v = common->get(slot); - return v; - } - - Thread::Variant Thread::demand(int slot) - { - /** - * pthread_mutex_lock(mtx); - * while(pass == 0) - * { - * pthread_mutex_unlock(mtx); - * pthread_cond_just_wait(cv); - * pthread_mutex_lock(mtx); - * } - * pthread_mutex_unlock(mtx); - */ - lock(); - while (!common->exist(slot)) - { - if (common->exist(ThreadData::SLOT_ERROR)) - return 0; - condition->wait(mutex); - } - Thread::Variant v = common->get(slot); - unlock(); - return v; - } - - void Thread::remove(int slot) - { - lock(); - common->remove(slot); - unlock(); - } - - } // namespace MultiThread -} // namespace JinEngine - -#endif // LIBJIN_MODULES_THREAD \ No newline at end of file diff --git a/src/libjin/Thread/je_thread.h b/src/libjin/Thread/je_thread.h deleted file mode 100644 index b39e4e2..0000000 --- a/src/libjin/Thread/je_thread.h +++ /dev/null @@ -1,166 +0,0 @@ -#ifndef __JE_THREAD_H -#define __JE_THREAD_H -#include "../core/je_configuration.h" -#if LIBJIN_MODULES_THREAD - -#include -#include -#if LIBJIN_THREAD_SDL - #include "SDL2/SDL_thread.h" -#elif LIBJIN_THREAD_CPP - #include - #include - #include -#endif - -namespace JinEngine -{ - namespace MultiThread - { - /** - * 互斥锁(英语:英语:Mutual exclusion,缩写 Mutex)是一种用于多线程编程中,防止两条线程同时对同一公共资源 - * 比如全局变量)进行读写的机制。该目的通过将代码切片成一个一个的临界区域(critical section)达成。临界区域 - * 指的是一块对公共资源进行访问的代码,并非一种机制或是算法。一个程序、进程、线程可以拥有多个临界区域,但是并 - * 不一定会应用互斥锁。需要此机制的资源的例子有:旗标、队列、计数器、中断处理程序等用于在多条并行运行的代码间 - * 传递数据、同步状态等的资源。维护这些资源的同步、一致和完整是很困难的,因为一条线程可能在任何一个时刻被暂停 - * 休眠)或者恢复(唤醒)。 - */ - class Mutex; - class Conditional; - - /** - * Thread::demand Receive a message from a thread. Wait for the message to exist before returning. - * Thread::getName Get the name of a thread. - * Thread::kill Forcefully terminate the thread. - * Thread::peek Receive a message from a thread, but leave it in the message box. - * Thread::receive Receive a message from a thread. - * Thread::send Send a message. - * Thread::set Set a value. - * Thread::start Starts the thread. - * Thread::wait Wait for a thread to finish. - */ - class Thread - { - public: - struct Variant - { - enum Type - { - NONE = 0, - INTERGER, - BOOLEAN, - CHARACTER, - CSTRING, - POINTER, - REAL, - }; - Type type; - union - { - int integer; - bool boolean; - char character; - const char* cstring; - void* pointer; - float real; - }; - Variant() :type(NONE) {}; - Variant(const Variant& v){ memcpy(this, &v, sizeof(v)); } - Variant(int i) : integer(i), type(INTERGER) {}; - Variant(float f) : real(f), type(REAL) {}; - Variant(bool b) : boolean(b), type(BOOLEAN) {}; - Variant(char c) : character(c), type(CHARACTER) {}; - Variant(const char* s) : cstring(s), type(CSTRING) {}; - Variant(void* p) : pointer(p), type(POINTER) {}; - }; - - private: - class ThreadData - { - public: - static const int SLOT_ERROR = -1; - static const int SLOT_WARN = -2; - static const int SLOT_INFO = -3; - static const int SLOT_DEBUG = -4; - - ThreadData(Mutex*, Conditional*); - ~ThreadData(); - bool exist(int slot); - void set(int slot, Variant value); - Variant get(int slot); - void remove(int slot); - - Conditional* condition; - Mutex* mutex; - - private: - std::map share; // threads shared value - - }; - - public: - typedef int(*ThreadRunner)(void* obj); - - Thread(const std::string name, ThreadRunner threadfuncs); - ~Thread(); - bool start(void* p); - void wait(); - void send(int slot, const Variant& value); - bool receive(int slot); - Variant fetch(int slot); - Variant demand(int slot); - void remove(int slot); - const char* getName(); - bool isRunning(); - void lock(); - void unlock(); - - protected: - #if LIBJIN_THREAD_SDL - SDL_Thread* handle; // SDL thread - #elif LIBJIN_THREAD_CPP - std::thread* handle; // cpp thread - #endif - Mutex* mutex; // mutex variable - Conditional* condition; // condition variable - ThreadRunner threadRunner; // thread function - ThreadData* common; // threads common data - const std::string name; // thread name, for debugging purposes - /** - * https://stackoverflow.com/questions/149932/naming-conventions-for-threads - * - * Use short names because they don't make the lines in a log file too long. - * - * Create names where the important part is at the beginning. Log viewers in a - * graphical user interface tend to have tables with columns, and the thread - * column is usually small or will be made small by you to read everything else. - * - * Do not use the word "thread" in the thread name because it is obvious. - * - * Make the thread names easily grep-able. Avoid similar sounding thread names - * - * If you have several threads of the same nature, enumerate them with IDs that - * are unique to one execution of the application or one log file, whichever fits - * your logging habits. - * - * Avoid generalizations like "WorkerThread" (how do you name the next 5 worker - * threads?), "GUIThread" (which GUI? is it for one window? for everything?) or - * "Calculation" (what does it calculate?). - * - * If you have a test group that uses thread names to grep your application's log - * files, do not rename your threads after some time. Your testers will hate you for - * doing so. Thread names in well-tested applications should be there to stay. - * - * When you have threads that service a network connection, try to include the target - * network address in the thread name (e.g. channel_123.212.123.3). Don't forget about - * enumeration though if there are multiple connections to the same host. - */ - bool running; // running - - }; - - } // namespace MultiThread -} // namespace JinEngine - -#endif // LIBJIN_MODULES_THREAD -#endif // __JE_THREAD_H \ No newline at end of file diff --git a/src/libjin/Time/je_timer.h b/src/libjin/Time/je_timer.h index e5488cc..82b4b52 100644 --- a/src/libjin/Time/je_timer.h +++ b/src/libjin/Time/je_timer.h @@ -11,19 +11,49 @@ namespace JinEngine namespace Time { + /// + /// + /// class Timers { public: typedef void(*timer_callback)(void* prameters); + /// + /// + /// Timers(); + + /// + /// + /// ~Timers(); + + /// + /// + /// void update(int ms); + + /// + /// + /// void every(int ms, timer_callback callback, void* paramters); + + /// + /// + /// void after(int ms, timer_callback callback, void* paramters); + + /// + /// + /// void repeat(int ms, int count, timer_callback callback, void* paramters); private: + + /// + /// + /// class Timer { public: @@ -48,7 +78,10 @@ namespace JinEngine std::vector timers; }; - + + /// + /// + /// inline void sleep(int ms) { #if LIBJIN_TIME_SDL @@ -56,6 +89,9 @@ namespace JinEngine #endif } + /// + /// + /// inline double getSecond() { #if LIBJIN_TIME_SDL @@ -63,6 +99,9 @@ namespace JinEngine #endif } + /// + /// + /// inline double getMilliSecond() { #if LIBJIN_TIME_SDL @@ -74,4 +113,5 @@ namespace JinEngine } // namespace JinEngine #endif // LIBJIN_MODULES_TIME -#endif // __JE_TIMER_H + +#endif // __JE_TIMER_H \ No newline at end of file diff --git a/src/libjin/Utils/je_log.h b/src/libjin/Utils/je_log.h index 9b3fe2c..8e4bf61 100644 --- a/src/libjin/Utils/je_log.h +++ b/src/libjin/Utils/je_log.h @@ -132,4 +132,4 @@ void Loghelper::close() #endif -#endif +#endif \ No newline at end of file diff --git a/src/libjin/ai/je_behavior_tree.cpp b/src/libjin/ai/je_behavior_tree.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/ai/je_behavior_tree.h b/src/libjin/ai/je_behavior_tree.h new file mode 100644 index 0000000..0654849 --- /dev/null +++ b/src/libjin/ai/je_behavior_tree.h @@ -0,0 +1,20 @@ +#ifndef __JE_BEHAVIOR_TREE_H +#define __JE_BEHAVIOR_TREE_H + +namespace JinEngine +{ + namespace AI + { + + /// + /// + /// + class BehaviorTree + { + + }; + + } +} + +#endif \ No newline at end of file diff --git a/src/libjin/ai/je_state_machine.cpp b/src/libjin/ai/je_state_machine.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/ai/je_state_machine.h b/src/libjin/ai/je_state_machine.h new file mode 100644 index 0000000..4e80da3 --- /dev/null +++ b/src/libjin/ai/je_state_machine.h @@ -0,0 +1,20 @@ +#ifndef __JE_STATEMACHINE_TREE_H +#define __JE_STATEMACHINE_TREE_H + +namespace JinEngine +{ + namespace AI + { + + /// + /// + /// + class StateMachine + { + + }; + + } +} + +#endif \ No newline at end of file diff --git a/src/libjin/core/README.md b/src/libjin/core/README.md new file mode 100644 index 0000000..b393007 --- /dev/null +++ b/src/libjin/core/README.md @@ -0,0 +1 @@ +此文件夹下的文件控制编译和提供一些版本信息 \ No newline at end of file diff --git a/src/libjin/core/je_configuration.h b/src/libjin/core/je_configuration.h index 819b34d..f70ae5a 100644 --- a/src/libjin/core/je_configuration.h +++ b/src/libjin/core/je_configuration.h @@ -66,4 +66,12 @@ #define jin_graphics_font #define jin_graphics_shader +#define jin_audio + +#define jin_filesystem + +#define jin_game + +#define jin_core + #endif \ No newline at end of file diff --git a/src/libjin/core/je_types.h b/src/libjin/core/je_types.h deleted file mode 100644 index 5fd6c12..0000000 --- a/src/libjin/core/je_types.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef __JE_CORE_TYPES_H -#define __JE_CORE_TYPES_H - -#include - -namespace JinEngine -{ - namespace Core - { - - typedef int8_t int8; ///< Signed integer with a size of 8 bits. Supports values from -128 to 127 - typedef uint8_t uint8; ///< Unsigned integer with a size of 8 bits. Supports values from 0 to 255. - typedef uint8 byte; ///< Unsigned integer with 8 bits (1 byte). Supports 256 values from 0 to 255. - typedef int16_t int16; ///< Signed integer with a size of 16 bits. Supports values from -32768 to 32767 - typedef uint16_t uint16; ///< Unsigned integer with a size of 16 bits. Supports values from 0 to 65535. - typedef int32_t int32; ///< Signed integer with a size of 32 bits. Supports values from -2147483648 to 2147483647. - typedef uint32_t uint32; ///< Unsigned integer with a size of 32 bits. Supports values from 0 to 4294967295, (2^32 - 1). - typedef int64_t int64; ///< Signed integer with a size of 64 bits. Supports values from -(2^63) to (2^63 - 1). - typedef uint64_t uint64; ///< Unsigned integer with a size of 64 bits, Supports values from 0 to (2^64 - 1). - - } -} - -#endif // __JE_CORE_TYPES_H \ No newline at end of file diff --git a/src/libjin/core/je_version.h b/src/libjin/core/je_version.h new file mode 100644 index 0000000..6a91797 --- /dev/null +++ b/src/libjin/core/je_version.h @@ -0,0 +1,26 @@ +#ifndef __JE_CORE_VERSION_H +#define __JE_CORE_VERSION_H + +namespace JinEngine +{ + namespace Core + { + + /// + /// Get version of Jin. + /// + /// @return Version of Jin. + /// + int getVersion(); + + /// + /// Get author of Jin. + /// + /// @return Author of Jin. + /// + int getAuthor(); + + } // namespace Core +} // namespace JinEngine + +#endif \ No newline at end of file diff --git a/src/libjin/jin.h b/src/libjin/jin.h index 443fbed..53b61fb 100644 --- a/src/libjin/jin.h +++ b/src/libjin/jin.h @@ -14,7 +14,7 @@ #include "net/je_net_manager.h" #include "graphics/je_graphics.h" #include "time/je_timer.h" -#include "thread/je_thread.h" +#include "multithread/je_thread.h" #include "common/je_common.h" #define LIBJIN_VERSION "Jin 0.1"; diff --git a/src/libjin/multithread/je_thread.cpp b/src/libjin/multithread/je_thread.cpp new file mode 100644 index 0000000..3c18657 --- /dev/null +++ b/src/libjin/multithread/je_thread.cpp @@ -0,0 +1,301 @@ +#include "../core/je_configuration.h" +#if LIBJIN_MODULES_THREAD + +#include "je_thread.h" + +namespace JinEngine +{ + namespace MultiThread + { + + class Mutex + { + public: + Mutex(); + ~Mutex(); + + void lock(); + void unlock(); + private: + #if LIBJIN_THREAD_SDL + SDL_mutex* mutex; + #endif + friend class Conditional; + }; + + // 条件变量线程间signal wait + class Conditional + { + public: + Conditional(); + ~Conditional(); + void signal(); + void broadcast(); + bool wait(Mutex* mutex, int timeout = -1); + private: + #if LIBJIN_THREAD_SDL + SDL_cond* cond; + #endif + }; + + class Lock + { + public: + Lock(Mutex* m) : mutex(m) { + mutex->lock(); + } + + Lock(Mutex& m) : mutex(&m) { + mutex->lock(); + } + + ~Lock() { + mutex->unlock(); + } + private: + Mutex* mutex; + + Lock(Lock&) {} + + }; + + ////////////////////////////////////////////////////////////////////// + + Mutex::Mutex() + { + #if LIBJIN_THREAD_SDL + mutex = SDL_CreateMutex(); + #endif + } + + Mutex::~Mutex() + { + #if LIBJIN_THREAD_SDL + SDL_DestroyMutex(mutex); + #endif + } + + void Mutex::lock() + { + #if LIBJIN_THREAD_SDL + SDL_LockMutex(mutex); + #endif + } + + void Mutex::unlock() + { + #if LIBJIN_THREAD_SDL + SDL_UnlockMutex(mutex); + #endif + } + + ////////////////////////////////////////////////////////////////////// + + Conditional::Conditional() + { + #if LIBJIN_THREAD_SDL + cond = SDL_CreateCond(); + #endif + } + + Conditional::~Conditional() + { + #if LIBJIN_THREAD_SDL + SDL_DestroyCond(cond); + #endif + } + + void Conditional::signal() + { + #if LIBJIN_THREAD_SDL + SDL_CondSignal(cond); + #endif + } + + void Conditional::broadcast() + { + #if LIBJIN_THREAD_SDL + SDL_CondBroadcast(cond); + #endif + } + + bool Conditional::wait(Mutex* mutex, int timeout) + { + #if LIBJIN_THREAD_SDL + if (timeout < 0) + return !SDL_CondWait(cond, mutex->mutex); + else + return (SDL_CondWaitTimeout(cond, mutex->mutex, timeout) == 0); + #endif + } + + ////////////////////////////////////////////////////////////////////// + + Thread::ThreadData::ThreadData(Mutex* m, Conditional* c) + : mutex(m) + , condition(c) + , share() + { + } + + Thread::ThreadData::~ThreadData() + { + } + + void Thread::ThreadData::set(int slot, Variant value) + { + Lock l(mutex); + share[slot] = value; + } + + Thread::Variant Thread::ThreadData::get(int slot) + { + Lock l(mutex); + return share[slot]; + } + + bool Thread::ThreadData::exist(int slot) + { + Lock l(mutex); + return share.count(slot) == 1; + } + + void Thread::ThreadData::remove(int slot) + { + Lock l(mutex); + if (exist(slot)) + { + share.erase(slot); + } + } + + ////////////////////////////////////////////////////////////////////// + + Thread::Thread(const std::string tname, ThreadRunner runner) + : name(tname) + , running(false) + , threadRunner(runner) + { + mutex = new Mutex(); + condition = new Conditional(); + common = new Thread::ThreadData(mutex, condition); + } + + Thread::~Thread() + { + #if LIBJIN_THREAD_SDL + #endif + } + + const char* Thread::getName() + { + Lock l(mutex); + return name.c_str(); + }; + + bool Thread::isRunning() + { + Lock l(mutex); + return running; + }; + + bool Thread::start(void* p) + { + Lock l(mutex); + if (running) + return false; + if (handle) + { + #if LIBJIN_THREAD_SDL + SDL_WaitThread(handle, nullptr); + #endif + } + #if LIBJIN_THREAD_SDL + handle = SDL_CreateThread(threadRunner, name.c_str(), p); + #elif LIBJIN_THREAD_CPP + handle = new std::thread(); + #endif + return (running = (handle != nullptr)); + } + + void Thread::wait() + { + { + Lock l(mutex); + if (!handle) + return; + } + #if LIBJIN_THREAD_SDL + SDL_WaitThread(handle, nullptr); + #endif + Lock l(mutex); + running = false; + handle = nullptr; + } + + void Thread::lock() + { + if (mutex != nullptr) + mutex->lock(); + } + + void Thread::unlock() + { + if (mutex != nullptr) + mutex->unlock(); + } + + void Thread::send(int slot, const Variant& value) + { + lock(); + common->set(slot, value); + unlock(); + condition->broadcast(); + } + + bool Thread::receive(int slot) + { + return common->exist(slot); + } + + Thread::Variant Thread::fetch(int slot) + { + Thread::Variant v = common->get(slot); + return v; + } + + Thread::Variant Thread::demand(int slot) + { + /** + * pthread_mutex_lock(mtx); + * while(pass == 0) + * { + * pthread_mutex_unlock(mtx); + * pthread_cond_just_wait(cv); + * pthread_mutex_lock(mtx); + * } + * pthread_mutex_unlock(mtx); + */ + lock(); + while (!common->exist(slot)) + { + if (common->exist(ThreadData::SLOT_ERROR)) + return 0; + condition->wait(mutex); + } + Thread::Variant v = common->get(slot); + unlock(); + return v; + } + + void Thread::remove(int slot) + { + lock(); + common->remove(slot); + unlock(); + } + + } // namespace MultiThread +} // namespace JinEngine + +#endif // LIBJIN_MODULES_THREAD \ No newline at end of file diff --git a/src/libjin/multithread/je_thread.h b/src/libjin/multithread/je_thread.h new file mode 100644 index 0000000..b3a3e2e --- /dev/null +++ b/src/libjin/multithread/je_thread.h @@ -0,0 +1,167 @@ +#ifndef __JE_THREAD_H +#define __JE_THREAD_H +#include "../core/je_configuration.h" +#if LIBJIN_MODULES_THREAD + +#include +#include +#if LIBJIN_THREAD_SDL + #include "SDL2/SDL_thread.h" +#elif LIBJIN_THREAD_CPP + #include + #include + #include +#endif + +namespace JinEngine +{ + namespace MultiThread + { + /** + * 互斥锁(英语:英语:Mutual exclusion,缩写 Mutex)是一种用于多线程编程中,防止两条线程同时对同一公共资源 + * 比如全局变量)进行读写的机制。该目的通过将代码切片成一个一个的临界区域(critical section)达成。临界区域 + * 指的是一块对公共资源进行访问的代码,并非一种机制或是算法。一个程序、进程、线程可以拥有多个临界区域,但是并 + * 不一定会应用互斥锁。需要此机制的资源的例子有:旗标、队列、计数器、中断处理程序等用于在多条并行运行的代码间 + * 传递数据、同步状态等的资源。维护这些资源的同步、一致和完整是很困难的,因为一条线程可能在任何一个时刻被暂停 + * 休眠)或者恢复(唤醒)。 + */ + class Mutex; + class Conditional; + + // + // Thread::demand Receive a message from a thread. Wait for the message to exist before returning. + // Thread::getName Get the name of a thread. + // Thread::kill Forcefully terminate the thread. + // Thread::peek Receive a message from a thread, but leave it in the message box. + // Thread::receive Receive a message from a thread. + // Thread::send Send a message. + // Thread::set Set a value. + // Thread::start Starts the thread. + // Thread::wait Wait for a thread to finish. + // + class Thread + { + public: + struct Variant + { + enum Type + { + NONE = 0, + INTERGER, + BOOLEAN, + CHARACTER, + CSTRING, + POINTER, + REAL, + }; + Type type; + union + { + int integer; + bool boolean; + char character; + const char* cstring; + void* pointer; + float real; + }; + Variant() :type(NONE) {}; + Variant(const Variant& v){ memcpy(this, &v, sizeof(v)); } + Variant(int i) : integer(i), type(INTERGER) {}; + Variant(float f) : real(f), type(REAL) {}; + Variant(bool b) : boolean(b), type(BOOLEAN) {}; + Variant(char c) : character(c), type(CHARACTER) {}; + Variant(const char* s) : cstring(s), type(CSTRING) {}; + Variant(void* p) : pointer(p), type(POINTER) {}; + }; + + private: + class ThreadData + { + public: + static const int SLOT_ERROR = -1; + static const int SLOT_WARN = -2; + static const int SLOT_INFO = -3; + static const int SLOT_DEBUG = -4; + + ThreadData(Mutex*, Conditional*); + ~ThreadData(); + bool exist(int slot); + void set(int slot, Variant value); + Variant get(int slot); + void remove(int slot); + + Conditional* condition; + Mutex* mutex; + + private: + std::map share; // threads shared value + + }; + + public: + typedef int(*ThreadRunner)(void* obj); + + Thread(const std::string name, ThreadRunner threadfuncs); + ~Thread(); + bool start(void* p); + void wait(); + void send(int slot, const Variant& value); + bool receive(int slot); + Variant fetch(int slot); + Variant demand(int slot); + void remove(int slot); + const char* getName(); + bool isRunning(); + void lock(); + void unlock(); + + protected: + #if LIBJIN_THREAD_SDL + SDL_Thread* handle; // SDL thread + #elif LIBJIN_THREAD_CPP + std::thread* handle; // cpp thread + #endif + Mutex* mutex; // mutex variable + Conditional* condition; // condition variable + ThreadRunner threadRunner; // thread function + ThreadData* common; // threads common data + const std::string name; // thread name, for debugging purposes + /** + * https://stackoverflow.com/questions/149932/naming-conventions-for-threads + * + * Use short names because they don't make the lines in a log file too long. + * + * Create names where the important part is at the beginning. Log viewers in a + * graphical user interface tend to have tables with columns, and the thread + * column is usually small or will be made small by you to read everything else. + * + * Do not use the word "thread" in the thread name because it is obvious. + * + * Make the thread names easily grep-able. Avoid similar sounding thread names + * + * If you have several threads of the same nature, enumerate them with IDs that + * are unique to one execution of the application or one log file, whichever fits + * your logging habits. + * + * Avoid generalizations like "WorkerThread" (how do you name the next 5 worker + * threads?), "GUIThread" (which GUI? is it for one window? for everything?) or + * "Calculation" (what does it calculate?). + * + * If you have a test group that uses thread names to grep your application's log + * files, do not rename your threads after some time. Your testers will hate you for + * doing so. Thread names in well-tested applications should be there to stay. + * + * When you have threads that service a network connection, try to include the target + * network address in the thread name (e.g. channel_123.212.123.3). Don't forget about + * enumeration though if there are multiple connections to the same host. + */ + bool running; // running + + }; + + } // namespace MultiThread +} // namespace JinEngine + +#endif // LIBJIN_MODULES_THREAD + +#endif // __JE_THREAD_H \ No newline at end of file -- cgit v1.1-26-g67d0