diff --git a/src/gui/GuiWindow.zig b/src/gui/GuiWindow.zig index 50694443..54418746 100644 --- a/src/gui/GuiWindow.zig +++ b/src/gui/GuiWindow.zig @@ -90,13 +90,13 @@ pub fn __init() !void { graphics.c.glUniform1i(windowUniforms.image, 0); backgroundTexture = Texture.init(); - const backgroundImage = try Image.readFromFile(main.threadAllocator, "assets/cubyz/ui/window_background.png"); - defer backgroundImage.deinit(main.threadAllocator); + const backgroundImage = try Image.readFromFile(gui.allocator, "assets/cubyz/ui/window_background.png"); + defer backgroundImage.deinit(gui.allocator); try backgroundTexture.generate(backgroundImage); titleTexture = Texture.init(); - const titleImage = try Image.readFromFile(main.threadAllocator, "assets/cubyz/ui/window_title.png"); - defer titleImage.deinit(main.threadAllocator); + const titleImage = try Image.readFromFile(gui.allocator, "assets/cubyz/ui/window_title.png"); + defer titleImage.deinit(gui.allocator); try titleTexture.generate(titleImage); } @@ -421,7 +421,7 @@ pub fn render(self: *const GuiWindow) !void { draw.restoreTranslation(oldTranslation); draw.restoreScale(oldScale); if(self.showTitleBar) { - var text = try graphics.TextBuffer.init(main.threadAllocator, self.title, .{}, false, .center); + var text = try graphics.TextBuffer.init(gui.allocator, self.title, .{}, false, .center); defer text.deinit(); const titleDimension = try text.calculateLineBreaks(16*scale, self.size[0]*scale); try text.render(self.pos[0] + self.size[0]*scale/2 - titleDimension[0]/2, self.pos[1], 16*scale); diff --git a/src/gui/components/Button.zig b/src/gui/components/Button.zig index ebff275f..12990442 100644 --- a/src/gui/components/Button.zig +++ b/src/gui/components/Button.zig @@ -56,8 +56,8 @@ pub fn __deinit() void { fn defaultOnAction() void {} -pub fn init(allocator: Allocator, pos: Vec2f, width: f32, text: []const u8, onAction: ?*const fn() void) Allocator.Error!GuiComponent { - const labelComponent = try Label.init(allocator, undefined, width - 3*border, text, .center); +pub fn init(pos: Vec2f, width: f32, text: []const u8, onAction: ?*const fn() void) Allocator.Error!GuiComponent { + const labelComponent = try Label.init(undefined, width - 3*border, text, .center); var self = Button { .onAction = if(onAction) |a| a else &defaultOnAction, .label = labelComponent.impl.label, diff --git a/src/gui/components/CheckBox.zig b/src/gui/components/CheckBox.zig index 76ef4334..6bf1be02 100644 --- a/src/gui/components/CheckBox.zig +++ b/src/gui/components/CheckBox.zig @@ -49,8 +49,8 @@ pub fn __deinit() void { textureEmpty.deinit(); } -pub fn init(allocator: Allocator, pos: Vec2f, width: f32, text: []const u8, initialValue: bool, onAction: *const fn(bool) void) Allocator.Error!GuiComponent { - const labelComponent = try Label.init(allocator, undefined, width - 3*border - boxSize, text, .left); +pub fn init(pos: Vec2f, width: f32, text: []const u8, initialValue: bool, onAction: *const fn(bool) void) Allocator.Error!GuiComponent { + const labelComponent = try Label.init(undefined, width - 3*border - boxSize, text, .left); var self = CheckBox { .state = initialValue, .onAction = onAction, diff --git a/src/gui/components/Label.zig b/src/gui/components/Label.zig index f73a0d42..bd312cce 100644 --- a/src/gui/components/Label.zig +++ b/src/gui/components/Label.zig @@ -18,9 +18,9 @@ const fontSize: f32 = 16; text: TextBuffer, textSize: Vec2f = undefined, -pub fn init(allocator: Allocator, pos: Vec2f, maxWidth: f32, text: []const u8, alignment: TextBuffer.Alignment) Allocator.Error!GuiComponent { +pub fn init(pos: Vec2f, maxWidth: f32, text: []const u8, alignment: TextBuffer.Alignment) Allocator.Error!GuiComponent { var self = Label { - .text = try TextBuffer.init(allocator, text, .{}, false, alignment), + .text = try TextBuffer.init(gui.allocator, text, .{}, false, alignment), }; self.textSize = try self.text.calculateLineBreaks(fontSize, maxWidth); return GuiComponent { diff --git a/src/gui/components/Slider.zig b/src/gui/components/Slider.zig index deb7a280..2fca0343 100644 --- a/src/gui/components/Slider.zig +++ b/src/gui/components/Slider.zig @@ -24,7 +24,6 @@ const fontSize: f32 = 16; var texture: Texture = undefined; -allocator: Allocator, callback: *const fn(u16) void, currentSelection: u16, text: []const u8, @@ -48,21 +47,20 @@ pub fn __deinit() void { texture.deinit(); } -pub fn init(allocator: Allocator, pos: Vec2f, width: f32, text: []const u8, comptime fmt: []const u8, valueList: anytype, initialValue: u16, callback: *const fn(u16) void) Allocator.Error!GuiComponent { - var values = try allocator.alloc([]const u8, valueList.len); +pub fn init(pos: Vec2f, width: f32, text: []const u8, comptime fmt: []const u8, valueList: anytype, initialValue: u16, callback: *const fn(u16) void) Allocator.Error!GuiComponent { + var values = try gui.allocator.alloc([]const u8, valueList.len); var maxLen: usize = 0; for(valueList, 0..) |value, i| { - values[i] = try std.fmt.allocPrint(allocator, fmt, .{value}); + values[i] = try std.fmt.allocPrint(gui.allocator, fmt, .{value}); maxLen = @max(maxLen, values[i].len); } - const initialText = try allocator.alloc(u8, text.len + maxLen); + const initialText = try gui.allocator.alloc(u8, text.len + maxLen); std.mem.copy(u8, initialText, text); std.mem.set(u8, initialText[text.len..], ' '); - const labelComponent = try Label.init(allocator, undefined, width - 3*border, initialText, .center); - const buttonComponent = try Button.init(allocator, undefined, undefined, "", null); + const labelComponent = try Label.init(undefined, width - 3*border, initialText, .center); + const buttonComponent = try Button.init(undefined, undefined, "", null); var self = Slider { - .allocator = allocator, .callback = callback, .currentSelection = initialValue, .text = text, @@ -87,10 +85,10 @@ pub fn deinit(self: Slider) void { self.label.deinit(); self.button.deinit(); for(self.values) |value| { - self.allocator.free(value); + gui.allocator.free(value); } - self.allocator.free(self.values); - self.allocator.free(self.currentText); + gui.allocator.free(self.values); + gui.allocator.free(self.currentText); } fn setButtonPosFromValue(self: *Slider, size: Vec2f) !void { @@ -100,11 +98,11 @@ fn setButtonPosFromValue(self: *Slider, size: Vec2f) !void { } fn updateLabel(self: *Slider, newValue: []const u8, width: f32) !void { - self.allocator.free(self.currentText); - self.currentText = try self.allocator.alloc(u8, newValue.len + self.text.len); + gui.allocator.free(self.currentText); + self.currentText = try gui.allocator.alloc(u8, newValue.len + self.text.len); std.mem.copy(u8, self.currentText, self.text); std.mem.copy(u8, self.currentText[self.text.len..], newValue); - const labelComponent = try Label.init(self.allocator, undefined, width - 3*border, self.currentText, .center); + const labelComponent = try Label.init(undefined, width - 3*border, self.currentText, .center); self.label.deinit(); self.label = labelComponent.impl.label; } diff --git a/src/gui/components/TextInput.zig b/src/gui/components/TextInput.zig index 03516205..85a479a7 100644 --- a/src/gui/components/TextInput.zig +++ b/src/gui/components/TextInput.zig @@ -41,10 +41,10 @@ pub fn __deinit() void { // TODO: Make this scrollable. -pub fn init(allocator: Allocator, pos: Vec2f, maxWidth: f32, text: []const u8) Allocator.Error!GuiComponent { +pub fn init(pos: Vec2f, maxWidth: f32, text: []const u8) Allocator.Error!GuiComponent { var self = TextInput { - .currentString = std.ArrayList(u8).init(allocator), - .textBuffer = try TextBuffer.init(allocator, text, .{}, true, .left), + .currentString = std.ArrayList(u8).init(gui.allocator), + .textBuffer = try TextBuffer.init(gui.allocator, text, .{}, true, .left), .maxWidth = maxWidth, }; try self.currentString.appendSlice(text); @@ -85,7 +85,7 @@ pub fn deselect(self: *TextInput) void { fn reloadText(self: *TextInput) !void { self.textBuffer.deinit(); - self.textBuffer = try TextBuffer.init(self.currentString.allocator, self.currentString.items, .{}, true, .left); + self.textBuffer = try TextBuffer.init(gui.allocator, self.currentString.items, .{}, true, .left); self.textSize = try self.textBuffer.calculateLineBreaks(fontSize, self.maxWidth); } diff --git a/src/gui/components/VerticalList.zig b/src/gui/components/VerticalList.zig index 86de3722..0505a564 100644 --- a/src/gui/components/VerticalList.zig +++ b/src/gui/components/VerticalList.zig @@ -17,9 +17,9 @@ children: std.ArrayList(GuiComponent), currentOffset: f32 = 0, maxWidth: f32 = 0, -pub fn init(allocator: Allocator) Allocator.Error!VerticalList { +pub fn init() Allocator.Error!VerticalList { const self = VerticalList { - .children = std.ArrayList(GuiComponent).init(allocator), + .children = std.ArrayList(GuiComponent).init(gui.allocator), }; return self; } diff --git a/src/gui/gui.zig b/src/gui/gui.zig index ba9058bb..ee06a24e 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -23,10 +23,13 @@ pub var openWindows: std.ArrayList(*GuiWindow) = undefined; pub var selectedWindow: ?*GuiWindow = null; // TODO: Make private. pub var selectedTextInput: ?*TextInput = null; -pub fn init() !void { - windowList = std.ArrayList(*GuiWindow).init(main.globalAllocator); - hudWindows = std.ArrayList(*GuiWindow).init(main.globalAllocator); - openWindows = std.ArrayList(*GuiWindow).init(main.globalAllocator); +pub var allocator: Allocator = undefined; + +pub fn init(_allocator: Allocator) !void { + allocator = _allocator; + windowList = std.ArrayList(*GuiWindow).init(allocator); + hudWindows = std.ArrayList(*GuiWindow).init(allocator); + openWindows = std.ArrayList(*GuiWindow).init(allocator); inline for(@typeInfo(windowlist).Struct.decls) |decl| { try @field(windowlist, decl.name).init(); } diff --git a/src/gui/windows/change_name.zig b/src/gui/windows/change_name.zig index b0af10ee..43b1a7c0 100644 --- a/src/gui/windows/change_name.zig +++ b/src/gui/windows/change_name.zig @@ -28,14 +28,10 @@ pub fn init() !void { const padding: f32 = 8; pub fn onOpen() Allocator.Error!void { - var list = try VerticalList.init(main.globalAllocator); + var list = try VerticalList.init(); // TODO Please change your name bla bla - try list.add(try TextInput.init(main.globalAllocator, .{0, 16}, 128, "gr da jkwa hfeka fuei \n ofuiewo\natg78o4ea74e8t\nz57 t4738qa0 47a80 t47803a t478aqv t487 5t478a0 tg478a09 t748ao t7489a rt4e5 okv5895 678v54vgvo6r z8or z578v rox74et8ys9otv 4z3789so z4oa9t z489saoyt z")); - // TODO - //try list.add(try Button.init(.{0, 16}, 128, main.globalAllocator, "Singleplayer TODO", &buttonCallbackTest)); - //try list.add(try Button.init(.{0, 16}, 128, main.globalAllocator, "Multiplayer TODO", &buttonCallbackTest)); - //try list.add(try Button.init(.{0, 16}, 128, main.globalAllocator, "Settings", gui.openWindowFunction("cubyz:settings"))); - //try list.add(try Button.init(.{0, 16}, 128, main.globalAllocator, "Exit TODO", &buttonCallbackTest)); + try list.add(try TextInput.init(.{0, 16}, 128, "gr da jkwa hfeka fuei \n ofuiewo\natg78o4ea74e8t\nz57 t4738qa0 47a80 t47803a t478aqv t487 5t478a0 tg478a09 t748ao t7489a rt4e5 okv5895 678v54vgvo6r z8or z578v rox74et8ys9otv 4z3789so z4oa9t z489saoyt z")); + // TODO: Done button. components[0] = list.toComponent(.{padding, padding}); window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding)); gui.updateWindowPositions(); diff --git a/src/gui/windows/controls.zig b/src/gui/windows/controls.zig index 25243d89..e6d89902 100644 --- a/src/gui/windows/controls.zig +++ b/src/gui/windows/controls.zig @@ -49,14 +49,14 @@ fn keypressListener(key: c_int, mouseButton: c_int, scancode: c_int) void { } pub fn onOpen() Allocator.Error!void { - var list = try VerticalList.init(main.globalAllocator); + var list = try VerticalList.init(); list.currentOffset = 8; inline for(comptime std.meta.fieldNames(@TypeOf(main.keyboard))) |field| { - var label = try Label.init(main.globalAllocator, .{0, 8}, 128, field, .left); + var label = try Label.init(.{0, 8}, 128, field, .left); var button = if(&@field(main.keyboard, field) == selectedKey) ( - try Button.init(main.globalAllocator, .{128 + 16, 8}, 128, "...", null) + try Button.init(.{128 + 16, 8}, 128, "...", null) ) else ( - try Button.init(main.globalAllocator, .{128 + 16, 8}, 128, @field(main.keyboard, field).getName(), &functionBuilder(field)) + try Button.init(.{128 + 16, 8}, 128, @field(main.keyboard, field).getName(), &functionBuilder(field)) ); if(label.size[1] > button.size[1]) { button.pos[1] += (label.size[1] - button.size[1])/2; diff --git a/src/gui/windows/graphics.zig b/src/gui/windows/graphics.zig index d53c1384..87fd1c21 100644 --- a/src/gui/windows/graphics.zig +++ b/src/gui/windows/graphics.zig @@ -47,14 +47,14 @@ fn vsyncCallback(newValue: bool) void { } pub fn onOpen() Allocator.Error!void { - var list = try VerticalList.init(main.globalAllocator); + var list = try VerticalList.init(); const renderDistances = [_]u32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; - try list.add(try Slider.init(main.globalAllocator, .{0, 16}, 128, "#ffffffRender Distance: ", "{}", &renderDistances, settings.renderDistance - 1, &renderDistanceCallback)); + try list.add(try Slider.init(.{0, 16}, 128, "#ffffffRender Distance: ", "{}", &renderDistances, settings.renderDistance - 1, &renderDistanceCallback)); const LODFactors = [_]f32{0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8}; - try list.add(try Slider.init(main.globalAllocator, .{0, 16}, 128, "#ffffffLOD Factor: ", "{d:.1}", &LODFactors, @floatToInt(u16, settings.LODFactor*2) - 1, &LODFactorCallback)); + try list.add(try Slider.init(.{0, 16}, 128, "#ffffffLOD Factor: ", "{d:.1}", &LODFactors, @floatToInt(u16, settings.LODFactor*2) - 1, &LODFactorCallback)); // TODO: fog? - try list.add(try CheckBox.init(main.globalAllocator, .{0, 16}, 128, "Bloom", settings.bloom, &bloomCallback)); - try list.add(try CheckBox.init(main.globalAllocator, .{0, 16}, 128, "Vertical Synchronization", settings.vsync, &vsyncCallback)); + try list.add(try CheckBox.init(.{0, 16}, 128, "Bloom", settings.bloom, &bloomCallback)); + try list.add(try CheckBox.init(.{0, 16}, 128, "Vertical Synchronization", settings.vsync, &vsyncCallback)); components[0] = list.toComponent(.{padding, padding}); window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding)); gui.updateWindowPositions(); diff --git a/src/gui/windows/main.zig b/src/gui/windows/main.zig index 55a8922d..e71a5e2a 100644 --- a/src/gui/windows/main.zig +++ b/src/gui/windows/main.zig @@ -30,11 +30,11 @@ pub fn buttonCallbackTest() void { const padding: f32 = 8; pub fn onOpen() Allocator.Error!void { - var list = try VerticalList.init(main.globalAllocator); - try list.add(try Button.init(main.globalAllocator, .{0, 16}, 128, "Singleplayer TODO", &buttonCallbackTest)); - try list.add(try Button.init(main.globalAllocator, .{0, 16}, 128, "Multiplayer TODO", &buttonCallbackTest)); - try list.add(try Button.init(main.globalAllocator, .{0, 16}, 128, "Settings", gui.openWindowFunction("cubyz:settings"))); - try list.add(try Button.init(main.globalAllocator, .{0, 16}, 128, "Exit TODO", &buttonCallbackTest)); + var list = try VerticalList.init(); + try list.add(try Button.init(.{0, 16}, 128, "Singleplayer TODO", &buttonCallbackTest)); + try list.add(try Button.init(.{0, 16}, 128, "Multiplayer TODO", &buttonCallbackTest)); + try list.add(try Button.init(.{0, 16}, 128, "Settings", gui.openWindowFunction("cubyz:settings"))); + try list.add(try Button.init(.{0, 16}, 128, "Exit TODO", &buttonCallbackTest)); components[0] = list.toComponent(.{padding, padding}); window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding)); gui.updateWindowPositions(); diff --git a/src/gui/windows/settings.zig b/src/gui/windows/settings.zig index 2024bbc0..48fe2a59 100644 --- a/src/gui/windows/settings.zig +++ b/src/gui/windows/settings.zig @@ -27,11 +27,11 @@ pub fn init() !void { const padding: f32 = 8; pub fn onOpen() Allocator.Error!void { - var list = try VerticalList.init(main.globalAllocator); - try list.add(try Button.init(main.globalAllocator, .{0, 16}, 128, "Graphics", gui.openWindowFunction("cubyz:graphics"))); - try list.add(try Button.init(main.globalAllocator, .{0, 16}, 128, "Sound", gui.openWindowFunction("cubyz:sound"))); - try list.add(try Button.init(main.globalAllocator, .{0, 16}, 128, "Controls", gui.openWindowFunction("cubyz:controls"))); - try list.add(try Button.init(main.globalAllocator, .{0, 16}, 128, "Change Name", gui.openWindowFunction("cubyz:change_name"))); + var list = try VerticalList.init(); + try list.add(try Button.init(.{0, 16}, 128, "Graphics", gui.openWindowFunction("cubyz:graphics"))); + try list.add(try Button.init(.{0, 16}, 128, "Sound", gui.openWindowFunction("cubyz:sound"))); + try list.add(try Button.init(.{0, 16}, 128, "Controls", gui.openWindowFunction("cubyz:controls"))); + try list.add(try Button.init(.{0, 16}, 128, "Change Name", gui.openWindowFunction("cubyz:change_name"))); components[0] = list.toComponent(.{padding, padding}); window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding)); gui.updateWindowPositions(); diff --git a/src/gui/windows/sound.zig b/src/gui/windows/sound.zig index c3ba1ff0..46e9163b 100644 --- a/src/gui/windows/sound.zig +++ b/src/gui/windows/sound.zig @@ -27,12 +27,8 @@ pub fn init() !void { const padding: f32 = 8; pub fn onOpen() Allocator.Error!void { - var list = try VerticalList.init(main.globalAllocator); + var list = try VerticalList.init(); // TODO - //try list.add(try Button.init(.{0, 16}, 128, main.globalAllocator, "Singleplayer TODO", &buttonCallbackTest)); - //try list.add(try Button.init(.{0, 16}, 128, main.globalAllocator, "Multiplayer TODO", &buttonCallbackTest)); - //try list.add(try Button.init(.{0, 16}, 128, main.globalAllocator, "Settings", gui.openWindowFunction("cubyz:settings"))); - //try list.add(try Button.init(.{0, 16}, 128, main.globalAllocator, "Exit TODO", &buttonCallbackTest)); components[0] = list.toComponent(.{padding, padding}); window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding)); gui.updateWindowPositions(); diff --git a/src/main.zig b/src/main.zig index df645a21..58fdecdc 100644 --- a/src/main.zig +++ b/src/main.zig @@ -450,7 +450,7 @@ pub fn main() !void { try graphics.init(); defer graphics.deinit(); - try gui.init(); + try gui.init(globalAllocator); defer gui.deinit(); try gui.openWindow("cubyz:hotbar"); try gui.openWindow("cubyz:hotbar2");