commit 91fd3c8abd50da0c8c1b14eaae2e3f39d209b000 Author: Evan Burkey Date: Wed Jan 11 14:57:19 2023 -0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed5ed45 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +compile_commands.json +sdlamp +.cache/ +*.wav diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e61a701 --- /dev/null +++ b/LICENSE @@ -0,0 +1,5 @@ +Copyright 2023 Evan Burkey + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c941490 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +CC_SDL=`sdl2-config --cflags --libs` + +all: + cc *.c -o sdlamp -std=c11 $(CC_SDL) diff --git a/README.md b/README.md new file mode 100644 index 0000000..e76a5e5 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# sdlamp + +A Winamp clone using SDL2. Based off [Ryan Gordon's SDL tutorial video series](https://www.youtube.com/watch?v=6hIaEHzlmFc&list=PL6m6sxLnXksbqdsAcpTh4znV9j70WkmqG) diff --git a/sdlamp.c b/sdlamp.c new file mode 100644 index 0000000..7685290 --- /dev/null +++ b/sdlamp.c @@ -0,0 +1,122 @@ +#include + +#include "SDL.h" +#include "SDL_audio.h" +#include "SDL_error.h" +#include "SDL_events.h" +#include "SDL_messagebox.h" +#include "SDL_rect.h" +#include "SDL_render.h" +#include "SDL_stdinc.h" +#include "SDL_video.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; +}