kaliber/src/engine/image_quad.h

79 lines
1.9 KiB
C
Raw Normal View History

#ifndef ENGINE_IMAGE_QUAD_H
#define ENGINE_IMAGE_QUAD_H
2020-04-13 11:24:53 +00:00
#include <array>
#include <string>
#include <unordered_map>
#include <variant>
2020-04-13 11:24:53 +00:00
#include "base/vecmath.h"
#include "engine/animatable.h"
2020-04-13 11:24:53 +00:00
namespace eng {
class Shader;
2020-04-13 11:24:53 +00:00
class Texture;
2023-05-12 21:22:18 +00:00
class ImageQuad final : public Animatable {
2020-04-13 11:24:53 +00:00
public:
2021-10-28 20:20:53 +00:00
ImageQuad();
2023-05-12 21:22:18 +00:00
~ImageQuad() final;
2020-04-13 11:24:53 +00:00
void Create(const std::string& asset_name,
2020-04-13 11:24:53 +00:00
std::array<int, 2> num_frames = {1, 1},
int frame_width = 0,
int frame_height = 0);
void Destory();
void AutoScale();
2023-05-17 09:03:04 +00:00
void SetCustomShader(const std::string& asset_name);
template <typename T>
void SetCustomUniform(const std::string& name, T value) {
custom_uniforms_[name] = UniformValue(value);
}
2020-04-13 11:24:53 +00:00
// Animatable interface.
2023-05-12 21:22:18 +00:00
void SetFrame(size_t frame) final;
size_t GetFrame() const final { return current_frame_; }
size_t GetNumFrames() const final;
void SetColor(const base::Vector4f& color) final { color_ = color; }
base::Vector4f GetColor() const final { return color_; }
2020-04-13 11:24:53 +00:00
// Drawable interface.
2023-05-12 21:22:18 +00:00
void Draw(float frame_frac) final;
2020-04-13 11:24:53 +00:00
private:
2021-02-18 14:42:08 +00:00
using UniformValue = std::variant<base::Vector2f,
base::Vector3f,
base::Vector4f,
base::Matrix4f,
float,
int>;
2021-10-28 20:20:53 +00:00
Texture* texture_ = nullptr;
2020-04-13 11:24:53 +00:00
2021-10-28 20:20:53 +00:00
Shader* custom_shader_ = nullptr;
std::unordered_map<std::string, UniformValue> custom_uniforms_;
2020-04-13 11:24:53 +00:00
size_t current_frame_ = 0;
std::array<int, 2> num_frames_ = {1, 1}; // horizontal, vertical
int frame_width_ = 0;
int frame_height_ = 0;
2021-02-18 14:42:08 +00:00
base::Vector4f color_ = {1, 1, 1, 1};
2020-04-13 11:24:53 +00:00
std::string asset_name_;
2020-04-13 11:24:53 +00:00
float GetFrameWidth() const;
float GetFrameHeight() const;
base::Vector2f GetUVOffset(size_t frame) const;
2020-04-13 11:24:53 +00:00
};
} // namespace eng
#endif // ENGINE_IMAGE_QUAD_H