Skip to content
Snippets Groups Projects
Commit c8b5b197 authored by BATON Theau's avatar BATON Theau
Browse files

Add kernel level tilemap

parent c15586cd
No related branches found
No related tags found
No related merge requests found
Showing
with 140 additions and 44 deletions
assets/tilemap.png

4.43 KiB

...@@ -34,10 +34,10 @@ namespace megu { ...@@ -34,10 +34,10 @@ namespace megu {
size_t count = 0; size_t count = 0;
for(size_t y = 0; y < vertexArray.height(); ++y) {
for(size_t x = 0; x < vertexArray.width(); ++x) { for(size_t x = 0; x < vertexArray.width(); ++x) {
for(size_t y = 0; y < vertexArray.height(); ++y) {
uOffsets.push_back(glm::translate(glm::mat4(1.f), {x, y, 0.f})); uOffsets.push_back(glm::translate(glm::mat4(1.f), {x, y, 0.f}));
uUvs.push_back(vertexArray.uvs()[y][x]); uUvs.push_back(vertexArray.uvs()[x][y]);
if(count > 128) { if(count > 128) {
this->_program.setUniform("uOffsets", uOffsets); this->_program.setUniform("uOffsets", uOffsets);
......
...@@ -57,7 +57,7 @@ namespace megu { ...@@ -57,7 +57,7 @@ namespace megu {
return typeid(T); return typeid(T);
} }
T _objects; T & _objects;
Module<T> * _module; Module<T> * _module;
}; };
......
#include "Sprite.hpp" #include "Sprite.hpp"
namespace megu { namespace megu {
Sprite::Sprite(const std::filesystem::path & path) Sprite::Sprite(const std::filesystem::path & path, bool flip)
: _frame(0, 0, 0, 0) { : _frame(0, 0, 0, 0) {
this->load(path, true); this->load(path, flip);
}
Sprite::Sprite(const std::filesystem::path & path, const Frame & frame, bool flip)
: _frame(frame) {
this->load(path, flip);
} }
void Sprite::load(const std::filesystem::path & path, bool flip) { void Sprite::load(const std::filesystem::path & path, bool flip) {
......
...@@ -13,7 +13,8 @@ namespace megu { ...@@ -13,7 +13,8 @@ namespace megu {
public: public:
using Frame = glm::vec4; using Frame = glm::vec4;
Sprite(const std::filesystem::path &); Sprite(const std::filesystem::path &, bool = true);
Sprite(const std::filesystem::path &, const Frame &, bool = true);
inline void setFrame(const Frame & frame) {this->_frame = frame;} inline void setFrame(const Frame & frame) {this->_frame = frame;}
...@@ -21,7 +22,15 @@ namespace megu { ...@@ -21,7 +22,15 @@ namespace megu {
inline void setOrigine(const Vec2 & pos) {this->_transformation.setOrigine(pos.x, pos.y);} inline void setOrigine(const Vec2 & pos) {this->_transformation.setOrigine(pos.x, pos.y);}
inline void setSize(const Vec2 & size) {this->_transformation.setScaling(size.x, size.y);} inline void setSize(const Vec2 & size) {this->_transformation.setScaling(size.x, size.y);}
inline void setRotation(float a) {this->_transformation.setRotation(a, Transformable::Axis::Z);} inline void setRotation(float a) {this->_transformation.setRotation(a, Transformable::Axis::Z);}
inline void setLayer(float l) {this->_transformation.setZ(l);}
inline const Vec3 & position() const {return this->_transformation.position();}
inline const Vec3 & origine() const {return this->_transformation.origine();}
inline const Vec3 & rotation() const {return this->_transformation.rotation();}
inline const Vec2 & size() const {return Vec2(this->_transformation.scaling().x, this->_transformation.scaling().y);}
inline float x() const {return this->_transformation.x();}
inline float y() const {return this->_transformation.y();}
inline float z() const {return this->_transformation.z();}
inline void move(const Vec2 & pos) {this->_transformation.move({pos.x, pos.y, 0.f});} inline void move(const Vec2 & pos) {this->_transformation.move({pos.x, pos.y, 0.f});}
inline void scale(const Vec2 & size) {this->_transformation.scale({size.x, size.y, 0.f});} inline void scale(const Vec2 & size) {this->_transformation.scale({size.x, size.y, 0.f});}
......
...@@ -11,17 +11,12 @@ namespace megu { ...@@ -11,17 +11,12 @@ namespace megu {
float twidth = static_cast<float>(this->_texture.width()); float twidth = static_cast<float>(this->_texture.width());
float theight = static_cast<float>(this->_texture.height()); float theight = static_cast<float>(this->_texture.height());
std::cout << twidth << "/" << theight << std::endl;
for(size_t y = 0; y < height; ++y) {
std::vector<glm::vec4> rows;
for(size_t x = 0; x < width; ++x) { for(size_t x = 0; x < width; ++x) {
std::vector<glm::vec4> rows;
for(size_t y = 0; y < height; ++y) {
rows.push_back({0.f, 0.f, twidth, theight}); rows.push_back({0.f, 0.f, twidth, theight});
} }
this->_uvs.push_back(rows); this->_uvs.push_back(rows);
} }
std::cout << this->_uvs.size() << std::endl;
this->_transformation.scale({size, size, 1.f});
} }
} }
\ No newline at end of file
...@@ -29,10 +29,13 @@ namespace megu { ...@@ -29,10 +29,13 @@ namespace megu {
inline size_t height() const {return this->_height;} inline size_t height() const {return this->_height;}
inline float size() const {return this->_size;} inline float size() const {return this->_size;}
inline void setUv(size_t x, size_t y, const glm::vec4 & uv) {this->_uvs[y][x] = uv;}
inline const Transformable & transformation() const {return this->_transformation;} inline const Transformable & transformation() const {return this->_transformation;}
inline const Texture & texture() const {return this->_texture;} inline const Texture & texture() const {return this->_texture;}
inline const std::vector<std::vector<glm::vec4>> & uvs() const {return this->_uvs;} inline const std::vector<std::vector<glm::vec4>> & uvs() const {return this->_uvs;}
std::vector<glm::vec4> & operator[](size_t index) {return this->_uvs[index];}
private: private:
Transformable _transformation; Transformable _transformation;
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
namespace megu { namespace megu {
using Vec2 = glm::vec2; using Vec2 = glm::vec2;
using Vec3 = glm::vec4; using Vec3 = glm::vec3;
using Vec4 = glm::vec4; using Vec4 = glm::vec4;
using Color = glm::vec4; using Color = glm::vec4;
......
...@@ -26,9 +26,9 @@ namespace megu::game { ...@@ -26,9 +26,9 @@ namespace megu::game {
megu::game::Object object(path); megu::game::Object object(path);
kernel.add(&object); kernel.add(&object);
megu::game::Object object2(path); /*megu::game::Object object2(path);
object2.tmp_setPos(20, 0); object2.tmp_setPos(20, 0);
kernel.add(&object2); kernel.add(&object2);*/
/* ------ */ /* ------ */
while(window.isOpen()) { while(window.isOpen()) {
......
...@@ -2,5 +2,13 @@ ...@@ -2,5 +2,13 @@
namespace megu::game { namespace megu::game {
Object::Object(std::filesystem::path & path) Object::Object(std::filesystem::path & path)
: _physic(0, 0, 64, 64), _graphic(path) {} : _physic(0, 0, 64, 64), _graphic(path), _map("assets/tilemap.png", 2, 2, 16.f, 16) {
this->_graphic.setFrame({0.f, 0.f, 51.f, 98.f});
this->_graphic.setSize({51.f, 98.f});
this->_map.setValue(0, 0, 1);
this->_map.setValue(1, 0, 1);
this->_map.setValue(0, 1, 2);
this->_map.setValue(1, 1, 1);
}
} }
\ No newline at end of file
...@@ -3,21 +3,19 @@ ...@@ -3,21 +3,19 @@
#include <kernel/back/props/Props.hpp> #include <kernel/back/props/Props.hpp>
#include <kernel/front/component/physic/Fixed.hpp> #include <kernel/front/component/physic/Fixed.hpp>
#include <kernel/front/component/graphic/Sprite.hpp> #include <kernel/front/component/graphic/Sprite.hpp>
#include <kernel/front/component/graphic/TileMap.hpp>
namespace megu::game { namespace megu::game {
class Object : public kernel::Props { class Object : public kernel::Props {
public: public:
Object(std::filesystem::path &); Object(std::filesystem::path &);
inline kernel::Physical<kernel::PhysicEngine> & getPhysicComponent() override {return this->_physic;} inline kernel::Physical<kernel::PhysicEngine> * getPhysicComponent() override {return &this->_physic;}
inline kernel::Graphical<kernel::GraphicEngine> & getGraphicComponent() override {return this->_graphic;} inline kernel::Graphical<kernel::GraphicEngine> * getGraphicComponent() override {return &this->_map;}
inline void tmp_setPos(float x, float y) {
this->_graphic.tmp_setPos(x, y);
}
private: private:
kernel::Fixed _physic; kernel::Fixed _physic;
kernel::Sprite _graphic; kernel::Sprite _graphic;
kernel::Tilemap _map;
}; };
} }
\ No newline at end of file
...@@ -4,10 +4,12 @@ ...@@ -4,10 +4,12 @@
namespace megu::kernel { namespace megu::kernel {
GraphicEngine::GraphicEngine(Window & window) GraphicEngine::GraphicEngine(Window & window)
: _engine(window), _renderer(360, 360) {} : _engine(window), _renderer(360, 360), _tmp_ta("assets/textures/Tile_Test_3.png", 1, 1, 32.f) {}
void GraphicEngine::boot(Kernel &) { void GraphicEngine::boot(Kernel &) {
this->_engine.push(0, this->_renderer); this->_engine.push(0, this->_renderer);
//this->_engine.push(0, 0, this->_tmp_ta, &this->_tmp_mod);
} }
void GraphicEngine::stop(Kernel &) { void GraphicEngine::stop(Kernel &) {
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
#include <engine/graphics/front/engine/Engine.hpp> #include <engine/graphics/front/engine/Engine.hpp>
#include <engine/graphics/front/engine/Renderer.hpp> #include <engine/graphics/front/engine/Renderer.hpp>
#include <engine/graphics/front/module/TileArray_Module.hpp>
#include <engine/graphics/front/object/TileArray.hpp>
#include <kernel/back/props/Graphical.hpp> #include <kernel/back/props/Graphical.hpp>
namespace megu::kernel { namespace megu::kernel {
...@@ -28,5 +31,8 @@ namespace megu::kernel { ...@@ -28,5 +31,8 @@ namespace megu::kernel {
private: private:
megu::GraphicEngine _engine; megu::GraphicEngine _engine;
megu::Renderer _renderer; megu::Renderer _renderer;
megu::TileArray _tmp_ta;
megu::TileArray_Module _tmp_mod;
}; };
} }
\ No newline at end of file
#pragma once #pragma once
#include "Linker.hpp" #include "Linker.hpp"
#include "PriorityPair.hpp"
#include <kernel/back/engine/GraphicEngine.hpp> #include <kernel/back/engine/GraphicEngine.hpp>
namespace megu::kernel { namespace megu::kernel {
struct Priority_Pair {
Priority layer;
Priority object;
bool operator<(const Priority_Pair & pp) const {
if(this->layer == pp.layer) {
return this->object < pp.object;
}
return this->layer < pp.layer;
}
};
template <class T> template <class T>
class GraphicLinker : public Linker<GraphicEngine> { class GraphicLinker : public Linker<GraphicEngine> {
public: public:
......
#pragma once
#include <engine/utility/Priority.hpp>
namespace megu::kernel {
struct Priority_Pair {
Priority layer;
Priority object;
bool operator<(const Priority_Pair & pp) const {
if(this->layer == pp.layer) {
return this->object < pp.object;
}
return this->layer < pp.layer;
}
};
}
\ No newline at end of file
#pragma once
#include "Linker.hpp"
#include "PriorityPair.hpp"
#include <kernel/back/engine/GraphicEngine.hpp>
namespace megu::kernel {
template <class T>
class UniqueGraphicLinker : public Linker<GraphicEngine> {
public:
UniqueGraphicLinker();
void link(T &, Priority, Priority);
void link(GraphicEngine &) override;
void setModule(Module<T> *);
inline bool haveModule() const {return this->_module.get() != nullptr;}
inline ref_set<T> & objects() {return this->_object;}
private:
std::unique_ptr<Module<T>> _module;
std::map<Priority_Pair, ref_set<T>> _objects;
std::set<Priority_Pair> _waiting;
};
}
#include "UniqueLinker.tpp"
\ No newline at end of file
#include "UniqueLinker.hpp"
namespace megu::kernel {
template <class T>
UniqueGraphicLinker<T>::UniqueGraphicLinker()
: _module(nullptr) {}
template <class T>
void UniqueGraphicLinker<T>::link(T & object, Priority l, Priority o) {
auto layers = Priority_Pair{l, o};
this->_objects[layers].insert(object);
this->_waiting.insert(layers);
}
template <class T>
void UniqueGraphicLinker<T>::link(GraphicEngine & engine) {
for(const auto & layers : this->_waiting) {
for(auto & object : this->_objects[layers]) {
engine.get().push<T>(layers.layer, layers.object, object, this->_module.get());
}
}
this->_waiting.clear();
}
template <class T>
void UniqueGraphicLinker<T>::setModule(Module<T> * mod) {
this->_module = std::unique_ptr<Module<T>>(mod);
}
}
\ No newline at end of file
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
namespace megu::kernel { namespace megu::kernel {
class Props : public Identifiable { class Props : public Identifiable {
public: public:
virtual Physical<PhysicEngine> & getPhysicComponent() = 0; virtual Physical<PhysicEngine> * getPhysicComponent() = 0;
virtual Graphical<GraphicEngine> & getGraphicComponent() = 0; virtual Graphical<GraphicEngine> * getGraphicComponent() = 0;
}; };
} }
\ No newline at end of file
...@@ -26,8 +26,14 @@ namespace megu::kernel { ...@@ -26,8 +26,14 @@ namespace megu::kernel {
void Kernel::add(Props * props) { void Kernel::add(Props * props) {
this->_props[props->id()] = props; this->_props[props->id()] = props;
auto * pComponent = props->getPhysicComponent();
if(pComponent != nullptr) {
this->_pEngine.add(*this, *pComponent);
}
this->_pEngine.add(*this, props->getPhysicComponent()); auto * gComponent = props->getGraphicComponent();
this->_gEngine.add(*this, props->getGraphicComponent()); if(gComponent != nullptr) {
this->_gEngine.add(*this, *gComponent);
}
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment