-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.zig
More file actions
179 lines (153 loc) · 5.11 KB
/
main.zig
File metadata and controls
179 lines (153 loc) · 5.11 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
//!zig-autodoc-section: BaseNuklear.Main
//! BaseNuklear//main.zig :
//! Template using Nuklear UI.
// Build using Zig 0.15.1
//=============================================================================
//#region MARK: GLOBAL
//=============================================================================
const std = @import("std");
const win = std.os.windows;
pub inline fn fmt(comptime format: []const u8, args: anytype) []u8 { return std.fmt.allocPrint(std.heap.page_allocator, format, args) catch unreachable; }
// NOTE ABOUT VSCODE + ZLS:
// Use full path for all cIncludes:
// @cInclude("C:/zig_workbench/BaseNuklear/lib/nuklear/nuklear.h");
const nk = @cImport({
@cDefine("NK_INCLUDE_FIXED_TYPES", "");
@cDefine("NK_INCLUDE_STANDARD_IO", "");
@cDefine("NK_INCLUDE_STANDARD_VARARGS", "");
@cDefine("NK_INCLUDE_DEFAULT_ALLOCATOR", "");
//@cDefine("NK_IMPLEMENTATION", "");
@cDefine("NK_GDI_IMPLEMENTATION", "");
@cInclude("nuklear.h");
@cInclude("nuklear_gdi.h");
@cDefine("NKGDI_IMPLEMENT_WINDOW", "");
@cInclude("window.h");
});
//#endregion ==================================================================
//#region MARK: MAIN
//=============================================================================
pub fn wWinMain(
hInstance: win.HINSTANCE,
hPrevInstance: ?win.HINSTANCE,
cmdArgs: win.PWSTR,
cmdShow: win.INT,
) win.INT {
_ = hInstance; _ = hPrevInstance; _ = cmdArgs; _ = cmdShow;
// Setup all required prerequisites
nk.nkgdi_window_init();
// Prepare two window contexts
var w1: nk.struct_nkgdi_window = std.mem.zeroes(nk.struct_nkgdi_window);
var w2: nk.struct_nkgdi_window = std.mem.zeroes(nk.struct_nkgdi_window);
// Configure and create window 1
w1.allow_sizing = 0;
w1.allow_maximize = 0;
w1.allow_move = 1;
w1.has_titlebar = 1;
w1.cb_on_draw = @ptrCast(&drawCallback);
nk.nkgdi_window_create(&w1, 500, 500, "F1", 10, 10);
// Configure and create window 2
w2.allow_sizing = 1;
w2.allow_maximize = 1;
w2.allow_move = 1;
w2.has_titlebar = 1;
w2.cb_on_draw = @ptrCast(&drawCallback);
nk.nkgdi_window_create(&w2, 500, 500, "F2", 520, 10);
// Update both windows as long as valid
while (nk.nkgdi_window_update(&w1) != 0 and nk.nkgdi_window_update(&w2) != 0) {
win.kernel32.Sleep(10);
}
// Destroy both window contexts
nk.nkgdi_window_destroy(&w1);
nk.nkgdi_window_destroy(&w2);
// Properly shut down the gdi window framework
nk.nkgdi_window_shutdown();
return 0;
}
//#endregion ==================================================================
//#region MARK: UTIL
//=============================================================================
fn drawCallback(ctx: *nk.struct_nk_context) callconv(.c) c_int {
var set: i32 = 0;
var prev: i32 = 0;
var op: i32 = 0;
const numbers = "789456123";
const ops = "+-*/";
var a: f64 = 0;
var b: f64 = 0;
var current: *f64 = &a;
var solve: i32 = 0;
// Buffer for input
var buffer: [256]u8 = undefined;
var len: usize = 0;
// Input field
nk.nk_layout_row_dynamic(ctx, 35, 1);
//len = snprintf(buffer, 256, "%.2f", *current);
_ = nk.nk_edit_string(ctx, nk.NK_EDIT_SIMPLE, &buffer[0], @ptrCast(&len), buffer.len, nk.nk_filter_float);
buffer[len] = 0;
//*current = atof(buffer);}
// Layout for buttons
nk.nk_layout_row_dynamic(ctx, 35, 4);
var i: usize = 0;
while (i < 16) : (i += 1) {
if (i >= 12 and i < 15) {
if (i > 12) continue;
if (nk.nk_button_label(ctx, "C") == 1) {
a = 0;
b = 0;
op = 0;
current = &a;
set = 0;
}
if (nk.nk_button_label(ctx, "0") == 1) {
current.* *= 10.0;
set = 0;
}
if (nk.nk_button_label(ctx, "=") == 1) {
solve = 1;
prev = op;
op = 0;
}
} else if ((i + 1) % 4 != 0) {
if (nk.nk_button_text(ctx, &numbers[(i / 4) * 3 + i % 4], 1) == 1) {
current.* = current.* * 10.0 + @as(f64, @floatFromInt(numbers[(i / 4) * 3 + i % 4] - '0'));
set = 0;
}
} else if (nk.nk_button_text(ctx, &ops[i / 4], 1) == 1) {
if (set == 0) {
if (current != &b) {
current = &b;
} else {
prev = op;
solve = 1;
}
}
op = ops[i / 4];
set = 1;
}
}
// Solve operation
if (solve != 0) {
if (prev == '+') a = a + b;
if (prev == '-') a = a - b;
if (prev == '*') a = a * b;
if (prev == '/') a = a / b;
current = &a;
if (set != 0) current = &b;
b = 0;
set = 0;
}
return 1;
}
// Fix for libc linking error.
pub export fn WinMain(hInstance: win.HINSTANCE, hPrevInstance: ?win.HINSTANCE,
pCmdLine: ?win.LPWSTR, nCmdShow: win.INT) callconv(.winapi) win.INT {
return wWinMain(hInstance, hPrevInstance, pCmdLine.?, nCmdShow);
}
//#endregion ==================================================================
//#region MARK: TEST
//=============================================================================
test " empty" {
try std.testing.expect(true);
}
//#endregion ==================================================================
//=============================================================================