From 663b8b8e83d874e9d56529104acf527fa6efcb48 Mon Sep 17 00:00:00 2001 From: somatcha Date: Wed, 4 Dec 2024 21:12:18 +0530 Subject: [PATCH 01/15] fix: empty host ip in multiplayer --- src/gui/windows/multiplayer.zig | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index b5f0a0bc..4a51f9d0 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -56,14 +56,17 @@ fn join(_: usize) void { ipAddress = ""; } if(connection) |_connection| { + if (ipAddressEntry.currentString.items.len == 0) { + // TODO: Show error message in the GUI + std.log.err("IP address cannot be empty", .{}); + return; + } _connection.world = &main.game.testWorld; main.globalAllocator.free(settings.lastUsedIPAddress); settings.lastUsedIPAddress = main.globalAllocator.dupe(u8, ipAddressEntry.currentString.items); settings.save(); main.game.world = &main.game.testWorld; - main.game.testWorld.init(ipAddressEntry.currentString.items, _connection) catch |err| { - std.log.err("Encountered error while opening world: {s}", .{@errorName(err)}); - }; + std.log.info("Connecting to world: {s}", .{ipAddressEntry.currentString.items}); connection = null; } else { std.log.err("No connection found. Cannot connect.", .{}); From b7b388b94bd8429d1bc4e418fd1cac64fd36fff3 Mon Sep 17 00:00:00 2001 From: somatcha Date: Wed, 4 Dec 2024 21:51:33 +0530 Subject: [PATCH 02/15] fix: multiplayer conn init missing --- src/gui/windows/multiplayer.zig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index 4a51f9d0..38ad86ea 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -67,6 +67,9 @@ fn join(_: usize) void { settings.save(); main.game.world = &main.game.testWorld; std.log.info("Connecting to world: {s}", .{ipAddressEntry.currentString.items}); + main.game.testWorld.init(ipAddressEntry.currentString.items, _connection) catch |err| { + std.log.err("Encountered error while opening world: {s}", .{@errorName(err)}); + }; connection = null; } else { std.log.err("No connection found. Cannot connect.", .{}); From d658ef78896dfe1dd08a1333c285a1bb45f29570 Mon Sep 17 00:00:00 2001 From: somatcha Date: Thu, 5 Dec 2024 01:17:18 +0530 Subject: [PATCH 03/15] upd: error notif. window --- src/gui/GuiWindow.zig | 1 + src/gui/gui.zig | 9 ++++++++ src/gui/windows/_windowlist.zig | 1 + src/gui/windows/multiplayer.zig | 2 +- src/gui/windows/notification.zig | 38 ++++++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/gui/windows/notification.zig diff --git a/src/gui/GuiWindow.zig b/src/gui/GuiWindow.zig index 1021492e..582b37cf 100644 --- a/src/gui/GuiWindow.zig +++ b/src/gui/GuiWindow.zig @@ -62,6 +62,7 @@ hideIfMouseIsGrabbed: bool = true, // TODO: Allow the user to change this with a closeIfMouseIsGrabbed: bool = false, closeable: bool = true, isHud: bool = false, +text: []const u8 = "", /// Called every frame. renderFn: *const fn()void = &defaultFunction, diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 1fb62ad8..54b5e0b6 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -22,6 +22,7 @@ pub const GuiComponent = @import("gui_component.zig").GuiComponent; pub const GuiWindow = @import("GuiWindow.zig"); pub const windowlist = @import("windows/_windowlist.zig"); +pub const notification = @import("windows/notification.zig"); const GamepadCursor = @import("gamepad_cursor.zig"); var windowList: List(*GuiWindow) = undefined; @@ -373,6 +374,14 @@ pub fn closeWindow(id: []const u8) void { std.log.warn("Could not find window with id {s}.", .{id}); } +pub fn showNotification(message: []const u8) void { + notification.onOpen(); + openWindow("notification"); + if (getWindowById("notification")) |notificationWindow| { + notificationWindow.text = message; + } +} + pub fn setSelectedTextInput(newSelectedTextInput: ?*TextInput) void { if(selectedTextInput) |current| { if(current != newSelectedTextInput) { diff --git a/src/gui/windows/_windowlist.zig b/src/gui/windows/_windowlist.zig index d7a7e69f..de20ee21 100644 --- a/src/gui/windows/_windowlist.zig +++ b/src/gui/windows/_windowlist.zig @@ -29,3 +29,4 @@ pub const settings = @import("settings.zig"); pub const shared_inventory_testing = @import("shared_inventory_testing.zig"); pub const sound = @import("sound.zig"); pub const workbench = @import("workbench.zig"); +pub const notification = @import("notification.zig"); diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index 38ad86ea..4490d7e8 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -57,8 +57,8 @@ fn join(_: usize) void { } if(connection) |_connection| { if (ipAddressEntry.currentString.items.len == 0) { - // TODO: Show error message in the GUI std.log.err("IP address cannot be empty", .{}); + gui.showNotification("IP address cannot be empty"); return; } _connection.world = &main.game.testWorld; diff --git a/src/gui/windows/notification.zig b/src/gui/windows/notification.zig new file mode 100644 index 00000000..644afed9 --- /dev/null +++ b/src/gui/windows/notification.zig @@ -0,0 +1,38 @@ +const std = @import("std"); + +const main = @import("root"); +const Vec2f = main.vec.Vec2f; + +const gui = @import("../gui.zig"); +const GuiWindow = gui.GuiWindow; +const Button = @import("../components/Button.zig"); +const VerticalList = @import("../components/VerticalList.zig"); +const Label = @import("../components/Label.zig"); + +pub var window: GuiWindow = GuiWindow { + .contentSize = Vec2f{128, 256}, + .text = "", +}; + +const padding: f32 = 16; +const width: f32 = 128; + +fn ack(_: usize) void { + gui.closeWindowFromRef(&window); +} + +pub fn onOpen() void { + const list = VerticalList.init(.{padding, 16 + padding}, 300, 16); + list.add(Label.init(.{0, 0}, width, window.text, .center)); + list.add(Button.initText(.{0, 0}, 100, "OK", .{ .callback = &ack })); + list.finish(.center); + window.rootComponent = list.toComponent(); + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @as(Vec2f, @splat(padding)); + gui.updateWindowPositions(); +} + +pub fn onClose() void { + if(window.rootComponent) |*comp| { + comp.deinit(); + } +} From fa9cdf5af692c797dc5d243931f012cde9f2a84f Mon Sep 17 00:00:00 2001 From: somatcha Date: Thu, 5 Dec 2024 01:21:06 +0530 Subject: [PATCH 04/15] format fix --- src/gui/gui.zig | 4 ++-- src/gui/windows/notification.zig | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 54b5e0b6..92d716fa 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -378,8 +378,8 @@ pub fn showNotification(message: []const u8) void { notification.onOpen(); openWindow("notification"); if (getWindowById("notification")) |notificationWindow| { - notificationWindow.text = message; - } + notificationWindow.text = message; + } } pub fn setSelectedTextInput(newSelectedTextInput: ?*TextInput) void { diff --git a/src/gui/windows/notification.zig b/src/gui/windows/notification.zig index 644afed9..6a7b7db5 100644 --- a/src/gui/windows/notification.zig +++ b/src/gui/windows/notification.zig @@ -11,7 +11,7 @@ const Label = @import("../components/Label.zig"); pub var window: GuiWindow = GuiWindow { .contentSize = Vec2f{128, 256}, - .text = "", + .text = "", }; const padding: f32 = 16; @@ -27,7 +27,7 @@ pub fn onOpen() void { list.add(Button.initText(.{0, 0}, 100, "OK", .{ .callback = &ack })); list.finish(.center); window.rootComponent = list.toComponent(); - window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @as(Vec2f, @splat(padding)); + window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @as(Vec2f, @splat(padding)); gui.updateWindowPositions(); } From 96e39c66f65ff0485cf51a40cdf5cd336ebc1433 Mon Sep 17 00:00:00 2001 From: somatcha Date: Thu, 5 Dec 2024 02:19:43 +0530 Subject: [PATCH 05/15] fix: notification text global var --- src/gui/GuiWindow.zig | 1 - src/gui/gui.zig | 9 --------- src/gui/windows/_windowlist.zig | 4 ++-- src/gui/windows/multiplayer.zig | 5 +++-- src/gui/windows/notification.zig | 5 +++-- 5 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/gui/GuiWindow.zig b/src/gui/GuiWindow.zig index 582b37cf..1021492e 100644 --- a/src/gui/GuiWindow.zig +++ b/src/gui/GuiWindow.zig @@ -62,7 +62,6 @@ hideIfMouseIsGrabbed: bool = true, // TODO: Allow the user to change this with a closeIfMouseIsGrabbed: bool = false, closeable: bool = true, isHud: bool = false, -text: []const u8 = "", /// Called every frame. renderFn: *const fn()void = &defaultFunction, diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 92d716fa..1fb62ad8 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -22,7 +22,6 @@ pub const GuiComponent = @import("gui_component.zig").GuiComponent; pub const GuiWindow = @import("GuiWindow.zig"); pub const windowlist = @import("windows/_windowlist.zig"); -pub const notification = @import("windows/notification.zig"); const GamepadCursor = @import("gamepad_cursor.zig"); var windowList: List(*GuiWindow) = undefined; @@ -374,14 +373,6 @@ pub fn closeWindow(id: []const u8) void { std.log.warn("Could not find window with id {s}.", .{id}); } -pub fn showNotification(message: []const u8) void { - notification.onOpen(); - openWindow("notification"); - if (getWindowById("notification")) |notificationWindow| { - notificationWindow.text = message; - } -} - pub fn setSelectedTextInput(newSelectedTextInput: ?*TextInput) void { if(selectedTextInput) |current| { if(current != newSelectedTextInput) { diff --git a/src/gui/windows/_windowlist.zig b/src/gui/windows/_windowlist.zig index de20ee21..caa2433e 100644 --- a/src/gui/windows/_windowlist.zig +++ b/src/gui/windows/_windowlist.zig @@ -20,6 +20,7 @@ pub const invite = @import("invite.zig"); pub const main = @import("main.zig"); pub const manage_players = @import("manage_players.zig"); pub const multiplayer = @import("multiplayer.zig"); +pub const notification = @import("notification.zig"); pub const pause = @import("pause.zig"); pub const pause_gear = @import("pause_gear.zig"); pub const performance_graph = @import("performance_graph.zig"); @@ -28,5 +29,4 @@ pub const save_selection = @import("save_selection.zig"); pub const settings = @import("settings.zig"); pub const shared_inventory_testing = @import("shared_inventory_testing.zig"); pub const sound = @import("sound.zig"); -pub const workbench = @import("workbench.zig"); -pub const notification = @import("notification.zig"); +pub const workbench = @import("workbench.zig"); \ No newline at end of file diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index 4490d7e8..dcf9861c 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -58,7 +58,8 @@ fn join(_: usize) void { if(connection) |_connection| { if (ipAddressEntry.currentString.items.len == 0) { std.log.err("IP address cannot be empty", .{}); - gui.showNotification("IP address cannot be empty"); + gui.windowlist.notification.text = "IP address cannot be empty"; + gui.openWindow("notification"); return; } _connection.world = &main.game.testWorld; @@ -66,7 +67,7 @@ fn join(_: usize) void { settings.lastUsedIPAddress = main.globalAllocator.dupe(u8, ipAddressEntry.currentString.items); settings.save(); main.game.world = &main.game.testWorld; - std.log.info("Connecting to world: {s}", .{ipAddressEntry.currentString.items}); + std.log.info("Connecting to server: {s}", .{ipAddressEntry.currentString.items}); main.game.testWorld.init(ipAddressEntry.currentString.items, _connection) catch |err| { std.log.err("Encountered error while opening world: {s}", .{@errorName(err)}); }; diff --git a/src/gui/windows/notification.zig b/src/gui/windows/notification.zig index 6a7b7db5..8b82aab0 100644 --- a/src/gui/windows/notification.zig +++ b/src/gui/windows/notification.zig @@ -11,9 +11,10 @@ const Label = @import("../components/Label.zig"); pub var window: GuiWindow = GuiWindow { .contentSize = Vec2f{128, 256}, - .text = "", }; +pub var text: []const u8 = ""; + const padding: f32 = 16; const width: f32 = 128; @@ -23,7 +24,7 @@ fn ack(_: usize) void { pub fn onOpen() void { const list = VerticalList.init(.{padding, 16 + padding}, 300, 16); - list.add(Label.init(.{0, 0}, width, window.text, .center)); + list.add(Label.init(.{0, 0}, width, text, .center)); list.add(Button.initText(.{0, 0}, 100, "OK", .{ .callback = &ack })); list.finish(.center); window.rootComponent = list.toComponent(); From 1818e85e535bb0a7d135acb1f81bcdb3d05ddacb Mon Sep 17 00:00:00 2001 From: somatcha Date: Thu, 5 Dec 2024 10:43:55 +0530 Subject: [PATCH 06/15] fix: error notif. refresh --- src/gui/gui.zig | 3 +++ src/gui/windows/_windowlist.zig | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 1fb62ad8..582f6028 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -307,6 +307,9 @@ pub fn openWindow(id: []const u8) void { defer updateWindowPositions(); for(windowList.items) |window| { if(std.mem.eql(u8, window.id, id)) { + if (std.mem.eql(u8, id, "notification")) { + window.onOpenFn(); + } openWindowFromRef(window); return; } diff --git a/src/gui/windows/_windowlist.zig b/src/gui/windows/_windowlist.zig index caa2433e..b891221e 100644 --- a/src/gui/windows/_windowlist.zig +++ b/src/gui/windows/_windowlist.zig @@ -29,4 +29,4 @@ pub const save_selection = @import("save_selection.zig"); pub const settings = @import("settings.zig"); pub const shared_inventory_testing = @import("shared_inventory_testing.zig"); pub const sound = @import("sound.zig"); -pub const workbench = @import("workbench.zig"); \ No newline at end of file +pub const workbench = @import("workbench.zig"); From 86ba756843360fee3bb470d175a092f71c00ae3a Mon Sep 17 00:00:00 2001 From: somatcha Date: Thu, 5 Dec 2024 13:12:23 +0530 Subject: [PATCH 07/15] fix: max width --- src/gui/windows/multiplayer.zig | 4 ++++ src/gui/windows/notification.zig | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index dcf9861c..cd159016 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -70,10 +70,14 @@ fn join(_: usize) void { std.log.info("Connecting to server: {s}", .{ipAddressEntry.currentString.items}); main.game.testWorld.init(ipAddressEntry.currentString.items, _connection) catch |err| { std.log.err("Encountered error while opening world: {s}", .{@errorName(err)}); + gui.windowlist.notification.text = std.fmt.allocPrint(main.globalAllocator.allocator, "Encountered error while opening world: {}", .{err}) catch unreachable; + gui.openWindow("notification"); }; connection = null; } else { std.log.err("No connection found. Cannot connect.", .{}); + gui.windowlist.notification.text = "No connection found. Cannot connect."; + gui.openWindow("notification"); } for(gui.openWindows.items) |openWindow| { gui.closeWindowFromRef(openWindow); diff --git a/src/gui/windows/notification.zig b/src/gui/windows/notification.zig index 8b82aab0..bef95416 100644 --- a/src/gui/windows/notification.zig +++ b/src/gui/windows/notification.zig @@ -16,7 +16,7 @@ pub var window: GuiWindow = GuiWindow { pub var text: []const u8 = ""; const padding: f32 = 16; -const width: f32 = 128; +const width: f32 = 256; fn ack(_: usize) void { gui.closeWindowFromRef(&window); From b8e6356273d4c4a4ad7309a660848848a42778e3 Mon Sep 17 00:00:00 2001 From: somatcha Date: Fri, 6 Dec 2024 14:32:11 +0530 Subject: [PATCH 08/15] fix: raiseNotification fn --- src/gui/windows/multiplayer.zig | 20 +++++++++++++------- src/gui/windows/notification.zig | 13 +++++++++++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index cd159016..e685c105 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -46,6 +46,12 @@ fn discoverIpAddressFromNewThread() void { discoverIpAddress(); } +fn raiseNotification(text: []const u8) void { + main.gui.closeWindow("notification"); + main.gui.windowlist.notification.setNotificationText(text); + main.gui.openWindow("notification"); +} + fn join(_: usize) void { if(thread) |_thread| { _thread.join(); @@ -58,8 +64,7 @@ fn join(_: usize) void { if(connection) |_connection| { if (ipAddressEntry.currentString.items.len == 0) { std.log.err("IP address cannot be empty", .{}); - gui.windowlist.notification.text = "IP address cannot be empty"; - gui.openWindow("notification"); + raiseNotification("IP address cannot be empty"); return; } _connection.world = &main.game.testWorld; @@ -69,15 +74,16 @@ fn join(_: usize) void { main.game.world = &main.game.testWorld; std.log.info("Connecting to server: {s}", .{ipAddressEntry.currentString.items}); main.game.testWorld.init(ipAddressEntry.currentString.items, _connection) catch |err| { - std.log.err("Encountered error while opening world: {s}", .{@errorName(err)}); - gui.windowlist.notification.text = std.fmt.allocPrint(main.globalAllocator.allocator, "Encountered error while opening world: {}", .{err}) catch unreachable; - gui.openWindow("notification"); + const formattedError = std.fmt.allocPrint(main.globalAllocator.allocator, "Encountered error while opening world: {s}", .{@errorName(err)}) catch unreachable; + std.log.err("{s}", .{formattedError}); + raiseNotification(formattedError); + main.globalAllocator.free(formattedError); + return; }; connection = null; } else { std.log.err("No connection found. Cannot connect.", .{}); - gui.windowlist.notification.text = "No connection found. Cannot connect."; - gui.openWindow("notification"); + raiseNotification("No connection found. Cannot connect."); } for(gui.openWindows.items) |openWindow| { gui.closeWindowFromRef(openWindow); diff --git a/src/gui/windows/notification.zig b/src/gui/windows/notification.zig index bef95416..23c7e47e 100644 --- a/src/gui/windows/notification.zig +++ b/src/gui/windows/notification.zig @@ -13,11 +13,20 @@ pub var window: GuiWindow = GuiWindow { .contentSize = Vec2f{128, 256}, }; -pub var text: []const u8 = ""; - const padding: f32 = 16; const width: f32 = 256; +var text: []const u8 = ""; + +pub fn deinit() void { + main.globalAllocator.free(text); +} + +pub fn setNotificationText(newText: []const u8) void { + main.globalAllocator.free(text); + text = main.globalAllocator.dupe(u8, newText); +} + fn ack(_: usize) void { gui.closeWindowFromRef(&window); } From 23dbe954cff3090bf6e3c6986e2e795e91a85491 Mon Sep 17 00:00:00 2001 From: somatcha Date: Fri, 6 Dec 2024 14:39:55 +0530 Subject: [PATCH 09/15] fix: cleanup --- src/gui/gui.zig | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 582f6028..1fb62ad8 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -307,9 +307,6 @@ pub fn openWindow(id: []const u8) void { defer updateWindowPositions(); for(windowList.items) |window| { if(std.mem.eql(u8, window.id, id)) { - if (std.mem.eql(u8, id, "notification")) { - window.onOpenFn(); - } openWindowFromRef(window); return; } From 86c8931bd2430f26071f8cd8c30b6797e9fcecd5 Mon Sep 17 00:00:00 2001 From: somatcha Date: Sat, 7 Dec 2024 00:01:00 +0530 Subject: [PATCH 10/15] fix: minor changes --- src/gui/windows/multiplayer.zig | 16 +++++----------- src/gui/windows/notification.zig | 9 ++++++++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index e685c105..8eb7ca09 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -46,12 +46,6 @@ fn discoverIpAddressFromNewThread() void { discoverIpAddress(); } -fn raiseNotification(text: []const u8) void { - main.gui.closeWindow("notification"); - main.gui.windowlist.notification.setNotificationText(text); - main.gui.openWindow("notification"); -} - fn join(_: usize) void { if(thread) |_thread| { _thread.join(); @@ -64,7 +58,7 @@ fn join(_: usize) void { if(connection) |_connection| { if (ipAddressEntry.currentString.items.len == 0) { std.log.err("IP address cannot be empty", .{}); - raiseNotification("IP address cannot be empty"); + main.gui.windowlist.notification.raiseNotification("IP address cannot be empty"); return; } _connection.world = &main.game.testWorld; @@ -74,16 +68,16 @@ fn join(_: usize) void { main.game.world = &main.game.testWorld; std.log.info("Connecting to server: {s}", .{ipAddressEntry.currentString.items}); main.game.testWorld.init(ipAddressEntry.currentString.items, _connection) catch |err| { - const formattedError = std.fmt.allocPrint(main.globalAllocator.allocator, "Encountered error while opening world: {s}", .{@errorName(err)}) catch unreachable; + const formattedError = std.fmt.allocPrint(main.stackAllocator.allocator, "Encountered error while opening world: {s}", .{@errorName(err)}) catch unreachable; std.log.err("{s}", .{formattedError}); - raiseNotification(formattedError); - main.globalAllocator.free(formattedError); + defer main.stackAllocator.free(formattedError); + main.gui.windowlist.notification.raiseNotification(formattedError); return; }; connection = null; } else { std.log.err("No connection found. Cannot connect.", .{}); - raiseNotification("No connection found. Cannot connect."); + main.gui.windowlist.notification.raiseNotification("No connection found. Cannot connect."); } for(gui.openWindows.items) |openWindow| { gui.closeWindowFromRef(openWindow); diff --git a/src/gui/windows/notification.zig b/src/gui/windows/notification.zig index 23c7e47e..731d94ce 100644 --- a/src/gui/windows/notification.zig +++ b/src/gui/windows/notification.zig @@ -22,11 +22,18 @@ pub fn deinit() void { main.globalAllocator.free(text); } -pub fn setNotificationText(newText: []const u8) void { +fn setNotificationText(newText: []const u8) void { main.globalAllocator.free(text); text = main.globalAllocator.dupe(u8, newText); } +pub fn raiseNotification(notifText: []const u8) void { + main.gui.closeWindow("notification"); + setNotificationText(notifText); + main.gui.openWindow("notification"); +} + + fn ack(_: usize) void { gui.closeWindowFromRef(&window); } From dfb7aec3163fa7dced6febb6b3d2146348887293 Mon Sep 17 00:00:00 2001 From: somatcha Date: Sat, 7 Dec 2024 00:15:03 +0530 Subject: [PATCH 11/15] fix: minor changes --- src/gui/windows/notification.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/gui/windows/notification.zig b/src/gui/windows/notification.zig index 731d94ce..9a9ad217 100644 --- a/src/gui/windows/notification.zig +++ b/src/gui/windows/notification.zig @@ -19,12 +19,12 @@ const width: f32 = 256; var text: []const u8 = ""; pub fn deinit() void { - main.globalAllocator.free(text); + main.stackAllocator.free(text); } fn setNotificationText(newText: []const u8) void { - main.globalAllocator.free(text); - text = main.globalAllocator.dupe(u8, newText); + main.stackAllocator.free(text); + text = main.stackAllocator.dupe(u8, newText); } pub fn raiseNotification(notifText: []const u8) void { From 5cb59738d1eec0ce39a4a9d131ca0f43ec5e713d Mon Sep 17 00:00:00 2001 From: somatcha Date: Sat, 7 Dec 2024 00:19:53 +0530 Subject: [PATCH 12/15] fix: defer line --- src/gui/windows/multiplayer.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index 8eb7ca09..6c754460 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -69,8 +69,8 @@ fn join(_: usize) void { std.log.info("Connecting to server: {s}", .{ipAddressEntry.currentString.items}); main.game.testWorld.init(ipAddressEntry.currentString.items, _connection) catch |err| { const formattedError = std.fmt.allocPrint(main.stackAllocator.allocator, "Encountered error while opening world: {s}", .{@errorName(err)}) catch unreachable; - std.log.err("{s}", .{formattedError}); defer main.stackAllocator.free(formattedError); + std.log.err("{s}", .{formattedError}); main.gui.windowlist.notification.raiseNotification(formattedError); return; }; From 90cbe73724a2053bb2992ba1bc6cf56a6b2707c9 Mon Sep 17 00:00:00 2001 From: somatcha Date: Sat, 7 Dec 2024 03:45:35 +0530 Subject: [PATCH 13/15] fix: revert global state on error --- src/gui/windows/multiplayer.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index 6c754460..dfb8d6d4 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -72,6 +72,10 @@ fn join(_: usize) void { defer main.stackAllocator.free(formattedError); std.log.err("{s}", .{formattedError}); main.gui.windowlist.notification.raiseNotification(formattedError); + main.globalAllocator.free(settings.lastUsedIPAddress); + settings.lastUsedIPAddress = ""; + main.game.world = null; + _connection.world = null; return; }; connection = null; From 124e4f4d1d2876ef653ec3056e9eefb9d838b671 Mon Sep 17 00:00:00 2001 From: somatcha Date: Sat, 7 Dec 2024 03:48:21 +0530 Subject: [PATCH 14/15] fix: format check --- src/gui/windows/multiplayer.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index dfb8d6d4..62147f77 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -72,10 +72,10 @@ fn join(_: usize) void { defer main.stackAllocator.free(formattedError); std.log.err("{s}", .{formattedError}); main.gui.windowlist.notification.raiseNotification(formattedError); - main.globalAllocator.free(settings.lastUsedIPAddress); - settings.lastUsedIPAddress = ""; - main.game.world = null; - _connection.world = null; + main.globalAllocator.free(settings.lastUsedIPAddress); + settings.lastUsedIPAddress = ""; + main.game.world = null; + _connection.world = null; return; }; connection = null; From df97702b9e29db12d8855ac5477889ad0a544873 Mon Sep 17 00:00:00 2001 From: somatcha Date: Sat, 7 Dec 2024 16:52:05 +0530 Subject: [PATCH 15/15] fix: save ip after successful conn --- src/gui/windows/multiplayer.zig | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/gui/windows/multiplayer.zig b/src/gui/windows/multiplayer.zig index 62147f77..d51c9414 100644 --- a/src/gui/windows/multiplayer.zig +++ b/src/gui/windows/multiplayer.zig @@ -56,15 +56,7 @@ fn join(_: usize) void { ipAddress = ""; } if(connection) |_connection| { - if (ipAddressEntry.currentString.items.len == 0) { - std.log.err("IP address cannot be empty", .{}); - main.gui.windowlist.notification.raiseNotification("IP address cannot be empty"); - return; - } _connection.world = &main.game.testWorld; - main.globalAllocator.free(settings.lastUsedIPAddress); - settings.lastUsedIPAddress = main.globalAllocator.dupe(u8, ipAddressEntry.currentString.items); - settings.save(); main.game.world = &main.game.testWorld; std.log.info("Connecting to server: {s}", .{ipAddressEntry.currentString.items}); main.game.testWorld.init(ipAddressEntry.currentString.items, _connection) catch |err| { @@ -72,12 +64,13 @@ fn join(_: usize) void { defer main.stackAllocator.free(formattedError); std.log.err("{s}", .{formattedError}); main.gui.windowlist.notification.raiseNotification(formattedError); - main.globalAllocator.free(settings.lastUsedIPAddress); - settings.lastUsedIPAddress = ""; main.game.world = null; _connection.world = null; return; }; + main.globalAllocator.free(settings.lastUsedIPAddress); + settings.lastUsedIPAddress = main.globalAllocator.dupe(u8, ipAddressEntry.currentString.items); + settings.save(); connection = null; } else { std.log.err("No connection found. Cannot connect.", .{});