Compare commits

...

3 Commits

Author SHA1 Message Date
2ea4211c4b fix build 2024-06-12 18:25:10 -04:00
ac27995669 a little lcean up 2024-06-12 18:24:11 -04:00
515c3028cc image rotation 2024-06-12 18:20:35 -04:00
4 changed files with 90 additions and 91 deletions

View File

@ -14,7 +14,7 @@ Collapsed=0
[Window][Main] [Window][Main]
Pos=0,0 Pos=0,0
Size=1654,1186 Size=1673,1237
Collapsed=0 Collapsed=0
[Window][Help] [Window][Help]

View File

@ -1,35 +0,0 @@
#include "uuid.h"
namespace uuid {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(0, 15);
static std::uniform_int_distribution<> dis2(8, 11);
std::string generate_uuid_v4() {
std::stringstream ss;
int i;
ss << std::hex;
for (i = 0; i < 8; i++) {
ss << dis(gen);
}
ss << "-";
for (i = 0; i < 4; i++) {
ss << dis(gen);
}
ss << "-4";
for (i = 0; i < 3; i++) {
ss << dis(gen);
}
ss << "-";
ss << dis2(gen);
for (i = 0; i < 3; i++) {
ss << dis(gen);
}
ss << "-";
for (i = 0; i < 12; i++) {
ss << dis(gen);
};
return ss.str();
}
} // namespace uuid

View File

@ -1,12 +0,0 @@
#ifndef UUID_H
#define UUID_H
#include <random>
#include <sstream>
#include <string>
namespace uuid {
std::string generate_uuid_v4();
}
#endif // UUID_H

120
main.cpp
View File

