tomato/external/sdl/SDL/test/testaudio.c

1070 lines
34 KiB
C
Raw Normal View History

Squashed 'external/sdl/SDL/' changes from b8d91252c6..ec0042081e ec0042081e Add .gitattributes file a5d9db0cd0 cmake: build tests for UWP b7889a7389 winrt: use windowsio in non-libc mode ea8757a748 Make testaudiostreamdynamicresample compatible with emscripten 1a7a74fb2e cmake: build emscripten tests as html page 64d570f027 Add minimal http server for emscripten test apps 8e898c4a21 SDL_test does not parse --samples argument 91cd5478be audio: Fix resampler overflowing input buffer. f290c85b22 testaudiocapture: Make sure we convert captured audio to output format. b75c751dfc rwlock: Make generic implmentations work on single-threaded platforms. 80850af7ce The controller update complete events are no longer disabled by default 3f486224a9 Fixed refresh rate calculation for KMSDRM 342ec51131 Fix overflow when doing SDL_sscanf("%hd", ...) 9129e1d557 Fixed crash when setting the default cursor twice 8e99a4f4f5 Undo variable rename be67f0de10 Fixed crashes related to the default cursor on WinRT and KMSDRM 94b3f78c44 Fix out of bound read of 'has_hat' array 94f48f19b0 Use more specific build destinations when creating an xcframework dabd45997e Back out change supporting multiple names for binding elements efe15588d5 Relabel back paddles as left or right be884f0c95 ci: disable visionos.yml by renaming the file ac094d00f5 ci: add workflow_dispatch event to visionos workflow 9be9e2292b build: Consistently use pathlib APIs in cmake/xxd.py a9f6950657 Fixed deadlock shutting down Android sensors d9f09e77f2 Actually make the sensors magical! 690eae7d22 Implement visionOS support e385d6da0a Fixed build warning 6b93e788fa Improved sensor thread-safety 4ee0e5a984 Fixed thread-safety warnings 12deed91f8 Added information on how to enable thread-safety analysis 5735d2b03b coreaudio: Fixed assertion when device fails/quits mid-iteration. 1022fd6e04 testaudio: the test framework opens an audio device at startup; close it. 0714da37a4 audio: Fix audio stream callback calculations when future buffer has space. 917e036f6f MSVC has __declspec(deprecated) 279ff8909f Changed example code to avoid potential divide by zero 8a1afc9b10 Fixed Android not sending controller event timestamps 463c456b98 Fill the correct member with the joystick ID in SDL_EVENT_JOYSTICK_UPDATE_COMPLETE 55cf1abaa6 test: Don't flag testsurround as suitable for non-interactive use a2d594269c Fixed pixel format compatibility with SDL2 79a190aa23 Fixed setting invalid bpp for FOURCC formats in SDL_GetMasksForPixelFormatEnum() 8fdebdd3e0 Sync SDL3 wiki -> header b903ccf945 SDL_rwops read/write functions return size_t again c03f5b4b69 Fixed rounding up in SDL_PrintFloat 75a020aa6b Only query serial number and firmware versions from Sony PS5 controllers fa189d302e Added the Victrix Pro FS for PS4/PS5 to the controller list 26205b659d Fixed PS4/PS5 touchpad for third party controllers 6af0448af9 include: fixed a typo in SDL_RenderGetMetalCommandEncoder docs. f3cb46b083 SDL_thread.h: do not conflict with sdl2-compat::sdl3_include_wrapper.h 080b1dfbdb Revert "Improved fallback for SDL_COMPILE_TIME_ASSERT() (thanks @icculus!)" 9d453daa23 Improved fallback for SDL_COMPILE_TIME_ASSERT() (thanks @icculus!) 1fb2419882 Removed reference to renamed function e7d56dd0b2 audio: Renamed new API SDL_UnpauseAudioDevice to SDL_ResumeAudioDevice. 2b0c0f5b6b Don't pass NULL to strncmp 778e8185cd Fix size of memcpy in SDL_AudioDeviceFormatChangedAlreadyLocked And add diagnostic that allows to find this kind of issue in clang-tidy 4bb426abad Sync SDL3 wiki -> header 3a752ce650 Reapply "Changed 'freesrc' parameter from int to SDL_bool" to SDL_wave.c 2ba03b4db0 fix build after previous commit. 0026adffd4 apply force_align_arg_pointer attribute to correct version of SDL_RunApp 77446e2029 Unaligned stacks on i686-w64-mingw32 may lead to crashes d3bcc3f057 Fixed build errors when OpenGL isn't enabled 35ad68e126 Sync SDL3 wiki -> header 70323a8350 Add a function to display the system menu for a window be5f66c84e testaudio: Fixed soundboard icon, which had a colorkey issue. c0a88930bf Sync SDL3 wiki -> header 18c59cc969 Merge the SDL3 audio subsystem redesign! 99b0e31788 The Steam Controller D-Pad is only pressed when the button is pressed down 103073d694 Set NSBluetoothAlwaysUsageDescription for testcontroller ca02bb6c8c We don't need testdropfile-Info.plist e063f662e9 Enable the controller update complete events 06bea1eb55 Added a gamepad mapping for the G-Shark GS-GP702 5ca3c50bf0 testaudio: Fix compiler warning. 1b1f02c5aa testaudio: Apparently compilers don't like this possibly being NULL now...? 2de9253b6c test: Added testaudio fb3ab3f113 SDL_video.c: move ngage video before offscreen. 843572d993 Don't mark autorelease keys as virtual 648de4f9b8 Fixed duplicate key press/release events on iOS a8abe612ed Only pass keypresses up the responder chain when text input is active c3288d113e Synchronize on-screen keyboard state with text input active state 5fb92ef2f7 Fixed whitespace f5ea6ae18d Revert "Stop beep when running iOS apps on ARM-based Macs" 546508b9b4 Allow test programs to run at full resolution on iPads 68a4bb01e0 Allocate displays as an array of pointers instead of an array of objects 07578fde3d Fixed crash if a display is enumerated twice a509771a87 fix ios CI workflow after commit e4460e897f 72ce76905a The scheme isn't always the same as the framework name (e.g. xmp_lite vs xmp-lite) e4460e897f By default Xcode expects the framework target name to be the name of the project. ac683773dc Added missing tests to the "All" target 7dd56eaafe Removed unnecessary reference to testoverlay-Info.plist e1c7f524ef Reduce the number of times SDL3 is duplicated in the xcframework script 65538011ca Make Xcode targets more specific efe114c300 Revert "Renamed the xcframework target from "SDL.xcframework" to "xcframework"" 73ed1d21a9 Renamed the xcframework target from "SDL.xcframework" to "xcframework" 76b4d8a0d8 Build the Framework instead of a static library for iOS and tvOS d1bf979160 Removed unnecessary setting from the "Create DMG" target c94cb3a5d8 Simplified the Xcode project to a single Framework target ea60474c65 cmake: don't build SDL3-static Apple framework 8f00d7856d Sync SDL3 wiki -> header d4a867a256 Rename SDL_GetPath to SDL_GetUserFolder 71099149b8 Fall back to Xlib if XRandR isn't available b7f32f74ce Note the removal of the SDL_RENDERER_TARGETTEXTURE flag 0eda582160 testaudiostreamdynamicresample: Load sample.wav correctly. 87eae9a0a1 aaudio: We need a mixbuf on capture devices, too. fb68e84646 wayland: Fix memory leaks b0edd23c00 testsurround: Log available audio output devices at the start. ae3090c387 androidaudio: Move Init/bootstrap code to bottom of source code. 18fc0db9e5 aaudio: Rearranged source code to match other backends. 2507c1d68b aaudio: Disconnect playing devices if error callback fires. 32a3fc3783 aaudio: Use the callback interface. b49ce86765 audio: Fixed compiler warning on Android NDK. 1c074e8d97 android: Fixed audio device detection. 82ce05ad01 pulseaudio: Be more aggressive with hotplug thread synchronization. 5cbdf1168e androidaudio: Fixed incorrect JNI call (thanks, @madebr!) 660054f3dc include: Correct comment about audio device hotplug events. ab68428a64 aaudio: Fixed for older SDKs and Android releases. 5ff87c6d4a android: Reworked audio backends for SDL3 audio API. 54af687210 testautomation_audio.c: Patched to compile. :/ 5e82090662 testautomation_audio.c: Apparently we aren't updating test code for C99 atm. 7f4488f625 wasapi: More fixes for Clang warnings. 29a0c689c9 wasapi: Patched to compile with Clang. 4aa95c21bc pspaudio: Patched to compile. 9a2a0a1463 ps2audio: Delete errant character that got inserted before previous commit. 2c578bd0d5 qnxaudio: Rewrite for SDL3 audio APIs. 455eef4cd9 audio: Use AtomicAdd for device counts, don't treat as a refcount. 095ea57f94 pspaudio: Patched to compile. d7cf63db67 ps2audio: Patched to compile. 027b9e8787 coreaudio: (maybe) patched to compile on iOS. 4836c2db07 pspaudio: Patched to compile. 86ca412436 n3dsaudio: Patched to compile. 66bcee2ca9 testaudiostreamdynamicresample.c: Fixed MSVC compiler warning. dbf993d358 vitaaudio: patched to compile. 5707e14716 audio: Fix up some things that broke when rebasing the branch against main. 6567285eae SDL_migration.cocci: Fix up SDL_(Pause|Unpause)Audio. 0b6255551e test: Fixed incorrect SDL_OpenAudioDevice call in testautomation. 107fd941cd vitaaudio: Clean up correctly in CloseDevice. 9fa4a6ef87 netbsdaudio: Minor fix. b0d89868c6 n3dsaudio: Updated (but untested!) for SDL3 audio API. ba27176106 vitaaudio: Untested attempt to move Vita audio to SDL3's audio API. 0b58e96d9e wasapi: Patched WinRT to compile. d6b4f48488 visualc: Turn on multiprocessor compilation. c58d95c343 wasapi: Reworked for new SDL3 audio API, other win32 fixes. dc04f85646 audio: whoops, that should be an int. be0dc630b7 audio: Fixed incorrect assertion 77b3fb06ee directsound: First shot at updating for SDL3 audio API. 4399b71715 audio: Generalize how backends can lookup an SDL_AudioDevice. 2fb122fe46 audio: backends now "find" instead of "obtain" devices by handle. c3f5a5fc72 dummyaudio: SDL3ify style 7d65ff86e2 diskaudio: Adjusted for later SDL3 audio API redesign changes. 4ba9c2eade dummyaudio: Configurable delay, other SDL3 API fixes. fb395d3ad7 sndio: Updated to the SDL3 audio API. 1a55282051 dsp: Some minor logic fixes 6bc85577d7 netbsdaudio: Updated for SDL3 audio API. 0f6e59312b netbsdaudio: Removed email address from source code. 51ae78c0af haikuaudio: Updated for SDL3 audio API. fc7ed18ca1 emscriptenaudio: don't forget to finalize the audio thread 4233c41ce2 pulseaudio: Removed unnecessary variable. a0528cd5ed emscriptenaudio: Updated for SDL3 audio API. 79cc29ba35 wave: Don't check if format->channels > INT_MAX, it's a Uint16. 1bfe97c235 pspaudio: Updated for SDL3 audio API. 121a2dce15 audio: Make sure `device->hidden` is NULL after CloseDevice 3d6ba0cafd ps2audio: Removed free of buffer that hasn't been allocated yet. 00ed6f8827 test: Fixed compiler warnings for unused vars. 6f12f68ec9 ps2audio: SDL3ified the style 4993743a02 ps2audio: Renamed `_this` to `device` 74568cdb2b ps2audio: Updated (but untested) for SDL3 audio API. c83b68ef26 jack: renamed `_this` to `device`. 3f4f004794 audio: Remove an assertion that no longer makes sense. 86243b2589 jack: Use ProvidesOwnCallbackThread. 18906a32b8 jack: First shot at updating for SDL3 audio API. a2b488359e dsp: Removed debug logging 6fd71185cd dsp: Updated for new SDL3 audio API. 3482d1215a alsa: Don't ever block in CaptureFromDevice. 65d296ef1a audio: Use SDL_powerof2 instead of reinventing it. 409b544505 alsa: Updated for new SDL3 audio API 0999a090a7 audio: More tweaking of `device->thread_alive` f94ffd6092 audio: Fixed logic error 4deb2970c9 alsa: Renamed `_this` to `device` 0fb9e4baae audio: Remove no-longer-used SupportsNonPow2Samples c653e57768 coreaudio: rewritten for SDL3 audio redesign! 533777eff5 audio: SDL_sysaudio.h comment conversion. 8473e522e0 audio: unify device thread naming. 258bc9efed audio: PlayDevice now passes the buffer, too, for convenience. e518149d14 audio: Fixed locking in SDL_AudioDeviceDisconnected 22afa5735f audio: FreeDeviceHandle should pass the whole device, for convenience. 9e3c5f93e0 coreaudio: Change `_this` to `device` e969160de0 audio: unset a freed variable to NULL 1fc01b0300 audio: Try to definitely have a default device set up. b60a56d368 audio: take first reported device if no default was specified. a8323ebe68 audio: Better handling of ProvidesOwnCallbackThread backends. 1dffb72c1d pipewire: Hooked up default device change notifications. a93fcf2444 audio: fixed flushed stream reporting bytes but not being able to get them. ad6c1781fc pulseaudio: Minor cleanups. cfc8a0d17d pipewire: First shot at moving to the new SDL3 audio interfaces. 13202642a3 aaudio: Fixed capitialization, plus some minor cleanups. 3e9991b535 audio: Make sure we don't write to a NULL pointer. 943351affb pulseaudio: GetDefaultAudioInfo isn't a thing anymore. 11dfc4d737 test: Update testautomation_audio for SDL3 audio API. 29afc2e42b test: Update testresample for SDL3 audio API. 3a02eecced test: Update testsurround for SDL3 audio API. e1c78718d4 test: testaudiocapture is updated for the SDL3 audio API. f48cb716c2 pulseaudio: a couple minor tweaks. dac25fe9eb audio: Seperate audio capture into Wait/Read operations. 3e10c0005d audio: Capture devices should respect logical device pausing. 7e700531c5 audio: Allow SDL_OpenAudioDevice to accept a NULL spec. bb1cbbd33a test: Update testaudioinfo for SDL3 audio API. 883aee32c5 audio: Let default formats differ for output and capture devices. 62cf24eeb9 pulseaudio: Listen for server events in addition to sources and sinks. 924f370bd7 pulseaudio: Fix deadlock in HotplugThread. 5d4e9e5f80 test: Updated testaudiostreamdynamicresample to SDL3 audio API. f883b9fc64 test: Updated testaudiohotplug to SDL3 audio API. 2be5f726d4 audio: Removed debug logging. 323ecce123 docs: Added migration note about SDL_AUDIODEVICEREMOVED. 47b0321ebf test: Removed loopwavequeue.c; obsolete in SDL3. 0e5a1d4f29 pulseaudio: Removed debug logging. f598626e46 test: loopwave shouldn't use an audiostream callback. eee407caf8 docs: migration guide note that SDL_LoadWAV has a different return type. b03c493fc4 test: Updated testmultiaudio to new SDL3 audio API fe1daf6fb5 audio: Mark disconnected default devices as "zombies". cdd2ba81de audio: Fixed adding new physical devices to a double-linked list. db39cbf208 audio: Allow SDL_GetAudioDeviceFormat() to query the default devices. ee10bab3cd audio: An enormous amount of work on managing default devices. c7a44eea83 audio: Fixed logic error. 089cd87cb5 audio: Make sure device count stays correct as hardware disconnects. e50cb72eb6 docs: Note that audio opening doesn't implicitly init SDL now. 97b2f747d0 docs: Corrections to audio section of README-migration.md 464640440f audio: Added SDL_GetAudioStreamBinding. 01f7b53865 audio: Readded (logical) device pausing. fd4c9f4e11 audio: documentation improvements. 4b78b789a7 audio: Switch SDL_audio.c and SDL_audiocvt.c to C99-ish syntax. d96a1db7d7 audio: Opening via a logical device ID should also track default device. b2e020958f audio: Wrap device access in opening of logical devices. 7ee2459927 audio: Check for unlikely failure case in WAV loaded. 3d65a2cefe audio: Made SDL_LoadWAV a real function, not just a macro. 26525f5fd3 audio: Readd SDL_AudioSpec, but just with format/channels/freq fields. e6aaed7d79 include: Audio is not, and has not been, a raw mixing buffer for a long time. 56b1bc2198 audio: SDL_AudioStream now has callbacks for Get and Put operations. 905c4fff5b audio: First shot at the SDL3 audio subsystem redesign! b221b59995 cmake: add SDL_REVISION option 0500fca00c Add missing break d3f2de7f29 fixed typo in prev. patch. 12b35c6a46 test/testnativecocoa.m: fixed deprecation warnings. e24b3e2fa4 cmake: rename SDL_TEST -> SDL_TEST_LIBRARY da5016d336 cmake: use pkg-config + test compile instead of Find module for detecting rpi deec574ff6 cmake: fix SDL_HIDAPI_LIBUSB f2ae00c1ad Sync SDL3 wiki -> header 41a96c8133 doc: document building of SDL tests with CMake 3174d0b970 Sorted controller list 27b8abb056 Add Steam Deck controller mapping to database. 41d436f0fe Use SetWindowPos to show windows when SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN is set to avoid activating the parent window when showing a child window 0dc85f3078 Improved the documentation for the gamepad paddle buttons 2fff999a41 Try to create the dummy mouse cursor after video backend initialization d086d9874d Sync SDL3 wiki -> header bce598addd SDL_pixels.c: Fixed compiler warning on Android NDK. ad0c0d3cde Sync SDL3 wiki -> header f8e8dff7ee tests: Fix automated window grab and positioning tests under Wayland/XWayland 4cffbc3644 Add VS code directory to gitignore 666f81bace Add more endian-specific aliases for 32 bit pixelformats 4749df0a63 Just disable the 4214 warning instead of trying to change the structure definition git-subtree-dir: external/sdl/SDL git-subtree-split: ec0042081ea104d5dd0ee291105210e00a4fe3d9
2023-08-12 20:17:29 +02:00
#include <stdlib.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
#include <SDL3/SDL_test.h>
#include <SDL3/SDL_test_common.h>
#include <SDL3/SDL_main.h>
#include "testutils.h"
#define POOF_LIFETIME 250
typedef struct Texture
{
SDL_Texture *texture;
float w;
float h;
} Texture;
typedef enum ThingType
{
THING_NULL,
THING_PHYSDEV,
THING_PHYSDEV_CAPTURE,
THING_LOGDEV,
THING_LOGDEV_CAPTURE,
THING_TRASHCAN,
THING_STREAM,
THING_POOF,
THING_WAV
} ThingType;
typedef struct Thing Thing;
struct Thing
{
ThingType what;
union {
struct {
SDL_AudioDeviceID devid;
SDL_bool iscapture;
SDL_AudioSpec spec;
char *name;
} physdev;
struct {
SDL_AudioDeviceID devid;
SDL_bool iscapture;
SDL_AudioSpec spec;
Thing *physdev;
} logdev;
struct {
SDL_AudioSpec spec;
Uint8 *buf;
Uint32 buflen;
} wav;
struct {
float startw;
float starth;
float centerx;
float centery;
} poof;
struct {
SDL_AudioStream *stream;
int total_ticks;
Uint64 next_level_update;
Uint8 levels[5];
} stream;
} data;
Thing *line_connected_to;
char *titlebar;
SDL_FRect rect;
float z;
Uint8 r, g, b, a;
float progress;
float scale;
Uint64 createticks;
Texture *texture;
const ThingType *can_be_dropped_onto;
void (*ontick)(Thing *thing, Uint64 now);
void (*ondrag)(Thing *thing, int button, float x, float y);
void (*ondrop)(Thing *thing, int button, float x, float y);
void (*ondraw)(Thing *thing, SDL_Renderer *renderer);
Thing *prev;
Thing *next;
};
static Uint64 app_ready_ticks = 0;
static int done = 0;
static SDLTest_CommonState *state = NULL;
static Thing *things = NULL;
static char *current_titlebar = NULL;
static Thing *droppable_highlighted_thing = NULL;
static Thing *dragging_thing = NULL;
static int dragging_button = -1;
static Texture *physdev_texture = NULL;
static Texture *logdev_texture = NULL;
static Texture *audio_texture = NULL;
static Texture *trashcan_texture = NULL;
static Texture *soundboard_texture = NULL;
static Texture *soundboard_levels_texture = NULL;
static void DestroyTexture(Texture *tex);
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void Quit(int rc)
{
DestroyTexture(physdev_texture);
DestroyTexture(logdev_texture);
DestroyTexture(audio_texture);
DestroyTexture(trashcan_texture);
DestroyTexture(soundboard_texture);
DestroyTexture(soundboard_levels_texture);
SDLTest_CommonQuit(state);
/* Let 'main()' return normally */
if (rc != 0) {
exit(rc);
}
}
static char *xstrdup(const char *str)
{
char *ptr = SDL_strdup(str);
if (!ptr) {
SDL_Log("Out of memory!");
Quit(1);
}
return ptr;
}
static void *xalloc(const size_t len)
{
void *ptr = SDL_calloc(1, len);
if (!ptr) {
SDL_Log("Out of memory!");
Quit(1);
}
return ptr;
}
static void SetTitleBar(const char *fmt, ...)
{
char *newstr = NULL;
va_list ap;
va_start(ap, fmt);
SDL_vasprintf(&newstr, fmt, ap);
va_end(ap);
if (newstr && (!current_titlebar || (SDL_strcmp(current_titlebar, newstr) != 0))) {
SDL_SetWindowTitle(state->windows[0], newstr);
SDL_free(current_titlebar);
current_titlebar = newstr;
} else {
SDL_free(newstr);
}
}
static void SetDefaultTitleBar(void)
{
SetTitleBar("testaudio: %s", SDL_GetCurrentAudioDriver());
}
static Thing *FindThingAtPoint(const float x, const float y)
{
const SDL_FPoint pt = { x, y };
Thing *retval = NULL;
Thing *i;
for (i = things; i != NULL; i = i->next) {
if ((i != dragging_thing) && SDL_PointInRectFloat(&pt, &i->rect)) {
retval = i; /* keep going, though, because things drawn on top are later in the list. */
}
}
return retval;
}
static Thing *UpdateMouseOver(const float x, const float y)
{
Thing *thing;
if (dragging_thing) {
thing = dragging_thing;
} else {
thing = FindThingAtPoint(x, y);
}
if (!thing) {
SetDefaultTitleBar();
} else if (thing->titlebar) {
SetTitleBar("%s", thing->titlebar);
}
return thing;
}
static Thing *CreateThing(ThingType what, float x, float y, float z, float w, float h, Texture *texture, char *titlebar)
{
Thing *last = NULL;
Thing *i;
Thing *thing;
thing = (Thing *) xalloc(sizeof (Thing));
if ((w < 0) || (h < 0)) {
SDL_assert(texture != NULL);
if (w < 0) {
w = texture->w;
}
if (h < 0) {
h = texture->h;
}
}
thing->what = what;
thing->rect.x = x;
thing->rect.y = y;
thing->rect.w = w;
thing->rect.h = h;
thing->z = z;
thing->r = 255;
thing->g = 255;
thing->b = 255;
thing->a = 255;
thing->scale = 1.0f;
thing->createticks = SDL_GetTicks();
thing->texture = texture;
thing->titlebar = titlebar;
/* insert in list by Z order (furthest from the "camera" first, so they get drawn over; negative Z is not drawn at all). */
if (things == NULL) {
things = thing;
return thing;
}
for (i = things; i != NULL; i = i->next) {
if (z > i->z) { /* insert here. */
thing->next = i;
thing->prev = i->prev;
SDL_assert(i->prev == last);
if (i->prev) {
i->prev->next = thing;
} else {
SDL_assert(i == things);
things = thing;
}
i->prev = thing;
return thing;
}
last = i;
}
if (last) {
last->next = thing;
thing->prev = last;
}
return thing;
}
static void DestroyThing(Thing *thing)
{
if (!thing) {
return;
}
switch (thing->what) {
case THING_POOF: break;
case THING_NULL: break;
case THING_TRASHCAN: break;
case THING_LOGDEV:
case THING_LOGDEV_CAPTURE:
SDL_CloseAudioDevice(thing->data.logdev.devid);
break;
case THING_PHYSDEV:
case THING_PHYSDEV_CAPTURE:
SDL_free(thing->data.physdev.name);
break;
case THING_WAV:
SDL_free(thing->data.wav.buf);
break;
case THING_STREAM:
SDL_DestroyAudioStream(thing->data.stream.stream);
break;
}
if (thing->prev) {
thing->prev->next = thing->next;
} else {
SDL_assert(thing == things);
things = thing->next;
}
if (thing->next) {
thing->next->prev = thing->prev;
}
SDL_free(thing->titlebar);
SDL_free(thing);
}
static void DrawOneThing(SDL_Renderer *renderer, Thing *thing)
{
SDL_FRect dst;
SDL_memcpy(&dst, &thing->rect, sizeof (SDL_FRect));
if (thing->scale != 1.0f) {
const float centerx = thing->rect.x + (thing->rect.w / 2);
const float centery = thing->rect.y + (thing->rect.h / 2);
SDL_assert(thing->texture != NULL);
dst.w = thing->texture->w * thing->scale;
dst.h = thing->texture->h * thing->scale;
dst.x = centerx - (dst.w / 2);
dst.y = centery - (dst.h / 2);
}
if (thing->texture) {
if (droppable_highlighted_thing == thing) {
SDL_SetRenderDrawColor(renderer, 255, 0, 255, 100);
SDL_RenderFillRect(renderer, &dst);
}
SDL_SetRenderDrawColor(renderer, thing->r, thing->g, thing->b, thing->a);
SDL_RenderTexture(renderer, thing->texture->texture, NULL, &dst);
} else {
SDL_SetRenderDrawColor(renderer, thing->r, thing->g, thing->b, thing->a);
SDL_RenderFillRect(renderer, &dst);
}
if (thing->ondraw) {
thing->ondraw(thing, renderer);
}
if (thing->progress > 0.0f) {
SDL_FRect r = { thing->rect.x, thing->rect.y + (thing->rect.h + 2.0f), 0.0f, 10.0f };
r.w = thing->rect.w * ((thing->progress > 1.0f) ? 1.0f : thing->progress);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 128);
SDL_RenderFillRect(renderer, &r);
}
}
static void DrawThings(SDL_Renderer *renderer)
{
Thing *i;
/* draw connecting lines first, so they're behind everything else. */
for (i = things; i && (i->z >= 0.0f); i = i->next) {
Thing *dst = i->line_connected_to;
if (dst) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderLine(renderer, i->rect.x + (i->rect.w / 2), i->rect.y + (i->rect.h / 2), dst->rect.x + (dst->rect.w / 2), dst->rect.y + (dst->rect.h / 2));
}
}
/* Draw the actual things. */
for (i = things; i && (i->z >= 0.0f); i = i->next) {
if (i != dragging_thing) {
DrawOneThing(renderer, i);
}
}
if (dragging_thing) {
DrawOneThing(renderer, dragging_thing); /* draw last so it's always on top. */
}
}
static void Draw(void)
{
SDL_Renderer *renderer = state->renderers[0];
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 64, 0, 64, 255);
SDL_RenderClear(renderer);
DrawThings(renderer);
SDL_RenderPresent(renderer);
}
static void RepositionRowOfThings(const ThingType what, const float y)
{
int total_things = 0;
float texw = 0.0f;
float texh = 0.0f;
Thing *i;
for (i = things; i != NULL; i = i->next) {
if (i->what == what) {
texw = i->rect.w;
texh = i->rect.h;
total_things++;
}
}
if (total_things > 0) {
int w, h;
SDL_GetWindowSize(state->windows[0], &w, &h);
const float spacing = w / ((float) total_things);
float x = (spacing - texw) / 2.0f;
for (i = things; i != NULL; i = i->next) {
if (i->what == what) {
i->rect.x = x;
i->rect.y = (y >= 0.0f) ? y : ((h + y) - texh);
x += spacing;
}
}
}
}
static const char *AudioFmtToString(const SDL_AudioFormat fmt)
{
switch (fmt) {
#define FMTCASE(x) case SDL_AUDIO_##x: return #x
FMTCASE(U8);
FMTCASE(S8);
FMTCASE(S16LSB);
FMTCASE(S16MSB);
FMTCASE(S32LSB);
FMTCASE(S32MSB);
FMTCASE(F32LSB);
FMTCASE(F32MSB);
#undef FMTCASE
}
return "?";
}
static const char *AudioChansToStr(const int channels)
{
switch (channels) {
case 1: return "mono";
case 2: return "stereo";
case 3: return "2.1";
case 4: return "quad";
case 5: return "4.1";
case 6: return "5.1";
case 7: return "6.1";
case 8: return "7.1";
default: break;
}
return "?";
}
static void PoofThing_ondrag(Thing *thing, int button, float x, float y)
{
dragging_thing = NULL; /* refuse to be dragged. */
}
static void PoofThing_ontick(Thing *thing, Uint64 now)
{
const int lifetime = POOF_LIFETIME;
const int elasped = (int) (now - thing->createticks);
if (elasped > lifetime) {
DestroyThing(thing);
} else {
const float pct = ((float) elasped) / ((float) lifetime);
thing->a = (Uint8) (int) (255.0f - (pct * 255.0f));
thing->scale = 1.0f - pct; /* shrink to nothing! */
}
}
static Thing *CreatePoofThing(Thing *poofing_thing)
{
const float centerx = poofing_thing->rect.x + (poofing_thing->rect.w / 2);
const float centery = poofing_thing->rect.y + (poofing_thing->rect.h / 2);
const float z = poofing_thing->z;
Thing *thing = CreateThing(THING_POOF, poofing_thing->rect.x, poofing_thing->rect.y, z, poofing_thing->rect.w, poofing_thing->rect.h, poofing_thing->texture, NULL);
thing->data.poof.startw = poofing_thing->rect.w;
thing->data.poof.starth = poofing_thing->rect.h;
thing->data.poof.centerx = centerx;
thing->data.poof.centery = centery;
thing->ontick = PoofThing_ontick;
thing->ondrag = PoofThing_ondrag;
return thing;
}
static void DestroyThingInPoof(Thing *thing)
{
if (thing) {
if (thing->what != THING_POOF) {
CreatePoofThing(thing);
}
DestroyThing(thing);
}
}
/* this poofs a thing and additionally poofs all things connected to the thing. */
static void TrashThing(Thing *thing)
{
Thing *i, *next;
for (i = things; i != NULL; i = next) {
next = i->next;
if (i->line_connected_to == thing) {
TrashThing(i);
next = things; /* start over in case this blew up the list. */
}
}
DestroyThingInPoof(thing);
}
static void StreamThing_ontick(Thing *thing, Uint64 now)
{
if (!thing->line_connected_to) {
return;
}
/* are we playing? See if we're done, or update state. */
if (thing->line_connected_to->what == THING_LOGDEV) {
const int available = SDL_GetAudioStreamAvailable(thing->data.stream.stream);
SDL_AudioSpec spec;
if (!available || (SDL_GetAudioStreamFormat(thing->data.stream.stream, NULL, &spec) < 0)) {
DestroyThingInPoof(thing);
} else {
const int ticksleft = (int) ((((Uint64) ((available / (SDL_AUDIO_BITSIZE(spec.format) / 8)) / spec.channels)) * 1000) / spec.freq);
const float pct = thing->data.stream.total_ticks ? (((float) (ticksleft)) / ((float) thing->data.stream.total_ticks)) : 0.0f;
thing->progress = 1.0f - pct;
}
}
if (thing->data.stream.next_level_update <= now) {
Uint64 perf = SDL_GetPerformanceCounter();
int i;
for (i = 0; i < SDL_arraysize(thing->data.stream.levels); i++) {
thing->data.stream.levels[i] = (Uint8) (perf % 6);
perf >>= 3;
}
thing->data.stream.next_level_update += 150;
}
}
static void StreamThing_ondrag(Thing *thing, int button, float x, float y)
{
if (button == SDL_BUTTON_RIGHT) { /* this is kinda hacky, but use this to disconnect from a playing source. */
if (thing->line_connected_to) {
SDL_UnbindAudioStream(thing->data.stream.stream); /* unbind from current device */
thing->line_connected_to = NULL;
}
}
}
static void StreamThing_ondrop(Thing *thing, int button, float x, float y)
{
if (droppable_highlighted_thing) {
if (droppable_highlighted_thing->what == THING_TRASHCAN) {
TrashThing(thing);
} else if (((droppable_highlighted_thing->what == THING_LOGDEV) || (droppable_highlighted_thing->what == THING_LOGDEV_CAPTURE)) && (droppable_highlighted_thing != thing->line_connected_to)) {
/* connect to a logical device! */
SDL_Log("Binding audio stream ('%s') to logical device %u", thing->titlebar, (unsigned int) droppable_highlighted_thing->data.logdev.devid);
if (thing->line_connected_to) {
const SDL_AudioSpec *spec = &droppable_highlighted_thing->data.logdev.spec;
SDL_UnbindAudioStream(thing->data.stream.stream); /* unbind from current device */
if (thing->line_connected_to->what == THING_LOGDEV_CAPTURE) {
SDL_FlushAudioStream(thing->data.stream.stream);
thing->data.stream.total_ticks = (int) (((((Uint64) (SDL_GetAudioStreamAvailable(thing->data.stream.stream) / (SDL_AUDIO_BITSIZE(spec->format) / 8))) / spec->channels) * 1000) / spec->freq);
}
}
SDL_BindAudioStream(droppable_highlighted_thing->data.logdev.devid, thing->data.stream.stream); /* bind to new device! */
thing->progress = 0.0f; /* ontick will adjust this if we're on an output device.*/
thing->data.stream.next_level_update = SDL_GetTicks() + 100;
thing->line_connected_to = droppable_highlighted_thing;
}
}
}
static void StreamThing_ondraw(Thing *thing, SDL_Renderer *renderer)
{
if (thing->line_connected_to) { /* are we playing? Update progress bar, and bounce the levels a little. */
static const float xlocs[5] = { 18, 39, 59, 79, 99 };
static const float ylocs[5] = { 49, 39, 29, 19, 10 };
const float blockw = soundboard_levels_texture->w;
const float blockh = soundboard_levels_texture->h / 5.0f;
int i, j;
SDL_SetRenderDrawColor(renderer, thing->r, thing->g, thing->b, thing->a);
for (i = 0; i < SDL_arraysize(thing->data.stream.levels); i++) {
const int level = (int) thing->data.stream.levels[i];
const float x = xlocs[i];
for (j = 0; j < level; j++) {
const SDL_FRect src = { 0, soundboard_levels_texture->h - ((j+1) * blockh), blockw, blockh };
const SDL_FRect dst = { thing->rect.x + x, thing->rect.y + ylocs[j], blockw, blockh };
SDL_RenderTexture(renderer, soundboard_levels_texture->texture, &src, &dst);
}
}
}
}
static Thing *CreateStreamThing(const SDL_AudioSpec *spec, const Uint8 *buf, const Uint32 buflen, const char *fname, const float x, const float y)
{
static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_LOGDEV, THING_LOGDEV_CAPTURE, THING_NULL };
Thing *thing = CreateThing(THING_STREAM, x, y, 0, -1, -1, soundboard_texture, fname ? xstrdup(fname) : NULL);
SDL_Log("Adding audio stream for %s", fname ? fname : "(null)");
thing->data.stream.stream = SDL_CreateAudioStream(spec, spec);
if (buf && buflen) {
SDL_PutAudioStreamData(thing->data.stream.stream, buf, (int) buflen);
SDL_FlushAudioStream(thing->data.stream.stream);
thing->data.stream.total_ticks = (int) (((((Uint64) (SDL_GetAudioStreamAvailable(thing->data.stream.stream) / (SDL_AUDIO_BITSIZE(spec->format) / 8))) / spec->channels) * 1000) / spec->freq);
}
thing->ontick = StreamThing_ontick;
thing->ondrag = StreamThing_ondrag;
thing->ondrop = StreamThing_ondrop;
thing->ondraw = StreamThing_ondraw;
thing->can_be_dropped_onto = can_be_dropped_onto;
return thing;
}
static void WavThing_ondrag(Thing *thing, int button, float x, float y)
{
if (button == SDL_BUTTON_RIGHT) { /* drag out a new audio stream. */
dragging_thing = CreateStreamThing(&thing->data.wav.spec, thing->data.wav.buf, thing->data.wav.buflen, thing->titlebar, x - (thing->rect.w / 2), y - (thing->rect.h / 2));
}
}
static void WavThing_ondrop(Thing *thing, int button, float x, float y)
{
if (droppable_highlighted_thing) {
if (droppable_highlighted_thing->what == THING_TRASHCAN) {
TrashThing(thing);
}
}
}
static Thing *LoadWavThing(const char *fname, float x, float y)
{
Thing *thing = NULL;
char *path;
SDL_AudioSpec spec;
Uint8 *buf = NULL;
Uint32 buflen = 0;
path = GetNearbyFilename(fname);
if (path) {
fname = path;
}
if (SDL_LoadWAV(fname, &spec, &buf, &buflen) == 0) {
static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
char *titlebar = NULL;
const char *nodirs = SDL_strrchr(fname, '/');
#ifdef __WINDOWS__
const char *nodirs2 = SDL_strrchr(nodirs ? nodirs : fname, '\\');
if (nodirs2) {
nodirs = nodirs2;
}
#endif
SDL_Log("Adding WAV file '%s'", fname);
if (nodirs) {
nodirs++;
} else {
nodirs = fname;
}
SDL_asprintf(&titlebar, "WAV file (\"%s\", %s, %s, %uHz)", nodirs, AudioFmtToString(spec.format), AudioChansToStr(spec.channels), (unsigned int) spec.freq);
thing = CreateThing(THING_WAV, x - (audio_texture->w / 2), y - (audio_texture->h / 2), 5, -1, -1, audio_texture, titlebar);
SDL_memcpy(&thing->data.wav.spec, &spec, sizeof (SDL_AudioSpec));
thing->data.wav.buf = buf;
thing->data.wav.buflen = buflen;
thing->can_be_dropped_onto = can_be_dropped_onto;
thing->ondrag = WavThing_ondrag;
thing->ondrop = WavThing_ondrop;
}
SDL_free(path);
return thing;
}
static Thing *LoadStockWavThing(const char *fname)
{
char *path = GetNearbyFilename(fname);
Thing *thing = LoadWavThing(path ? path : fname, 0.0f, 0.0f); /* will reposition in a moment. */
SDL_free(path);
return thing;
}
static void LoadStockWavThings(void)
{
LoadStockWavThing("sample.wav");
RepositionRowOfThings(THING_WAV, -10.0f);
}
static void DestroyTexture(Texture *tex)
{
if (tex) {
SDL_DestroyTexture(tex->texture);
SDL_free(tex);
}
}
static Texture *CreateTexture(const char *fname)
{
Texture *tex = (Texture *) xalloc(sizeof (Texture));
int texw, texh;
tex->texture = LoadTexture(state->renderers[0], fname, SDL_TRUE, &texw, &texh);
if (!tex->texture) {
SDL_Log("Failed to load '%s': %s", fname, SDL_GetError());
SDL_free(tex);
Quit(1);
}
SDL_SetTextureBlendMode(tex->texture, SDL_BLENDMODE_BLEND);
tex->w = (float) texw;
tex->h = (float) texh;
return tex;
}
static Thing *CreateLogicalDeviceThing(Thing *parent, const SDL_AudioDeviceID which, const float x, const float y);
static void DeviceThing_ondrag(Thing *thing, int button, float x, float y)
{
if ((button == SDL_BUTTON_MIDDLE) && (thing->what == THING_LOGDEV_CAPTURE)) { /* drag out a new stream. This is a UX mess. :/ */
dragging_thing = CreateStreamThing(&thing->data.logdev.spec, NULL, 0, NULL, x, y);
dragging_thing->data.stream.next_level_update = SDL_GetTicks() + 100;
SDL_BindAudioStream(thing->data.logdev.devid, dragging_thing->data.stream.stream); /* bind to new device! */
dragging_thing->line_connected_to = thing;
} else if (button == SDL_BUTTON_RIGHT) { /* drag out a new logical device. */
const SDL_AudioDeviceID which = ((thing->what == THING_LOGDEV) || (thing->what == THING_LOGDEV_CAPTURE)) ? thing->data.logdev.devid : thing->data.physdev.devid;
const SDL_AudioDeviceID devid = SDL_OpenAudioDevice(which, NULL);
dragging_thing = devid ? CreateLogicalDeviceThing(thing, devid, x - (thing->rect.w / 2), y - (thing->rect.h / 2)) : NULL;
}
}
static void SetLogicalDeviceTitlebar(Thing *thing)
{
SDL_AudioSpec *spec = &thing->data.logdev.spec;
SDL_GetAudioDeviceFormat(thing->data.logdev.devid, spec);
SDL_asprintf(&thing->titlebar, "Logical device #%u (%s, %s, %s, %uHz)", (unsigned int) thing->data.logdev.devid, thing->data.logdev.iscapture ? "CAPTURE" : "OUTPUT", AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq);
}
static void LogicalDeviceThing_ondrop(Thing *thing, int button, float x, float y)
{
if (droppable_highlighted_thing) {
if (droppable_highlighted_thing->what == THING_TRASHCAN) {
TrashThing(thing);
}
}
}
static Thing *CreateLogicalDeviceThing(Thing *parent, const SDL_AudioDeviceID which, const float x, const float y)
{
static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
Thing *physthing = ((parent->what == THING_LOGDEV) || (parent->what == THING_LOGDEV_CAPTURE)) ? parent->data.logdev.physdev : parent;
const SDL_bool iscapture = physthing->data.physdev.iscapture;
Thing *thing;
SDL_Log("Adding logical audio device %u", (unsigned int) which);
thing = CreateThing(iscapture ? THING_LOGDEV_CAPTURE : THING_LOGDEV, x, y, 5, -1, -1, logdev_texture, NULL);
thing->data.logdev.devid = which;
thing->data.logdev.iscapture = iscapture;
thing->data.logdev.physdev = physthing;
thing->line_connected_to = physthing;
thing->ondrag = DeviceThing_ondrag;
thing->ondrop = LogicalDeviceThing_ondrop;
thing->can_be_dropped_onto = can_be_dropped_onto;
SetLogicalDeviceTitlebar(thing);
return thing;
}
static void SetPhysicalDeviceTitlebar(Thing *thing)
{
SDL_AudioSpec *spec = &thing->data.physdev.spec;
SDL_GetAudioDeviceFormat(thing->data.physdev.devid, spec);
if (thing->data.physdev.devid == SDL_AUDIO_DEVICE_DEFAULT_CAPTURE) {
SDL_asprintf(&thing->titlebar, "Default system device (CAPTURE, %s, %s, %uHz)", AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq);
} else if (thing->data.physdev.devid == SDL_AUDIO_DEVICE_DEFAULT_OUTPUT) {
SDL_asprintf(&thing->titlebar, "Default system device (OUTPUT, %s, %s, %uHz)", AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq);
} else {
SDL_asprintf(&thing->titlebar, "Physical device #%u (%s, \"%s\", %s, %s, %uHz)", (unsigned int) thing->data.physdev.devid, thing->data.physdev.iscapture ? "CAPTURE" : "OUTPUT", thing->data.physdev.name, AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq);
}
}
static void PhysicalDeviceThing_ondrop(Thing *thing, int button, float x, float y)
{
if (droppable_highlighted_thing) {
if (droppable_highlighted_thing->what == THING_TRASHCAN) {
TrashThing(thing);
}
}
}
static void PhysicalDeviceThing_ontick(Thing *thing, Uint64 now)
{
const int lifetime = POOF_LIFETIME;
const int elasped = (int) (now - thing->createticks);
if (elasped > lifetime) {
thing->scale = 1.0f;
thing->a = 255;
thing->ontick = NULL; /* no more ticking. */
} else {
const float pct = ((float) elasped) / ((float) lifetime);
thing->a = (Uint8) (int) (pct * 255.0f);
thing->scale = pct; /* grow to normal size */
}
}
static Thing *CreatePhysicalDeviceThing(const SDL_AudioDeviceID which, const SDL_bool iscapture)
{
static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
static float next_physdev_x = 0;
Thing *thing;
int winw, winh;
SDL_GetWindowSize(state->windows[0], &winw, &winh);
if (next_physdev_x > (winw-physdev_texture->w)) {
next_physdev_x = 0;
}
SDL_Log("Adding physical audio device %u", (unsigned int) which);
thing = CreateThing(iscapture ? THING_PHYSDEV_CAPTURE : THING_PHYSDEV, next_physdev_x, 170, 5, -1, -1, physdev_texture, NULL);
thing->data.physdev.devid = which;
thing->data.physdev.iscapture = iscapture;
thing->data.physdev.name = SDL_GetAudioDeviceName(which);
thing->ondrag = DeviceThing_ondrag;
thing->ondrop = PhysicalDeviceThing_ondrop;
thing->ontick = PhysicalDeviceThing_ontick;
thing->can_be_dropped_onto = can_be_dropped_onto;
SetPhysicalDeviceTitlebar(thing);
if (SDL_GetTicks() <= (app_ready_ticks + 2000)) { /* assume this is the initial batch if it happens in the first two seconds. */
RepositionRowOfThings(THING_PHYSDEV, 10.0f); /* don't rearrange them after the initial add. */
RepositionRowOfThings(THING_PHYSDEV_CAPTURE, 170.0f); /* don't rearrange them after the initial add. */
next_physdev_x = 0.0f;
} else {
next_physdev_x += physdev_texture->w * 1.5f;
}
return thing;
}
static Thing *CreateTrashcanThing(void)
{
int winw, winh;
SDL_GetWindowSize(state->windows[0], &winw, &winh);
return CreateThing(THING_TRASHCAN, winw - trashcan_texture->w, winh - trashcan_texture->h, 10, -1, -1, trashcan_texture, "Drag things here to remove them.");
}
static Thing *CreateDefaultPhysicalDevice(const SDL_bool iscapture)
{
return CreatePhysicalDeviceThing(iscapture ? SDL_AUDIO_DEVICE_DEFAULT_CAPTURE : SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, iscapture);
}
static void TickThings(void)
{
Thing *i;
Thing *next;
const Uint64 now = SDL_GetTicks();
for (i = things; i != NULL; i = next) {
next = i->next; /* in case this deletes itself. */
if (i->ontick) {
i->ontick(i, now);
}
}
}
static void WindowResized(const int newwinw, const int newwinh)
{
Thing *i;
const float neww = (float) newwinw;
const float newh = (float) newwinh;
const float oldw = (float) state->window_w;
const float oldh = (float) state->window_h;
for (i = things; i != NULL; i = i->next) {
const float halfw = i->rect.w / 2.0f;
const float halfh = i->rect.h / 2.0f;
const float x = (i->rect.x + halfw) / oldw;
const float y = (i->rect.y + halfh) / oldh;
i->rect.x = (x * neww) - halfw;
i->rect.y = (y * newh) - halfh;
}
state->window_w = newwinw;
state->window_h = newwinh;
}
static void Loop(void)
{
SDL_Event event;
SDL_bool saw_event = SDL_FALSE;
if (app_ready_ticks == 0) {
app_ready_ticks = SDL_GetTicks();
}
while (SDL_PollEvent(&event)) {
Thing *thing = NULL;
saw_event = SDL_TRUE;
switch (event.type) {
case SDL_EVENT_MOUSE_MOTION:
thing = UpdateMouseOver(event.motion.x, event.motion.y);
if ((dragging_button == -1) && event.motion.state) {
if (event.motion.state & SDL_BUTTON_LMASK) {
dragging_button = SDL_BUTTON_LEFT;
} else if (event.motion.state & SDL_BUTTON_RMASK) {
dragging_button = SDL_BUTTON_RIGHT;
} else if (event.motion.state & SDL_BUTTON_MMASK) {
dragging_button = SDL_BUTTON_MIDDLE;
}
if (dragging_button != -1) {
dragging_thing = thing;
if (thing && thing->ondrag) {
thing->ondrag(thing, dragging_button, event.motion.x, event.motion.y);
}
}
}
droppable_highlighted_thing = NULL;
if (dragging_thing) {
dragging_thing->rect.x = event.motion.x - (dragging_thing->rect.w / 2);
dragging_thing->rect.y = event.motion.y - (dragging_thing->rect.h / 2);
if (dragging_thing->can_be_dropped_onto) {
thing = FindThingAtPoint(event.motion.x, event.motion.y);
if (thing) {
int i;
for (i = 0; dragging_thing->can_be_dropped_onto[i]; i++) {
if (dragging_thing->can_be_dropped_onto[i] == thing->what) {
droppable_highlighted_thing = thing;
break;
}
}
}
}
}
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
thing = UpdateMouseOver(event.button.x, event.button.y);
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
if (dragging_button == event.button.button) {
Thing *dropped_thing = dragging_thing;
dragging_thing = NULL;
dragging_button = -1;
if (dropped_thing && dropped_thing->ondrop) {
dropped_thing->ondrop(dropped_thing, event.button.button, event.button.x, event.button.y);
}
droppable_highlighted_thing = NULL;
}
thing = UpdateMouseOver(event.button.x, event.button.y);
break;
case SDL_EVENT_MOUSE_WHEEL:
UpdateMouseOver(event.wheel.mouseX, event.wheel.mouseY);
break;
case SDL_EVENT_DROP_FILE:
SDL_Log("Drop file! '%s'", event.drop.file);
LoadWavThing(event.drop.file, event.drop.x, event.drop.y);
/* SDLTest_CommonEvent will free the string, below. */
break;
case SDL_EVENT_WINDOW_RESIZED:
WindowResized(event.window.data1, event.window.data2);
break;
case SDL_EVENT_AUDIO_DEVICE_ADDED:
CreatePhysicalDeviceThing(event.adevice.which, event.adevice.iscapture);
break;
case SDL_EVENT_AUDIO_DEVICE_REMOVED: {
const SDL_AudioDeviceID which = event.adevice.which;
Thing *i, *next;
SDL_Log("Removing audio device %u", (unsigned int) which);
for (i = things; i != NULL; i = next) {
next = i->next;
if (((i->what == THING_PHYSDEV) || (i->what == THING_PHYSDEV_CAPTURE)) && (i->data.physdev.devid == which)) {
TrashThing(i);
next = things; /* in case we mangled the list. */
} else if (((i->what == THING_LOGDEV) || (i->what == THING_LOGDEV_CAPTURE)) && (i->data.logdev.devid == which)) {
TrashThing(i);
next = things; /* in case we mangled the list. */
}
}
break;
}
default: break;
}
SDLTest_CommonEvent(state, &event, &done);
}
TickThings();
Draw();
if (!saw_event) {
SDL_Delay(10);
}
#ifdef __EMSCRIPTEN__
if (done) {
emscripten_cancel_main_loop();
}
#endif
}
int main(int argc, char *argv[])
{
int i;
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO | SDL_INIT_AUDIO);
if (state == NULL) {
Quit(1);
}
state->window_flags |= SDL_WINDOW_RESIZABLE;
for (i = 1; i < argc;) {
int consumed = SDLTest_CommonArg(state, i);
if (consumed == 0) {
consumed = -1;
/* add our own command lines here. */
}
if (consumed < 0) {
static const char *options[] = {
/* add our own command lines here. */
/*"[--blend none|blend|add|mod|mul|sub]",*/
NULL
};
SDLTest_CommonLogUsage(state, argv[0], options);
Quit(1);
}
i += consumed;
}
if (!SDLTest_CommonInit(state)) {
Quit(2);
}
if (state->audio_id) {
SDL_CloseAudioDevice(state->audio_id);
state->audio_id = 0;
}
SetDefaultTitleBar();
physdev_texture = CreateTexture("physaudiodev.bmp");
logdev_texture = CreateTexture("logaudiodev.bmp");
audio_texture = CreateTexture("audiofile.bmp");
trashcan_texture = CreateTexture("trashcan.bmp");
soundboard_texture = CreateTexture("soundboard.bmp");
soundboard_levels_texture = CreateTexture("soundboard_levels.bmp");
LoadStockWavThings();
CreateTrashcanThing();
CreateDefaultPhysicalDevice(SDL_FALSE);
CreateDefaultPhysicalDevice(SDL_TRUE);
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(Loop, 0, 1);
#else
while (!done) {
Loop();
}
#endif
Quit(0);
return 0;
}