Skip to content

Instantly share code, notes, and snippets.

@araml
Last active April 15, 2018 23:44
Show Gist options
  • Save araml/7191ba537e497cc1ccd7e0199c65bdc6 to your computer and use it in GitHub Desktop.
Save araml/7191ba537e497cc1ccd7e0199c65bdc6 to your computer and use it in GitHub Desktop.
SDL Mixer Test
// g++ sdl_mixer_test.cpp -o sdl_mixer -lSDL2 -lSDL2_mixer -std=c++11
#include <SDL2/SDL.h>
#include <chrono>
#include <SDL2/SDL_mixer.h>
using namespace std::chrono;
SDL_Window *window = NULL;
int main(int argc, char **argv) {
SDL_Init(SDL_INIT_VIDEO);
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024);
window = SDL_CreateWindow("Audio", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 400, SDL_WINDOW_SHOWN);
bool run = true;
SDL_Event e;
Mix_Music *m1 = Mix_LoadMUS("mp3file1"),
*m2 = Mix_LoadMUS("mp3file2");
Mix_PlayMusic(m1, -1);
auto old = steady_clock::now();
bool test, m1_running = true;
while (run) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT)
run = false;
}
auto nnew = steady_clock::now();
if (nnew - old > seconds{4} && m1_running) {
Mix_PlayMusic(m2, -1);
old = nnew;
m1_running = false;
} else if (nnew - old > seconds{4}) {
Mix_PlayMusic(m1, -1);
old = nnew;
m1_running = true;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment