From 4794b2ea02eb6b183f64c3dccfc4c82360bafa15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9au?= <theau.baton@etu.univ-amu.fr> Date: Fri, 10 Jan 2025 00:47:05 +0100 Subject: [PATCH] Add Kernel + Physic Engine --- old2/FrameBufferModule.hpp | 25 --- old2/ImageModule.cpp | 26 --- old2/ImageModule.hpp | 22 --- old2/Quad_Module.cpp | 180 ------------------ old2/SpriteModule.cpp | 16 -- old2/SpriteModule.hpp | 17 -- .../engine/graphics/front/engine/Engine.cpp | 15 ++ .../engine/graphics/front/engine/Engine.hpp | 1 + .../front/engine/FrameBufferMerger.cpp | 2 +- source/engine/graphics/front/engine/Layer.hpp | 4 +- .../engine/graphics/front/engine/Renderer.hpp | 2 +- .../graphics/front/module/Image_Module.cpp | 2 +- .../graphics/front/module/Image_Module.hpp | 2 +- .../graphics/front/module/Sprite_Module.cpp | 2 +- .../graphics/front/module/Sprite_Module.hpp | 2 +- .../graphics/front/module/Text_Module.cpp | 2 +- .../graphics/front/module/Text_Module.hpp | 2 +- source/engine/graphics/front/object/Image.cpp | 1 - source/engine/graphics/front/object/Image.hpp | 6 +- .../engine/graphics/front/object/Sprite.hpp | 6 +- source/engine/graphics/front/object/Text.hpp | 6 +- .../graphics/front/object/TileArray.hpp | 6 +- source/engine/physic/back/Position.cpp | 70 +++++++ source/engine/physic/back/Position.hpp | 45 +++++ source/engine/physic/back/SquareBox.cpp | 58 ++++++ source/engine/physic/back/SquareBox.hpp | 42 ++++ source/engine/physic/front/engine/Engine.cpp | 46 +++++ source/engine/physic/front/engine/Engine.hpp | 36 ++++ .../engine/physic/front/object/Collision.cpp | 27 +++ .../engine/physic/front/object/Collision.hpp | 24 +++ .../engine/physic/front/object/Tangible.cpp | 21 ++ .../engine/physic/front/object/Tangible.hpp | 38 ++++ .../physic/front/object/TangibleMovable.cpp | 10 + .../physic/front/object/TangibleMovable.hpp | 16 ++ .../physic/front/object/TangibleStatic.cpp | 10 + .../physic/front/object/TangibleStatic.hpp | 16 ++ .../front/engine => utility}/Priority.hpp | 0 .../utility/array_generator.hpp | 0 .../{graphics => }/utility/overloaded.hpp | 0 .../engine/{graphics => }/utility/ref_set.hpp | 0 .../utility/reference_sorter.hpp | 0 .../utility/reference_sorter.tpp | 0 .../utility/texture_comparator.hpp | 0 source/engine/{graphics => }/utility/type.hpp | 0 source/kernel/Kernel.hpp | 10 - source/kernel/back/engine/Engine.hpp | 18 ++ source/kernel/back/engine/GraphicEngine.cpp | 22 +++ source/kernel/back/engine/GraphicEngine.hpp | 35 ++++ source/kernel/back/engine/PhysicEngine.cpp | 49 +++++ source/kernel/back/engine/PhysicEngine.hpp | 24 +++ source/kernel/back/props/Component.hpp | 13 ++ source/kernel/back/props/Graphical.hpp | 10 + source/kernel/back/props/Physical.hpp | 11 ++ source/kernel/back/props/Props.hpp | 17 ++ source/kernel/front/Kernel.cpp | 32 ++++ source/kernel/front/Kernel.hpp | 31 +++ .../kernel/front/resolver/PhysicResolver.cpp | 20 ++ .../kernel/front/resolver/PhysicResolver.hpp | 12 ++ source/kernel/front/resolver/Resolver.hpp | 11 ++ source/main.cpp | 123 ++---------- source/utility/Identifiable.cpp | 42 ++++ source/utility/Identifiable.hpp | 35 ++++ 62 files changed, 900 insertions(+), 421 deletions(-) delete mode 100644 old2/FrameBufferModule.hpp delete mode 100644 old2/ImageModule.cpp delete mode 100644 old2/ImageModule.hpp delete mode 100644 old2/Quad_Module.cpp delete mode 100644 old2/SpriteModule.cpp delete mode 100644 old2/SpriteModule.hpp create mode 100644 source/engine/physic/back/Position.cpp create mode 100644 source/engine/physic/back/Position.hpp create mode 100644 source/engine/physic/back/SquareBox.cpp create mode 100644 source/engine/physic/back/SquareBox.hpp create mode 100644 source/engine/physic/front/engine/Engine.cpp create mode 100644 source/engine/physic/front/engine/Engine.hpp create mode 100644 source/engine/physic/front/object/Collision.cpp create mode 100644 source/engine/physic/front/object/Collision.hpp create mode 100644 source/engine/physic/front/object/Tangible.cpp create mode 100644 source/engine/physic/front/object/Tangible.hpp create mode 100644 source/engine/physic/front/object/TangibleMovable.cpp create mode 100644 source/engine/physic/front/object/TangibleMovable.hpp create mode 100644 source/engine/physic/front/object/TangibleStatic.cpp create mode 100644 source/engine/physic/front/object/TangibleStatic.hpp rename source/engine/{graphics/front/engine => utility}/Priority.hpp (100%) rename source/engine/{graphics => }/utility/array_generator.hpp (100%) rename source/engine/{graphics => }/utility/overloaded.hpp (100%) rename source/engine/{graphics => }/utility/ref_set.hpp (100%) rename source/engine/{graphics => }/utility/reference_sorter.hpp (100%) rename source/engine/{graphics => }/utility/reference_sorter.tpp (100%) rename source/engine/{graphics => }/utility/texture_comparator.hpp (100%) rename source/engine/{graphics => }/utility/type.hpp (100%) delete mode 100644 source/kernel/Kernel.hpp create mode 100644 source/kernel/back/engine/Engine.hpp create mode 100644 source/kernel/back/engine/GraphicEngine.cpp create mode 100644 source/kernel/back/engine/GraphicEngine.hpp create mode 100644 source/kernel/back/engine/PhysicEngine.cpp create mode 100644 source/kernel/back/engine/PhysicEngine.hpp create mode 100644 source/kernel/back/props/Component.hpp create mode 100644 source/kernel/back/props/Graphical.hpp create mode 100644 source/kernel/back/props/Physical.hpp create mode 100644 source/kernel/back/props/Props.hpp create mode 100644 source/kernel/front/Kernel.cpp create mode 100644 source/kernel/front/Kernel.hpp create mode 100644 source/kernel/front/resolver/PhysicResolver.cpp create mode 100644 source/kernel/front/resolver/PhysicResolver.hpp create mode 100644 source/kernel/front/resolver/Resolver.hpp create mode 100644 source/utility/Identifiable.cpp create mode 100644 source/utility/Identifiable.hpp diff --git a/old2/FrameBufferModule.hpp b/old2/FrameBufferModule.hpp deleted file mode 100644 index cc90570..0000000 --- a/old2/FrameBufferModule.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include <any> - -#include <engine/graphics/back/buffers/VertexArray.hpp> -#include <engine/graphics/back/buffers/VerticeBuffer.hpp> -#include <engine/graphics/back/shaders/Program.hpp> - -#include <engine/graphics/front/object/Image.hpp> -#include <engine/graphics/utility/module.hpp> - -namespace megu { - class FrameBuffer_Module : public Module<std::any> { - public: - FrameBuffer_Module(); - ~FrameBuffer_Module() = default; - - void render(const Window &, const Camera &, std::any &, const TextureArray &) override; - - private: - VertexArray _vao; - VerticeBuffer _vbo; - Program _program; - }; -} \ No newline at end of file diff --git a/old2/ImageModule.cpp b/old2/ImageModule.cpp deleted file mode 100644 index f56a235..0000000 --- a/old2/ImageModule.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "ImageModule.hpp" - -#include <engine/graphics/front/object/Image.hpp> -#include <engine/graphics/utility/array_generator.hpp> - -namespace megu { - ImageModule::ImageModule() - : _vbo(this->_vao, Quads::Layout(), Quads::Vertices().size(), megu::EditMode::STATIC) { - Source vert("assets/shaders/Image-Instanced-Fat.vert", Source::Categorie::VERTEX); - this->_program.attach(vert); - - Source frag("assets/shaders/Texture-Fat.frag", Source::Categorie::FRAGMENT); - this->_program.attach(frag); - - this->_program.link(); - - this->_vbo << Quads::Vertices(); - - vert.release(); - frag.release(); - } - - void ImageModule::render(Image & image) { - std::cout << "Image !" << std::endl; - } -} \ No newline at end of file diff --git a/old2/ImageModule.hpp b/old2/ImageModule.hpp deleted file mode 100644 index 7f7adbc..0000000 --- a/old2/ImageModule.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "QuadGraphicModule.hpp" - -#include <engine/graphics/back/buffers/VertexArray.hpp> -#include <engine/graphics/back/buffers/VerticeBuffer.hpp> -#include <engine/graphics/back/shaders/Program.hpp> - -namespace megu { - class ImageModule : public virtual QuadGraphicModule { - public: - ImageModule(); - ~ImageModule() = default; - - virtual void render(Image &) override; - - private: - VertexArray _vao; - VerticeBuffer _vbo; - Program _program; - }; -} \ No newline at end of file diff --git a/old2/Quad_Module.cpp b/old2/Quad_Module.cpp deleted file mode 100644 index d39aea1..0000000 --- a/old2/Quad_Module.cpp +++ /dev/null @@ -1,180 +0,0 @@ -#include "QuadInstanced.hpp" - -#include <engine/graphics/front/object/Image.hpp> -#include <engine/graphics/front/object/Sprite.hpp> -#include <engine/graphics/front/object/Text.hpp> - -#include <engine/graphics/utility/array_generator.hpp> - -#include <iostream> - -namespace megu { - QuadInstancedModule::QuadInstancedModule() - : _vbo(this->_vao, Quads::Layout(), Quads::Vertices().size(), megu::EditMode::STATIC) { - { - Source vert("assets/shaders/Image-Instanced-Fat.vert", Source::Categorie::VERTEX); - this->_imageProgram.attach(vert); - - Source frag("assets/shaders/Texture-Fat.frag", Source::Categorie::FRAGMENT); - this->_imageProgram.attach(frag); - - this->_imageProgram.link(); - - vert.release(); - frag.release(); - } - - { - Source vert("assets/shaders/Sprite.vert", Source::Categorie::VERTEX); - this->_spriteProgram.attach(vert); - - Source frag("assets/shaders/Sprite.frag", Source::Categorie::FRAGMENT); - this->_spriteProgram.attach(frag); - - this->_spriteProgram.link(); - - vert.release(); - frag.release(); - } - - { - Source vert("assets/shaders/Text.vert", Source::Categorie::VERTEX); - this->_textProgram.attach(vert); - - Source frag("assets/shaders/Text.frag", Source::Categorie::FRAGMENT); - this->_textProgram.attach(frag); - - this->_textProgram.link(); - - vert.release(); - frag.release(); - } - - this->_vbo << Quads::Vertices(); - } - - void QuadInstancedModule::draw(ref_set<Image> & images, const Window & window, const Camera & camera, const TextureArray &) const { - this->_vao.bind(); - this->_imageProgram.use(); - - this->_imageProgram.setUniform("uProj", camera.projection()); - this->_imageProgram.setUniform("uView", camera.view()); - - std::vector<std::reference_wrapper<const Texture>> textures; - - std::vector<glm::mat4> uModels; - std::vector<GLint> uTextures; - - static auto uSampler = array_generator<GLint, 32>().array; - this->_imageProgram.setUniform("uSampler", uSampler); - - for(auto & image : images) { - - std::vector<std::reference_wrapper<const Texture>>::iterator it = std::find(textures.begin(), textures.end(), image.get().texture()); - if(it != textures.end()) { - uModels.push_back(image.get().transformation().model()); - uTextures.push_back(static_cast<GLint>(it - textures.begin())); - } - else { - if(textures.size() >= 8 || uModels.size() >= 124) { - this->_imageProgram.setUniform("uModel", uModels); - this->_imageProgram.setUniform("uTextures", uTextures); - - glDrawArraysInstanced(Quads::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(uModels.size())); - - textures.clear(); - uModels.clear(); - uTextures.clear(); - } - - textures.push_back(image.get().texture()); - image.get().texture().bind(static_cast<GLint>(textures.size()-1)); - uTextures.push_back(static_cast<GLint>(textures.size()-1)); - uModels.push_back(image.get().transformation().model()); - } - } - - if(!textures.empty()) { - this->_imageProgram.setUniform("uModel", uModels); - this->_imageProgram.setUniform("uTextures", uTextures); - - glDrawArraysInstanced(Quads::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(uModels.size())); - } - } - - void QuadInstancedModule::draw(ref_set<Sprite> & sprites, const Window &, const Camera & camera, const TextureArray &) const { - this->_vao.bind(); - this->_spriteProgram.use(); - - this->_spriteProgram.setUniform("uProj", camera.projection()); - this->_spriteProgram.setUniform("uView", camera.view()); - - std::vector<std::reference_wrapper<const Texture>> textures; - - std::vector<glm::mat4> uModels; - std::vector<glm::vec4> uFrames; - std::vector<glm::vec2> uSizes; - std::vector<GLint> uTextures; - - static auto uSampler = array_generator<GLint, 32>().array; - this->_spriteProgram.setUniform("uSampler", uSampler); - - for(auto & image : sprites) { - std::vector<std::reference_wrapper<const Texture>>::iterator it = std::find(textures.begin(), textures.end(), image.get().texture()); - if(it != textures.end()) { - uModels.push_back(image.get().transformation().model()); - uFrames.push_back(image.get().frame()); - uSizes.push_back({it->get().width(), it->get().height()}); - - uTextures.push_back(static_cast<GLint>(it - textures.begin())); - } - else { - if(textures.size() >= 8 || uModels.size() >= 124) { - this->_spriteProgram.setUniform("uModel", uModels); - this->_spriteProgram.setUniform("uTextures", uTextures); - this->_spriteProgram.setUniform("uFrames", uFrames); - this->_spriteProgram.setUniform("uSizes", uSizes); - - glDrawArraysInstanced(Quads::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(uModels.size())); - - textures.clear(); - uModels.clear(); - uTextures.clear(); - uSizes.clear(); - } - - textures.push_back(image.get().texture()); - image.get().texture().bind(static_cast<GLint>(textures.size()-1)); - uTextures.push_back(static_cast<GLint>(textures.size()-1)); - - uModels.push_back(image.get().transformation().model()); - uFrames.push_back(image.get().frame()); - uSizes.push_back({image.get().texture().width(), image.get().texture().height()}); - } - } - - if(!textures.empty()) { - this->_spriteProgram.setUniform("uModel", uModels); - this->_spriteProgram.setUniform("uTextures", uTextures); - this->_spriteProgram.setUniform("uFrames", uFrames); - this->_spriteProgram.setUniform("uSizes", uSizes); - - glDrawArraysInstanced(Quads::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(uModels.size())); - } - } - - void QuadInstancedModule::draw(Text & text, const Window &, const Camera & camera, const TextureArray &) const { - this->_vao.bind(); - this->_textProgram.use(); - - text.texture().bind(); - this->_textProgram.setUniform("uSampler", 0); - - glm::vec4 glyphFrame = text['w']; - - this->_textProgram.setUniform("uSize", glm::vec2{text.texture().width(), text.texture().height()}); - this->_textProgram.setUniform("uGlyphFrame", glyphFrame); - - glDrawArrays(Quads::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size())); - } -} \ No newline at end of file diff --git a/old2/SpriteModule.cpp b/old2/SpriteModule.cpp deleted file mode 100644 index 70651e7..0000000 --- a/old2/SpriteModule.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "SpriteModule.hpp" - -#include <engine/graphics/utility/array_generator.hpp> -#include <engine/graphics/front/object/Sprite.hpp> - -#include <iostream> - -namespace megu { - SpriteModule::SpriteModule() { - - } - - void SpriteModule::render(Sprite & sprite) { - std::cout << "Sprite !" << std::endl; - } -} \ No newline at end of file diff --git a/old2/SpriteModule.hpp b/old2/SpriteModule.hpp deleted file mode 100644 index 383f0c8..0000000 --- a/old2/SpriteModule.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "QuadGraphicModule.hpp" - -#include <engine/graphics/back/buffers/VertexArray.hpp> -#include <engine/graphics/back/buffers/VerticeBuffer.hpp> -#include <engine/graphics/back/shaders/Program.hpp> - -namespace megu { - class SpriteModule : public virtual QuadGraphicModule { - public: - SpriteModule(); - ~SpriteModule() = default; - - virtual void render(Sprite &) override; - }; -} \ No newline at end of file diff --git a/source/engine/graphics/front/engine/Engine.cpp b/source/engine/graphics/front/engine/Engine.cpp index b8765f4..a47862c 100644 --- a/source/engine/graphics/front/engine/Engine.cpp +++ b/source/engine/graphics/front/engine/Engine.cpp @@ -49,6 +49,21 @@ namespace megu { } } + void GraphicEngine::step(Priority priority) { + if(this->_window.isOpen()) { + TextureArray textures; + + auto & layer = this->_layers[priority]; + const glm::vec2 & dimension = layer->renderer().dimension(); + glViewport(0, 0, static_cast<GLsizei>(dimension.x), static_cast<GLsizei>(dimension.y)); + textures.push_back(layer->draw(this->_window, textures)); + + FrameBuffer::BindDefaultFrameBuffer(); + this->_merger.render(this->_window, textures); + this->_window.swapBuffers(); + } + } + void GraphicEngine::setClearColor(float x, float y, float z) { for(auto & [priority, layer] : this->_layers) { layer.get()->renderer().setClearColor(x, y, z); diff --git a/source/engine/graphics/front/engine/Engine.hpp b/source/engine/graphics/front/engine/Engine.hpp index 0a1a427..e634670 100644 --- a/source/engine/graphics/front/engine/Engine.hpp +++ b/source/engine/graphics/front/engine/Engine.hpp @@ -31,6 +31,7 @@ namespace megu { inline const std::map<Priority, std::unique_ptr<Layer>> & layers() const {return this->_layers;} void step(); + void step(Priority); void setClearColor(float, float, float); private: diff --git a/source/engine/graphics/front/engine/FrameBufferMerger.cpp b/source/engine/graphics/front/engine/FrameBufferMerger.cpp index 03b86d1..42a1777 100644 --- a/source/engine/graphics/front/engine/FrameBufferMerger.cpp +++ b/source/engine/graphics/front/engine/FrameBufferMerger.cpp @@ -1,7 +1,7 @@ #include "FrameBufferMerger.hpp" #include <engine/graphics/front/geometry/Plane.hpp> -#include <engine/graphics/utility/array_generator.hpp> +#include <engine/utility/array_generator.hpp> namespace megu { FrameBufferMerger::FrameBufferMerger() diff --git a/source/engine/graphics/front/engine/Layer.hpp b/source/engine/graphics/front/engine/Layer.hpp index fc05c6f..3cefe8d 100644 --- a/source/engine/graphics/front/engine/Layer.hpp +++ b/source/engine/graphics/front/engine/Layer.hpp @@ -7,12 +7,12 @@ #include <engine/graphics/back/buffers/FrameBuffer.hpp> #include <engine/graphics/front/object/Renderable.hpp> -#include <engine/graphics/utility/reference_sorter.hpp> +#include <engine/utility/reference_sorter.hpp> #include <engine/graphics/front/object/Renderable.hpp> #include <engine/graphics/front/module/Module.hpp> -#include "Priority.hpp" +#include <engine/utility/Priority.hpp> #include "Renderer.hpp" namespace megu { diff --git a/source/engine/graphics/front/engine/Renderer.hpp b/source/engine/graphics/front/engine/Renderer.hpp index ea5e844..12e13e8 100644 --- a/source/engine/graphics/front/engine/Renderer.hpp +++ b/source/engine/graphics/front/engine/Renderer.hpp @@ -2,7 +2,7 @@ #include <optional> -#include <engine/graphics/utility/reference_sorter.hpp> +#include <engine/utility/reference_sorter.hpp> #include <engine/graphics/back/buffers/FrameBuffer.hpp> #include <engine/graphics/back/cameras/View.hpp> diff --git a/source/engine/graphics/front/module/Image_Module.cpp b/source/engine/graphics/front/module/Image_Module.cpp index 888f516..9b0b8c5 100644 --- a/source/engine/graphics/front/module/Image_Module.cpp +++ b/source/engine/graphics/front/module/Image_Module.cpp @@ -1,7 +1,7 @@ #include "Image_Module.hpp" #include <engine/graphics/back/cameras/Camera.hpp> -#include <engine/graphics/utility/array_generator.hpp> +#include <engine/utility/array_generator.hpp> namespace megu { Image_Module::Image_Module() diff --git a/source/engine/graphics/front/module/Image_Module.hpp b/source/engine/graphics/front/module/Image_Module.hpp index 6cbea51..e35d1ba 100644 --- a/source/engine/graphics/front/module/Image_Module.hpp +++ b/source/engine/graphics/front/module/Image_Module.hpp @@ -7,7 +7,7 @@ #include <engine/graphics/back/shaders/Program.hpp> #include <engine/graphics/front/object/Image.hpp> -#include <engine/graphics/utility/ref_set.hpp> +#include <engine/utility/ref_set.hpp> namespace megu { class Image_Module : public Module<ref_set<Image>> { diff --git a/source/engine/graphics/front/module/Sprite_Module.cpp b/source/engine/graphics/front/module/Sprite_Module.cpp index 691983f..e95ba8d 100644 --- a/source/engine/graphics/front/module/Sprite_Module.cpp +++ b/source/engine/graphics/front/module/Sprite_Module.cpp @@ -1,7 +1,7 @@ #include "Sprite_Module.hpp" #include <engine/graphics/back/cameras/Camera.hpp> -#include <engine/graphics/utility/array_generator.hpp> +#include <engine/utility/array_generator.hpp> namespace megu { Sprite_Module::Sprite_Module() diff --git a/source/engine/graphics/front/module/Sprite_Module.hpp b/source/engine/graphics/front/module/Sprite_Module.hpp index ebc7f92..a596e28 100644 --- a/source/engine/graphics/front/module/Sprite_Module.hpp +++ b/source/engine/graphics/front/module/Sprite_Module.hpp @@ -7,7 +7,7 @@ #include <engine/graphics/back/shaders/Program.hpp> #include <engine/graphics/front/object/Sprite.hpp> -#include <engine/graphics/utility/ref_set.hpp> +#include <engine/utility/ref_set.hpp> namespace megu { class Sprite_Module : public Module<ref_set<Sprite>> { diff --git a/source/engine/graphics/front/module/Text_Module.cpp b/source/engine/graphics/front/module/Text_Module.cpp index 15b73c3..9ccf73b 100644 --- a/source/engine/graphics/front/module/Text_Module.cpp +++ b/source/engine/graphics/front/module/Text_Module.cpp @@ -1,7 +1,7 @@ #include "Text_Module.hpp" #include <engine/graphics/back/cameras/Camera.hpp> -#include <engine/graphics/utility/array_generator.hpp> +#include <engine/utility/array_generator.hpp> namespace megu { Text_Module::Text_Module() diff --git a/source/engine/graphics/front/module/Text_Module.hpp b/source/engine/graphics/front/module/Text_Module.hpp index 78effb7..71aacd0 100644 --- a/source/engine/graphics/front/module/Text_Module.hpp +++ b/source/engine/graphics/front/module/Text_Module.hpp @@ -7,7 +7,7 @@ #include <engine/graphics/back/shaders/Program.hpp> #include <engine/graphics/front/object/Text.hpp> -#include <engine/graphics/utility/ref_set.hpp> +#include <engine/utility/ref_set.hpp> namespace megu { class Text_Module : public Module<Text> { diff --git a/source/engine/graphics/front/object/Image.cpp b/source/engine/graphics/front/object/Image.cpp index 55563ee..1dc1619 100644 --- a/source/engine/graphics/front/object/Image.cpp +++ b/source/engine/graphics/front/object/Image.cpp @@ -9,7 +9,6 @@ namespace megu { this->link(texture); } - Image Image::operator=(const Image & image) { this->_texture = image._texture; this->_transformation = image._transformation; diff --git a/source/engine/graphics/front/object/Image.hpp b/source/engine/graphics/front/object/Image.hpp index e6abd96..82bad96 100644 --- a/source/engine/graphics/front/object/Image.hpp +++ b/source/engine/graphics/front/object/Image.hpp @@ -1,15 +1,17 @@ #pragma once +#include <utility/Identifiable.hpp> + #include <filesystem> #include <glm/vec2.hpp> #include <engine/graphics/back/geometry/Transformable.hpp> #include <engine/graphics/back/textures/Texture.hpp> #include <engine/graphics/front/geometry/Quads.hpp> -#include <engine/graphics/utility/type.hpp> +#include <engine/utility/type.hpp> namespace megu { - class Image : public Quads { + class Image : public Quads, virtual public Identifiable { public: Image() = default; Image(const std::filesystem::path &); diff --git a/source/engine/graphics/front/object/Sprite.hpp b/source/engine/graphics/front/object/Sprite.hpp index 96b508e..3ddff4e 100644 --- a/source/engine/graphics/front/object/Sprite.hpp +++ b/source/engine/graphics/front/object/Sprite.hpp @@ -1,13 +1,15 @@ #pragma once +#include <utility/Identifiable.hpp> + #include <engine/graphics/back/geometry/Transformable.hpp> #include <engine/graphics/back/textures/Texture.hpp> #include <engine/graphics/front/geometry/Quads.hpp> -#include <engine/graphics/utility/type.hpp> +#include <engine/utility/type.hpp> namespace megu { - class Sprite : public Quads { + class Sprite : public Quads, virtual public Identifiable { public: using Frame = glm::vec4; diff --git a/source/engine/graphics/front/object/Text.hpp b/source/engine/graphics/front/object/Text.hpp index 1e3a070..2b45d63 100644 --- a/source/engine/graphics/front/object/Text.hpp +++ b/source/engine/graphics/front/object/Text.hpp @@ -1,5 +1,7 @@ #pragma once +#include <utility/Identifiable.hpp> + #include <string> #include <filesystem> #include <map> @@ -7,10 +9,10 @@ #include <engine/graphics/back/geometry/Transformable.hpp> #include <engine/graphics/back/textures/Texture.hpp> #include <engine/graphics/front/geometry/Quads.hpp> -#include <engine/graphics/utility/type.hpp> +#include <engine/utility/type.hpp> namespace megu { - class Text : public Quads { + class Text : public Quads, public Identifiable { public: using Frame = Vec4Int; using CharacterMap = std::map<unsigned char, Frame>; diff --git a/source/engine/graphics/front/object/TileArray.hpp b/source/engine/graphics/front/object/TileArray.hpp index f3a8dcb..656f168 100644 --- a/source/engine/graphics/front/object/TileArray.hpp +++ b/source/engine/graphics/front/object/TileArray.hpp @@ -1,15 +1,17 @@ #pragma once +#include <utility/Identifiable.hpp> + #include <array> #include <engine/graphics/back/geometry/Transformable.hpp> #include <engine/graphics/back/textures/Texture.hpp> #include <engine/graphics/front/geometry/Quads.hpp> -#include <engine/graphics/utility/type.hpp> +#include <engine/utility/type.hpp> namespace megu { - class TileArray : public Quads { + class TileArray : public Quads, public Identifiable { public: TileArray(const std::filesystem::path &, size_t, size_t, float); diff --git a/source/engine/physic/back/Position.cpp b/source/engine/physic/back/Position.cpp new file mode 100644 index 0000000..5409292 --- /dev/null +++ b/source/engine/physic/back/Position.cpp @@ -0,0 +1,70 @@ +#include "Position.hpp" + +namespace megu { + Position::Position() + : Position(0.f, 0.f) {} + + Position::Position(float x, float y, float z) + : _position(x, y, z) {} + + Position::Position(const glm::vec3 & vec) + : _position(vec) {} + + Position::Position(const Position & src) + : _position(src._position) {} + + Position Position::operator=(const Position & src) { + this->_position = src._position; + return *this; + } + + void Position::setPosition(const glm::vec3 & position) { + this->_position = position; + } + + void Position::move(const glm::vec3 & direction) { + this->_position.x += direction.x; + this->_position.y += direction.y; + this->_position.z += direction.z; + } + + bool Position::operator==(const Position & position) const { + return this->_position == position._position; + } + + bool Position::operator!=(const Position & position) const { + return !(*this == position); + } + + Position operator+(const Position & pa, const Position & pb) { + return Position(pa.x() + pb.x(), pa.y() + pb.y(), pa.z() + pb.z()); + } + + Position operator-(const Position & pa, const Position & pb) { + return Position(pa.x() - pb.x(), pa.y() - pb.y(), pa.z() - pb.z()); + } + + Position operator*(const Position & pa, const Position & pb) { + return Position(pa.x() * pb.x(), pa.y() * pb.y(), pa.z() * pb.z()); + } + + Position operator/(const Position & pa, const Position & pb) { + return Position(pa.x() / pb.x(), pa.y() / pb.y(), pa.z() / pb.z()); + } + + bool operator<(const Position & pa, const Position & pb) { + return pa.x() < pb.x() && pa.y() < pb.y() && pa.z() < pb.z(); + } + + bool operator>(const Position & pa, const Position & pb) { + return pa.x() > pb.x() && pa.y() > pb.y() && pa.z() > pb.z(); + } + + bool operator<=(const Position & pa, const Position & pb) { + return pa.x() <= pb.x() && pa.y() <= pb.y() && pa.z() <= pb.z(); + } + + bool operator>=(const Position & pa, const Position & pb) { + return pa.x() >= pb.x() && pa.y() >= pb.y() && pa.z() >= pb.z(); + } +} \ No newline at end of file diff --git a/source/engine/physic/back/Position.hpp b/source/engine/physic/back/Position.hpp new file mode 100644 index 0000000..334a968 --- /dev/null +++ b/source/engine/physic/back/Position.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include <glm/glm.hpp> + +namespace megu { + class Position { + public: + Position(); + Position(float, float, float = 0.f); + Position(const glm::vec3 &); + Position(const Position &); + Position operator=(const Position &); + + inline float x() const {return this->_position.x;} + inline float y() const {return this->_position.y;} + inline float z() const {return this->_position.z;} + + inline const glm::vec3 & position() const {return this->_position;} + + void setPosition(const glm::vec3 &); + inline void setPosition(float x, float y, float z) {this->setPosition({x, y, z});} + + void move(const glm::vec3 &); + inline void move(float x, float y, float z) {this->move({x, y, z});} + + bool operator==(const Position &) const; + bool operator!=(const Position &) const; + + friend Position operator+(const Position &, const Position &); + friend Position operator-(const Position &, const Position &); + friend Position operator*(const Position &, const Position &); + friend Position operator/(const Position &, const Position &); + + friend bool operator<(const Position &, const Position &); + friend bool operator>(const Position &, const Position &); + + friend bool operator<=(const Position &, const Position &); + friend bool operator>=(const Position &, const Position &); + + private: + glm::vec3 _position; + }; + + using Direction = Position; +} \ No newline at end of file diff --git a/source/engine/physic/back/SquareBox.cpp b/source/engine/physic/back/SquareBox.cpp new file mode 100644 index 0000000..a8b4575 --- /dev/null +++ b/source/engine/physic/back/SquareBox.cpp @@ -0,0 +1,58 @@ +#include "SquareBox.hpp" + +namespace megu { + SquareBox::SquareBox(const Position & position, const Dimension & dimension) + : _position(position), _dimension(dimension) {} + + SquareBox::SquareBox(const SquareBox & src) + : _position(src._position), _dimension(src._dimension) {} + + SquareBox SquareBox::operator=(const SquareBox & src) { + this->_position = src._position; + this->_dimension = src._dimension; + return *this; + } + + Cube SquareBox::asCube() const { + return { + this->_position, + this->_position + Position(this->_dimension.x, 0.f, 0.f), + this->_position + Position(0.f, 0.f, this->_dimension.z), + this->_position + Position(this->_dimension.x, 0.f, this->_dimension.z), + + this->_position + Position(0.f, this->_dimension.y, 0.f), + this->_position + Position(this->_dimension.x, this->_dimension.y, 0.f), + this->_position + Position(0.f, this->_dimension.y, this->_dimension.z), + this->_position + Position(this->_dimension.x, this->_dimension.y, this->_dimension.z) + }; + } + + void SquareBox::move(const Direction & direction) { + this->_position.move(direction.x(), direction.y(), direction.z()); + } + + bool SquareBox::contain(const Position & position) const { + if(position >= this->_position && position <= (this->_position + Position(this->_dimension))) { + return true; + } + return false; + } + + bool SquareBox::intersect(const SquareBox & SquareBox) const { + const Cube cube = SquareBox.asCube(); + for(const auto & position : cube) { + if(this->contain(position)) { + return true; + } + } + return false; + } + + bool SquareBox::operator==(const SquareBox & squareBox) const { + return this->_position == squareBox._position && this->_dimension == squareBox._dimension; + } + + bool SquareBox::operator!=(const SquareBox & squareBox) const { + return !(*this == squareBox); + } +} \ No newline at end of file diff --git a/source/engine/physic/back/SquareBox.hpp b/source/engine/physic/back/SquareBox.hpp new file mode 100644 index 0000000..aa2e7df --- /dev/null +++ b/source/engine/physic/back/SquareBox.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include <array> + +#include "Position.hpp" + +namespace megu { + using Dimension = glm::vec3; + using Cube = std::array<Position, 8>; + + class SquareBox { + public: + SquareBox() = delete; + SquareBox(const Position &, const Dimension &); + SquareBox(const SquareBox &); + SquareBox operator=(const SquareBox &); + + inline const Position & position() const {return this->_position;} + inline const Dimension & dimension() const {return this->_dimension;} + + inline float width() const {return this->_dimension.x;} + inline float height() const {return this->_dimension.y;} + inline float depth() const {return this->_dimension.z;} + + inline void setPosition(const Position & position) {this->_position = position;} + inline void setDimension(const Dimension & dimension) {this->_dimension = dimension;} + + Cube asCube() const; + + void move(const Direction &); + + bool contain(const Position &) const; + bool intersect(const SquareBox &) const; + + bool operator==(const SquareBox &) const; + bool operator!=(const SquareBox &) const; + + private: + Position _position; + Dimension _dimension; + }; +} \ No newline at end of file diff --git a/source/engine/physic/front/engine/Engine.cpp b/source/engine/physic/front/engine/Engine.cpp new file mode 100644 index 0000000..db989ec --- /dev/null +++ b/source/engine/physic/front/engine/Engine.cpp @@ -0,0 +1,46 @@ +#include "Engine.hpp" + +#include <iostream> + +namespace megu { + PhysicEngine::PhysicEngine() {} + + void PhysicEngine::push(Priority p, TangibleStatic & t) { + this->_statics[p].push_back(t); + if(!this->_dynamic.contains(p)) { + this->_dynamic[p] = {}; + } + } + + void PhysicEngine::push(Priority p, TangibleMovable & t) { + this->_dynamic[p].push_back(t); + if(!this->_statics.contains(p)) { + this->_statics[p] = {}; + } + } + + void PhysicEngine::step() { + this->_collisions.clear(); + for(const auto & [priority, layer] : this->_dynamic) { + this->step(priority); + } + } + + void PhysicEngine::step(Priority priority) { + for(auto & source : this->_dynamic[priority]) { + source.get().update_physic(); + + for(auto & target : this->_statics[priority]) { + if(source.get().isColliding(target)) { + this->_collisions.insert(Collision(source, target)); + } + } + + for(auto & target : this->_dynamic[priority]) { + if(source.get().isColliding(target)) { + this->_collisions.insert(Collision(source, target)); + } + } + } + } +} \ No newline at end of file diff --git a/source/engine/physic/front/engine/Engine.hpp b/source/engine/physic/front/engine/Engine.hpp new file mode 100644 index 0000000..3b7abde --- /dev/null +++ b/source/engine/physic/front/engine/Engine.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include <vector> +#include <map> +#include <set> + +#include <engine/physic/front/object/Tangible.hpp> +#include <engine/physic/front/object/TangibleStatic.hpp> +#include <engine/physic/front/object/TangibleMovable.hpp> +#include <engine/physic/front/object/Collision.hpp> + +#include <engine/utility/Priority.hpp> +#include <engine/utility/reference_sorter.hpp> +#include <engine/utility/ref_set.hpp> + +namespace megu { + class PhysicEngine { + public: + PhysicEngine(); + + void push(Priority, TangibleStatic &); + void push(Priority, TangibleMovable &); + + void step(); + void step(Priority); + + inline void clearCollision() {this->_collisions.clear();} + + const std::set<Collision, reference_sorter<Collision>> & collision() const {return this->_collisions;} + + private: + std::map<Priority, std::vector<std::reference_wrapper<TangibleStatic>>> _statics; + std::map<Priority, std::vector<std::reference_wrapper<TangibleMovable>>> _dynamic; + std::set<Collision, reference_sorter<Collision>> _collisions; + }; +} \ No newline at end of file diff --git a/source/engine/physic/front/object/Collision.cpp b/source/engine/physic/front/object/Collision.cpp new file mode 100644 index 0000000..cf66bc4 --- /dev/null +++ b/source/engine/physic/front/object/Collision.cpp @@ -0,0 +1,27 @@ +#include "Collision.hpp" + +namespace megu { + Collision::Collision(Tangible & source, Tangible & target) + : _source(source), _target(target) {} + + Collision::Collision(const Collision & src) + : _source(src._source), _target(src._target) {} + + Collision Collision::operator=(const Collision & src) { + this->_source = src._source; + this->_target = src._target; + return *this; + } + + bool Collision::operator&(const Tangible & tangible) const { + return this->_source == tangible || this->_target == tangible; + } + + bool Collision::operator==(const Collision & collision) const { + return this->_source == collision._source && this->_target == collision._target; + } + + bool Collision::operator!=(const Collision & collision) const { + return !(*this == collision); + } +} \ No newline at end of file diff --git a/source/engine/physic/front/object/Collision.hpp b/source/engine/physic/front/object/Collision.hpp new file mode 100644 index 0000000..aca5166 --- /dev/null +++ b/source/engine/physic/front/object/Collision.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "Tangible.hpp" + +namespace megu { + class Collision { + public: + Collision() = delete; + Collision(Tangible &, Tangible &); + Collision(const Collision &); + Collision operator=(const Collision &); + + inline const Tangible & source() const {return this->_source;} + inline const Tangible & target() const {return this->_target;} + + bool operator&(const Tangible &) const; + bool operator==(const Collision &) const; + bool operator!=(const Collision &) const; + + private: + Tangible & _source; + Tangible & _target; + }; +} \ No newline at end of file diff --git a/source/engine/physic/front/object/Tangible.cpp b/source/engine/physic/front/object/Tangible.cpp new file mode 100644 index 0000000..f514928 --- /dev/null +++ b/source/engine/physic/front/object/Tangible.cpp @@ -0,0 +1,21 @@ +#include "Tangible.hpp" + +namespace megu { + Tangible::Tangible(const Position & position, const Dimension & dimension) + : _box(position, dimension) {} + + bool Tangible::isColliding(const Tangible & entity) const { + if(this != &entity) { + return this->_box.intersect(entity._box); + } + return false; + } + + bool Tangible::operator==(const Tangible & entity) const { + return this->_box == entity._box; + } + + bool Tangible::operator!=(const Tangible & entity) const { + return !(*this == entity); + } +} \ No newline at end of file diff --git a/source/engine/physic/front/object/Tangible.hpp b/source/engine/physic/front/object/Tangible.hpp new file mode 100644 index 0000000..7717e69 --- /dev/null +++ b/source/engine/physic/front/object/Tangible.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include <utility/Identifiable.hpp> + +#include <engine/physic/back/Position.hpp> +#include <engine/physic/back/SquareBox.hpp> + +namespace megu { + class Tangible : virtual public Identifiable { + public: + Tangible() = delete; + Tangible(const Position &, const Dimension &); + virtual ~Tangible() = default; + + inline const Position & getPosition() const {return this->_box.position();} + inline const SquareBox & getBox() const {return this->_box;} + + inline float x() const {return this->_box.position().x();} + inline float y() const {return this->_box.position().y();} + inline float z() const {return this->_box.position().z();} + + inline void setPosition(const Position & position) {this->_box.setPosition(position);} + inline void move(const Direction & direction) {this->_box.move(direction);} + inline void move(float x, float y, float z = 0.f) {return this->move(Direction(x, y, z));} + + bool isColliding(const Tangible &) const; + + bool operator==(const Tangible &) const; + bool operator!=(const Tangible &) const; + + protected: + virtual void update() = 0; + + private: + SquareBox _box; + }; + +} \ No newline at end of file diff --git a/source/engine/physic/front/object/TangibleMovable.cpp b/source/engine/physic/front/object/TangibleMovable.cpp new file mode 100644 index 0000000..6d1515d --- /dev/null +++ b/source/engine/physic/front/object/TangibleMovable.cpp @@ -0,0 +1,10 @@ +#include "TangibleMovable.hpp" + +namespace megu { + TangibleMovable::TangibleMovable(const Position & position, const Dimension & dimension) + : Tangible(position, dimension) {} + + void TangibleMovable::update() { + this->update_physic(); + } +} \ No newline at end of file diff --git a/source/engine/physic/front/object/TangibleMovable.hpp b/source/engine/physic/front/object/TangibleMovable.hpp new file mode 100644 index 0000000..c0b7f6d --- /dev/null +++ b/source/engine/physic/front/object/TangibleMovable.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "Tangible.hpp" + +namespace megu { + class TangibleMovable : public Tangible { + public: + TangibleMovable() = delete; + TangibleMovable(const Position &, const Dimension &); + + virtual void update_physic() {}; + + protected: + void update() override; + }; +} \ No newline at end of file diff --git a/source/engine/physic/front/object/TangibleStatic.cpp b/source/engine/physic/front/object/TangibleStatic.cpp new file mode 100644 index 0000000..8596eef --- /dev/null +++ b/source/engine/physic/front/object/TangibleStatic.cpp @@ -0,0 +1,10 @@ +#include "TangibleStatic.hpp" + +namespace megu { + TangibleStatic::TangibleStatic(const Position & position, const Dimension & dimension) + : Tangible(position, dimension) {} + + void TangibleStatic::update() { + this->update_physic(); + } +} \ No newline at end of file diff --git a/source/engine/physic/front/object/TangibleStatic.hpp b/source/engine/physic/front/object/TangibleStatic.hpp new file mode 100644 index 0000000..81b24b9 --- /dev/null +++ b/source/engine/physic/front/object/TangibleStatic.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "Tangible.hpp" + +namespace megu { + class TangibleStatic : public Tangible { + public: + TangibleStatic() = delete; + TangibleStatic(const Position &, const Dimension &); + + virtual void update_physic() const {}; + + protected: + void update() override; + }; +} \ No newline at end of file diff --git a/source/engine/graphics/front/engine/Priority.hpp b/source/engine/utility/Priority.hpp similarity index 100% rename from source/engine/graphics/front/engine/Priority.hpp rename to source/engine/utility/Priority.hpp diff --git a/source/engine/graphics/utility/array_generator.hpp b/source/engine/utility/array_generator.hpp similarity index 100% rename from source/engine/graphics/utility/array_generator.hpp rename to source/engine/utility/array_generator.hpp diff --git a/source/engine/graphics/utility/overloaded.hpp b/source/engine/utility/overloaded.hpp similarity index 100% rename from source/engine/graphics/utility/overloaded.hpp rename to source/engine/utility/overloaded.hpp diff --git a/source/engine/graphics/utility/ref_set.hpp b/source/engine/utility/ref_set.hpp similarity index 100% rename from source/engine/graphics/utility/ref_set.hpp rename to source/engine/utility/ref_set.hpp diff --git a/source/engine/graphics/utility/reference_sorter.hpp b/source/engine/utility/reference_sorter.hpp similarity index 100% rename from source/engine/graphics/utility/reference_sorter.hpp rename to source/engine/utility/reference_sorter.hpp diff --git a/source/engine/graphics/utility/reference_sorter.tpp b/source/engine/utility/reference_sorter.tpp similarity index 100% rename from source/engine/graphics/utility/reference_sorter.tpp rename to source/engine/utility/reference_sorter.tpp diff --git a/source/engine/graphics/utility/texture_comparator.hpp b/source/engine/utility/texture_comparator.hpp similarity index 100% rename from source/engine/graphics/utility/texture_comparator.hpp rename to source/engine/utility/texture_comparator.hpp diff --git a/source/engine/graphics/utility/type.hpp b/source/engine/utility/type.hpp similarity index 100% rename from source/engine/graphics/utility/type.hpp rename to source/engine/utility/type.hpp diff --git a/source/kernel/Kernel.hpp b/source/kernel/Kernel.hpp deleted file mode 100644 index d4f26ce..0000000 --- a/source/kernel/Kernel.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -namespace megu { - class Kernel { - public: - virtual void initilize_io() = 0; - virtual void initilize_physics() = 0; - virtual void initilize_graphics() = 0; - }; -} \ No newline at end of file diff --git a/source/kernel/back/engine/Engine.hpp b/source/kernel/back/engine/Engine.hpp new file mode 100644 index 0000000..6a8111d --- /dev/null +++ b/source/kernel/back/engine/Engine.hpp @@ -0,0 +1,18 @@ +#pragma once + +namespace megu::kernel { + class Props; + class Kernel; + + template <class T, class C> + class Engine { + public: + virtual void boot(Kernel &) = 0; + virtual void stop(Kernel &) = 0; + virtual void step(Kernel &, double) = 0; + + virtual T & get() = 0; + + virtual void add(Kernel &, C &) = 0; + }; +} \ No newline at end of file diff --git a/source/kernel/back/engine/GraphicEngine.cpp b/source/kernel/back/engine/GraphicEngine.cpp new file mode 100644 index 0000000..a2c490a --- /dev/null +++ b/source/kernel/back/engine/GraphicEngine.cpp @@ -0,0 +1,22 @@ +#include "GraphicEngine.hpp" + +namespace megu::kernel { + GraphicEngine::GraphicEngine(Window & window) + : _engine(window), _renderer(360, 360) {} + + void GraphicEngine::boot(Kernel &) { + + } + + void GraphicEngine::stop(Kernel &) { + + } + + void GraphicEngine::step(Kernel &, double) { + this->_engine.step(); + } + + void GraphicEngine::add(Kernel & kernel, Graphical<GraphicEngine> & graphical) { + graphical.apply(kernel, *this); + } +} \ No newline at end of file diff --git a/source/kernel/back/engine/GraphicEngine.hpp b/source/kernel/back/engine/GraphicEngine.hpp new file mode 100644 index 0000000..4dee18f --- /dev/null +++ b/source/kernel/back/engine/GraphicEngine.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include "Engine.hpp" + +#include <engine/io/Window.hpp> + +#include <engine/graphics/front/engine/Engine.hpp> +#include <engine/graphics/front/engine/Renderer.hpp> +#include <engine/graphics/front/module/Quad_Module.hpp> + +#include <kernel/back/props/Graphical.hpp> + +namespace megu::kernel { + class GraphicEngine : public Engine<megu::GraphicEngine, Graphical<GraphicEngine>> { + public: + GraphicEngine(Window &); + + void boot(Kernel &) override; + void stop(Kernel &) override; + void step(Kernel &, double) override; + + void add(Kernel &, Graphical<GraphicEngine> &) override; + + inline megu::GraphicEngine & get() {return this->_engine;} + + inline const megu::GraphicEngine & engine() const {return this->_engine;} + inline const megu::Renderer & renderer() const {return this->_renderer;} + inline const megu::Quad_Module & module() const {return this->_module;} + + private: + megu::GraphicEngine _engine; + megu::Renderer _renderer; + megu::Quad_Module _module; + }; +} \ No newline at end of file diff --git a/source/kernel/back/engine/PhysicEngine.cpp b/source/kernel/back/engine/PhysicEngine.cpp new file mode 100644 index 0000000..b259f9d --- /dev/null +++ b/source/kernel/back/engine/PhysicEngine.cpp @@ -0,0 +1,49 @@ +#include "PhysicEngine.hpp" + +#include <iostream> + +namespace megu::kernel { + PhysicEngine::PhysicEngine() + : _engine() {} + + void PhysicEngine::boot(Kernel &) { + try { + megu::TangibleMovable a(megu::Position{0.f, 0.f}, megu::Dimension{2.f, 1.f, 0.f}); + a.setId(0); + + megu::TangibleMovable b(megu::Position{10.f, 0.0f}, megu::Dimension{1.f, 1.f, 0.f}); + b.setId(1); + + megu::PhysicEngine engine; + + engine.push(0, a); + engine.push(0, b); + + for(size_t i = 0; i < 8; ++i) { + a.move(1.f, 0.f); + engine.step(); + } + + auto collisions = engine.collision(); + + for(const auto & collision : collisions) { + std::cout << "Collision between " << collision.source().id() << " and " << collision.target().id() << std::endl; + } + } + catch(std::exception & error) { + std::cerr << error.what() << std::endl; + } + } + + void PhysicEngine::stop(Kernel &) { + + } + + void PhysicEngine::step(Kernel &, double time) { + this->_engine.step(); + } + + void PhysicEngine::add(Kernel & kernel, Physical<PhysicEngine> & props) { + props.apply(kernel, *this); + } +} \ No newline at end of file diff --git a/source/kernel/back/engine/PhysicEngine.hpp b/source/kernel/back/engine/PhysicEngine.hpp new file mode 100644 index 0000000..d1c100f --- /dev/null +++ b/source/kernel/back/engine/PhysicEngine.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "Engine.hpp" + +#include <engine/physic/front/engine/Engine.hpp> +#include <kernel/back/props/Physical.hpp> + +namespace megu::kernel { + class PhysicEngine : public Engine<megu::PhysicEngine, Physical<PhysicEngine>> { + public: + PhysicEngine(); + + void boot(Kernel &) override; + void stop(Kernel &) override; + void step(Kernel &, double) override; + + void add(Kernel &, Physical<PhysicEngine> &) override; + + inline megu::PhysicEngine & get() override {return this->_engine;} + + private: + megu::PhysicEngine _engine; + }; +} \ No newline at end of file diff --git a/source/kernel/back/props/Component.hpp b/source/kernel/back/props/Component.hpp new file mode 100644 index 0000000..229e22b --- /dev/null +++ b/source/kernel/back/props/Component.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include <kernel/back/engine/Engine.hpp> + +namespace megu::kernel { + class Kernel; + + template <class E> + class Component { + public: + virtual void apply(Kernel & k, E &) = 0; + }; +} \ No newline at end of file diff --git a/source/kernel/back/props/Graphical.hpp b/source/kernel/back/props/Graphical.hpp new file mode 100644 index 0000000..ab56c64 --- /dev/null +++ b/source/kernel/back/props/Graphical.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include "Component.hpp" + +namespace megu::kernel { + template <class Ge> + class Graphical : public Component<Ge> { + + }; +} \ No newline at end of file diff --git a/source/kernel/back/props/Physical.hpp b/source/kernel/back/props/Physical.hpp new file mode 100644 index 0000000..dcc0b44 --- /dev/null +++ b/source/kernel/back/props/Physical.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include "Component.hpp" + +namespace megu::kernel { + template <class Pe> + class Physical : public Component<Pe> { + public: + virtual void onCollide(double, const Identifiable &, Physical &) = 0; + }; +} \ No newline at end of file diff --git a/source/kernel/back/props/Props.hpp b/source/kernel/back/props/Props.hpp new file mode 100644 index 0000000..a3a8cab --- /dev/null +++ b/source/kernel/back/props/Props.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include <kernel/back/engine/GraphicEngine.hpp> +#include <kernel/back/engine/PhysicEngine.hpp> +#include <utility/Identifiable.hpp> + +#include "Physical.hpp" +#include "Graphical.hpp" + +namespace megu::kernel { + class Props : public Identifiable { + public: + virtual Physical<PhysicEngine> & getPhysicComponent() const = 0; + virtual Graphical<GraphicEngine> & getGraphicComponent() const = 0; + + }; +} \ No newline at end of file diff --git a/source/kernel/front/Kernel.cpp b/source/kernel/front/Kernel.cpp new file mode 100644 index 0000000..de6b7e0 --- /dev/null +++ b/source/kernel/front/Kernel.cpp @@ -0,0 +1,32 @@ +#include "Kernel.hpp" + +namespace megu::kernel { + Kernel::Kernel(Window & window) + : _window(window), _pEngine(), _gEngine(window) { + this->_pEngine.boot(*this); + this->_gEngine.boot(*this); + } + + Kernel::~Kernel() { + this->_pEngine.stop(*this); + this->_pEngine.stop(*this); + } + + void Kernel::step() { + double delta = Window::Time(); + + if(this->_window.isOpen()) { + this->_gEngine.step(*this, delta); + } + + this->_pEngine.step(*this, delta); + this->_resolver.resolve(delta, this->_pEngine, this->_props); + } + + void Kernel::add(Props * props) { + this->_props[props->id()] = props; + + this->_pEngine.add(*this, props->getPhysicComponent()); + this->_gEngine.add(*this, props->getGraphicComponent()); + } +} \ No newline at end of file diff --git a/source/kernel/front/Kernel.hpp b/source/kernel/front/Kernel.hpp new file mode 100644 index 0000000..b3169be --- /dev/null +++ b/source/kernel/front/Kernel.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include <map> + +#include <engine/io/Window.hpp> + +#include <kernel/back/engine/PhysicEngine.hpp> +#include <kernel/back/engine/GraphicEngine.hpp> +#include <kernel/back/props/Props.hpp> + +#include "resolver/PhysicResolver.hpp" + +namespace megu::kernel { + class Kernel { + public: + Kernel(Window &); + ~Kernel(); + + void step(); + void add(Props *); + + private: + Window & _window; + + PhysicEngine _pEngine; + GraphicEngine _gEngine; + + Identifiable_Map<Props> _props; + PhysicResolver _resolver; + }; +} \ No newline at end of file diff --git a/source/kernel/front/resolver/PhysicResolver.cpp b/source/kernel/front/resolver/PhysicResolver.cpp new file mode 100644 index 0000000..0fb0775 --- /dev/null +++ b/source/kernel/front/resolver/PhysicResolver.cpp @@ -0,0 +1,20 @@ +#include "PhysicResolver.hpp" + +namespace megu::kernel { + void PhysicResolver::resolve(double time, PhysicEngine & engine, const Identifiable_Map<Props> & props) { + auto & collisions = engine.get().collision(); + + for(auto & collision : collisions) { + const Tangible & source = collision.source(); + const Tangible & target = collision.target(); + + if(props.contains(source.id()) && props.contains(target.id())) { + Props * props_source = props.at(source.id()); + Props * props_target = props.at(target.id()); + props_source->getPhysicComponent().onCollide(time, target, props_target->getPhysicComponent()); + } + } + + engine.get().clearCollision(); + } +} \ No newline at end of file diff --git a/source/kernel/front/resolver/PhysicResolver.hpp b/source/kernel/front/resolver/PhysicResolver.hpp new file mode 100644 index 0000000..0cd194d --- /dev/null +++ b/source/kernel/front/resolver/PhysicResolver.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "Resolver.hpp" +#include <kernel/back/engine/PhysicEngine.hpp> +#include <kernel/back/props/Props.hpp> + +namespace megu::kernel { + class PhysicResolver : public Resolver<PhysicEngine, Props> { + public: + void resolve(double, PhysicEngine &, const Identifiable_Map<Props> &) override; + }; +} \ No newline at end of file diff --git a/source/kernel/front/resolver/Resolver.hpp b/source/kernel/front/resolver/Resolver.hpp new file mode 100644 index 0000000..d7484af --- /dev/null +++ b/source/kernel/front/resolver/Resolver.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include <utility/Identifiable.hpp> + +namespace megu::kernel { + template <class E, class O> + class Resolver { + public: + virtual void resolve(double, E &, const Identifiable_Map<O> &) = 0; + }; +} \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index ac230b0..789c084 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,117 +1,29 @@ #include <iostream> -#include <thread> -#include <GL/glew.h> -#include <GLFW/glfw3.h> +#include <engine/physic/front/object/TangibleMovable.hpp> +#include <engine/physic/front/object/TangibleStatic.hpp> +#include <engine/physic/front/object/Collision.hpp> +#include <engine/physic/front/engine/Engine.hpp> -//#include <imgui.h> -//#include <imgui_impl_glfw.h> -//#include <imgui_impl_opengl3.h> - -#define WINDOW_WIDTH 1280 +#define WINDOW_WIDTH 720 #define WINDOW_HEIGHT 720 -#include <engine/io/Window.hpp> -#include <engine/graphics/back/cameras/View.hpp> -#include <engine/graphics/front/object/Image.hpp> -#include <engine/graphics/front/object/Sprite.hpp> -#include <engine/graphics/front/object/Text.hpp> -#include <engine/graphics/front/object/TileArray.hpp> -#include <engine/graphics/front/module/Quad_Module.hpp> -#include <engine/graphics/front/engine/Renderer.hpp> -#include <engine/graphics/front/engine/Engine.hpp> -#include <engine/graphics/errors.hpp> - -const float i_x = 1.f; -const float i_y = 0.5f; -const float j_x = -1.f; -const float j_y = 0.5f; - -megu::Vec2 to_screen_coordinate(const megu::Vec2 & tile, float w, float h, float layer = 0.0f) { - return { - (tile.x + layer) * i_x * 0.5f * w + (tile.y + layer) * j_x * 0.5f * w, - (tile.x + layer) * i_y * 0.5f * h + (tile.y + layer) * j_y * 0.5f * h - }; -} - -/* -megu::Vertex to_screen_coordinate(int x, int y, unsigned int u, unsigned int v, const megu::Texture & texture) { - return {{x, y}, {static_cast<float>(u) / static_cast<float>(texture.width()), static_cast<float>(v) / static_cast<float>(texture.height())}}; -} -*/ +#include <kernel/front/Kernel.hpp> int main(int argc, const char * argv[]) { + std::cout << "Program Init" << std::endl; try { megu::Window window; - window.open("Isometric Window", 360, 360); - std::cout << "Window Inited" << std::endl; - - megu::Image image("assets/textures/Image_Test.png"); - - megu::Sprite sprite("assets/textures/Neera.png"); - sprite.setSize({25, 49}); - - std::vector<glm::vec4> frames; - frames.push_back({0, 0, 51, 98}); - frames.push_back({51, 0, 51, 98}); - frames.push_back({102, 0, 51, 98}); - - frames.push_back({0, 98, 51, 98}); - frames.push_back({51, 98, 51, 98}); - frames.push_back({102, 98, 51, 98}); - - sprite.setFrame(frames[0]); - sprite.move({32, 0}); - - megu::Text text("Hello World !"); - text.loadFont("assets/textures/letters.png"); - - char chars[36] = { - ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; - - float x = 0; - for(auto c : chars) { - text.addGlyph(c, {x, 0, 9, 11}); - x += 9; - } + window.open("Kernel Test", WINDOW_WIDTH, WINDOW_HEIGHT); + std::cout << "Window Init" << std::endl; - std::cout << "Using " << text.characters().size() << " characters !" << std::endl; - - text.scale({200, 200}); - - megu::TileArray tiles("assets/textures/Tile_Test_4.png", 3, 3, 32.f); - - megu::GraphicEngine engine(window); - megu::Renderer basic_renderer(128, 128); - - megu::ref_set<megu::Image> images_set; - images_set.insert(image); - - megu::ref_set<megu::Sprite> sprites_set; - sprites_set.insert(sprite); - - megu::Quad_Module modul; - - engine.push(0, basic_renderer); - engine.push(0, 1, images_set, &modul); - engine.push(0, 3, sprites_set, &modul); - engine.push(0, 2, text, &modul); - engine.push(0, 0, tiles, &modul); + megu::kernel::Kernel kernel(window); + std::cout << "Kernel Init" << std::endl; double previousTime = megu::Window::Time(); int frameCount = 0; - - std::thread t([&frames, &sprite, &window](){ - size_t i = 0; - while(window.isOpen()) { - std::this_thread::sleep_for(std::chrono::milliseconds(30)); - sprite.setFrame(frames[i%frames.size()]); - ++i; - } - }); - - std::cout << "Render Loop Start !" << std::endl; + + std::cout << "Render Loop Init" << std::endl; while(window.isOpen()) { double currentTime = megu::Window::Time(); frameCount++; @@ -124,15 +36,14 @@ int main(int argc, const char * argv[]) { } window.pollEvents(); - engine.step(); + kernel.step(); } - - t.join(); - std::cout << "Render Loop End !" << std::endl; + std::cout << "Render Loop End" << std::endl; } catch(std::exception & error) { std::cerr << error.what() << std::endl; } + std::cout << "Program End" << std::endl; return EXIT_SUCCESS; -} +} \ No newline at end of file diff --git a/source/utility/Identifiable.cpp b/source/utility/Identifiable.cpp new file mode 100644 index 0000000..206ab70 --- /dev/null +++ b/source/utility/Identifiable.cpp @@ -0,0 +1,42 @@ +#include "Identifiable.hpp" + +namespace megu { + size_t Identifiable::_GlobalId = 0; + + Identifiable::Identifiable() + : _id(Identifiable::_GlobalId++) {} + + Identifiable::Identifiable(size_t id) + : _id(id) {} + + Identifiable::Identifiable(const Identifiable & src) + : _id(src._id) {} + + Identifiable::~Identifiable() { + --Identifiable::_GlobalId; + } + + bool Identifiable::operator==(const Identifiable & identifiable) const { + return this->_id == identifiable._id; + } + + bool Identifiable::operator!=(const Identifiable & identifiable) const { + return !(*this == identifiable); + } + + bool Identifiable::operator<=(const Identifiable & identifiable) const { + return this->_id <= identifiable._id; + } + + bool Identifiable::operator>=(const Identifiable & identifiable) const { + return this->_id <= identifiable._id; + } + + bool Identifiable::operator<(const Identifiable & identifiable) const { + return !(this->_id >= identifiable._id); + } + + bool Identifiable::operator>(const Identifiable & identifiable) const { + return !(this->_id <= identifiable._id); + } +} \ No newline at end of file diff --git a/source/utility/Identifiable.hpp b/source/utility/Identifiable.hpp new file mode 100644 index 0000000..e29ee8e --- /dev/null +++ b/source/utility/Identifiable.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include <map> +#include <stddef.h> + +namespace megu { + class Identifiable { + public: + Identifiable(); + Identifiable(size_t); + Identifiable(const Identifiable &); + virtual ~Identifiable(); + + inline size_t id() const {return this->_id;} + inline void setId(size_t id) {this->_id = id;} + + bool operator==(const Identifiable &) const; + bool operator!=(const Identifiable &) const; + bool operator>=(const Identifiable &) const; + bool operator<=(const Identifiable &) const; + + bool operator>(const Identifiable &) const; + bool operator<(const Identifiable &) const; + + private: + size_t _id; + static size_t _GlobalId; + }; + + template <class T> + using Identifiable_Map = std::map<size_t, T *>; + + template <class T> + class Identifiable_Unique : public Identifiable {}; +} \ No newline at end of file -- GitLab