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.
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
brew install pkgconfig sdl2 sdl2_ttf sdl2_image sdl2_net sdl2_mixer sdl2_gfx libpng cmake
sudo apt-get install cmake libpng-dev libcurl4-openssl-dev libsdl2-dev libsdl2-mixer-dev libsdl2-gfx-dev libsdl2-image-dev libsdl2-net-dev libsdl2-ttf-dev libmikmod-dev libncurses5-dev libbz2-dev libflac-dev libvorbis-dev libwebp-dev libfreetype6-dev build-essential clang
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:
g++ program.cpp -o test -l SplashKit
You can use this command to compile the code below:
g++ program.cpp -o test -lSDL2_mixer -lmingw32 -mwindows -lSDL2main -lSDL2
Note: If you have issues, you can find out the compiling flags needed using these commands:
pkg-config --libs sdl2 sdl2_mixerpkg-config --cflags sdl2
g++ program.cpp -o test -L/opt/homebrew/Cellar/sdl2_mixer/2.8.0/lib -lSDL2_mixer -L/opt/homebrew/lib -lSDL2 -I/opt/homebrew/include -I/opt/homebrew/include/SDL2 -D_THREAD_SAFE
Note: If you have issues, you can find out the compiling flags needed using these commands:
pkg-config --libs sdl2 sdl2_mixerpkg-config --cflags sdl2
g++ program.cpp -o test -lSDL2_mixer -lSDL2 -I/usr/include/SDL2 -D_REENTRANT
Note: If you have issues, you can find out the compiling flags needed using these commands:
pkg-config --libs sdl2 SDL2_mixerpkg-config --cflags sdl2
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;}
#include <iostream>#ifdef __APPLE__#include <SDL.h>#include <SDL_mixer.h>#else#include <SDL2/SDL.h>#include <SDL2/SDL_mixer.h>#endif
SDL_Window *sdl_open_window(){ // Open window without SplashKit.
// Declare Variables SDL_Window *window = nullptr;
// Check for successful initialisation if (SDL_Init(SDL_INIT_VIDEO) < 0) { std::cout << "SDL could not be initialized: " << SDL_GetError(); exit(1); }
// Create Window window = SDL_CreateWindow( "No SK Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL); // Error handling for window if (window == NULL) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Could not create window: %s\n", SDL_GetError()); exit(1); }
return window;}
int main(int argv, char **args){ // Print instructions in terminal std::cout << "\n1. Press Spacebar for \"jump\" sound\n"; std::cout << "2. Click anywhere in the window for \"explosion\" sound\n\n";
// Open Window and load sound effects SDL_Window *window = sdl_open_window();
// Check for successful initialisation if (SDL_Init(SDL_INIT_VIDEO) < 0 | SDL_Init(SDL_INIT_AUDIO) < 0) { std::cout << "SDL could not be initialized: " << SDL_GetError(); exit(1); } if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 4096) < 0) { std::cout << "SDL Mixer could not be initialized: " << SDL_GetError(); exit(1); }
Mix_Chunk *jump = Mix_LoadWAV("Resources/sounds/jump.wav"); Mix_Chunk *explosion = Mix_LoadWAV("Resources/sounds/explosion.wav"); Mix_Music *music = Mix_LoadMUS("Resources/sounds/time_for_adventure.mp3"); if (jump == NULL || explosion == NULL || music == NULL) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Could not create audio: %s\n", Mix_GetError()); exit(1); } Mix_PlayMusic(music, -1);
// Hang window until quit SDL_Event event; while (event.type != SDL_QUIT) {
if (event.type == SDL_MOUSEBUTTONDOWN) { Mix_PlayChannel(-1, explosion, 0); } if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_SPACE) { Mix_PlayChannel(-1, jump, 0); } SDL_PollEvent(&event); } // Cleanup and free memory SDL_DestroyWindow(window); Mix_FreeMusic(music); Mix_FreeChunk(jump); Mix_FreeChunk(explosion);
return 0;}