forked from JonSnowbd/ZT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.zig
119 lines (103 loc) · 4.23 KB
/
main.zig
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
const std = @import("std");
const zt = @import("zt");
usingnamespace @import("imgui");
usingnamespace zt.custom_components;
const scenes_n = [_][]const u8{
"2D Rendering",
"2D Shaders",
"2D Render Targets",
"2D Spatial Hash (Squares)",
"2D Collider Support",
"ImGui Demo",
};
const scenes = [_]fn (*SampleApplication.Context) void{
@import("scenes/renderer.zig").update,
@import("scenes/shaders.zig").update,
@import("scenes/rendertarget.zig").update,
@import("scenes/spatialhash_squares.zig").update,
@import("scenes/colliders.zig").update,
@import("scenes/imgui.zig").update,
};
/// SampleData will be available through the application's context.
pub const SampleData = struct {
currentScene: usize = 0,
render: zt.game.Renderer = undefined,
sheet: zt.gl.Texture = undefined,
redShader: zt.gl.Shader = undefined,
consoleOpen:bool = true,
};
pub const SampleApplication = zt.App(SampleData);
pub fn main() !void {
var context = SampleApplication.begin(std.heap.c_allocator);
// Lets customize!
var io = igGetIO();
var font = context.addFont(zt.path("public-sans.ttf"), 14.0);
io.*.FontDefault = font;
// Set up state
context.settings.energySaving = false; // Some examples are games, and will benefit from this.
context.data.render = zt.game.Renderer.init();
context.data.sheet = try zt.gl.Texture.init(zt.path("texture/sheet.png"));
context.data.sheet.setNearestFilter(); // Pixel art looks better with nearest filters.
// Creating a shader from `zt.game.Renderer` only needs a fragment shader's source as the vertex shader
// will be provided by `zt.game.Renderer`. If you need more flexibility than this you'll want to
// edit ZT itself, or create your own buffer type.
context.data.redShader = zt.game.Renderer.createShader(@embedFile("scenes/shader/red.fragment"));
context.setWindowSize(1280, 720);
context.setWindowTitle("ZT Demo");
context.setWindowIcon(zt.path("texture/ico.png"));
// You control your own main loop, all you got to do is call begin and end frame,
// and zt will handle the rest.
while (context.open) {
context.beginFrame();
inspectContext(context);
// Call into the selected demo:
var index = std.math.clamp(context.data.currentScene, 0, scenes.len - 1);
scenes[index](context);
context.endFrame();
}
context.data.sheet.deinit();
context.data.render.deinit();
context.deinit();
}
// This is a simple side panel that will display information about the scene, your context, and settings.
fn inspectContext(ctx: *SampleApplication.Context) void {
// Basic toggle
const glfw = @import("glfw");
var io = igGetIO();
if(io.*.KeysDownDuration[glfw.GLFW_KEY_GRAVE_ACCENT] == 0.0) {
ctx.data.consoleOpen = !ctx.data.consoleOpen;
}
if(!ctx.data.consoleOpen) return;
const flags =
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoResize;
igSetNextWindowPos(zt.math.vec2(8, 8), ImGuiCond_Always, .{});
igSetNextWindowSize(zt.math.vec2(300, io.*.DisplaySize.y - 16), ImGuiCond_Always);
if (igBegin("Context Inspection", null, flags)) {
igText("Data Settings");
// ztEdit("Current Scene", &ctx.data.currentScene);
if (igBeginListBox("##Listbox pog", .{})) {
var i: usize = 0;
while (i < scenes.len) : (i += 1) {
if (igSelectable_Bool(scenes_n[i].ptr, i == ctx.data.currentScene, ImGuiSelectableFlags_SpanAvailWidth, .{})) {
ctx.data.currentScene = i;
}
}
igEndListBox();
}
igSeparator();
igText("Settings");
_ = ztEdit("Energy Saving", &ctx.settings.energySaving);
if (igCheckbox("V Sync", &ctx.settings.vsync)) {
// The vsync setting is only a getter, setting it does nothing.
// So on change, we follow through with the real call that changes it.
ctx.setVsync(ctx.settings.vsync);
}
_ = ztEdit("ImGui Active (Warning!!)", &ctx.settings.imguiActive);
igSeparator();
igText("Information");
ztText("{d:.1}fps", .{ctx.time.fps});
}
igEnd();
}