Refactor: Use one global allocator for all gui related stuff.

This commit is contained in:
IntegratedQuantum 2023-03-10 17:24:36 +01:00
parent 1c12ba3459
commit 44eed8026d
15 changed files with 60 additions and 67 deletions

View File

@ -90,13 +90,13 @@ pub fn __init() !void {
graphics.c.glUniform1i(windowUniforms.image, 0); graphics.c.glUniform1i(windowUniforms.image, 0);
backgroundTexture = Texture.init(); backgroundTexture = Texture.init();
const backgroundImage = try Image.readFromFile(main.threadAllocator, "assets/cubyz/ui/window_background.png"); const backgroundImage = try Image.readFromFile(gui.allocator, "assets/cubyz/ui/window_background.png");
defer backgroundImage.deinit(main.threadAllocator); defer backgroundImage.deinit(gui.allocator);
try backgroundTexture.generate(backgroundImage); try backgroundTexture.generate(backgroundImage);
titleTexture = Texture.init(); titleTexture = Texture.init();
const titleImage = try Image.readFromFile(main.threadAllocator, "assets/cubyz/ui/window_title.png"); const titleImage = try Image.readFromFile(gui.allocator, "assets/cubyz/ui/window_title.png");
defer titleImage.deinit(main.threadAllocator); defer titleImage.deinit(gui.allocator);
try titleTexture.generate(titleImage); try titleTexture.generate(titleImage);
} }
@ -421,7 +421,7 @@ pub fn render(self: *const GuiWindow) !void {
draw.restoreTranslation(oldTranslation); draw.restoreTranslation(oldTranslation);
draw.restoreScale(oldScale); draw.restoreScale(oldScale);
if(self.showTitleBar) { 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(); defer text.deinit();
const titleDimension = try text.calculateLineBreaks(16*scale, self.size[0]*scale); 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); try text.render(self.pos[0] + self.size[0]*scale/2 - titleDimension[0]/2, self.pos[1], 16*scale);

View File

@ -56,8 +56,8 @@ pub fn __deinit() void {
fn defaultOnAction() void {} fn defaultOnAction() void {}
pub fn init(allocator: Allocator, pos: Vec2f, width: f32, text: []const u8, onAction: ?*const fn() void) Allocator.Error!GuiComponent { pub fn init(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); const labelComponent = try Label.init(undefined, width - 3*border, text, .center);
var self = Button { var self = Button {
.onAction = if(onAction) |a| a else &defaultOnAction, .onAction = if(onAction) |a| a else &defaultOnAction,
.label = labelComponent.impl.label, .label = labelComponent.impl.label,

View File

@ -49,8 +49,8 @@ pub fn __deinit() void {
textureEmpty.deinit(); textureEmpty.deinit();
} }
pub fn init(allocator: Allocator, pos: Vec2f, width: f32, text: []const u8, initialValue: bool, onAction: *const fn(bool) void) Allocator.Error!GuiComponent { 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(allocator, undefined, width - 3*border - boxSize, text, .left); const labelComponent = try Label.init(undefined, width - 3*border - boxSize, text, .left);
var self = CheckBox { var self = CheckBox {
.state = initialValue, .state = initialValue,
.onAction = onAction, .onAction = onAction,

View File

@ -18,9 +18,9 @@ const fontSize: f32 = 16;
text: TextBuffer, text: TextBuffer,
textSize: Vec2f = undefined, 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 { 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); self.textSize = try self.text.calculateLineBreaks(fontSize, maxWidth);
return GuiComponent { return GuiComponent {

View File

@ -24,7 +24,6 @@ const fontSize: f32 = 16;
var texture: Texture = undefined; var texture: Texture = undefined;
allocator: Allocator,
callback: *const fn(u16) void, callback: *const fn(u16) void,
currentSelection: u16, currentSelection: u16,
text: []const u8, text: []const u8,
@ -48,21 +47,20 @@ pub fn __deinit() void {
texture.deinit(); 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 { 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 allocator.alloc([]const u8, valueList.len); var values = try gui.allocator.alloc([]const u8, valueList.len);
var maxLen: usize = 0; var maxLen: usize = 0;
for(valueList, 0..) |value, i| { 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); 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.copy(u8, initialText, text);
std.mem.set(u8, initialText[text.len..], ' '); std.mem.set(u8, initialText[text.len..], ' ');
const labelComponent = try Label.init(allocator, undefined, width - 3*border, initialText, .center); const labelComponent = try Label.init(undefined, width - 3*border, initialText, .center);
const buttonComponent = try Button.init(allocator, undefined, undefined, "", null); const buttonComponent = try Button.init(undefined, undefined, "", null);
var self = Slider { var self = Slider {
.allocator = allocator,
.callback = callback, .callback = callback,
.currentSelection = initialValue, .currentSelection = initialValue,
.text = text, .text = text,
@ -87,10 +85,10 @@ pub fn deinit(self: Slider) void {
self.label.deinit(); self.label.deinit();
self.button.deinit(); self.button.deinit();
for(self.values) |value| { for(self.values) |value| {
self.allocator.free(value); gui.allocator.free(value);
} }
self.allocator.free(self.values); gui.allocator.free(self.values);
self.allocator.free(self.currentText); gui.allocator.free(self.currentText);
} }
fn setButtonPosFromValue(self: *Slider, size: Vec2f) !void { 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 { fn updateLabel(self: *Slider, newValue: []const u8, width: f32) !void {
self.allocator.free(self.currentText); gui.allocator.free(self.currentText);
self.currentText = try self.allocator.alloc(u8, newValue.len + self.text.len); 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);
std.mem.copy(u8, self.currentText[self.text.len..], newValue); 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.deinit();
self.label = labelComponent.impl.label; self.label = labelComponent.impl.label;
} }

View File

@ -41,10 +41,10 @@ pub fn __deinit() void {
// TODO: Make this scrollable. // 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 { var self = TextInput {
.currentString = std.ArrayList(u8).init(allocator), .currentString = std.ArrayList(u8).init(gui.allocator),
.textBuffer = try TextBuffer.init(allocator, text, .{}, true, .left), .textBuffer = try TextBuffer.init(gui.allocator, text, .{}, true, .left),
.maxWidth = maxWidth, .maxWidth = maxWidth,
}; };
try self.currentString.appendSlice(text); try self.currentString.appendSlice(text);
@ -85,7 +85,7 @@ pub fn deselect(self: *TextInput) void {
fn reloadText(self: *TextInput) !void { fn reloadText(self: *TextInput) !void {
self.textBuffer.deinit(); 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); self.textSize = try self.textBuffer.calculateLineBreaks(fontSize, self.maxWidth);
} }

View File

@ -17,9 +17,9 @@ children: std.ArrayList(GuiComponent),
currentOffset: f32 = 0, currentOffset: f32 = 0,
maxWidth: f32 = 0, maxWidth: f32 = 0,
pub fn init(allocator: Allocator) Allocator.Error!VerticalList { pub fn init() Allocator.Error!VerticalList {
const self = VerticalList { const self = VerticalList {
.children = std.ArrayList(GuiComponent).init(allocator), .children = std.ArrayList(GuiComponent).init(gui.allocator),
}; };
return self; return self;
} }

View File

@ -23,10 +23,13 @@ pub var openWindows: std.ArrayList(*GuiWindow) = undefined;
pub var selectedWindow: ?*GuiWindow = null; // TODO: Make private. pub var selectedWindow: ?*GuiWindow = null; // TODO: Make private.
pub var selectedTextInput: ?*TextInput = null; pub var selectedTextInput: ?*TextInput = null;
pub fn init() !void { pub var allocator: Allocator = undefined;
windowList = std.ArrayList(*GuiWindow).init(main.globalAllocator);
hudWindows = std.ArrayList(*GuiWindow).init(main.globalAllocator); pub fn init(_allocator: Allocator) !void {
openWindows = std.ArrayList(*GuiWindow).init(main.globalAllocator); 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| { inline for(@typeInfo(windowlist).Struct.decls) |decl| {
try @field(windowlist, decl.name).init(); try @field(windowlist, decl.name).init();
} }

View File

@ -28,14 +28,10 @@ pub fn init() !void {
const padding: f32 = 8; const padding: f32 = 8;
pub fn onOpen() Allocator.Error!void { 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 // 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")); 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 // TODO: Done button.
//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}); components[0] = list.toComponent(.{padding, padding});
window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding)); window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding));
gui.updateWindowPositions(); gui.updateWindowPositions();

View File

@ -49,14 +49,14 @@ fn keypressListener(key: c_int, mouseButton: c_int, scancode: c_int) void {
} }
pub fn onOpen() Allocator.Error!void { pub fn onOpen() Allocator.Error!void {
var list = try VerticalList.init(main.globalAllocator); var list = try VerticalList.init();
list.currentOffset = 8; list.currentOffset = 8;
inline for(comptime std.meta.fieldNames(@TypeOf(main.keyboard))) |field| { 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) ( 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 ( ) 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]) { if(label.size[1] > button.size[1]) {
button.pos[1] += (label.size[1] - button.size[1])/2; button.pos[1] += (label.size[1] - button.size[1])/2;

View File

@ -47,14 +47,14 @@ fn vsyncCallback(newValue: bool) void {
} }
pub fn onOpen() Allocator.Error!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}; 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}; 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? // TODO: fog?
try list.add(try CheckBox.init(main.globalAllocator, .{0, 16}, 128, "Bloom", settings.bloom, &bloomCallback)); try list.add(try CheckBox.init(.{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, "Vertical Synchronization", settings.vsync, &vsyncCallback));
components[0] = list.toComponent(.{padding, padding}); components[0] = list.toComponent(.{padding, padding});
window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding)); window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding));
gui.updateWindowPositions(); gui.updateWindowPositions();

View File

@ -30,11 +30,11 @@ pub fn buttonCallbackTest() void {
const padding: f32 = 8; const padding: f32 = 8;
pub fn onOpen() Allocator.Error!void { pub fn onOpen() Allocator.Error!void {
var list = try VerticalList.init(main.globalAllocator); var list = try VerticalList.init();
try list.add(try Button.init(main.globalAllocator, .{0, 16}, 128, "Singleplayer TODO", &buttonCallbackTest)); try list.add(try Button.init(.{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(.{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(.{0, 16}, 128, "Settings", gui.openWindowFunction("cubyz:settings")));
try list.add(try Button.init(main.globalAllocator, .{0, 16}, 128, "Exit TODO", &buttonCallbackTest)); try list.add(try Button.init(.{0, 16}, 128, "Exit TODO", &buttonCallbackTest));
components[0] = list.toComponent(.{padding, padding}); components[0] = list.toComponent(.{padding, padding});
window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding)); window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding));
gui.updateWindowPositions(); gui.updateWindowPositions();

View File

@ -27,11 +27,11 @@ pub fn init() !void {
const padding: f32 = 8; const padding: f32 = 8;
pub fn onOpen() Allocator.Error!void { pub fn onOpen() Allocator.Error!void {
var list = try VerticalList.init(main.globalAllocator); var list = try VerticalList.init();
try list.add(try Button.init(main.globalAllocator, .{0, 16}, 128, "Graphics", gui.openWindowFunction("cubyz:graphics"))); try list.add(try Button.init(.{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(.{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(.{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"))); try list.add(try Button.init(.{0, 16}, 128, "Change Name", gui.openWindowFunction("cubyz:change_name")));
components[0] = list.toComponent(.{padding, padding}); components[0] = list.toComponent(.{padding, padding});
window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding)); window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding));
gui.updateWindowPositions(); gui.updateWindowPositions();

View File

@ -27,12 +27,8 @@ pub fn init() !void {
const padding: f32 = 8; const padding: f32 = 8;
pub fn onOpen() Allocator.Error!void { pub fn onOpen() Allocator.Error!void {
var list = try VerticalList.init(main.globalAllocator); var list = try VerticalList.init();
// TODO // 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}); components[0] = list.toComponent(.{padding, padding});
window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding)); window.contentSize = components[0].size + @splat(2, @as(f32, 2*padding));
gui.updateWindowPositions(); gui.updateWindowPositions();

View File

@ -450,7 +450,7 @@ pub fn main() !void {
try graphics.init(); try graphics.init();
defer graphics.deinit(); defer graphics.deinit();
try gui.init(); try gui.init(globalAllocator);
defer gui.deinit(); defer gui.deinit();
try gui.openWindow("cubyz:hotbar"); try gui.openWindow("cubyz:hotbar");
try gui.openWindow("cubyz:hotbar2"); try gui.openWindow("cubyz:hotbar2");