example: add audio_sound_loading

This commit is contained in:
Frost-Phoenix 2025-02-04 19:39:17 +01:00 committed by Nikolas
parent 9882dd4ba1
commit 09ec2ca22b
4 changed files with 51 additions and 0 deletions

View File

@ -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",

View File

@ -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);
//----------------------------------------------------------------------------------
}
}

BIN
resources/audio/sound.wav Normal file

Binary file not shown.

BIN
resources/audio/target.ogg Normal file

Binary file not shown.