mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[wpigui] Refactor texture handling
The platform-specific code now only has create, update, and delete texture. Image reading functions have been moved to common code. Also add pixel data functions and image data functions in addition to image file loading.
This commit is contained in:
@@ -130,7 +130,56 @@ void SetClearColor(ImVec4 color);
|
||||
void EmitViewMenu();
|
||||
|
||||
/**
|
||||
* Loads a texture from a file.
|
||||
* Pixel formats for texture pixel data.
|
||||
*/
|
||||
enum PixelFormat { kPixelRGBA, kPixelBGRA };
|
||||
|
||||
/**
|
||||
* Creates a texture from pixel data.
|
||||
*
|
||||
* @param format pixel format
|
||||
* @param width image width
|
||||
* @param height image height
|
||||
* @param data pixel data
|
||||
* @return Texture
|
||||
*/
|
||||
ImTextureID CreateTexture(PixelFormat format, int width, int height,
|
||||
const unsigned char* data);
|
||||
|
||||
/**
|
||||
* Updates a texture from pixel data.
|
||||
* The passed-in width and height must match the width and height of the
|
||||
* texture.
|
||||
*
|
||||
* @param texture texture
|
||||
* @param format pixel format
|
||||
* @param width texture width
|
||||
* @param height texture height
|
||||
* @param data pixel data
|
||||
*/
|
||||
void UpdateTexture(ImTextureID texture, PixelFormat format, int width,
|
||||
int height, const unsigned char* data);
|
||||
|
||||
/**
|
||||
* Updates a texture from image data.
|
||||
* The pixel format of the texture must be RGBA. The passed-in width and
|
||||
* height must match the width and height of the texture. If the width and
|
||||
* height of the image differ from the passed-in width and height, a new
|
||||
* texture is created (note this may be inefficient).
|
||||
*
|
||||
* @param texture texture (pointer, may be updated)
|
||||
* @param width texture width
|
||||
* @param height texture height
|
||||
* @param data image data
|
||||
* @param len image data length
|
||||
*
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
bool UpdateTextureFromImage(ImTextureID* texture, int width, int height,
|
||||
const unsigned char* data, int len);
|
||||
|
||||
/**
|
||||
* Creates a texture from an image file.
|
||||
*
|
||||
* @param filename filename
|
||||
* @param out_texture texture (output)
|
||||
@@ -138,8 +187,22 @@ void EmitViewMenu();
|
||||
* @param out_height image height (output)
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
bool LoadTextureFromFile(const char* filename, ImTextureID* out_texture,
|
||||
int* out_width, int* out_height);
|
||||
bool CreateTextureFromFile(const char* filename, ImTextureID* out_texture,
|
||||
int* out_width, int* out_height);
|
||||
|
||||
/**
|
||||
* Creates a texture from image data.
|
||||
*
|
||||
* @param data image data
|
||||
* @param len image data length
|
||||
* @param out_texture texture (output)
|
||||
* @param out_width image width (output)
|
||||
* @param out_height image height (output)
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
bool CreateTextureFromImage(const unsigned char* data, int len,
|
||||
ImTextureID* out_texture, int* out_width,
|
||||
int* out_height);
|
||||
|
||||
/**
|
||||
* Deletes a texture.
|
||||
@@ -148,4 +211,144 @@ bool LoadTextureFromFile(const char* filename, ImTextureID* out_texture,
|
||||
*/
|
||||
void DeleteTexture(ImTextureID texture);
|
||||
|
||||
/**
|
||||
* RAII wrapper around ImTextureID. Also keeps track of width, height, and
|
||||
* pixel format.
|
||||
*/
|
||||
class Texture {
|
||||
public:
|
||||
Texture() = default;
|
||||
|
||||
/**
|
||||
* Constructs a texture from pixel data.
|
||||
*
|
||||
* @param format pixel format
|
||||
* @param width image width
|
||||
* @param height image height
|
||||
* @param data pixel data
|
||||
*/
|
||||
Texture(PixelFormat format, int width, int height, const unsigned char* data)
|
||||
: m_format{format}, m_width{width}, m_height{height} {
|
||||
m_texture = CreateTexture(format, width, height, data);
|
||||
}
|
||||
|
||||
Texture(const Texture&) = delete;
|
||||
Texture(Texture&& oth)
|
||||
: m_texture{oth.m_texture},
|
||||
m_format{oth.m_format},
|
||||
m_width{oth.m_width},
|
||||
m_height{oth.m_height} {
|
||||
oth.m_texture = 0;
|
||||
}
|
||||
|
||||
Texture& operator=(const Texture&) = delete;
|
||||
Texture& operator=(Texture&& oth) {
|
||||
if (m_texture) DeleteTexture(m_texture);
|
||||
m_texture = oth.m_texture;
|
||||
oth.m_texture = 0;
|
||||
m_format = oth.m_format;
|
||||
m_width = oth.m_width;
|
||||
m_height = oth.m_height;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Texture() {
|
||||
if (m_texture) DeleteTexture(m_texture);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates to true if the texture is valid.
|
||||
*/
|
||||
explicit operator bool() const { return m_texture; }
|
||||
|
||||
/**
|
||||
* Implicit conversion to ImTextureID.
|
||||
*/
|
||||
operator ImTextureID() const { return m_texture; }
|
||||
|
||||
/**
|
||||
* Gets the texture pixel format.
|
||||
*
|
||||
* @return pixel format
|
||||
*/
|
||||
PixelFormat GetFormat() const { return m_format; }
|
||||
|
||||
/**
|
||||
* Gets the texture width.
|
||||
*
|
||||
* @return width
|
||||
*/
|
||||
int GetWidth() const { return m_width; }
|
||||
|
||||
/**
|
||||
* Gets the texture height.
|
||||
*
|
||||
* @return height
|
||||
*/
|
||||
int GetHeight() const { return m_height; }
|
||||
|
||||
/**
|
||||
* Updates the texture from pixel data.
|
||||
* The image data size and format is assumed to match that of the texture.
|
||||
*
|
||||
* @param format pixel format
|
||||
* @param data pixel data
|
||||
*/
|
||||
void Update(const unsigned char* data) {
|
||||
UpdateTexture(m_texture, m_format, m_width, m_height, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the texture from image data.
|
||||
* The pixel format of the texture must be RGBA. If the width and height of
|
||||
* the image differ from the texture width and height, a new texture is
|
||||
* created (note this may be inefficient).
|
||||
*
|
||||
* @param data image data
|
||||
* @param len image data length
|
||||
*
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
bool UpdateFromImage(const unsigned char* data, int len) {
|
||||
return UpdateTextureFromImage(&m_texture, m_width, m_height, data, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a texture by loading an image file.
|
||||
*
|
||||
* @param filename filename
|
||||
*
|
||||
* @return Texture, or invalid (empty) texture on failure.
|
||||
*/
|
||||
static Texture CreateFromFile(const char* filename) {
|
||||
Texture texture;
|
||||
if (!CreateTextureFromFile(filename, &texture.m_texture, &texture.m_width,
|
||||
&texture.m_height))
|
||||
return {};
|
||||
return texture;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a texture from image data.
|
||||
*
|
||||
* @param data image data
|
||||
* @param len image data length
|
||||
*
|
||||
* @return Texture, or invalid (empty) texture on failure.
|
||||
*/
|
||||
static Texture CreateFromImage(const unsigned char* data, int len) {
|
||||
Texture texture;
|
||||
if (!CreateTextureFromImage(data, len, &texture.m_texture, &texture.m_width,
|
||||
&texture.m_height))
|
||||
return {};
|
||||
return texture;
|
||||
}
|
||||
|
||||
private:
|
||||
ImTextureID m_texture = nullptr;
|
||||
PixelFormat m_format = kPixelRGBA;
|
||||
int m_width = 0;
|
||||
int m_height = 0;
|
||||
};
|
||||
|
||||
} // namespace wpi::gui
|
||||
|
||||
Reference in New Issue
Block a user