diff --git a/build.zig b/build.zig index f4f1235..cadabc4 100644 --- a/build.zig +++ b/build.zig @@ -207,6 +207,11 @@ pub fn build(b: *std.Build) !void { .path = "examples/audio/music_stream.zig", .desc = "Use music stream to play an audio file", }, + .{ + .name = "sound_loading", + .path = "examples/audio/sound_loading.zig", + .desc = "Load and play a song", + }, .{ .name = "basic_screen_manager", .path = "examples/core/basic_screen_manager.zig", diff --git a/examples/audio/sound_loading.zig b/examples/audio/sound_loading.zig new file mode 100644 index 0000000..faba5b8 --- /dev/null +++ b/examples/audio/sound_loading.zig @@ -0,0 +1,46 @@ +const rl = @import("raylib"); + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +pub fn main() !void { + // Initialization + //-------------------------------------------------------------------------------------- + const screenWidth = 800; + const screenHeight = 450; + + rl.initWindow(screenWidth, screenHeight, "raylib [audio] example - sound loading and playing"); + defer rl.closeWindow(); // Close window and OpenGL context + + rl.initAudioDevice(); // Initialize audio device + defer rl.closeAudioDevice(); // Close audio device + + const fxWav: rl.Sound = try rl.loadSound("resources/audio/sound.wav"); // Load WAV audio file + const fxOgg: rl.Sound = try rl.loadSound("resources/audio/target.ogg"); // Load OGG audio file + defer rl.unloadSound(fxWav); // Unload sound data + defer rl.unloadSound(fxOgg); // Unload sound data + + rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!rl.windowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if (rl.isKeyPressed(rl.KeyboardKey.space)) rl.playSound(fxWav); // Play WAV sound + if (rl.isKeyPressed(rl.KeyboardKey.enter)) rl.playSound(fxOgg); // Play OGG sound + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + rl.beginDrawing(); + defer rl.endDrawing(); + + rl.clearBackground(rl.Color.white); + + rl.drawText("Press SPACE to PLAY the WAV sound!", 200, 180, 20, rl.Color.light_gray); + rl.drawText("Press ENTER to PLAY the OGG sound!", 200, 220, 20, rl.Color.light_gray); + //---------------------------------------------------------------------------------- + } +} diff --git a/resources/audio/sound.wav b/resources/audio/sound.wav new file mode 100644 index 0000000..b5d01c9 Binary files /dev/null and b/resources/audio/sound.wav differ diff --git a/resources/audio/target.ogg b/resources/audio/target.ogg new file mode 100644 index 0000000..2b73e1c Binary files /dev/null and b/resources/audio/target.ogg differ