#include #include "SDL.h" static SDL_AudioDeviceID audio_device = 0; static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; #if defined(__clang__) || defined(__GNUC__) static void panic_and_abort(const char *title, const char *text) __attribute__((noreturn)); #endif static void panic_and_abort(const char *title, const char *text) { fprintf(stderr, "PANIC: %s ... %s\n", title, text); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, text, window); SDL_Quit(); exit(1); } int main(int argc, char **argv) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) { panic_and_abort("SDL_Init failed", SDL_GetError()); } window = SDL_CreateWindow("sdlamp", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1024, 768, 0); if (!window) { panic_and_abort("SDL_CreateWindow failed", SDL_GetError()); } renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_PRESENTVSYNC); if (!renderer) { panic_and_abort("SDL_CreateRenderer failed", SDL_GetError()); } // Enable dropfile events SDL_EventState(SDL_DROPFILE, SDL_ENABLE); const SDL_Rect rewind_rect = {100, 100, 100, 100}; const SDL_Rect pause_rect = {400, 100, 100, 100}; SDL_AudioSpec wavspec; Uint8 *wavbuf = NULL; Uint32 wavlen = 0; SDL_bool paused = SDL_TRUE; SDL_bool quit = SDL_FALSE; while (!quit) { SDL_Event e; while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: quit = SDL_TRUE; break; case SDL_MOUSEBUTTONDOWN: { SDL_Point pt = { e.button.x, e.button.y }; if (SDL_PointInRect(&pt, &rewind_rect)) { SDL_ClearQueuedAudio(audio_device); SDL_QueueAudio(audio_device, wavbuf, wavlen); } else if (SDL_PointInRect(&pt, &pause_rect)) { paused = paused ? SDL_FALSE : SDL_TRUE; SDL_PauseAudioDevice(audio_device, paused); } break; } case SDL_DROPFILE: { if (audio_device) { SDL_CloseAudioDevice(audio_device); audio_device = 0; } SDL_FreeWAV(wavbuf); wavbuf = NULL; wavlen = 0; if (SDL_LoadWAV(e.drop.file, &wavspec, &wavbuf, &wavlen) == NULL) { SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't load wav file!", SDL_GetError(), window); } audio_device = SDL_OpenAudioDevice(NULL, 0, &wavspec, NULL, 0); if (audio_device == 0) { SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't load audio device!", SDL_GetError(), window); SDL_free(wavbuf); wavbuf = NULL; wavlen = 0; } else { SDL_QueueAudio(audio_device, wavbuf, wavlen); SDL_PauseAudioDevice(audio_device, 0); } SDL_free(e.drop.file); break; } } } SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_RenderFillRect(renderer, &rewind_rect); SDL_RenderFillRect(renderer, &pause_rect); SDL_RenderPresent(renderer); } SDL_FreeWAV(wavbuf); SDL_CloseAudioDevice(audio_device); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; }