aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Marina Barros <azulmarina799@gmail.com> 2026-10-03 19:10:43 +0000
committerGravatar Marina Barros <azulmarina799@gmail.com> 2026-10-03 19:14:07 +0000
commit1fb3ea4c29ed57e6405375f506291081b229f181 (patch)
treee4ba732bf56fddf028dcd7f2387844c65834555b /src
primeira iteração: zig + sdl3. memory leak no reader
Diffstat (limited to 'src')
-rw-r--r--src/main.zig86
-rw-r--r--src/root.zig18
2 files changed, 104 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig
new file mode 100644
index 0000000..7dd3341
--- /dev/null
+++ b/src/main.zig
@@ -0,0 +1,86 @@
+const sdl3 = @import("sdl3");
+const std = @import("std");
+
+/// Frequency of the generated tone, in Hz (440 Hz is concert A).
+const tone_hz = 440;
+/// Amplitude of the generated tone, from 0.0 (silence) to 1.0 (full volume).
+const tone_amplitude = 0.3;
+/// How long to sleep between event-loop iterations, in milliseconds.
+const poll_interval_ms = 50;
+
+const autumn_trees = "assets//autumn_trees.wav";
+
+pub fn main(
+ init: std.process.Init,
+) !void {
+ defer sdl3.shutdown();
+
+ const log_app = sdl3.log.Category.application;
+
+ try sdl3.init(.{ .audio = true, .events = true });
+ defer sdl3.quit(.{ .audio = true, .events = true });
+
+ try sdl3.mixer.init();
+ defer sdl3.mixer.quit();
+
+ const compiled_version = sdl3.mixer.Version.compiled_against;
+ const linked_version = sdl3.mixer.Version.get();
+ try log_app.logInfo("Using SDL_mixer {d}.{d}.{d}", .{ compiled_version.getMajor(), compiled_version.getMinor(), compiled_version.getMicro() });
+ try log_app.logInfo("Linked against SDL_mixer {d}.{d}.{d}", .{ linked_version.getMajor(), linked_version.getMinor(), linked_version.getMicro() });
+
+ // Open a mixer on the default playback device, letting SDL mixer pick the format.
+ const mixer = try sdl3.mixer.Mixer.initDevice(sdl3.audio.Device.default_playback, null);
+ defer mixer.deinit();
+
+ const wav = try sdl3.mixer.Audio.init(mixer, autumn_trees, true);
+ defer wav.deinit();
+ // Generate an infinite sine wave (`null` duration).
+ // const tone = try sdl3.mixer.Audio.initSineWaveAudio(mixer, tone_hz, tone_amplitude, null);
+ // defer tone.deinit();
+
+ const track = try sdl3.mixer.Track.init(mixer);
+ defer track.deinit();
+
+ try track.setAudio(wav);
+ // try track.setAudio(tone);
+
+ // try log_app.logInfo("Playing a wav {d} track. Press Ctrl+C to quit.", .{wav});
+ // try log_app.logInfo("Playing a continuous {d} Hz tone. Press Ctrl+C to quit.", .{tone_hz});
+ try track.play(.{});
+
+ const stdin = std.Io.File.stdin();
+
+ var quit_app = false;
+ while (!quit_app) {
+ var buffer: [1024]u8 = undefined;
+ const stdin_reader = stdin.reader(init.io, &buffer);
+ var reader = stdin_reader.interface;
+ const char = try reader.takeDelimiterExclusive('\n');
+ if (std.mem.eql(u8, char, "a")) {
+ quit_app = true;
+ }
+ // switch (char) {
+ // 'a' => std.debug.print("oi", .{}),
+ // 'b' => quit_app = true,
+ // else => {},
+ // }
+ // while (sdl3.events.poll()) |event| {
+ // switch (event) {
+ // .quit, .terminating => quit_app = true,
+ // .key_up => |key| {
+ // if (key.key == .space) {
+ // try track.pause();
+ // } else if (key.key == .escape) {
+ // quit_app = true;
+ // }
+ // },
+ // else => {},
+ // }
+ // }
+
+ sdl3.timer.delayMilliseconds(poll_interval_ms);
+ }
+
+ try track.stop(0);
+ try log_app.logInfo("Quit.", .{});
+}
diff --git a/src/root.zig b/src/root.zig
new file mode 100644
index 0000000..5a71250
--- /dev/null
+++ b/src/root.zig
@@ -0,0 +1,18 @@
+//! By convention, root.zig is the root source file when making a package.
+const std = @import("std");
+const Io = std.Io;
+
+/// This is a documentation comment to explain the `printAnotherMessage` function below.
+///
+/// Accepting an `Io.Writer` instance is a handy way to write reusable code.
+pub fn printAnotherMessage(writer: *Io.Writer) Io.Writer.Error!void {
+ try writer.print("Run `zig build test` to run the tests.\n", .{});
+}
+
+pub fn add(a: i32, b: i32) i32 {
+ return a + b;
+}
+
+test "basic add functionality" {
+ try std.testing.expect(add(3, 7) == 10);
+}