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

Add animation on tilemap

parent c8b5b197
No related branches found
No related tags found
No related merge requests found
assets/tilemap.png

4.43 KiB | W: | H:

assets/tilemap.png

4.44 KiB | W: | H:

assets/tilemap.png
assets/tilemap.png
assets/tilemap.png
assets/tilemap.png
  • 2-up
  • Swipe
  • Onion skin
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
namespace megu { namespace megu {
Texture::Texture(GLuint slot, GLuint type) 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); glGenTextures(1, &this->_id);
} }
...@@ -38,26 +38,6 @@ namespace megu { ...@@ -38,26 +38,6 @@ namespace megu {
this->bind(); 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) { void Texture::changeSlot(GLuint slot) {
this->_slot = slot; this->_slot = slot;
} }
...@@ -91,6 +71,9 @@ namespace megu { ...@@ -91,6 +71,9 @@ namespace megu {
glGenTextures(1, &this->_id); glGenTextures(1, &this->_id);
} }
this->_width = width;
this->_height = height;
glBindTexture(this->_type, this->_id); glBindTexture(this->_type, this->_id);
glTexImage2D(this->_type, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); glTexImage2D(this->_type, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(this->_type); glGenerateMipmap(this->_type);
......
...@@ -39,8 +39,8 @@ namespace megu { ...@@ -39,8 +39,8 @@ namespace megu {
void bind() const; void bind() const;
void bind(GLuint) const; void bind(GLuint) const;
GLint width() const; inline GLint width() const {return this->_width;}
GLint height() const; inline GLint height() const {return this->_height;}
inline GLuint slot() const {return this->_slot;} inline GLuint slot() const {return this->_slot;}
inline GLuint type() const {return this->_type;} inline GLuint type() const {return this->_type;}
...@@ -72,5 +72,6 @@ namespace megu { ...@@ -72,5 +72,6 @@ namespace megu {
GLuint _id; GLuint _id;
GLuint _type; GLuint _type;
mutable GLuint _slot; mutable GLuint _slot;
GLsizei _width, _height;
}; };
} }
\ No newline at end of file
...@@ -7,8 +7,11 @@ namespace megu::game { ...@@ -7,8 +7,11 @@ namespace megu::game {
this->_graphic.setSize({51.f, 98.f}); this->_graphic.setSize({51.f, 98.f});
this->_map.setValue(0, 0, 1); this->_map.setValue(0, 0, 1);
this->_map.setValue(1, 0, 1); this->_map.setValue(1, 0, 2);
this->_map.setValue(0, 1, 2); this->_map.setValue(0, 1, 1);
this->_map.setValue(1, 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
...@@ -3,23 +3,19 @@ ...@@ -3,23 +3,19 @@
namespace megu::kernel { namespace megu::kernel {
UniqueGraphicLinker<TileArray> Tilemap::_Linker = UniqueGraphicLinker<TileArray>(); UniqueGraphicLinker<TileArray> Tilemap::_Linker = UniqueGraphicLinker<TileArray>();
Tilemap::Tilemap(const std::filesystem::path & path, size_t width, size_t height, float size, size_t 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) { : TileArray(path, width , height, size), _tileSize(lenght), _duration(aUpdate) {
if(lenght % width != 0 && lenght % height != 0) { if(lenght % width != 0 && lenght % height != 0) {
throw std::runtime_error("Tilemap dimension not matching tiles size."); 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() - this->_tileSize*2; x += this->_tileSize) {
for(size_t x = 0; x < this->texture().width(); x += this->_tileSize) { for(size_t y = 0; y < this->texture().height() - this->_tileSize*2; y += this->_tileSize) {
j = 0; this->_tilesPosition.push_back({y, x});
for(size_t y = 0; y < this->texture().height(); y += this->_tileSize) {
this->_tilesPosition.push_back({j * this->_tileSize, i * this->_tileSize});
++j;
} }
++i;
} }
i = 0; size_t i = 0;
for(size_t x = 0; x < width * this->_tileSize; x += this->_tileSize) { for(size_t x = 0; x < width * this->_tileSize; x += this->_tileSize) {
this->_tilesValue.push_back({}); this->_tilesValue.push_back({});
for(size_t y = 0; y < height * this->_tileSize; y += this->_tileSize) { for(size_t y = 0; y < height * this->_tileSize; y += this->_tileSize) {
...@@ -42,7 +38,7 @@ namespace megu::kernel { ...@@ -42,7 +38,7 @@ namespace megu::kernel {
this->_tileSize, 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; this->_tilesValue[x][y] = value;
} }
...@@ -53,8 +49,28 @@ namespace megu::kernel { ...@@ -53,8 +49,28 @@ namespace megu::kernel {
throw std::runtime_error("Tilemap coords out of bound"); 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) { void Tilemap::update(double time) {
//... Peut-être des tiles animées si j'ai le temps 🙌 (spoiler non). //... 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) { void Tilemap::apply(Kernel & kernel, GraphicEngine & engine) {
......
...@@ -25,19 +25,25 @@ namespace megu::kernel { ...@@ -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); void setValue(size_t, size_t, size_t);
size_t getValue(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 update(double) override;
void apply(Kernel &, GraphicEngine &) override; void apply(Kernel &, GraphicEngine &) override;
private: private:
std::vector<TilePosition> _tilesPosition; std::vector<TilePosition> _tilesPosition;
std::vector<std::vector<size_t>> _tilesValue; std::vector<std::vector<size_t>> _tilesValue;
std::map<TilePosition, std::pair<std::vector<size_t>, size_t>> _animations;
TileArray_Module _module; TileArray_Module _module;
size_t _tileSize; size_t _tileSize;
double _duration;
double _previous;
static UniqueGraphicLinker<TileArray> _Linker; static UniqueGraphicLinker<TileArray> _Linker;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment