Skip to content

Commit

Permalink
Implement title, artist, and level sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Oct 30, 2023
1 parent e0249e9 commit 712527b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
9 changes: 7 additions & 2 deletions game/music.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
58 changes: 53 additions & 5 deletions game/screens/song_select.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 => {},
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 712527b

Please sign in to comment.