diff --git a/assets/tilemap.png b/assets/tilemap.png index 48ec4457cb7f48a2078e595d0f56a72cc4239534..b336765460a48008cfa66ca7a72ecdc290c9aa51 100644 Binary files a/assets/tilemap.png and b/assets/tilemap.png differ diff --git a/source/engine/graphics/back/textures/Texture.cpp b/source/engine/graphics/back/textures/Texture.cpp index 658169ba697730bcd1716eeb762c3ffe5d84f9b3..88821c8ad580c3f280857b3b5ec5a8cc1a75e1f4 100644 --- a/source/engine/graphics/back/textures/Texture.cpp +++ b/source/engine/graphics/back/textures/Texture.cpp @@ -5,7 +5,7 @@ namespace megu { Texture::Texture(GLuint slot, GLuint type) - : _id(0), _type(type), _slot(slot) { + : _id(0), _type(type), _slot(slot), _width(0), _height(0) { glGenTextures(1, &this->_id); } @@ -38,26 +38,6 @@ namespace megu { this->bind(); } - GLint Texture::width() const { - if(this->_id == 0) { - return 0; - } - - GLint width; - glGetTexLevelParameteriv(this->_type, 0, GL_TEXTURE_WIDTH, &width); - return width; - } - - GLint Texture::height() const { - if(this->_id == 0) { - return 0; - } - - GLint height; - glGetTexLevelParameteriv(this->_type, 0, GL_TEXTURE_HEIGHT, &height); - return height; - } - void Texture::changeSlot(GLuint slot) { this->_slot = slot; } @@ -90,6 +70,9 @@ namespace megu { glDeleteTextures(1, &this->_id); glGenTextures(1, &this->_id); } + + this->_width = width; + this->_height = height; glBindTexture(this->_type, this->_id); glTexImage2D(this->_type, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); diff --git a/source/engine/graphics/back/textures/Texture.hpp b/source/engine/graphics/back/textures/Texture.hpp index 221d6286d9b1367a3f3630ffbde63f59d0cd49f7..c58c0132a4d289bbbbd7c8d192404b1d0b563442 100644 --- a/source/engine/graphics/back/textures/Texture.hpp +++ b/source/engine/graphics/back/textures/Texture.hpp @@ -39,8 +39,8 @@ namespace megu { void bind() const; void bind(GLuint) const; - GLint width() const; - GLint height() const; + inline GLint width() const {return this->_width;} + inline GLint height() const {return this->_height;} inline GLuint slot() const {return this->_slot;} inline GLuint type() const {return this->_type;} @@ -71,6 +71,7 @@ namespace megu { private: GLuint _id; GLuint _type; - mutable GLuint _slot; + mutable GLuint _slot; + GLsizei _width, _height; }; } \ No newline at end of file diff --git a/source/game/object/Test.cpp b/source/game/object/Test.cpp index bcdf5552f1f0b80412255c096cb005d84eb0b431..f2c6e96403b9b3c8bbab5b5eb985d7c5fc2e1aaf 100644 --- a/source/game/object/Test.cpp +++ b/source/game/object/Test.cpp @@ -7,8 +7,11 @@ namespace megu::game { 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, 0, 2); + this->_map.setValue(0, 1, 1); this->_map.setValue(1, 1, 1); + + this->_map.addAnimation(0, 1, {18, 19, 20, 21}); + this->_map.addAnimation(1, 1, {18, 19, 20, 21}); } } \ No newline at end of file diff --git a/source/kernel/front/component/graphic/TileMap.cpp b/source/kernel/front/component/graphic/TileMap.cpp index da61dc8f6f9bb9747a2cd813ed68c09cd056f7d5..252c86febc0ed9e19759c805c3b42ea032e1b67a 100644 --- a/source/kernel/front/component/graphic/TileMap.cpp +++ b/source/kernel/front/component/graphic/TileMap.cpp @@ -3,23 +3,19 @@ namespace megu::kernel { UniqueGraphicLinker<TileArray> Tilemap::_Linker = UniqueGraphicLinker<TileArray>(); - Tilemap::Tilemap(const std::filesystem::path & path, size_t width, size_t height, float size, size_t lenght) - : TileArray(path, width , height, size), _tileSize(lenght) { + Tilemap::Tilemap(const std::filesystem::path & path, size_t width, size_t height, float size, size_t lenght, double aUpdate) + : TileArray(path, width , height, size), _tileSize(lenght), _duration(aUpdate) { if(lenght % width != 0 && lenght % height != 0) { throw std::runtime_error("Tilemap dimension not matching tiles size."); } - size_t i = 0, j = 0; - for(size_t x = 0; x < this->texture().width(); x += this->_tileSize) { - j = 0; - for(size_t y = 0; y < this->texture().height(); y += this->_tileSize) { - this->_tilesPosition.push_back({j * this->_tileSize, i * this->_tileSize}); - ++j; + for(size_t x = 0; x < this->texture().width() - this->_tileSize*2; x += this->_tileSize) { + for(size_t y = 0; y < this->texture().height() - this->_tileSize*2; y += this->_tileSize) { + this->_tilesPosition.push_back({y, x}); } - ++i; } - i = 0; + size_t i = 0; for(size_t x = 0; x < width * this->_tileSize; x += this->_tileSize) { this->_tilesValue.push_back({}); for(size_t y = 0; y < height * this->_tileSize; y += this->_tileSize) { @@ -38,11 +34,11 @@ namespace megu::kernel { void Tilemap::setValue(size_t x, size_t y, size_t value) { TilePosition position = this->_tilesPosition[value]; glm::vec4 uv = {position.x, - this->texture().height() - position.y - this->_tileSize, + this->texture().height() - position.y - this->_tileSize, this->_tileSize, this->_tileSize}; - this->setUv(x, this->width() - 1 - y, uv); + this->setUv(this->height() - 1 - y, x, uv); this->_tilesValue[x][y] = value; } @@ -53,8 +49,28 @@ namespace megu::kernel { throw std::runtime_error("Tilemap coords out of bound"); } + void Tilemap::addAnimation(size_t x, size_t y, const std::vector<size_t> & ids) { + this->_animations[{x, y}] = std::pair<std::vector<size_t>, size_t>(ids, 0); + } + + void Tilemap::removeAnimation(size_t x, size_t y) { + this->_animations.erase({x, y}); + } + void Tilemap::update(double time) { //... Peut-être des tiles animées si j'ai le temps 🙌 (spoiler non). + if(this->_previous == 0.0) { + this->_previous = time; + } + else { + if(time - this->_previous > this->_duration) { + for(auto & [coords, animation] : this->_animations) { + animation.second = (animation.second+1) % animation.first.size(); + this->setValue(coords.x, coords.y, animation.first[animation.second]); + } + this->_previous = time; + } + } } void Tilemap::apply(Kernel & kernel, GraphicEngine & engine) { diff --git a/source/kernel/front/component/graphic/TileMap.hpp b/source/kernel/front/component/graphic/TileMap.hpp index 38349535010d59c03aade7b4993a3547f6d44365..39f43c8d99dc92008f3dcc7fac8425fdead63982 100644 --- a/source/kernel/front/component/graphic/TileMap.hpp +++ b/source/kernel/front/component/graphic/TileMap.hpp @@ -25,19 +25,25 @@ namespace megu::kernel { } }; - Tilemap(const std::filesystem::path &, size_t, size_t, float, size_t); + Tilemap(const std::filesystem::path &, size_t, size_t, float, size_t, double = 0.1); void setValue(size_t, size_t, size_t); size_t getValue(size_t, size_t); + void addAnimation(size_t, size_t, const std::vector<size_t> &); + void removeAnimation(size_t, size_t); + void update(double) override; void apply(Kernel &, GraphicEngine &) override; private: std::vector<TilePosition> _tilesPosition; std::vector<std::vector<size_t>> _tilesValue; + std::map<TilePosition, std::pair<std::vector<size_t>, size_t>> _animations; TileArray_Module _module; size_t _tileSize; + double _duration; + double _previous; static UniqueGraphicLinker<TileArray> _Linker; };