diff --git a/README.md b/README.md index a8af6c6..26ffed2 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,10 @@ - [Prerequisites](#prerequisites) - [Build and run](#build-and-run) - [Examples screen shots](#examples-screen-shots) - - [glfw_opengl3 / sdl2_opengl3 / sdl3_opengl3](#glfw_opengl3--sdl2_opengl3--sdl3_opengl3) - - [glfw_opengl3_jp](#glfw_opengl3_jp) - - [iconFontViewer](#iconfontviewer) - - [glfw_opengl3_image_load](#glfw_opengl3_image_load) + - [Image load](#image-load) + - [Showing CJK fonts and text input](#showing-cjk-fonts-and-text-input) + - [Icon font viewer](#icon-font-viewer) + - [Image load / save](#image-load--save) - [glfw_opengl3_implot](#glfw_opengl3_implot) - [ImPlot Demo written in Zig lang.](#implot-demo-written-in-zig-lang) - [Build and run](#build-and-run-1) @@ -30,7 +30,8 @@ - [Show / Hide console window](#show--hide-console-window) - [SDL libraries](#sdl-libraries) - [My tools version](#my-tools-version) - - [Similar project](#similar-project) + - [Similar project ImGui / CImGui](#similar-project-imgui--cimgui) + - [SDL game tutorial Platfromer](#sdl-game-tutorial-platfromer) @@ -57,6 +58,7 @@ ImGui / CImGui version **1.91.4dock** (2024/10) --- - Zig version + 1. zig-windows-x86_64-0.14.0-dev.2265 is OK (2024/11/21) 1. [zig-windows-x86_64-0.14.0-dev.2183+ee9f00d67.zip](https://ziglang.org/builds/zig-windows-x86_64-0.14.0-dev.2183+ee9f00d67.zip) OK 1. [zig-linux-x86_64-0.14.0-dev.2183+ee9f00d67.tar.xz](https://ziglang.org/builds/zig-linux-x86_64-0.14.0-dev.2183+ee9f00d67.tar.xz) OK Probably [zig-windows-x86_64-0.14.0-dev.xxxx+yyyy...](https://ziglang.org/download/) might be OK @@ -102,40 +104,44 @@ Linux Mint 22 is OK. (or Ubuntu / Debian families ?) ### Examples screen shots -#### glfw_opengl3 / sdl2_opengl3 / sdl3_opengl3 +#### Image load --- -[glfw_opengl3](examples/glfw_opengl3/src/main.zig) / -[sdl2_opengl3](examples/sdl2_opengl3/src/main.zig) / -[sdl3_opengl3](examples/sdl3_opengl3/src/main.zig) +Image load and magnifying glass -![alt](img/glfw_opengl3.png) -#### glfw_opengl3_jp +[glfw_opengl3.zig](https://github.com/dinau/imguinz/raw/main/examples/glfw_opengl3/src/main.zig) +[sdl2_opengl3.zig](https://github.com/dinau/imguinz/raw/main/examples/sdl2_opengl3/src/main.zig) +[sdl3_opengl3.zig](https://github.com/dinau/imguinz/raw/main/examples/sdl3_opengl3/src/main.zig) + +![alt](https://github.com/dinau/imguinz/raw/main/img/sdl2_opengl3.png) + +#### Showing CJK fonts and text input --- -Load / Show CJK fonts -[glfw_opengl3_jp](examples/glfw_opengl3_jp/src/main.zig) +[glfw_opengl3_jp.zig](https://github.com/dinau/imguinz/raw/main/examples/glfw_opengl3_jp/src/main.zig) -![alt](img/glfw_opengl3_jp.png) +![alt](https://github.com/dinau/imguinz/raw/main/img/glfw_opengl3_jp.png) -#### IconFontViewer +#### Icon font viewer --- -[iconFontViewer](examples/iconFontViewer/src/main.zig) with magnifying glass +[iconFontViewer.zig](https://github.com/dinau/imguinz/raw/main/examples/iconFontViewer/src/main.zig) and magnifying glass -![alt](img/iconFontViewer.png) +![alt](https://github.com/dinau/imguinz/raw/main/img/iconFontViewer.png) -#### glfw_opengl3_image_load +#### Image load / save --- -[glfw_opengl3_image_load](examples/glfw_opengl3_image_load/src/main.zig) with magnifying glass +Image load / save and magnifying glass + +[glfw_opengl3_image_load.zig](https://github.com/dinau/imguinz/raw/main/examples/glfw_opengl3_image_load/src/main.zig) -![alt](img/glfw_opengl3_image_load.png) +![alt](https://github.com/dinau/imguinz/raw/main/img/glfw_opengl3_image_load.png) Image file captured would be saved in the folder `./zig-out/bin`. Image can be saved as `JPEG / PNG / BMP / TGA` file. @@ -143,9 +149,9 @@ Image can be saved as `JPEG / PNG / BMP / TGA` file. --- -[glfw_opengl3_implot](examples/glfw_opengl3_implot/src/main.zig) +[glfw_opengl3_implot.zig](https://github.com/dinau/imguinz/raw/main/examples/glfw_opengl3_implot/src/main.zig) -![alt](img/glfw_opengl3_implot.png) +![alt](https://github.com/dinau/imguinz/raw/main/img/glfw_opengl3_implot.png) ### ImPlot Demo written in Zig lang. @@ -166,7 +172,7 @@ make run # or zig build --release=fast run --- -[demoAll.zig](examples/imPlotDemo/src/demoAll.zig) +[demoAll.zig](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/src/demoAll.zig) #### Plots Tab @@ -174,31 +180,30 @@ make run # or zig build --release=fast run ##### LinePlots (Dynamic) -![alt](examples/imPlotDemo/img/LinePlots.png) - +![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/LinePlots.png) ##### BarGroups -![alt](examples/imPlotDemo/img/BarGroups.png) +![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/BarGroups.png) ##### BarStacks -![alt](examples/imPlotDemo/img/BarStacks.png) +![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/BarStacks.png) ##### PieCharts -![alt](examples/imPlotDemo/img/PieCharts.png) +![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/PieCharts.png) ##### Heatmaps -![alt](examples/imPlotDemo/img/Heatmaps.png) +![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/Heatmaps.png) ##### Histogram2D -![alt](examples/imPlotDemo/img/Histogram2D.png) +![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/Histogram2D.png) ##### Images -![alt](examples/imPlotDemo/img/Images.png) +![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/Images.png) #### Axes Tab @@ -206,7 +211,7 @@ make run # or zig build --release=fast run ##### LogScale -![alt](examples/imPlotDemo/img/LogScale.png) +![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/LogScale.png) #### Subplots Tab @@ -214,7 +219,7 @@ make run # or zig build --release=fast run ##### Tables (Dynamic) -![alt](examples/imPlotDemo/img/Tables.png) +![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/Tables.png) #### Tools Tab @@ -222,7 +227,7 @@ make run # or zig build --release=fast run ##### DragRects -![alt](examples/imPlotDemo/img/DragRects.png) +![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/DragRects.png) ### Show / Hide console window @@ -269,14 +274,27 @@ https://github.com/JBetz/build-sdl3-win32 - SDL2 ver.2.30.0 - Git version 2.43.0 -### Similar project +#### Similar project ImGui / CImGui --- -| Language | Project | -| -------------------: | :----------------------------------------------------------------: | -| **Nim** | [ImGuin](https://github.com/dinau/imguin), [Nimgl_test](https://github.com/dinau/nimgl_test), [Nim_implot](https://github.com/dinau/nim_implot) | -| **Lua** | [LuaJITImGui](https://github.com/dinau/luajitimgui) | -| **Python** | [DearPyGui for 32bit WindowsOS Binary](https://github.com/dinau/DearPyGui32/tree/win32) | -| **Zig**, C lang. | [Dear_Bindings_Build](https://github.com/dinau/dear_bindings_build) | -| **Zig** | [ImGuinZ](https://github.com/dinau/imguinz) | +| Language | | Project | +| -------------------: | :---: | :----------------------------------------------------------------: | +| **Nim** | Compiler | [ImGuin](https://github.com/dinau/imguin), [Nimgl_test](https://github.com/dinau/nimgl_test), [Nim_implot](https://github.com/dinau/nim_implot) | +| **Lua** | Script | [LuaJITImGui](https://github.com/dinau/luajitImGui) | +| **Zig**, C lang. | Compiler | [Dear_Bindings_Build](https://github.com/dinau/dear_bindings_build) | +| **Zig** | Compiler | [ImGuinZ](https://github.com/dinau/imguinz) | +| **NeLua** | Compiler | [NeLuaImGui](https://github.com/dinau/neluaImGui) | +| **Python** | Script | [DearPyGui for 32bit WindowsOS Binary](https://github.com/dinau/DearPyGui32/tree/win32) | + +#### SDL game tutorial Platfromer + +--- + +![ald](https://github.com/dinau/nelua-platformer/raw/main/img/game.png) + +| Language | | Project | +| -------------------: | :---: | :----------------------------------------------------------------: | +| **Nim** | Compiler | [Nim-Platformer](https://github.com/dinau/nim-platformer) | +| **LuaJIT** | Script | [LuaJIT-Platformer](https://github.com/dinau/luajit-platformer) | +| **Nelua** | Compiler | [NeLua-Platformer](https://github.com/dinau/nelua-platformer) | diff --git a/examples/glfw_opengl3/beans-400.jpg b/examples/glfw_opengl3/beans-400.jpg new file mode 100644 index 0000000..764a37c Binary files /dev/null and b/examples/glfw_opengl3/beans-400.jpg differ diff --git a/examples/glfw_opengl3/build.zig b/examples/glfw_opengl3/build.zig index 0dbd403..5e5eed7 100644 --- a/examples/glfw_opengl3/build.zig +++ b/examples/glfw_opengl3/build.zig @@ -168,7 +168,8 @@ pub fn build(b: *std.Build) void { // step when running `zig build`). b.installArtifact(exe); - const resBin = [_][]const u8{ "imgui.ini"}; + const resBin = [_][]const u8{ "imgui.ini" + , "beans-400.jpg" }; const resUtils = [_][]const u8{ "fonticon/fa6/fa-solid-900.ttf" , "fonticon/fa6/LICENSE.txt"}; const resIcon = "src/res/z.png"; diff --git a/examples/glfw_opengl3/imgui.ini b/examples/glfw_opengl3/imgui.ini index 62dfc53..bb5e5b2 100644 --- a/examples/glfw_opengl3/imgui.ini +++ b/examples/glfw_opengl3/imgui.ini @@ -4,7 +4,7 @@ Size=400,400 Collapsed=0 [Window][Dear ImGui Demo] -Pos=462,5 +Pos=440,10 Size=550,680 Collapsed=0 @@ -40,6 +40,11 @@ Collapsed=0 [Window][ Dear ImGui] Pos=9,7 -Size=444,340 +Size=416,340 +Collapsed=0 + +[Window][Image load test] +Pos=10,353 +Size=416,435 Collapsed=0 diff --git a/examples/glfw_opengl3/src/main.zig b/examples/glfw_opengl3/src/main.zig index 5ddedf6..75259b4 100644 --- a/examples/glfw_opengl3/src/main.zig +++ b/examples/glfw_opengl3/src/main.zig @@ -2,6 +2,7 @@ const std = @import ("std"); const builtin = @import ("builtin"); const ig = @import ("imgui.zig"); const fonts = @import("fonts.zig"); +const utils = @import("utils.zig"); const IMGUI_HAS_DOCK = false; // true: Can't compile at this time. @@ -107,6 +108,7 @@ pub fn main () !void { _ = ig.ImGui_ImplOpenGL3_Init(glsl_version); defer ig.ImGui_ImplOpenGL3_Shutdown (); + //------------- // Global vars //------------- @@ -127,7 +129,18 @@ pub fn main () !void { //ig.igStyleColorsDark (null); //ig.igStyleColorsLight (null); + //------------ + // Load image + //------------ + const ImageName = "beans-400.jpg"; + var textureId : ig.GLuint = undefined; + var textureWidth: c_int = 0; + var textureHeight : c_int = 0; + _ = ig.LoadTextureFromFile(ImageName, &textureId, &textureWidth, &textureHeight); fonts.setupFonts(); // Setup CJK fonts and Icon fonts + + var zoomTextureID: ig.GLuint = 0; //# Must be == 0 at first + defer ig.glDeleteTextures(1, &zoomTextureID); const DefaultButtonSize = ig.ImVec2 {.x = 0, .y = 0} ; //--------------- @@ -201,6 +214,27 @@ pub fn main () !void { if (ig.igButton ("Close Me", DefaultButtonSize)) showAnotherWindow = false; } + //------------------------ + // Show image load window + //------------------------ + if (ig.igBegin("Image load test", null, 0)) { + defer ig.igEnd(); + var imageBoxPosTop:ig.ImVec2 = undefined; + var imageBoxPosEnd:ig.ImVec2 = undefined; + // Load image + const size = ig.ImVec2 {.x = @floatFromInt(textureWidth), .y = @floatFromInt(textureHeight)}; + const uv0 = ig.ImVec2 {.x = 0, .y = 0}; + const uv1 = ig.ImVec2 {.x = 1, .y = 1}; + const tint_col = ig.ImVec4 {.x = 1, .y = 1, .z = 1, .w = 1}; + const border_col = ig.ImVec4 {.x = 0, .y = 0, .z = 0, .w = 0}; + ig.igGetCursorScreenPos(&imageBoxPosTop);// # Get absolute pos. + ig.igImage(@intCast(textureId), size, uv0, uv1, tint_col, border_col); + ig.igGetCursorScreenPos(&imageBoxPosEnd);// # Get absolute pos. + if(ig.igIsItemHovered(ig.ImGuiHoveredFlags_DelayNone)){ + utils.zoomGlass(&zoomTextureID, textureWidth, imageBoxPosTop, imageBoxPosEnd); + } + } + //----------- // End procs //----------- diff --git a/examples/glfw_opengl3/src/utils.zig b/examples/glfw_opengl3/src/utils.zig new file mode 100644 index 0000000..4fe6e77 --- /dev/null +++ b/examples/glfw_opengl3/src/utils.zig @@ -0,0 +1,56 @@ +const std = @import ("std"); +const builtin = @import ("builtin"); +const ig = @import ("imgui.zig"); +const fonts = @import("fonts.zig"); + +//#-------------- +//#--- zoomGlass +//#-------------- +pub fn zoomGlass(pTextureID:*ig.GLuint, itemWidth:i32, itemPosTop:ig.ImVec2, itemPosEnd:ig.ImVec2) void { + //# itemPosTop and itemPosEnd are absolute position in main window. + if(ig.igBeginItemTooltip()){ + defer ig.igEndTooltip(); + const itemHeight:i32 = @intFromFloat(itemPosEnd.y - itemPosTop.y); + const my_tex_w:f32 = @floatFromInt(itemWidth); + const my_tex_h:f32 = @floatFromInt(itemHeight); + const wkSize = ig.igGetMainViewport().*.WorkSize; + ig.loadTextureFromBuffer(pTextureID //# TextureID + , @intFromFloat(itemPosTop.x) //# x start pos + , @intFromFloat(wkSize.y - itemPosEnd.y) //# y start pos + , itemWidth ,itemHeight); //# Image width and height must be 2^n. + //#igText("lbp: (%.2f, %.2f)", pio.MousePos.x, pio.MousePos.y) + const pio = ig.igGetIO(); + const region_sz = 32.0; + var region_x = pio.*.MousePos.x - itemPosTop.x - region_sz * 0.5; + var region_y = pio.*.MousePos.y - itemPosTop.y - region_sz * 0.5; + const zoom = 4.0; + if(region_x < 0.0){ + region_x = 0.0; + } + else if(region_x > (my_tex_w - region_sz)){ + region_x = my_tex_w - region_sz; + } + if(region_y < 0.0){ + region_y = 0.0; + } else if(region_y > my_tex_h - region_sz){ + region_y = my_tex_h - region_sz; + } + const uv0 = ig.ImVec2{.x = region_x / my_tex_w, .y = region_y / my_tex_h}; + const uv1 = ig.ImVec2{.x = (region_x + region_sz) / my_tex_w, .y = (region_y + region_sz) / my_tex_h}; + const tint_col = ig.ImVec4{.x = 1.0, .y = 1.0, .z = 1.0, .w = 1.0}; // # No tint + const border_col = ig.ImVec4{.x = 0.22, .y = 0.56, .z = 0.22, .w = 1.0}; // # Green + ig.igImage(pTextureID.*, ig.ImVec2{.x = region_sz * zoom, .y = region_sz * zoom}, uv0, uv1, tint_col, border_col); + } +} + +//#--------------- +//#--- setTooltip +//#--------------- +pub fn setTooltip(str:[]const u8, delay:ig.ImGuiHoveredFlags) void{ + if(ig.igIsItemHovered(delay)){ + if(ig.igBeginTooltip()){ + ig.igText(str.ptr); + ig.igEndTooltip(); + } + } +} diff --git a/examples/makefile.common.mk b/examples/makefile.common.mk index 5013022..f5f0f6c 100644 --- a/examples/makefile.common.mk +++ b/examples/makefile.common.mk @@ -40,5 +40,8 @@ INCS += -I"C:/Program Files/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC trans: zig translate-c test.c $(INCS) > test.zig -upx: +upx: all upx --lzma $(TARGET_EXE) + +dupx: + upx -d $(TARGET_EXE) diff --git a/examples/sdl2_opengl3/beans-400.jpg b/examples/sdl2_opengl3/beans-400.jpg new file mode 100644 index 0000000..764a37c Binary files /dev/null and b/examples/sdl2_opengl3/beans-400.jpg differ diff --git a/examples/sdl2_opengl3/build.zig b/examples/sdl2_opengl3/build.zig index 5a70510..1170094 100644 --- a/examples/sdl2_opengl3/build.zig +++ b/examples/sdl2_opengl3/build.zig @@ -194,7 +194,8 @@ pub fn build(b: *std.Build) void { // step when running `zig build`). b.installArtifact(exe); - const resBin = [_][]const u8{ "imgui.ini"}; + const resBin = [_][]const u8{ "imgui.ini" + , "beans-400.jpg" }; const resUtils = [_][]const u8{ "fonticon/fa6/fa-solid-900.ttf" , "fonticon/fa6/LICENSE.txt"}; const resIcon = "src/res/z.png"; diff --git a/examples/sdl2_opengl3/imgui.ini b/examples/sdl2_opengl3/imgui.ini index a22ffb4..c2f164e 100644 --- a/examples/sdl2_opengl3/imgui.ini +++ b/examples/sdl2_opengl3/imgui.ini @@ -4,7 +4,7 @@ Size=400,400 Collapsed=0 [Window][Dear ImGui Demo] -Pos=405,8 +Pos=441,8 Size=550,680 Collapsed=0 @@ -35,6 +35,11 @@ Collapsed=0 [Window][ ImGui: Dear Bindings] Pos=10,6 -Size=382,343 +Size=418,343 +Collapsed=0 + +[Window][Image load test] +Pos=10,351 +Size=417,436 Collapsed=0 diff --git a/examples/sdl2_opengl3/src/imgui.zig b/examples/sdl2_opengl3/src/imgui.zig index 8dfb225..28a3f4a 100644 --- a/examples/sdl2_opengl3/src/imgui.zig +++ b/examples/sdl2_opengl3/src/imgui.zig @@ -1,6 +1,13 @@ pub usingnamespace @cImport ({ @cInclude ("cimgui.h"); @cInclude ("cimgui_impl.h"); + @cInclude ("SDL.h"); @cInclude ("SDL_opengl.h"); + + @cInclude ("loadImage.h"); + @cInclude ("saveImage.h"); + @cInclude ("stb_image.h"); + + }); diff --git a/examples/sdl2_opengl3/src/main.zig b/examples/sdl2_opengl3/src/main.zig index 7b6ddde..b6e044c 100644 --- a/examples/sdl2_opengl3/src/main.zig +++ b/examples/sdl2_opengl3/src/main.zig @@ -2,6 +2,7 @@ const std = @import ("std"); const builtin = @import ("builtin"); const ig = @import ("imgui.zig"); const fonts = @import ("fonts.zig"); +const utils = @import("utils.zig"); const IMGUI_HAS_DOCK = false; // true: Can't compile at this time. @@ -88,6 +89,15 @@ pub fn main () !void { _ = ig.ImGui_ImplOpenGL3_Init(glsl_version); defer ig.ImGui_ImplOpenGL3_Shutdown (); + //------------ + // Load image + //------------ + const ImageName = "beans-400.jpg"; + var textureId : ig.GLuint = undefined; + var textureWidth: c_int = 0; + var textureHeight : c_int = 0; + _ = ig.LoadTextureFromFile(ImageName, &textureId, &textureWidth, &textureHeight); + //------------- // Global vars //------------- @@ -105,6 +115,9 @@ pub fn main () !void { const sz = ig.ImVec2 {.x = 0, .y = 0} ; + var zoomTextureID: ig.GLuint = 0; //# Must be == 0 at first + defer ig.glDeleteTextures(1, &zoomTextureID); + var done = false; while (!done) { var event: ig.SDL_Event = undefined; @@ -183,6 +196,27 @@ pub fn main () !void { if (ig.igButton ("Close Me", sz)) showAnotherWindow = false; } + //------------------------ + // Show image load window + //------------------------ + if (ig.igBegin("Image load test", null, 0)) { + defer ig.igEnd(); + var imageBoxPosTop:ig.ImVec2 = undefined; + var imageBoxPosEnd:ig.ImVec2 = undefined; + // Load image + const size = ig.ImVec2 {.x = @floatFromInt(textureWidth), .y = @floatFromInt(textureHeight)}; + const uv0 = ig.ImVec2 {.x = 0, .y = 0}; + const uv1 = ig.ImVec2 {.x = 1, .y = 1}; + const tint_col = ig.ImVec4 {.x = 1, .y = 1, .z = 1, .w = 1}; + const border_col = ig.ImVec4 {.x = 0, .y = 0, .z = 0, .w = 0}; + ig.igGetCursorScreenPos(&imageBoxPosTop);// # Get absolute pos. + ig.igImage(@intCast(textureId), size, uv0, uv1, tint_col, border_col); + ig.igGetCursorScreenPos(&imageBoxPosEnd);// # Get absolute pos. + if(ig.igIsItemHovered(ig.ImGuiHoveredFlags_DelayNone)){ + utils.zoomGlass(&zoomTextureID, textureWidth, imageBoxPosTop, imageBoxPosEnd); + } + } + //----------- // End procs //----------- diff --git a/examples/sdl2_opengl3/src/utils.zig b/examples/sdl2_opengl3/src/utils.zig new file mode 100644 index 0000000..4fe6e77 --- /dev/null +++ b/examples/sdl2_opengl3/src/utils.zig @@ -0,0 +1,56 @@ +const std = @import ("std"); +const builtin = @import ("builtin"); +const ig = @import ("imgui.zig"); +const fonts = @import("fonts.zig"); + +//#-------------- +//#--- zoomGlass +//#-------------- +pub fn zoomGlass(pTextureID:*ig.GLuint, itemWidth:i32, itemPosTop:ig.ImVec2, itemPosEnd:ig.ImVec2) void { + //# itemPosTop and itemPosEnd are absolute position in main window. + if(ig.igBeginItemTooltip()){ + defer ig.igEndTooltip(); + const itemHeight:i32 = @intFromFloat(itemPosEnd.y - itemPosTop.y); + const my_tex_w:f32 = @floatFromInt(itemWidth); + const my_tex_h:f32 = @floatFromInt(itemHeight); + const wkSize = ig.igGetMainViewport().*.WorkSize; + ig.loadTextureFromBuffer(pTextureID //# TextureID + , @intFromFloat(itemPosTop.x) //# x start pos + , @intFromFloat(wkSize.y - itemPosEnd.y) //# y start pos + , itemWidth ,itemHeight); //# Image width and height must be 2^n. + //#igText("lbp: (%.2f, %.2f)", pio.MousePos.x, pio.MousePos.y) + const pio = ig.igGetIO(); + const region_sz = 32.0; + var region_x = pio.*.MousePos.x - itemPosTop.x - region_sz * 0.5; + var region_y = pio.*.MousePos.y - itemPosTop.y - region_sz * 0.5; + const zoom = 4.0; + if(region_x < 0.0){ + region_x = 0.0; + } + else if(region_x > (my_tex_w - region_sz)){ + region_x = my_tex_w - region_sz; + } + if(region_y < 0.0){ + region_y = 0.0; + } else if(region_y > my_tex_h - region_sz){ + region_y = my_tex_h - region_sz; + } + const uv0 = ig.ImVec2{.x = region_x / my_tex_w, .y = region_y / my_tex_h}; + const uv1 = ig.ImVec2{.x = (region_x + region_sz) / my_tex_w, .y = (region_y + region_sz) / my_tex_h}; + const tint_col = ig.ImVec4{.x = 1.0, .y = 1.0, .z = 1.0, .w = 1.0}; // # No tint + const border_col = ig.ImVec4{.x = 0.22, .y = 0.56, .z = 0.22, .w = 1.0}; // # Green + ig.igImage(pTextureID.*, ig.ImVec2{.x = region_sz * zoom, .y = region_sz * zoom}, uv0, uv1, tint_col, border_col); + } +} + +//#--------------- +//#--- setTooltip +//#--------------- +pub fn setTooltip(str:[]const u8, delay:ig.ImGuiHoveredFlags) void{ + if(ig.igIsItemHovered(delay)){ + if(ig.igBeginTooltip()){ + ig.igText(str.ptr); + ig.igEndTooltip(); + } + } +} diff --git a/examples/sdl3_opengl3/build.zig b/examples/sdl3_opengl3/build.zig index b11ad8f..acb6787 100644 --- a/examples/sdl3_opengl3/build.zig +++ b/examples/sdl3_opengl3/build.zig @@ -181,7 +181,8 @@ pub fn build(b: *std.Build) void { // step when running `zig build`). b.installArtifact(exe); - const resBin = [_][]const u8{ "imgui.ini"}; + const resBin = [_][]const u8{ "imgui.ini" + , "sailboat-400.jpg" }; const resUtils = [_][]const u8{ "fonticon/fa6/fa-solid-900.ttf" , "fonticon/fa6/LICENSE.txt"}; const resIcon = "src/res/z.png"; diff --git a/examples/sdl3_opengl3/imgui.ini b/examples/sdl3_opengl3/imgui.ini index a22ffb4..e549a65 100644 --- a/examples/sdl3_opengl3/imgui.ini +++ b/examples/sdl3_opengl3/imgui.ini @@ -4,7 +4,7 @@ Size=400,400 Collapsed=0 [Window][Dear ImGui Demo] -Pos=405,8 +Pos=447,8 Size=550,680 Collapsed=0 @@ -34,7 +34,12 @@ Size=339,180 Collapsed=0 [Window][ ImGui: Dear Bindings] -Pos=10,6 -Size=382,343 +Pos=11,5 +Size=414,343 +Collapsed=0 + +[Window][Image load test] +Pos=9,355 +Size=416,435 Collapsed=0 diff --git a/examples/sdl3_opengl3/sailboat-400.jpg b/examples/sdl3_opengl3/sailboat-400.jpg new file mode 100644 index 0000000..f1bd4c6 Binary files /dev/null and b/examples/sdl3_opengl3/sailboat-400.jpg differ diff --git a/examples/sdl3_opengl3/src/imgui.zig b/examples/sdl3_opengl3/src/imgui.zig index 8dfb225..2500a8d 100644 --- a/examples/sdl3_opengl3/src/imgui.zig +++ b/examples/sdl3_opengl3/src/imgui.zig @@ -1,6 +1,13 @@ pub usingnamespace @cImport ({ @cInclude ("cimgui.h"); @cInclude ("cimgui_impl.h"); - @cInclude ("SDL.h"); - @cInclude ("SDL_opengl.h"); + + @cDefine ("SDL_ENABLE_OLD_NAMES",""); + @cInclude ("SDL3/SDL.h"); + @cInclude ("SDL3/SDL_opengl.h"); + @cInclude ("SDL3/SDL_mouse.h"); + + @cInclude ("loadImage.h"); + @cInclude ("saveImage.h"); + @cInclude ("stb_image.h"); }); diff --git a/examples/sdl3_opengl3/src/main.zig b/examples/sdl3_opengl3/src/main.zig index 45f93be..7e41794 100644 --- a/examples/sdl3_opengl3/src/main.zig +++ b/examples/sdl3_opengl3/src/main.zig @@ -1,17 +1,9 @@ const std = @import ("std"); const builtin = @import ("builtin"); -pub const ig = @cImport ({ - @cInclude ("cimgui.h"); - @cInclude ("cimgui_impl.h"); - - @cDefine ("SDL_ENABLE_OLD_NAMES",""); - @cInclude ("SDL3/SDL.h"); - @cInclude ("SDL3/SDL_opengl.h"); - @cInclude ("SDL3/SDL_mouse.h"); -}); - +const ig= @import ("imgui.zig"); const fonts = @import ("fonts.zig"); +const utils = @import("utils.zig"); const IMGUI_HAS_DOCK = false; // true: Can't compile at this time. @@ -95,6 +87,15 @@ pub fn main () !void { _ = ig.ImGui_ImplOpenGL3_Init(glsl_version); defer ig.ImGui_ImplOpenGL3_Shutdown (); + //------------ + // Load image + //------------ + const ImageName = "sailboat-400.jpg"; + var textureId : ig.GLuint = undefined; + var textureWidth: c_int = 0; + var textureHeight : c_int = 0; + _ = ig.LoadTextureFromFile(ImageName, &textureId, &textureWidth, &textureHeight); + //------------- // Global vars //------------- @@ -112,6 +113,9 @@ pub fn main () !void { const sz = ig.ImVec2 {.x = 0, .y = 0} ; + var zoomTextureID: ig.GLuint = 0; //# Must be == 0 at first + defer ig.glDeleteTextures(1, &zoomTextureID); + var done = false; while (!done) { var event: ig.SDL_Event = undefined; @@ -188,6 +192,27 @@ pub fn main () !void { if (ig.igButton ("Close Me", sz)) showAnotherWindow = false; } + //------------------------ + // Show image load window + //------------------------ + if (ig.igBegin("Image load test", null, 0)) { + defer ig.igEnd(); + var imageBoxPosTop:ig.ImVec2 = undefined; + var imageBoxPosEnd:ig.ImVec2 = undefined; + // Load image + const size = ig.ImVec2 {.x = @floatFromInt(textureWidth), .y = @floatFromInt(textureHeight)}; + const uv0 = ig.ImVec2 {.x = 0, .y = 0}; + const uv1 = ig.ImVec2 {.x = 1, .y = 1}; + const tint_col = ig.ImVec4 {.x = 1, .y = 1, .z = 1, .w = 1}; + const border_col = ig.ImVec4 {.x = 0, .y = 0, .z = 0, .w = 0}; + ig.igGetCursorScreenPos(&imageBoxPosTop);// # Get absolute pos. + ig.igImage(@intCast(textureId), size, uv0, uv1, tint_col, border_col); + ig.igGetCursorScreenPos(&imageBoxPosEnd);// # Get absolute pos. + if(ig.igIsItemHovered(ig.ImGuiHoveredFlags_DelayNone)){ + utils.zoomGlass(&zoomTextureID, textureWidth, imageBoxPosTop, imageBoxPosEnd); + } + } + //----------- // End procs //----------- diff --git a/examples/sdl3_opengl3/src/utils.zig b/examples/sdl3_opengl3/src/utils.zig new file mode 100644 index 0000000..4fe6e77 --- /dev/null +++ b/examples/sdl3_opengl3/src/utils.zig @@ -0,0 +1,56 @@ +const std = @import ("std"); +const builtin = @import ("builtin"); +const ig = @import ("imgui.zig"); +const fonts = @import("fonts.zig"); + +//#-------------- +//#--- zoomGlass +//#-------------- +pub fn zoomGlass(pTextureID:*ig.GLuint, itemWidth:i32, itemPosTop:ig.ImVec2, itemPosEnd:ig.ImVec2) void { + //# itemPosTop and itemPosEnd are absolute position in main window. + if(ig.igBeginItemTooltip()){ + defer ig.igEndTooltip(); + const itemHeight:i32 = @intFromFloat(itemPosEnd.y - itemPosTop.y); + const my_tex_w:f32 = @floatFromInt(itemWidth); + const my_tex_h:f32 = @floatFromInt(itemHeight); + const wkSize = ig.igGetMainViewport().*.WorkSize; + ig.loadTextureFromBuffer(pTextureID //# TextureID + , @intFromFloat(itemPosTop.x) //# x start pos + , @intFromFloat(wkSize.y - itemPosEnd.y) //# y start pos + , itemWidth ,itemHeight); //# Image width and height must be 2^n. + //#igText("lbp: (%.2f, %.2f)", pio.MousePos.x, pio.MousePos.y) + const pio = ig.igGetIO(); + const region_sz = 32.0; + var region_x = pio.*.MousePos.x - itemPosTop.x - region_sz * 0.5; + var region_y = pio.*.MousePos.y - itemPosTop.y - region_sz * 0.5; + const zoom = 4.0; + if(region_x < 0.0){ + region_x = 0.0; + } + else if(region_x > (my_tex_w - region_sz)){ + region_x = my_tex_w - region_sz; + } + if(region_y < 0.0){ + region_y = 0.0; + } else if(region_y > my_tex_h - region_sz){ + region_y = my_tex_h - region_sz; + } + const uv0 = ig.ImVec2{.x = region_x / my_tex_w, .y = region_y / my_tex_h}; + const uv1 = ig.ImVec2{.x = (region_x + region_sz) / my_tex_w, .y = (region_y + region_sz) / my_tex_h}; + const tint_col = ig.ImVec4{.x = 1.0, .y = 1.0, .z = 1.0, .w = 1.0}; // # No tint + const border_col = ig.ImVec4{.x = 0.22, .y = 0.56, .z = 0.22, .w = 1.0}; // # Green + ig.igImage(pTextureID.*, ig.ImVec2{.x = region_sz * zoom, .y = region_sz * zoom}, uv0, uv1, tint_col, border_col); + } +} + +//#--------------- +//#--- setTooltip +//#--------------- +pub fn setTooltip(str:[]const u8, delay:ig.ImGuiHoveredFlags) void{ + if(ig.igIsItemHovered(delay)){ + if(ig.igBeginTooltip()){ + ig.igText(str.ptr); + ig.igEndTooltip(); + } + } +} diff --git a/examples/utils/loadImage.c b/examples/utils/loadImage.c index 47b16df..2f65fa5 100644 --- a/examples/utils/loadImage.c +++ b/examples/utils/loadImage.c @@ -9,6 +9,7 @@ #include #endif + #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #include "utils.h" diff --git a/img/sdl2_opengl3.png b/img/sdl2_opengl3.png new file mode 100644 index 0000000..68f77b7 Binary files /dev/null and b/img/sdl2_opengl3.png differ