diff --git a/game/music.zig b/game/music.zig index d6e1540..d0e7f6f 100644 --- a/game/music.zig +++ b/game/music.zig @@ -17,6 +17,8 @@ fumen_file_name: [:0]const u8, ranking_file_name: [:0]const u8, comment: []const [:0]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, fumen: *Fumen, @@ -42,8 +44,9 @@ pub fn deinit(self: *Self) void { self.* = undefined; } -pub fn readFromFile(allocator: std.mem.Allocator, path: std.fs.Dir, file: std.fs.File) !Self { +pub fn readFromFile(allocator: std.mem.Allocator, path: std.fs.Dir, file: std.fs.File, sort_idx: usize) !Self { var self: Self = undefined; + self.sort_idx = sort_idx; self.allocator = allocator; var iconv = IConv.ziconv_open("Shift_JIS", "UTF-8"); @@ -164,6 +167,7 @@ pub fn readUTypingList(allocator: std.mem.Allocator) ![]Self { music_list.deinit(); } + var i: usize = 0; while (true) { var line = try reader.readUntilDelimiterOrEofAlloc(allocator, '\n', 100) orelse break; defer allocator.free(line); @@ -178,9 +182,10 @@ pub fn readUTypingList(allocator: std.mem.Allocator) ![]Self { var music_dir = try std.fs.cwd().openDir(std.fs.path.dirname(normalized) orelse "", .{}); defer music_dir.close(); - var music = try readFromFile(allocator, music_dir, music_file); + var music = try readFromFile(allocator, music_dir, music_file, i); try music_list.append(music); + i += 1; } return try music_list.toOwnedSlice(); diff --git a/game/screens/song_select.zig b/game/screens/song_select.zig index e6c6eba..5ce4e67 100644 --- a/game/screens/song_select.zig +++ b/game/screens/song_select.zig @@ -131,7 +131,7 @@ pub fn keyDown(self: *Screen, key: c.SDL_Keysym) anyerror!void { } else if (data.challenge_info.speed < 4) { data.challenge_info.speed += 0.5; } - //Round to nearest tenth to prevent floating pointrounding errors + //Round to nearest tenth to prevent floating point rounding errors data.challenge_info.speed = @round(data.challenge_info.speed * 10.0) / 10.0; }, //Speed down @@ -161,6 +161,11 @@ pub fn keyDown(self: *Screen, key: c.SDL_Keysym) anyerror!void { c.SDLK_F6 => data.challenge_info.sin = !data.challenge_info.sin, c.SDLK_F7 => data.challenge_info.cos = !data.challenge_info.cos, c.SDLK_F8 => data.challenge_info.tan = !data.challenge_info.tan, + c.SDLK_0 => data.draw_info.music_iter.noSort(), + c.SDLK_1 => data.draw_info.music_iter.sortTitle(), + c.SDLK_2 => data.draw_info.music_iter.sortArtist(), + c.SDLK_3 => data.draw_info.music_iter.sortLevel(), + // c.SDLK_4 => data.draw_info.music_iter.sortAchievement(), else => {}, } } @@ -324,13 +329,10 @@ const DrawInfo = struct { //Jump to the random selection self.jump(selection); } - - //TODO: sorting funcs here - //pub fn noSort(self: *DrawInfo) void {} }; pub const MusicIterator = struct { - music: []const Music, + music: []Music, idx: usize, pub fn prev(self: *MusicIterator) void { @@ -349,6 +351,52 @@ pub const MusicIterator = struct { pub fn curr(self: *MusicIterator) *const Music { return &self.music[self.idx]; } + + fn noSortComparison(context: void, lhs: Music, rhs: Music) bool { + _ = context; + return lhs.sort_idx < rhs.sort_idx; + } + + pub fn noSort(self: *MusicIterator) void { + std.sort.block(Music, self.music, {}, noSortComparison); + } + + fn titleComparison(context: void, lhs: Music, rhs: Music) bool { + _ = context; + return std.ascii.lessThanIgnoreCase(lhs.title, rhs.title); + } + + pub fn sortTitle(self: *MusicIterator) void { + std.sort.block(Music, self.music, {}, titleComparison); + } + + fn artistComparison(context: void, lhs: Music, rhs: Music) bool { + _ = context; + return std.ascii.lessThanIgnoreCase(lhs.artist, rhs.artist); + } + + pub fn sortArtist(self: *MusicIterator) void { + std.sort.block(Music, self.music, {}, artistComparison); + } + + fn levelComparison(context: void, lhs: Music, rhs: Music) bool { + _ = context; + return lhs.level < rhs.level; + } + + pub fn sortLevel(self: *MusicIterator) void { + std.sort.block(Music, self.music, {}, levelComparison); + } + + // fn achievementComparison(context: void, lhs: Music, rhs: Music) bool { + // _ = context; + // //TODO: implement this properly + // return lhs.level < rhs.level; + // } + + // pub fn sortAchievement(self: *MusicIterator) void { + // std.sort.block(Music, self.music, {}, levelComparison); + // } }; pub const h_ranking = 252;