From 62e440e2af0e19bae62b479afd5d5648ebf83f54 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 20 Mar 2024 14:46:58 -0700 Subject: [PATCH] ditch dupez, save memory --- game/app.zig | 2 ++ game/convert.zig | 12 ++++++------ game/fumen.zig | 12 ++++++------ game/music.zig | 36 ++++++++++++++++++------------------ game/screens/gameplay.zig | 2 +- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/game/app.zig b/game/app.zig index 36de5ee..0f570a8 100644 --- a/game/app.zig +++ b/game/app.zig @@ -91,8 +91,10 @@ pub fn init(app: *App) !void { else 0; + //Init bass try bass.init(.default, null, .{}, bass_window_ptr); + //Set the volume to the one specified in the config try bass.setConfig(.global_stream_volume, @intFromFloat(app.config.volume * 10000)); //Create the bind group for the texture diff --git a/game/convert.zig b/game/convert.zig index c953cd9..946b43e 100644 --- a/game/convert.zig +++ b/game/convert.zig @@ -6,9 +6,9 @@ const Self = @This(); pub const Conversion = struct { allocator: std.mem.Allocator, - hiragana: [:0]const u8, - romaji: [:0]const u8, - end_cut: ?[:0]const u8, + hiragana: []const u8, + romaji: []const u8, + end_cut: ?[]const u8, length: usize, pub fn deinit(self: Conversion) void { @@ -114,9 +114,9 @@ pub fn readUTypingConversions(conv: Conv, allocator: std.mem.Allocator) !Self { try conversions.append(.{ .allocator = allocator, - .hiragana = try allocator.dupeZ(u8, hiragana), - .romaji = try allocator.dupeZ(u8, romaji), - .end_cut = if (end_cut == null) null else try allocator.dupeZ(u8, end_cut.?), + .hiragana = try allocator.dupe(u8, hiragana), + .romaji = try allocator.dupe(u8, romaji), + .end_cut = if (end_cut == null) null else try allocator.dupe(u8, end_cut.?), .length = if (end_cut == null) romaji.len else romaji.len - end_cut.?.len, }); } diff --git a/game/fumen.zig b/game/fumen.zig index c210591..711bbf0 100644 --- a/game/fumen.zig +++ b/game/fumen.zig @@ -13,7 +13,7 @@ pub const Lyric = struct { poor = 3, }; - text: [:0]const u8, + text: []const u8, time: f64, /// The hit result that they *could* get if they completed the note @@ -33,7 +33,7 @@ pub const LyricCutoff = struct { }; pub const LyricKanji = struct { pub const Part = struct { - text: [:0]const u8, + text: []const u8, color: Gfx.ColorF, }; @@ -76,7 +76,7 @@ pub const BeatLine = struct { type: Type, }; -audio_path: [:0]const u8, +audio_path: []const u8, lyrics: []Lyric, lyric_cutoffs: []const LyricCutoff, lyrics_kanji: []const LyricKanji, @@ -158,14 +158,14 @@ pub fn readFromFile(conv: Conv, allocator: std.mem.Allocator, file: std.fs.File, switch (converted.items[0]) { '@' => { - self.audio_path = try allocator.dupeZ(u8, without_identifier); + self.audio_path = try allocator.dupe(u8, without_identifier); }, '+' => { const space_idx = std.mem.indexOf(u8, without_identifier, &.{' '}).?; const lyric = Lyric{ .time = try std.fmt.parseFloat(f64, without_identifier[0..space_idx]), - .text = try allocator.dupeZ(u8, without_identifier[(space_idx + 1)..]), + .text = try allocator.dupe(u8, without_identifier[(space_idx + 1)..]), }; try lyrics.append(lyric); @@ -198,7 +198,7 @@ pub fn readFromFile(conv: Conv, allocator: std.mem.Allocator, file: std.fs.File, var next = split_iterator.next(); while (next != null) { try part_list.append(LyricKanji.Part{ - .text = try allocator.dupeZ(u8, next.?), + .text = try allocator.dupe(u8, next.?), .color = if (grey) .{ 0.25, 0.25, 1.0 / 3.0, 1 } else Gfx.WhiteF, }); diff --git a/game/music.zig b/game/music.zig index d221c5d..55669c5 100644 --- a/game/music.zig +++ b/game/music.zig @@ -10,13 +10,13 @@ const Conv = @import("conv.zig"); const Self = @This(); -title: [:0]const u8, -artist: [:0]const u8, -author: [:0]const u8, +title: []const u8, +artist: []const u8, +author: []const u8, level: u3, -fumen_file_name: [:0]const u8, -ranking_file_name: [:0]const u8, -comment: []const [:0]const u8, +fumen_file_name: []const u8, +ranking_file_name: []const u8, +comment: []const []const u8, folder_path: []const u8, ///The index assigned at load time for the song, used for "no sort" option, to return to original order sort_idx: usize, @@ -51,7 +51,7 @@ pub fn readFromFile(conv: Conv, allocator: std.mem.Allocator, path: std.fs.Dir, self.sort_idx = sort_idx; self.allocator = allocator; - var comments = std.ArrayList([:0]const u8).init(allocator); + var comments = std.ArrayList([]const u8).init(allocator); errdefer { for (comments.items) |comment| { allocator.free(comment); @@ -62,11 +62,11 @@ pub fn readFromFile(conv: Conv, allocator: std.mem.Allocator, path: std.fs.Dir, //Set to 12 to match COMMENT_N_LINES in UTyping.cpp try comments.ensureTotalCapacity(12); - var title: ?[:0]const u8 = null; - var artist: ?[:0]const u8 = null; - var author: ?[:0]const u8 = null; - var fumen_file_name: ?[:0]const u8 = null; - var ranking_file_name: ?[:0]const u8 = null; + var title: ?[]const u8 = null; + var artist: ?[]const u8 = null; + var author: ?[]const u8 = null; + var fumen_file_name: ?[]const u8 = null; + var ranking_file_name: ?[]const u8 = null; errdefer if (title) |title_ptr| allocator.free(title_ptr); errdefer if (artist) |artist_ptr| allocator.free(artist_ptr); @@ -96,11 +96,11 @@ pub fn readFromFile(conv: Conv, allocator: std.mem.Allocator, path: std.fs.Dir, } if (i == 0) { - title = try allocator.dupeZ(u8, line); + title = try allocator.dupe(u8, line); } else if (i == 1) { - artist = try allocator.dupeZ(u8, line); + artist = try allocator.dupe(u8, line); } else if (i == 2) { - author = try allocator.dupeZ(u8, line); + author = try allocator.dupe(u8, line); } else if (i == 3) { self.level = try std.fmt.parseUnsigned(u3, line, 10); @@ -109,11 +109,11 @@ pub fn readFromFile(conv: Conv, allocator: std.mem.Allocator, path: std.fs.Dir, return error.MusicInvalidLevel; } } else if (i == 4) { - fumen_file_name = try allocator.dupeZ(u8, line); + fumen_file_name = try allocator.dupe(u8, line); } else if (i == 5) { - ranking_file_name = try allocator.dupeZ(u8, line); + ranking_file_name = try allocator.dupe(u8, line); } else { - try comments.append(try allocator.dupeZ(u8, line)); + try comments.append(try allocator.dupe(u8, line)); } i += 1; diff --git a/game/screens/gameplay.zig b/game/screens/gameplay.zig index ca3e703..f52d08a 100644 --- a/game/screens/gameplay.zig +++ b/game/screens/gameplay.zig @@ -425,7 +425,7 @@ pub fn char(self: *Screen, typed_codepoint: u21) anyerror!void { var next_note: ?*Fumen.Lyric = if (data.active_note + 1 == data.music.fumen.lyrics.len) null else &data.music.fumen.lyrics[data.active_note + 1]; var hiragana_to_type = current_note.text[data.typed_hiragana.len..]; - const hiragana_to_type_next: ?[:0]const u8 = if (next_note != null) next_note.?.text else &.{}; + const hiragana_to_type_next: ?[]const u8 = if (next_note != null) next_note.?.text else &.{}; const Match = struct { //The matched conversion