goku
goku game engine_en
goku game engine_en
  • Chapter 1: Introduction to SDL2, Rust, and goku
  • Chapter2: The Base Structure: SpriteSheet
  • Chapter 3: Animated Textures
  • Chapter 4: Handling Textures: TextureManager
  • Chapter 5: Bringing Textures to Life: GameObject
  • Chapter 6: The Foundation: Tile System
  • Chapter 7: Moving the View: Camera
  • Chapter 8: Particle System
  • Chapter 9: The User Interface Layer
  • Chapter 10: The Button Element
  • Chapter 11: The Text Box Element
  • Chapter 12: The Checkbox Element
  • Chapter 13: The Slider Element
  • Chapter 14: The Audio System
  • Chapter 15: The AI System
  • Chapter 16: The Window System
  • Chapter 17: User Input Handling
  • Chapter 18: Rendering a Parallax Background
  • Chapter 19: Timing and Frame Rate Management
  • Chapter 20: Conclusion
Powered by GitBook
On this page

Chapter 14: The Audio System

The AudioPlayer struct forms the core of the audio system for this game. This system uses the sdl2::mixer module from the SDL2 library in Rust. It can load and play .mp3, .flac, .mod, and .ogg audio files. The AudioPlayer struct contains a single field - mixer_context.

An AudioPlayer is instantiated with a specific number of channels (numchans). The constructor function (new) initializes SDL2, opens an audio device with a specific frequency, chunk size, and number of channels.

It provides a method play that loads an audio file, plays it in a loop a given number of times and sets its volume. The play method returns a Channel that can be paused, resumed, or stopped using the respective methods in AudioPlayer.

When an AudioPlayer instance is dropped, it ends the currently playing music and prints a message to the console.

PreviousChapter 13: The Slider ElementNextChapter 15: The AI System

Last updated 1 year ago