Frostbite2D/Fostbite2D/include/fostbite2D/render/texture.h

155 lines
3.6 KiB
C
Raw Normal View History

2026-02-17 13:28:38 +08:00
#pragma once
#include <fostbite2D/core/math_types.h>
#include <fostbite2D/core/types.h>
namespace frostbite2D {
// ============================================================================
// 像素格式枚举
// ============================================================================
enum class PixelFormat {
R8, // 单通道灰度
RG8, // 双通道
RGB8, // RGB
RGBA8, // RGBA
R16F, // 半精度浮点单通道
RG16F, // 半精度浮点双通道
RGB16F, // 半精度浮点RGB
RGBA16F, // 半精度浮点RGBA
R32F, // 单精度浮点单通道
RG32F, // 单精度浮点双通道
RGB32F, // 单精度浮点RGB
RGBA32F, // 单精度浮点RGBA
// 压缩格式
ETC2_RGB8,
ETC2_RGBA8,
ASTC_4x4,
ASTC_6x6,
ASTC_8x8,
};
// ============================================================================
// 纹理抽象基类
// ============================================================================
class Texture {
public:
virtual ~Texture() = default;
/**
* @brief
* @return
*/
virtual int getWidth() const = 0;
/**
* @brief
* @return
*/
virtual int getHeight() const = 0;
/**
* @brief
* @return
*/
virtual Size getSize() const = 0;
/**
* @brief
* @return
*/
virtual int getChannels() const = 0;
/**
* @brief
* @return
*/
virtual PixelFormat getFormat() const = 0;
/**
* @brief
* @return OpenGL纹理ID
*/
virtual void *getNativeHandle() const = 0;
/**
* @brief
* @return truefalse
*/
virtual bool isValid() const = 0;
/**
* @brief
* @param linear true使用线性过滤false使用最近邻过滤
*/
virtual void setFilter(bool linear) = 0;
/**
* @brief
* @param repeat true使用重复模式false使用边缘拉伸模式
*/
virtual void setWrap(bool repeat) = 0;
};
// ============================================================================
// Alpha遮罩 - 用于像素级碰撞检测
// ============================================================================
class AlphaMask {
public:
AlphaMask() = default;
AlphaMask(AlphaMask &&) = default;
AlphaMask &operator=(AlphaMask &&) = default;
/**
* @brief Alpha遮罩
* @param pixels
* @param width
* @param height
* @param channels
* @return AlphaMask
*/
static AlphaMask createFromPixels(const uint8_t *pixels, int width,
int height, int channels);
/**
* @brief
* @return true
*/
bool isValid() const { return !data_.empty() && width_ > 0 && height_ > 0; }
/**
* @brief
* @param x X坐标
* @param y Y坐标
* @return 0-255
*/
uint8_t getAlpha(int x, int y) const;
/**
* @brief
* @param x X坐标
* @param y Y坐标
* @return true
*/
bool isOpaque(int x, int y) const;
/**
* @brief
* @return
*/
int getWidth() const { return width_; }
/**
* @brief
* @return
*/
int getHeight() const { return height_; }
private:
std::vector<uint8_t> data_;
int width_ = 0;
int height_ = 0;
};
} // namespace frostbite2D