Skip to content

Playing Sound Effects and Music Without SplashKit

This brief guide provides code to compare how to load and play music and sound effects with and without SplashKit (using SDL).
Written by: Simon Rhook and Olivia McKeon
Last updated: December 2024


Getting Started without SplashKit

Installing SDL2 and SDL2_mixer

If you have not already installed SplashKit, you will need to install SDL2 and SDL2_mixer to be able to compile the code shown in this guide.

You can use the following command, which is usually run during the SplashKit installation, which will ensure you have installed the required libraries/dependencies.

Terminal window
pacman -S --needed --disable-download-timeout mingw-w64-x86_64-clang mingw-w64-x86_64-gcc mingw-w64-x86_64-gdb mingw-w64-x86_64-cmake mingw-w64-x86_64-SDL2 mingw-w64-x86_64-SDL2_gfx mingw-w64-x86_64-SDL2_mixer mingw-w64-x86_64-SDL2_image mingw-w64-x86_64-SDL2_ttf mingw-w64-x86_64-SDL2_net mingw-w64-x86_64-civetweb

Compiling Your Code

With C++ programs, you will need to adjust the compiling command to link to any libraries being used. Below you will see the different commands to compile with and without SplashKit.

Now, assuming the code filename is program.cpp.

As usual, you will compile the SplashKit C++ code using the following command:

Terminal window
g++ program.cpp -o test -l SplashKit

Example Code

The following code compares loading and playing music and sound effects with and without SplashKit (using SDL):

#include "splashkit.h"
int main(int argv, char** args)
{
// Print instructions in terminal
write_line("\n1. Press Spacebar for \"jump\" sound");
write_line("2. Click anywhere in the window for \"explosion\" sound\n");
double volume = 0.5;
// Open Window and load sound effects
open_window("test", 600,600);
load_sound_effect("jump","jump.wav");
load_sound_effect("explosion","explosion.wav");
load_music("adventure", "time_for_adventure.mp3");
play_music("adventure",1000);
// Hang window until quit
while (!quit_requested())
{
process_events();
if(key_down(SPACE_KEY))
play_sound_effect("jump");
if(mouse_clicked(LEFT_BUTTON))
play_sound_effect("explosion",volume);
}
// Cleanup and free memory
close_all_windows();
free_all_sound_effects();
free_all_music();
return 0;
}