Block animation

This commit is contained in:
@clasher113
2024-01-21 15:23:03 +02:00
parent fa6eb6ae22
commit 5fb34daf52
9 changed files with 264 additions and 24 deletions
+1
View File
@@ -40,6 +40,7 @@ public:
AtlasBuilder() {}
void add(std::string name, ImageData* image);
bool has(std::string name) const;
const std::set<std::string>& getNames() { return names; };
Atlas* build(uint extrusion, uint maxResolution=8192);
};
+52
View File
@@ -0,0 +1,52 @@
#include "TextureAnimation.h"
#include "Texture.h"
#include "Framebuffer.h"
#include <GL\glew.h>
#include <unordered_set>
TextureAnimator::TextureAnimator() :
frameBuffer(new Framebuffer(1u, 1u))
{
}
TextureAnimator::~TextureAnimator() {
delete frameBuffer;
}
void TextureAnimator::addAnimations(const std::vector<TextureAnimation>& animations) {
for (const auto& elem : animations) {
addAnimation(elem);
}
}
void TextureAnimator::update(float delta) {
std::unordered_set<uint> changedTextures;
frameBuffer->bind();
for (auto& elem : animations) {
if (changedTextures.find(elem.dstTexture->id) == changedTextures.end()) changedTextures.insert(elem.dstTexture->id);
elem.timer -= delta;
Frame& frame = elem.frames[elem.currentFrame];
if (elem.timer <= 0) {
elem.timer = frame.duration;
elem.currentFrame++;
if (elem.currentFrame >= elem.frames.size()) elem.currentFrame = 0;
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, elem.srcTexture->id, 0);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, elem.dstTexture->id, 0);
glDrawBuffer(GL_COLOR_ATTACHMENT1);
float srcPosY = elem.srcTexture->height - frame.size.y - frame.srcPos.y; // vertical flip
glBlitFramebuffer(frame.srcPos.x, srcPosY, frame.srcPos.x + frame.size.x, srcPosY + frame.size.y,
frame.dstPos.x, frame.dstPos.y, frame.dstPos.x + frame.size.x, frame.dstPos.y + frame.size.y,
GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
}
for (auto& elem : changedTextures) {
glBindTexture(GL_TEXTURE_2D, elem);
glGenerateMipmap(GL_TEXTURE_2D);
}
glBindTexture(GL_TEXTURE_2D, 0);
}
+52
View File
@@ -0,0 +1,52 @@
#ifndef TEXTURE_ANIMATION_H
#define TEXTURE_ANIMATION_H
#include "../typedefs.h"
#include <glm/glm.hpp>
#include <vector>
class Assets;
class Texture;
class Framebuffer;
constexpr float DEFAULT_FRAME_DURATION = 0.150f;
struct Frame {
glm::ivec2 srcPos;
glm::ivec2 dstPos;
glm::ivec2 size;
float duration = DEFAULT_FRAME_DURATION;
};
class TextureAnimation {
public:
TextureAnimation(Texture* srcTex, Texture* dstTex) : srcTexture(srcTex), dstTexture(dstTex) {};
~TextureAnimation() {};
void addFrame(const Frame& frame) { frames.emplace_back(frame); };
size_t currentFrame = 0;
float timer = 0.f;
Texture* srcTexture;
Texture* dstTexture;
std::vector<Frame> frames;
};
class TextureAnimator {
public:
TextureAnimator();
~TextureAnimator();
void addAnimation(const TextureAnimation& animation) { animations.emplace_back(animation); };
void addAnimations(const std::vector<TextureAnimation>& animations);
void update(float delta);
private:
Framebuffer* frameBuffer;
std::vector<TextureAnimation> animations;
};
#endif // !TEXTURE_ANIMATION_H