diff --git a/old2/FrameBufferModule.hpp b/old2/FrameBufferModule.hpp deleted file mode 100644 index cc90570ad2107ef02bad1441c49d0d3e8c53540e..0000000000000000000000000000000000000000 --- 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 f56a235cd0a48b19e32184936a9d560398a9b6a3..0000000000000000000000000000000000000000 --- 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 7f7adbc2b7c15d401149bfaa45eb3cda818806e1..0000000000000000000000000000000000000000 --- 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 d39aea104e12e1c71fd52be4082ae6f40924137e..0000000000000000000000000000000000000000 --- 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 70651e7d88715e438b6dcc19816dbc7d0c737c72..0000000000000000000000000000000000000000 --- 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 383f0c8c5bb63a4db2b1f65bd9812be3774b54e3..0000000000000000000000000000000000000000 --- 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 b8765f4e1bcbfe686197f94486d0a106fb989187..a47862c12a3480ea7938e8d605986230b9dd1f61 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 0a1a4272a7682aeb5f42a22d7d2556c17b176f01..e63467055f30d20c72f48fc26f0e0a6063420d08 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 03b86d1e24b088d48e4c51ea993404aee40c2a16..42a1777e769b1b8a222a881263cbdbb8d7d43b29 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 fc05c6fc60c4759677740f42383ab92db8c56a47..3cefe8deea66bc011a08f2e5119a0a719a1725c0 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 ea5e8442ef7d515225de84f68e5e004068de918e..12e13e8b26e7fce13be067feafc09863925f34b1 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 888f516095a81cbe512593af5d80258606e075b5..9b0b8c59f3c05674543b52244beb14dcb5699945 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 6cbea51e919e5be9f17e55a12b4f8ebb3b5e20e2..e35d1ba01306c81e0c651e96d5c625d4e1991ac6 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 691983f0b5357b7e93309ecbefd061640bc57827..e95ba8dfa0b7ff9f8d4094b05f64b9858f2d4192 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 ebc7f92ea3e3f62994b85e1be0b126e2b5699321..a596e28b1cd6b67084759fa1653053031bd08b06 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 15b73c309f3219bd9a820fd2a82b58217c0fd112..9ccf73b216478cdd95abe1425137c132982bb324 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 78effb7ad89fb0b17d80c44da3c7b258649cdbd5..71aacd0a5da7815074d804708b8dade68a6a0504 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 55563ee1a1ca367c2420dfdcafe511e0e2041910..1dc161947f2703d95a7ca710ad2624d999d2bb4c 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 e6abd9670c624d99d150ed5e9b0916f201757c22..82bad96380619d4dfbe4c4c913016a1c7d8e7989 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 96b508ef605877e9fb13cb22df0065096eeb3cd2..3ddff4eb017f4e3f1073a4ccf753a44afaef0a86 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 1e3a070a7dae5ae314c2e6a2a0b60aa17fda6b19..2b45d6325d586073c0ba0c06c058475b5d8a4ee1 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 f3a8dcb11318cd4d92ad018d517335c70ce77c95..656f168a89c897e679cc2ed53754bda7e12287e6 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 0000000000000000000000000000000000000000..540929210aa9e7731cdbf0f46bc81f71691cf999 --- /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 0000000000000000000000000000000000000000..334a9681cd2480bb6e14ff8f21689636365f9d73 --- /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 0000000000000000000000000000000000000000..a8b45757ce6210507cc6e700c8dc8cdf4f520b1a --- /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 0000000000000000000000000000000000000000..aa2e7dfcabf023c96e175cf7e12a5e9da589e076 --- /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 0000000000000000000000000000000000000000..db989eca302e8ebcb1684f2495c790293ac9bbe2 --- /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 0000000000000000000000000000000000000000..3b7abdea6973e85b96e268a157f3def3e1a9a20b --- /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 0000000000000000000000000000000000000000..cf66bc4f5c2c8fccd18af38b693f5ac9c06bddd6 --- /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 0000000000000000000000000000000000000000..aca51668a2ad30c9249d7bd317ae70d655c7c3af --- /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 0000000000000000000000000000000000000000..f514928425089c84e6ab4f29259f7b3c9afe5b08 --- /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 0000000000000000000000000000000000000000..7717e6947151c2bb11d2d3eb27d93cd8099e9d95 --- /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 0000000000000000000000000000000000000000..6d1515d6f98f700efffe6bd7ad151b4add54ad5a --- /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 0000000000000000000000000000000000000000..c0b7f6d972851d60b7fe7015facc980cc6f6918f --- /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 0000000000000000000000000000000000000000..8596eeffbb93e4ed9907d868a0ffc8a91ce49cc6 --- /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 0000000000000000000000000000000000000000..81b24b96f0990b0b36f6f7d8299fcf36b72511e2 --- /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 d4f26ce7f084c473c4f429388199a09507afb4f9..0000000000000000000000000000000000000000 --- 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 0000000000000000000000000000000000000000..6a8111da165ae67641792c5f2dea5ba548ff0f88 --- /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 0000000000000000000000000000000000000000..a2c490a1930e1223ddbb329ee2b4c8f5817876d7 --- /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 0000000000000000000000000000000000000000..4dee18fc12bb76174f31dce5cffe330c87d0a551 --- /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 0000000000000000000000000000000000000000..b259f9d77397fbd675bb9ab133f43161fb28a42c --- /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 0000000000000000000000000000000000000000..d1c100f92d4360b6bacdc8352c70c2b1bce737e0 --- /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 0000000000000000000000000000000000000000..229e22b780900b4b35f8f81dc64907443ae47eff --- /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 0000000000000000000000000000000000000000..ab56c64312e72a9ee7712b159354a37360d136ca --- /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 0000000000000000000000000000000000000000..dcc0b44c0c99cac59714570180484013dd371125 --- /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 0000000000000000000000000000000000000000..a3a8cab6f7e53afb287cc4e707273cd284b60952 --- /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 0000000000000000000000000000000000000000..de6b7e0ea6abc9d6b6ebb648b5b93475b7a2495e --- /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 0000000000000000000000000000000000000000..b3169be3a837cc750fb1c3a05edee33b2f5ee1a2 --- /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 0000000000000000000000000000000000000000..0fb0775c6ca369e204aa5fd5fc15c0ea79bae614 --- /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 0000000000000000000000000000000000000000..0cd194d649ec522bdfd2f63e09c606aac35a32fa --- /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 0000000000000000000000000000000000000000..d7484afdda030c0de6ca3646b87d6b296ed3a980 --- /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 ac230b0e39f59eed22de3ba680a14ac9010edf4c..789c0849f6358175bd19736f779577f1e127c470 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 0000000000000000000000000000000000000000..206ab70bbc245d12ed2168003297a878faabc131 --- /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 0000000000000000000000000000000000000000..e29ee8e1e3c5d76876223179d9f9fb99f965867e --- /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