@ -1,14 +1,3 @@
// Dear ImGui: standalone example application for SDL2 + OpenGL
// (SDL is a cross-platform general purpose library for handling windows,
// inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
// - Getting Started https://dearimgui.com/getting-started
// - Documentation https://dearimgui.com/docs (same as your local docs/
// folder).
// - Introduction, links and more at the top of imgui.cpp
#include <SDL2/SDL_events.h> #include <SDL2/SDL_events.h>
#include <SDL2/SDL_scancode.h> #include <SDL2/SDL_scancode.h>
#include <cstddef> #include <cstddef>
@ -17,6 +6,7 @@
#include "lib/backends/imgui_impl_sdl2.h" #include "lib/backends/imgui_impl_sdl2.h"
#include "lib/imgui.h" #include "lib/imgui.h"
#include "lib/imgui_internal.h" #include "lib/imgui_internal.h"
#include <cstdint>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <stdio.h> #include <stdio.h>
@ -52,14 +42,17 @@ struct Texture
{ {
ImTextureID texture; ImTextureID texture;
ImVec2 size; ImVec2 size;
int channels;
}; };
static uint8_t* image = nullptr;
Texture LoadTexture(const char * path) Texture LoadTexture(const char * path)
{ {
const int channelCount = 4; const int channelCount = 4;
int imageFileChannelCount; int imageFileChannelCount;
int width, height; int width, height;
uint8_t *image = (uint8_t *)stbi_load(path, &width, &height, &imageFileChannelCount, channelCount); image = (uint8_t *)stbi_load(path, &width, &height, &imageFileChannelCount, channelCount);
if (image == NULL) if (image == NULL)
{ {
fprintf(stderr, "%s\nFailed to open %s\n", stbi_failure_reason(), path); fprintf(stderr, "%s\nFailed to open %s\n", stbi_failure_reason(), path);
@ -78,16 +71,57 @@ Texture LoadTexture(const char * path)
Texture t; Texture t;
t.texture = (void*)(uintptr_t)(textureHandle); t.texture = (void*)(uintptr_t)(textureHandle);
t.size = ImVec2((float)width,(float)height); t.size = ImVec2((float)width,(float)height);
t.channels = channelCount;
stbi_image_free(image);
return t; return t;
} }
Texture ReloadTexture(ImTextureID id, int width, int height) {
GLuint tid = (GLuint)(uintptr_t)id;
glDeleteTextures((GLsizei)1, &tid);
static bool init = true; GLenum dataFormat = GL_RGBA;
static bool showHelp = false; GLuint textureHandle;
static int mode = 0; glGenTextures(1, &textureHandle);
const int maxAnnotatedTexels = 10000; glBindTexture(GL_TEXTURE_2D, textureHandle);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, dataFormat, GL_UNSIGNED_BYTE, image);
Texture t;
t.texture = (void*)(uintptr_t)(textureHandle);
t.size = ImVec2((float)width,(float)height);
return t;
}
void RotateImage(Texture t) {
int height = t.size.y;
int width = t.size.x;
int channels = t.channels;
const unsigned int sizeBuffer = width * height * channels;
unsigned char *tempBuffer = new unsigned char[sizeBuffer];
int nidx = 0;
for (int x = 0; x < width; x++)
{
for (int y = height - 1; y >= 0; y--)
{
int idx = (x + y * width) * channels;
for (int i = 0; i < channels; i++)
tempBuffer[nidx + i] = image[idx + i];
nidx = nidx + 4;
}
}
memcpy(image, tempBuffer, sizeBuffer);
delete[] tempBuffer;
}
const int MAX_ANNOATED_TEXELS = 10000;
// Main code // Main code
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@ -99,8 +133,10 @@ int main(int argc, char* argv[]) {
} }
bool TOOLTIP_ENABLED = false; bool TOOLTIP_ENABLED = false;
bool GRID_ENABLED = true; bool GRID_ENABLED = false;
bool AA_ENABLED = true; bool AA_ENABLED = true;
bool SHOW_HELP = false;
int MODE = 0;
// Decide GL+GLSL versions // Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2) #if defined(IMGUI_IMPL_OPENGL_ES2)
@ -154,7 +190,6 @@ int main(int argc, char* argv[]) {
// Setup Dear ImGui context // Setup Dear ImGui context
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
ImGui::CreateContext(); ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO(); ImGuiIO &io = ImGui::GetIO();
(void)io; (void)io;
@ -163,20 +198,15 @@ int main(int argc, char* argv[]) {
ImGuiTexInspect::ImplOpenGL3_Init(); // Or DirectX 11 equivalent (check your chosen backend header file) ImGuiTexInspect::ImplOpenGL3_Init(); // Or DirectX 11 equivalent (check your chosen backend header file)
ImGuiTexInspect::Init(); ImGuiTexInspect::Init();
ImGuiTexInspect::CreateContext(); ImGuiTexInspect::CreateContext();
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
ImGuiStyle &style = ImGui::GetStyle(); ImGuiStyle &style = ImGui::GetStyle();
ImGui_ImplSDL2_InitForOpenGL(window, gl_context); ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
ImGui_ImplOpenGL3_Init(glsl_version); ImGui_ImplOpenGL3_Init(glsl_version);
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
Texture t; Texture t;
auto flags = ImGuiTexInspect::InspectorFlags_FillVertical | ImGuiTexInspect::InspectorFlags_FillHorizontal;
if (init) {
init = false;
try { try {
auto args = argparse::parse<Args>(argc, argv, true); auto args = argparse::parse<Args>(argc, argv, true);
t = LoadTexture(args.fpath.c_str()); t = LoadTexture(args.fpath.c_str());
@ -184,13 +214,11 @@ int main(int argc, char* argv[]) {
std::cerr << "failed to parse arguments: " << e.what() << std::endl; std::cerr << "failed to parse arguments: " << e.what() << std::endl;
return -1; return -1;
} }
}
// Main loop // Main loop
bool done = false; bool done = false;
while (!done) while (!done)
{ {
auto flags = ImGuiTexInspect::InspectorFlags_FillVertical | ImGuiTexInspect::InspectorFlags_FillHorizontal;
// Poll and handle events (inputs, window resize, etc.) // Poll and handle events (inputs, window resize, etc.)
SDL_Event event; SDL_Event event;
@ -217,10 +245,14 @@ int main(int argc, char* argv[]) {
AA_ENABLED = !AA_ENABLED; AA_ENABLED = !AA_ENABLED;
break; break;
case SDL_SCANCODE_H: case SDL_SCANCODE_H:
showHelp = !showHelp; SHOW_HELP = !SHOW_HELP;
break; break;
case SDL_SCANCODE_D: case SDL_SCANCODE_D:
mode = (mode + 1) % 5; MODE = (MODE + 1) % 5;
break;
case SDL_SCANCODE_R:
RotateImage(t);
t = ReloadTexture(t.texture, t.size.y, t.size.x);
break; break;
case SDL_SCANCODE_Q: case SDL_SCANCODE_Q:
done = true; done = true;
@ -286,18 +318,18 @@ int main(int argc, char* argv[]) {
CurrentInspector_SetFlags(ImGuiTexInspect::InspectorFlags_NoForceFilterNearest); CurrentInspector_SetFlags(ImGuiTexInspect::InspectorFlags_NoForceFilterNearest);
} }
switch(mode) { switch(MODE) {
case 1: case 1:
ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::Arrow().UsePreset(ImGuiTexInspect::Arrow::NormalMap), maxAnnotatedTexels); ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::Arrow().UsePreset(ImGuiTexInspect::Arrow::NormalMap), MAX_ANNOATED_TEXELS);
break; break;
case 2: case 2:
ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::ValueText(ImGuiTexInspect::ValueText::Format::HexString), maxAnnotatedTexels); ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::ValueText(ImGuiTexInspect::ValueText::Format::HexString), MAX_ANNOATED_TEXELS);
break; break;
case 3: case 3:
ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::ValueText(ImGuiTexInspect::ValueText::Format::BytesDec), maxAnnotatedTexels); ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::ValueText(ImGuiTexInspect::ValueText::Format::BytesDec), MAX_ANNOATED_TEXELS);
break; break;
case 4: case 4:
ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::ValueText(ImGuiTexInspect::ValueText::Format::Floats), maxAnnotatedTexels); ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::ValueText(ImGuiTexInspect::ValueText::Format::Floats), MAX_ANNOATED_TEXELS);
break; break;
default: default:
break; break;
@ -310,19 +342,29 @@ int main(int argc, char* argv[]) {
ImGui::PopStyleVar(); ImGui::PopStyleVar();
ImGui::PopStyleVar(); ImGui::PopStyleVar();
if (showHelp){ if (SHOW_HELP){
ImGui::OpenPopup("HelpPopup"); ImGui::OpenPopup("HelpPopup");
showHelp = !showHelp; SHOW_HELP = !SHOW_HELP;
} }
if (ImGui::BeginPopup("HelpPopup")) { if (ImGui::BeginPopup("HelpPopup")) {
ImGui::Text("tview Help"); ImGui::Text("tview Help");
ImGui::Separator(); ImGui::Separator();
ImGui::Text("r - rotate 90 deg clockwise");
ImGui::Text("g - toggle grid"); ImGui::Text("g - toggle grid");
ImGui::Text("a - toggle filtering"); ImGui::Text("a - toggle filtering");
ImGui::Text("t - toggle tooltip"); ImGui::Text("t - toggle tooltip");
ImGui::Text("d - cycle pixel detail mode"); ImGui::Text("d - cycle pixel detail mode");
ImGui::Text("q - quit"); ImGui::Separator();
ImGui::Text("modes:");
ImGui::Text("\tOff");
ImGui::Text("\tGradient Arrow");
ImGui::Text("\tHex Code");
ImGui::Text("\tRGB Values");
ImGui::Text("\tFloat Values");
ImGui::Separator();
ImGui::Text("h - show help popup"); ImGui::Text("h - show help popup");
ImGui::Text("q - quit");
ImGui::Separator(); ImGui::Separator();
ImGui::Text("click anywhere to continue"); ImGui::Text("click anywhere to continue");
ImGui::EndPopup(); ImGui::EndPopup();
@ -339,6 +381,10 @@ int main(int argc, char* argv[]) {
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
} }
if (image != nullptr) {
stbi_image_free(image);
}
// Cleanup // Cleanup
ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown(); ImGui_ImplSDL2_Shutdown();