Skip to content

Commit

Permalink
Build the compiler into the game + speed changes ingame
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Oct 6, 2023
1 parent 8c04e64 commit 7155a4f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
2 changes: 2 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ pub fn build(b: *std.Build) !void {
fumen_compile.addModule("clap", clap.module("clap"));
b.installArtifact(fumen_compile);

exe.addModule("fumen_compiler", b.addModule("fumen_compiler", .{ .source_file = .{ .path = root_path ++ "util/fumen_compile.zig" } }));

const fumen_compile_cmd = b.addRunArtifact(fumen_compile);
fumen_compile_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
Expand Down
6 changes: 4 additions & 2 deletions game/fumen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ allocator: std.mem.Allocator,

const Self = @This();

pub fn deinit(self: Self) void {
pub fn deinit(self: *Self) void {
for (self.lyrics) |lyric| {
self.allocator.free(lyric.text);
}
Expand All @@ -107,9 +107,11 @@ pub fn deinit(self: Self) void {
self.allocator.free(self.lyrics_kanji);
self.allocator.free(self.beat_lines);
self.allocator.free(self.fumen_folder);

self.* = undefined;
}

pub fn readFromFile(allocator: std.mem.Allocator, file: *std.fs.File, dir: std.fs.Dir) !Self {
pub fn readFromFile(allocator: std.mem.Allocator, file: std.fs.File, dir: std.fs.Dir) !Self {
var self: Self = .{
.allocator = allocator,
.audio_path = &.{},
Expand Down
19 changes: 15 additions & 4 deletions game/music.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ level: u3,
fumen_file_name: [:0]const u8,
ranking_file_name: [:0]const u8,
comment: []const [:0]const u8,
folder_path: []const u8,

fumen: Fumen,
fumen: *Fumen,

allocator: std.mem.Allocator,

Expand All @@ -27,16 +28,21 @@ pub fn deinit(self: *Self) void {
self.allocator.free(self.author);
self.allocator.free(self.fumen_file_name);
self.allocator.free(self.ranking_file_name);
self.allocator.free(self.folder_path);

for (self.comment) |comment| {
self.allocator.free(comment);
}

self.allocator.free(self.comment);

self.fumen.deinit();
self.allocator.destroy(self.fumen);

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) !Self {
var self: Self = undefined;
self.allocator = allocator;

Expand Down Expand Up @@ -127,13 +133,18 @@ pub fn readFromFile(allocator: std.mem.Allocator, path: std.fs.Dir, file: *std.f

self.comment = try comments.toOwnedSlice();

self.folder_path = try path.realpathAlloc(allocator, ".");
errdefer allocator.free(self.folder_path);

var fumen_path = try path.realpathAlloc(allocator, self.fumen_file_name);
defer allocator.free(fumen_path);

var fumen_file = try std.fs.openFileAbsolute(fumen_path, .{});
defer fumen_file.close();

self.fumen = try Fumen.readFromFile(allocator, &fumen_file, path);
self.fumen = try allocator.create(Fumen);
errdefer allocator.destroy(self.fumen);
self.fumen.* = try Fumen.readFromFile(allocator, fumen_file, path);
errdefer self.fumen.deinit();

return self;
Expand Down Expand Up @@ -167,7 +178,7 @@ 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);

try music_list.append(music);
}
Expand Down
53 changes: 51 additions & 2 deletions game/screens/gameplay.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const bass = @import("bass");
const builtin = @import("builtin");
const fumen_compiler = @import("fumen_compiler");

const c = @import("../main.zig").c;

Expand Down Expand Up @@ -61,6 +62,10 @@ const GameplayData = struct {
///Get the actively playing music
music: *Music,

///The current speed of the music
speed: f32 = 1,
starting_freq: f32,

///The current note the user is on
active_note: usize = 0,

Expand Down Expand Up @@ -281,6 +286,7 @@ pub fn initScreen(self: *Screen, allocator: std.mem.Allocator, gfx: Gfx) anyerro
.accuracy_text = try std.time.Timer.start(),
},
.gauge_data = GaugeData.init(self.state.current_map.?.fumen.lyrics.len),
.starting_freq = undefined,
};

//Reset the hit status for all the lyrics before the game starts
Expand Down Expand Up @@ -310,6 +316,8 @@ pub fn initScreen(self: *Screen, allocator: std.mem.Allocator, gfx: Gfx) anyerro
},
},
);

data.starting_freq = try self.state.audio_tracker.music.?.getAttribute(bass.ChannelAttribute.frequency);
}

pub fn deinitScreen(self: *Screen) void {
Expand Down Expand Up @@ -468,7 +476,7 @@ pub fn char(self: *Screen, typed_char: []const u8) anyerror!void {
var delta = current_time - current_note.time;

//Get the hit result, using the absolute value
var hit_result = deltaToHitResult(@fabs(delta));
var hit_result = deltaToHitResult(@abs(delta));

//If the user is past the note, and they are too far away, mark it as poor
//This allows them to hit notes at any point *after*
Expand Down Expand Up @@ -586,7 +594,7 @@ pub fn char(self: *Screen, typed_char: []const u8) anyerror!void {
var delta = current_time - next_note.?.time;

//Get the hit result, using the absolute value
var hit_result = deltaToHitResult(@fabs(delta));
var hit_result = deltaToHitResult(@abs(delta));

// std.debug.print("next \"{s}\"/{d}/{any}\n", .{ matched_for_next_note.conversion.romaji, delta, hit_result });

Expand Down Expand Up @@ -741,6 +749,47 @@ pub fn keyDown(self: *Screen, key: c.SDL_Keysym) anyerror!void {
try self.state.audio_tracker.music.?.play(false);
}
},
c.SDLK_RETURN => {
//Deinit the current fumen information
data.music.fumen.deinit();

//Get the filename of the source file
var source_filename = try std.fmt.allocPrint(data.music.allocator, "{s}_src.txt", .{std.fs.path.stem(data.music.fumen_file_name)});
defer data.music.allocator.free(source_filename);

//Open the output dir
var out_dir = try std.fs.openDirAbsolute(data.music.folder_path, .{});
defer out_dir.close();

//Open the source file
var source_file = try out_dir.openFile(source_filename, .{ .mode = .read_write });
defer source_file.close();

//Run the compiler
try fumen_compiler.compile(data.music.allocator, source_file, data.music.folder_path);

//Open the new fumen file
var fumen_file = try out_dir.openFile(data.music.fumen_file_name, .{});
defer fumen_file.close();

//Read the new fumen information from the file
data.music.fumen.* = try Fumen.readFromFile(data.music.allocator, fumen_file, out_dir);

//Reset parts of the game state to let the user replay the map basically
data.phase = Phase.main;
data.active_note = 0;
data.score = .{};
data.gauge_data = GaugeData.init(data.music.fumen.lyrics.len);
data.current_lyric_kanji = null;
},
c.SDLK_RIGHTBRACKET => {
data.speed += 0.1;
try self.state.audio_tracker.music.?.setAttribute(bass.ChannelAttribute.frequency, data.starting_freq * data.speed);
},
c.SDLK_LEFTBRACKET => {
data.speed -= 0.1;
try self.state.audio_tracker.music.?.setAttribute(bass.ChannelAttribute.frequency, data.starting_freq * data.speed);
},
else => {
if (data.phase == .ready) {
data.phase = .main;
Expand Down
2 changes: 1 addition & 1 deletion libs/zig-bass
Submodule zig-bass updated 2 files
+1 −1 build.zig
+10 −0 src/bass.zig
8 changes: 6 additions & 2 deletions util/fumen_compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ pub fn main() !void {
const file = try std.fs.openFileAbsolute(file_name, .{});
defer file.close();

var buffered_reader = std.io.bufferedReader(file.reader());
try compile(allocator, file, dir_name);
}

pub fn compile(allocator: std.mem.Allocator, input_file: std.fs.File, output_dir_name: []const u8) !void {
var buffered_reader = std.io.bufferedReader(input_file.reader());
var reader = buffered_reader.reader();

var info = Info{
Expand Down Expand Up @@ -130,7 +134,7 @@ pub fn main() !void {
}

//Get the full path of the destination file
const dest_name_path = try std.fs.path.resolve(allocator, &.{ dir_name, dest_name });
const dest_name_path = try std.fs.path.resolve(allocator, &.{ output_dir_name, dest_name });
defer allocator.free(dest_name_path);

//Open the output file for writing
Expand Down

0 comments on commit 7155a4f

Please sign in to comment.