-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.zig
More file actions
243 lines (212 loc) · 9.69 KB
/
main.zig
File metadata and controls
243 lines (212 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const std = @import("std");
const builtin = @import("builtin");
const ig = @import("cimgui");
const ifa = @import("fonticon");
const stf = @import("setupfont");
const utils = @import("utils");
const sdl = @import("sdl3");
const impl_sdl3 = @import("impl_sdl3");
const impl_opengl3 = @import("impl_opengl3");
const img_load = @import("loadimage");
const IMGUI_HAS_DOCK = false; // Docking feature
const MainWinWidth: i32 = 1024;
const MainWinHeight: i32 = 900;
//--------
// main()
//--------
pub fn main() !void {
// Setup SDL
if (sdl.SDL_Init(sdl.SDL_INIT_VIDEO | sdl.SDL_INIT_GAMEPAD) == false) {
std.debug.print("Error: {s}\n", .{sdl.SDL_GetError()});
return error.SDL_init;
}
defer sdl.SDL_Quit();
var glsl_version_buf: [30]u8 = undefined;
var versions = [_][2]u16{ [_]u16{ 4, 6 }, [_]u16{ 4, 5 }, [_]u16{ 4, 4 }, [_]u16{ 4, 3 }, [_]u16{ 4, 2 }, [_]u16{ 4, 1 }, [_]u16{ 4, 0 }, [_]u16{ 3, 3 } };
switch (builtin.target.os.tag) {
.linux => {
versions[0][0] = 3;
versions[0][1] = 3;
},
.macos => {},
else => {},
}
var glsl_version: [:0]u8 = undefined;
//-------------------------
// Decide GL+GLSL versions
//-------------------------
var window: *sdl.SDL_Window = undefined;
for (versions) |ver| {
//_ = ver;
_ = sdl.SDL_GL_SetAttribute(sdl.SDL_GL_CONTEXT_FLAGS, 0);
_ = sdl.SDL_GL_SetAttribute(sdl.SDL_GL_CONTEXT_PROFILE_MASK, sdl.SDL_GL_CONTEXT_PROFILE_CORE);
_ = sdl.SDL_GL_SetAttribute(sdl.SDL_GL_CONTEXT_MAJOR_VERSION, ver[0]);
_ = sdl.SDL_GL_SetAttribute(sdl.SDL_GL_CONTEXT_MINOR_VERSION, ver[1]);
//_ = sdl.SDL_SetHint(sdl.SDL_HINT_IME_SHOW_UI, "1");
// Create window with graphics context
_ = sdl.SDL_GL_SetAttribute(sdl.SDL_GL_DOUBLEBUFFER, 1);
_ = sdl.SDL_GL_SetAttribute(sdl.SDL_GL_DEPTH_SIZE, 24);
_ = sdl.SDL_GL_SetAttribute(sdl.SDL_GL_STENCIL_SIZE, 8);
// Initialy main window is hidden. See: showWindowDelay
const window_flags = (sdl.SDL_WINDOW_OPENGL | sdl.SDL_WINDOW_RESIZABLE | sdl.SDL_WINDOW_HIDDEN);
if (sdl.SDL_CreateWindow("Dear ImGui SDL3+OpenGL3 example", MainWinWidth, MainWinHeight, window_flags)) |pointer| {
window = pointer;
glsl_version = try std.fmt.bufPrintZ(&glsl_version_buf, "#version {d}", .{ver[0] * 100 + ver[1] * 10});
std.debug.print("{s} \n", .{glsl_version});
break;
}
} else {
std.debug.print("Error: SDL_CreateWindow(): {s}\n", .{sdl.SDL_GetError()});
return error.SDL_CreatWindow;
}
defer sdl.SDL_DestroyWindow(window);
_ = sdl.SDL_SetWindowPosition(window, sdl.SDL_WINDOWPOS_CENTERED, sdl.SDL_WINDOWPOS_CENTERED);
const gl_context = sdl.SDL_GL_CreateContext(window);
defer _ = sdl.SDL_GL_DeleteContext(gl_context);
_ = sdl.SDL_GL_MakeCurrent(window, gl_context);
_ = sdl.SDL_GL_SetSwapInterval(1); // Enable vsync
// Setup Dear ImGui context
if (ig.igCreateContext(null) == null) {
return error.ImGuiCreateContextFailure;
}
defer ig.igDestroyContext(null);
const pio = ig.igGetIO_Nil();
pio.*.ConfigFlags |= ig.ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
pio.*.ConfigFlags |= ig.ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup doncking feature --- can't compile well at this moment.
if (IMGUI_HAS_DOCK) {
pio.*.ConfigFlags |= ig.ImGuiConfigFlags_DockingEnable; // Enable Docking
pio.*.ConfigFlags |= ig.ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
}
// Setup Dear ImGui style
ig.igStyleColorsDark(null);
// ig.igStyleColorsLight(null);
// ig.igStyleColorsClassic(null);
// Setup Platform/Renderer backends
_ = impl_sdl3.ImGui_ImplSDL3_InitForOpenGL(@ptrCast(window), gl_context);
defer impl_sdl3.ImGui_ImplSDL3_Shutdown();
_ = impl_opengl3.ImGui_ImplOpenGL3_Init(glsl_version.ptr);
defer impl_opengl3.ImGui_ImplOpenGL3_Shutdown();
//------------
// Load image
//------------
const ImageName = "./resources/fuji-poke-480.png";
var textureId: impl_opengl3.GLuint = undefined;
var textureWidth: c_int = 0;
var textureHeight: c_int = 0;
_ = img_load.LoadTextureFromFile(ImageName, &textureId, &textureWidth, &textureHeight);
//-------------
// Global vars
//-------------
var showDemoWindow = true;
var showAnotherWindow = false;
var fval: f32 = 0.0;
var counter: i32 = 0;
// Back ground color
var clearColor = [_]f32{ 0.25, 0.55, 0.9, 1.0 };
// Input text buffer
var sTextInuputBuf = [_:0]u8{0} ** 200;
var showWindowDelay: i32 = 2; // TODO: Avoid flickering of window at startup.
_ = stf.setupFonts();
const sz = ig.ImVec2{ .x = 0, .y = 0 };
var done = false;
while (!done) {
var event: sdl.SDL_Event = undefined;
while (sdl.SDL_PollEvent(&event)) {
_ = impl_sdl3.ImGui_ImplSDL3_ProcessEvent(@ptrCast(&event));
if (event.type == sdl.SDL_EVENT_QUIT)
done = true;
if ((event.type == sdl.SDL_EVENT_WINDOW_CLOSE_REQUESTED) and (event.window.windowID == sdl.SDL_GetWindowID(window)))
done = true;
}
// Start the Dear ImGui frame
impl_opengl3.ImGui_ImplOpenGL3_NewFrame();
impl_sdl3.ImGui_ImplSDL3_NewFrame();
ig.igNewFrame();
//------------------
// Show demo window
//------------------
if (showDemoWindow) {
ig.igShowDemoWindow(&showDemoWindow);
}
//------------------
// Show main window
//------------------
{
_ = ig.igBegin(ifa.ICON_FA_THUMBS_UP ++ " ImGui: Dear Bindings", null, 0);
defer ig.igEnd();
ig.igText(ifa.ICON_FA_COMMENT ++ " SDL3 v");
ig.igSameLine(0, -1.0);
ig.igText("[%d],[%s]", sdl.SDL_GetVersion(), sdl.SDL_GetRevision());
ig.igText(ifa.ICON_FA_COMMENT ++ " OpenGL v");
ig.igSameLine(0, -1.0);
ig.igText("%s", impl_opengl3.glGetString(impl_opengl3.GL_VERSION));
ig.igText("%s", ifa.ICON_FA_CIRCLE_INFO ++ " Dear ImGui v");
ig.igSameLine(0, -1.0);
ig.igText("%s", ig.igGetVersion());
ig.igText("%s", ifa.ICON_FA_CIRCLE_INFO ++ " Zig v");
ig.igSameLine(0, -1.0);
ig.igText("%s", builtin.zig_version_string);
ig.igSpacing();
_ = ig.igInputTextWithHint("InputText", "Input text here", &sTextInuputBuf, sTextInuputBuf.len, 0, null, null);
ig.igText("%s", "Input result:");
ig.igSameLine(0, -1.0);
ig.igText("%s", &sTextInuputBuf);
ig.igSpacing();
_ = ig.igCheckbox("Demo Window", &showDemoWindow);
_ = ig.igCheckbox("Another Window", &showAnotherWindow);
_ = ig.igSliderFloat("Float", &fval, 0.0, 1.0, "%.3f", 0);
_ = ig.igColorEdit3("Clear color", &clearColor, 0);
if (ig.igButton("Button", sz)) counter += 1;
ig.igSameLine(0, -1.0);
ig.igText("Counter = %d", counter);
ig.igText("Application average %.3f ms/frame (%.1f FPS)", 1000.0 / pio.*.Framerate, pio.*.Framerate);
// Show icon fonts
ig.igSeparatorText(ifa.ICON_FA_WRENCH ++ " Icon font test ");
ig.igText("%s", ifa.ICON_FA_TRASH_CAN ++ " Trash");
ig.igSpacing();
ig.igText("%s", ifa.ICON_FA_MAGNIFYING_GLASS_PLUS ++ " " ++ ifa.ICON_FA_POWER_OFF ++ " " ++ ifa.ICON_FA_MICROPHONE ++ " " ++ ifa.ICON_FA_MICROCHIP ++ " " ++ ifa.ICON_FA_VOLUME_HIGH ++ " " ++ ifa.ICON_FA_SCISSORS ++ " " ++ ifa.ICON_FA_SCREWDRIVER_WRENCH ++ " " ++ ifa.ICON_FA_BLOG);
} // end main window
//---------------------
// Show another window
//---------------------
if (showAnotherWindow) {
_ = ig.igBegin("Another Window", &showAnotherWindow, 0);
defer ig.igEnd();
ig.igText("Hello from another window!");
if (ig.igButton("Close Me", sz)) showAnotherWindow = false;
}
//------------------------
// Show image load window
//------------------------
{
_ = ig.igBegin("Image load test", null, 0);
defer ig.igEnd();
// 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 imageBoxPosTop = ig.igGetCursorScreenPos(); // # Get absolute pos.
ig.igImage(ig.ImTextureRef{ ._TexData = null, ._TexID = textureId }, size, uv0, uv1);
const imageBoxPosEnd = ig.igGetCursorScreenPos(); // # Get absolute pos.
if (ig.igIsItemHovered(ig.ImGuiHoveredFlags_DelayNone)) {
utils.zoomGlass(&textureId, textureWidth, imageBoxPosTop, imageBoxPosEnd, false);
}
}
//-----------
// Rendering
//-----------
ig.igRender();
impl_opengl3.glViewport(0, 0, @intFromFloat(pio.*.DisplaySize.x), @intFromFloat(pio.*.DisplaySize.y));
impl_opengl3.glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
impl_opengl3.glClear(impl_opengl3.GL_COLOR_BUFFER_BIT);
impl_opengl3.ImGui_ImplOpenGL3_RenderDrawData(@ptrCast(ig.igGetDrawData()));
_ = sdl.SDL_GL_SwapWindow(window);
if (showWindowDelay >= 0) {
showWindowDelay -= 1;
}
if (showWindowDelay == 0) { // Visible main window here at start up
_ = sdl.SDL_ShowWindow(window);
}
} // while end
} // main end