Extra2D/include/renderer/texture.h

107 lines
2.3 KiB
C++

#pragma once
#include <types/ptr/ref_counted.h>
#include <types/ptr/intrusive_ptr.h>
#include <renderer/render_types.h>
#include <types/base/types.h>
#include <string>
// 前向声明 OpenGL 类型
typedef unsigned int GLuint;
namespace extra2d {
/**
* @brief 纹理类
*
* 管理 OpenGL 纹理的创建、加载和绑定
* 支持从文件或内存加载
*/
class Texture : public RefCounted {
public:
/**
* @brief 默认构造函数
*/
Texture();
/**
* @brief 析构函数
*/
~Texture() override;
/**
* @brief 从文件加载纹理
* @param path 文件路径
* @return 加载是否成功
*/
bool loadFromFile(const std::string& path);
/**
* @brief 从内存加载纹理
* @param data 像素数据
* @param width 宽度
* @param height 高度
* @param format 像素格式
* @return 加载是否成功
*/
bool loadFromMemory(const uint8_t* data, int width, int height, TextureFormat format);
/**
* @brief 创建空纹理
* @param width 宽度
* @param height 高度
* @param format 像素格式
* @return 创建是否成功
*/
bool create(int width, int height, TextureFormat format);
/**
* @brief 绑定纹理到指定槽位
* @param slot 纹理槽位
*/
void bind(uint32_t slot = 0) const;
/**
* @brief 解绑纹理
*/
void unbind() const;
/**
* @brief 获取纹理宽度
* @return 宽度
*/
int getWidth() const { return width_; }
/**
* @brief 获取纹理高度
* @return 高度
*/
int getHeight() const { return height_; }
/**
* @brief 获取纹理格式
* @return 像素格式
*/
TextureFormat getFormat() const { return format_; }
/**
* @brief 获取 OpenGL 纹理对象
* @return OpenGL 纹理对象
*/
GLuint getHandle() const { return texture_; }
/**
* @brief 检查是否已加载
* @return 是否已加载
*/
bool isLoaded() const { return texture_ != 0; }
private:
GLuint texture_ = 0; // OpenGL 纹理对象
int width_ = 0; // 纹理宽度
int height_ = 0; // 纹理高度
TextureFormat format_ = TextureFormat::RGBA8; // 像素格式
};
} // namespace extra2d