mirror of
https://github.com/Not-Nik/raylib-zig.git
synced 2025-09-09 03:57:29 +00:00
example: add audio_sound_loading
This commit is contained in:
parent
9882dd4ba1
commit
09ec2ca22b
@ -207,6 +207,11 @@ pub fn build(b: *std.Build) !void {
|
|||||||
.path = "examples/audio/music_stream.zig",
|
.path = "examples/audio/music_stream.zig",
|
||||||
.desc = "Use music stream to play an audio file",
|
.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",
|
.name = "basic_screen_manager",
|
||||||
.path = "examples/core/basic_screen_manager.zig",
|
.path = "examples/core/basic_screen_manager.zig",
|
||||||
|
46
examples/audio/sound_loading.zig
Normal file
46
examples/audio/sound_loading.zig
Normal 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
BIN
resources/audio/sound.wav
Normal file
Binary file not shown.
BIN
resources/audio/target.ogg
Normal file
BIN
resources/audio/target.ogg
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user