Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dinau committed Nov 22, 2024
1 parent 1b9e42e commit 7f91426
Show file tree
Hide file tree
Showing 21 changed files with 380 additions and 65 deletions.
102 changes: 60 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand All @@ -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
Expand Down Expand Up @@ -102,50 +104,54 @@ 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.

#### glfw_opengl3_implot

---

[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.

Expand All @@ -166,63 +172,62 @@ 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

---

##### 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

---

##### LogScale

![alt](examples/imPlotDemo/img/LogScale.png)
![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/LogScale.png)

#### Subplots Tab

---

##### Tables (Dynamic)

![alt](examples/imPlotDemo/img/Tables.png)
![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/Tables.png)

#### Tools Tab

---

##### DragRects

![alt](examples/imPlotDemo/img/DragRects.png)
![alt](https://github.com/dinau/imguinz/raw/main/examples/imPlotDemo/img/DragRects.png)


### Show / Hide console window
Expand Down Expand Up @@ -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) |
Binary file added examples/glfw_opengl3/beans-400.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion examples/glfw_opengl3/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
9 changes: 7 additions & 2 deletions examples/glfw_opengl3/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Size=400,400
Collapsed=0

[Window][Dear ImGui Demo]
Pos=462,5
Pos=440,10
Size=550,680
Collapsed=0

Expand Down Expand Up @@ -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

34 changes: 34 additions & 0 deletions examples/glfw_opengl3/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -107,6 +108,7 @@ pub fn main () !void {
_ = ig.ImGui_ImplOpenGL3_Init(glsl_version);
defer ig.ImGui_ImplOpenGL3_Shutdown ();


//-------------
// Global vars
//-------------
Expand All @@ -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} ;
//---------------
Expand Down Expand Up @@ -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
//-----------
Expand Down
56 changes: 56 additions & 0 deletions examples/glfw_opengl3/src/utils.zig
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
5 changes: 4 additions & 1 deletion examples/makefile.common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Binary file added examples/sdl2_opengl3/beans-400.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion examples/sdl2_opengl3/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading

0 comments on commit 7f91426

Please sign in to comment.