mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +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:
@@ -17,6 +17,7 @@
|
||||
#include <imgui_impl_glfw.h>
|
||||
#include <imgui_internal.h>
|
||||
#include <implot.h>
|
||||
#include <stb_image.h>
|
||||
|
||||
#include "wpigui_internal.h"
|
||||
|
||||
@@ -271,7 +272,10 @@ void gui::CommonRenderFrame() {
|
||||
ImGui::Render();
|
||||
}
|
||||
|
||||
void gui::Exit() { gContext->exit = true; }
|
||||
void gui::Exit() {
|
||||
if (!gContext) return;
|
||||
gContext->exit = true;
|
||||
}
|
||||
|
||||
void gui::AddInit(std::function<void()> initialize) {
|
||||
if (initialize) gContext->initializers.emplace_back(std::move(initialize));
|
||||
@@ -354,4 +358,59 @@ void gui::EmitViewMenu() {
|
||||
}
|
||||
}
|
||||
|
||||
bool gui::UpdateTextureFromImage(ImTextureID* texture, int width, int height,
|
||||
const unsigned char* data, int len) {
|
||||
// Load from memory
|
||||
int width2 = 0;
|
||||
int height2 = 0;
|
||||
unsigned char* imgData =
|
||||
stbi_load_from_memory(data, len, &width2, &height2, nullptr, 4);
|
||||
if (!data) return false;
|
||||
|
||||
if (width2 == width && height2 == height)
|
||||
UpdateTexture(texture, kPixelRGBA, width2, height2, imgData);
|
||||
else
|
||||
*texture = CreateTexture(kPixelRGBA, width2, height2, imgData);
|
||||
|
||||
stbi_image_free(imgData);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool gui::CreateTextureFromFile(const char* filename, ImTextureID* out_texture,
|
||||
int* out_width, int* out_height) {
|
||||
// Load from file
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
unsigned char* data = stbi_load(filename, &width, &height, nullptr, 4);
|
||||
if (!data) return false;
|
||||
|
||||
*out_texture = CreateTexture(kPixelRGBA, width, height, data);
|
||||
if (out_width) *out_width = width;
|
||||
if (out_height) *out_height = height;
|
||||
|
||||
stbi_image_free(data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool gui::CreateTextureFromImage(const unsigned char* data, int len,
|
||||
ImTextureID* out_texture, int* out_width,
|
||||
int* out_height) {
|
||||
// Load from memory
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
unsigned char* imgData =
|
||||
stbi_load_from_memory(data, len, &width, &height, nullptr, 4);
|
||||
if (!imgData) return false;
|
||||
|
||||
*out_texture = CreateTexture(kPixelRGBA, width, height, imgData);
|
||||
if (out_width) *out_width = width;
|
||||
if (out_height) *out_height = height;
|
||||
|
||||
stbi_image_free(imgData);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace wpi
|
||||
|
||||
Reference in New Issue
Block a user