Skip to content

Commit

Permalink
ditch dupez, save memory
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Mar 20, 2024
1 parent ffcd0be commit 62e440e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
2 changes: 2 additions & 0 deletions game/app.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions game/convert.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
});
}
Expand Down
12 changes: 6 additions & 6 deletions game/fumen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
});

Expand Down
36 changes: 18 additions & 18 deletions game/music.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion game/screens/gameplay.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 62e440e

Please sign in to comment.