testaudio.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. #define SDL_MAIN_USE_CALLBACKS 1
  2. #include <SDL3/SDL_test.h>
  3. #include <SDL3/SDL_test_common.h>
  4. #include <SDL3/SDL_main.h>
  5. #include "testutils.h"
  6. #define POOF_LIFETIME 250
  7. #define VISUALIZER_WIDTH 100
  8. #define VISUALIZER_HEIGHT 50
  9. typedef struct Texture
  10. {
  11. SDL_Texture *texture;
  12. float w;
  13. float h;
  14. } Texture;
  15. typedef enum ThingType
  16. {
  17. THING_NULL,
  18. THING_PHYSDEV,
  19. THING_PHYSDEV_CAPTURE,
  20. THING_LOGDEV,
  21. THING_LOGDEV_CAPTURE,
  22. THING_TRASHCAN,
  23. THING_STREAM,
  24. THING_POOF,
  25. THING_WAV
  26. } ThingType;
  27. typedef struct Thing Thing;
  28. struct Thing
  29. {
  30. ThingType what;
  31. union {
  32. struct {
  33. SDL_AudioDeviceID devid;
  34. SDL_bool iscapture;
  35. SDL_AudioSpec spec;
  36. char *name;
  37. } physdev;
  38. struct {
  39. SDL_AudioDeviceID devid;
  40. SDL_bool iscapture;
  41. SDL_AudioSpec spec;
  42. Thing *physdev;
  43. SDL_bool visualizer_enabled;
  44. SDL_bool visualizer_updated;
  45. SDL_Texture *visualizer;
  46. SDL_Mutex *postmix_lock;
  47. float *postmix_buffer;
  48. int postmix_buflen;
  49. int postmix_allocated;
  50. SDL_AudioSpec postmix_spec;
  51. SDL_AtomicInt postmix_updated;
  52. } logdev;
  53. struct {
  54. SDL_AudioSpec spec;
  55. Uint8 *buf;
  56. Uint32 buflen;
  57. } wav;
  58. struct {
  59. float startw;
  60. float starth;
  61. float centerx;
  62. float centery;
  63. } poof;
  64. struct {
  65. SDL_AudioStream *stream;
  66. int total_bytes;
  67. Uint64 next_level_update;
  68. Uint8 levels[5];
  69. } stream;
  70. } data;
  71. Thing *line_connected_to;
  72. char *titlebar;
  73. SDL_FRect rect;
  74. float z;
  75. Uint8 r, g, b, a;
  76. float progress;
  77. float scale;
  78. Uint64 createticks;
  79. Texture *texture;
  80. const ThingType *can_be_dropped_onto;
  81. void (*ontick)(Thing *thing, Uint64 now);
  82. void (*ondrag)(Thing *thing, int button, float x, float y);
  83. void (*ondrop)(Thing *thing, int button, float x, float y);
  84. void (*ondraw)(Thing *thing, SDL_Renderer *renderer);
  85. Thing *prev;
  86. Thing *next;
  87. };
  88. static Uint64 app_ready_ticks = 0;
  89. static SDLTest_CommonState *state = NULL;
  90. static Thing *things = NULL;
  91. static char *current_titlebar = NULL;
  92. static Thing *mouseover_thing = NULL;
  93. static Thing *droppable_highlighted_thing = NULL;
  94. static Thing *dragging_thing = NULL;
  95. static int dragging_button = -1;
  96. static int dragging_button_real = -1;
  97. static SDL_bool ctrl_held = SDL_FALSE;
  98. static SDL_bool alt_held = SDL_FALSE;
  99. static Texture *physdev_texture = NULL;
  100. static Texture *logdev_texture = NULL;
  101. static Texture *audio_texture = NULL;
  102. static Texture *trashcan_texture = NULL;
  103. static Texture *soundboard_texture = NULL;
  104. static Texture *soundboard_levels_texture = NULL;
  105. static void SetTitleBar(const char *fmt, ...)
  106. {
  107. char *newstr = NULL;
  108. va_list ap;
  109. va_start(ap, fmt);
  110. SDL_vasprintf(&newstr, fmt, ap);
  111. va_end(ap);
  112. if (newstr && (!current_titlebar || (SDL_strcmp(current_titlebar, newstr) != 0))) {
  113. SDL_SetWindowTitle(state->windows[0], newstr);
  114. SDL_free(current_titlebar);
  115. current_titlebar = newstr;
  116. } else {
  117. SDL_free(newstr);
  118. }
  119. }
  120. static void SetDefaultTitleBar(void)
  121. {
  122. SetTitleBar("testaudio: %s", SDL_GetCurrentAudioDriver());
  123. }
  124. static Thing *FindThingAtPoint(const float x, const float y)
  125. {
  126. const SDL_FPoint pt = { x, y };
  127. Thing *retval = NULL;
  128. Thing *i;
  129. for (i = things; i; i = i->next) {
  130. if ((i != dragging_thing) && SDL_PointInRectFloat(&pt, &i->rect)) {
  131. retval = i; /* keep going, though, because things drawn on top are later in the list. */
  132. }
  133. }
  134. return retval;
  135. }
  136. static Thing *UpdateMouseOver(const float x, const float y)
  137. {
  138. Thing *thing;
  139. if (dragging_thing) {
  140. thing = dragging_thing;
  141. } else {
  142. thing = FindThingAtPoint(x, y);
  143. }
  144. mouseover_thing = thing;
  145. if (!thing) {
  146. SetDefaultTitleBar();
  147. } else if (thing->titlebar) {
  148. SetTitleBar("%s", thing->titlebar);
  149. }
  150. return thing;
  151. }
  152. static Thing *CreateThing(ThingType what, float x, float y, float z, float w, float h, Texture *texture, const char *titlebar)
  153. {
  154. Thing *last = NULL;
  155. Thing *i;
  156. Thing *thing;
  157. thing = (Thing *) SDL_calloc(1, sizeof (Thing));
  158. if (!thing) {
  159. SDL_Log("Out of memory!");
  160. return NULL;
  161. }
  162. if ((w < 0) || (h < 0)) {
  163. SDL_assert(texture != NULL);
  164. if (w < 0) {
  165. w = texture->w;
  166. }
  167. if (h < 0) {
  168. h = texture->h;
  169. }
  170. }
  171. thing->what = what;
  172. thing->rect.x = x;
  173. thing->rect.y = y;
  174. thing->rect.w = w;
  175. thing->rect.h = h;
  176. thing->z = z;
  177. thing->r = 255;
  178. thing->g = 255;
  179. thing->b = 255;
  180. thing->a = 255;
  181. thing->scale = 1.0f;
  182. thing->createticks = SDL_GetTicks();
  183. thing->texture = texture;
  184. thing->titlebar = titlebar ? SDL_strdup(titlebar) : NULL; /* if allocation fails, oh well. */
  185. /* insert in list by Z order (furthest from the "camera" first, so they get drawn over; negative Z is not drawn at all). */
  186. if (!things) {
  187. things = thing;
  188. return thing;
  189. }
  190. for (i = things; i; i = i->next) {
  191. if (z > i->z) { /* insert here. */
  192. thing->next = i;
  193. thing->prev = i->prev;
  194. SDL_assert(i->prev == last);
  195. if (i->prev) {
  196. i->prev->next = thing;
  197. } else {
  198. SDL_assert(i == things);
  199. things = thing;
  200. }
  201. i->prev = thing;
  202. return thing;
  203. }
  204. last = i;
  205. }
  206. if (last) {
  207. last->next = thing;
  208. thing->prev = last;
  209. }
  210. return thing;
  211. }
  212. static void DestroyThing(Thing *thing)
  213. {
  214. if (!thing) {
  215. return;
  216. }
  217. if (mouseover_thing == thing) {
  218. mouseover_thing = NULL;
  219. }
  220. if (droppable_highlighted_thing == thing) {
  221. droppable_highlighted_thing = NULL;
  222. }
  223. if (dragging_thing == thing) {
  224. dragging_thing = NULL;
  225. }
  226. switch (thing->what) {
  227. case THING_POOF: break;
  228. case THING_NULL: break;
  229. case THING_TRASHCAN: break;
  230. case THING_LOGDEV:
  231. case THING_LOGDEV_CAPTURE:
  232. SDL_CloseAudioDevice(thing->data.logdev.devid);
  233. if (state->renderers[0] != NULL) {
  234. SDL_DestroyTexture(thing->data.logdev.visualizer);
  235. }
  236. SDL_DestroyMutex(thing->data.logdev.postmix_lock);
  237. SDL_free(thing->data.logdev.postmix_buffer);
  238. break;
  239. case THING_PHYSDEV:
  240. case THING_PHYSDEV_CAPTURE:
  241. SDL_free(thing->data.physdev.name);
  242. break;
  243. case THING_WAV:
  244. SDL_free(thing->data.wav.buf);
  245. break;
  246. case THING_STREAM:
  247. SDL_DestroyAudioStream(thing->data.stream.stream);
  248. break;
  249. }
  250. if (thing->prev) {
  251. thing->prev->next = thing->next;
  252. } else {
  253. SDL_assert(thing == things);
  254. things = thing->next;
  255. }
  256. if (thing->next) {
  257. thing->next->prev = thing->prev;
  258. }
  259. SDL_free(thing->titlebar);
  260. SDL_free(thing);
  261. }
  262. static void DrawOneThing(SDL_Renderer *renderer, Thing *thing)
  263. {
  264. SDL_FRect dst;
  265. SDL_memcpy(&dst, &thing->rect, sizeof (SDL_FRect));
  266. if (thing->scale != 1.0f) {
  267. const float centerx = thing->rect.x + (thing->rect.w / 2);
  268. const float centery = thing->rect.y + (thing->rect.h / 2);
  269. const int w = thing->texture ? (int) thing->texture->w : 128;
  270. const int h = thing->texture ? (int) thing->texture->h : 128;
  271. dst.w = w * thing->scale;
  272. dst.h = h * thing->scale;
  273. dst.x = centerx - (dst.w / 2);
  274. dst.y = centery - (dst.h / 2);
  275. }
  276. if (thing->texture) {
  277. if (droppable_highlighted_thing == thing) {
  278. SDL_SetRenderDrawColor(renderer, 255, 0, 255, 100);
  279. SDL_RenderFillRect(renderer, &dst);
  280. }
  281. SDL_SetRenderDrawColor(renderer, thing->r, thing->g, thing->b, thing->a);
  282. SDL_RenderTexture(renderer, thing->texture->texture, NULL, &dst);
  283. } else {
  284. SDL_SetRenderDrawColor(renderer, thing->r, thing->g, thing->b, thing->a);
  285. SDL_RenderFillRect(renderer, &dst);
  286. }
  287. if (thing->ondraw) {
  288. thing->ondraw(thing, renderer);
  289. }
  290. if (thing->progress > 0.0f) {
  291. SDL_FRect r = { thing->rect.x, thing->rect.y + (thing->rect.h + 2.0f), 0.0f, 10.0f };
  292. r.w = thing->rect.w * ((thing->progress > 1.0f) ? 1.0f : thing->progress);
  293. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 128);
  294. SDL_RenderFillRect(renderer, &r);
  295. }
  296. }
  297. static void DrawThings(SDL_Renderer *renderer)
  298. {
  299. Thing *i;
  300. /* draw connecting lines first, so they're behind everything else. */
  301. for (i = things; i && (i->z >= 0.0f); i = i->next) {
  302. Thing *dst = i->line_connected_to;
  303. if (dst) {
  304. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  305. 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));
  306. }
  307. }
  308. /* Draw the actual things. */
  309. for (i = things; i && (i->z >= 0.0f); i = i->next) {
  310. if (i != dragging_thing) {
  311. DrawOneThing(renderer, i);
  312. }
  313. }
  314. if (dragging_thing) {
  315. DrawOneThing(renderer, dragging_thing); /* draw last so it's always on top. */
  316. }
  317. }
  318. static void Draw(void)
  319. {
  320. SDL_Renderer *renderer = state->renderers[0];
  321. if (renderer) { /* might be NULL if we're shutting down. */
  322. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  323. SDL_SetRenderDrawColor(renderer, 64, 0, 64, 255);
  324. SDL_RenderClear(renderer);
  325. DrawThings(renderer);
  326. SDL_RenderPresent(renderer);
  327. }
  328. }
  329. static void RepositionRowOfThings(const ThingType what, const float y)
  330. {
  331. int total_things = 0;
  332. float texw = 0.0f;
  333. float texh = 0.0f;
  334. Thing *i;
  335. for (i = things; i; i = i->next) {
  336. if (i->what == what) {
  337. texw = i->rect.w;
  338. texh = i->rect.h;
  339. total_things++;
  340. }
  341. }
  342. if (total_things > 0) {
  343. int w, h;
  344. SDL_GetWindowSize(state->windows[0], &w, &h);
  345. const float spacing = w / ((float) total_things);
  346. float x = (spacing - texw) / 2.0f;
  347. for (i = things; i; i = i->next) {
  348. if (i->what == what) {
  349. i->rect.x = x;
  350. i->rect.y = (y >= 0.0f) ? y : ((h + y) - texh);
  351. x += spacing;
  352. }
  353. }
  354. }
  355. }
  356. static const char *AudioFmtToString(const SDL_AudioFormat fmt)
  357. {
  358. switch (fmt) {
  359. #define FMTCASE(x) case SDL_AUDIO_##x: return #x
  360. FMTCASE(U8);
  361. FMTCASE(S8);
  362. FMTCASE(S16LE);
  363. FMTCASE(S16BE);
  364. FMTCASE(S32LE);
  365. FMTCASE(S32BE);
  366. FMTCASE(F32LE);
  367. FMTCASE(F32BE);
  368. #undef FMTCASE
  369. }
  370. return "?";
  371. }
  372. static const char *AudioChansToStr(const int channels)
  373. {
  374. switch (channels) {
  375. case 1: return "mono";
  376. case 2: return "stereo";
  377. case 3: return "2.1";
  378. case 4: return "quad";
  379. case 5: return "4.1";
  380. case 6: return "5.1";
  381. case 7: return "6.1";
  382. case 8: return "7.1";
  383. default: break;
  384. }
  385. return "?";
  386. }
  387. static void PoofThing_ondrag(Thing *thing, int button, float x, float y)
  388. {
  389. dragging_thing = NULL; /* refuse to be dragged. */
  390. }
  391. static void PoofThing_ontick(Thing *thing, Uint64 now)
  392. {
  393. const int lifetime = POOF_LIFETIME;
  394. const int elasped = (int) (now - thing->createticks);
  395. if (elasped > lifetime) {
  396. DestroyThing(thing);
  397. } else {
  398. const float pct = ((float) elasped) / ((float) lifetime);
  399. thing->a = (Uint8) (int) (255.0f - (pct * 255.0f));
  400. thing->scale = 1.0f - pct; /* shrink to nothing! */
  401. }
  402. }
  403. static Thing *CreatePoofThing(Thing *poofing_thing)
  404. {
  405. const float centerx = poofing_thing->rect.x + (poofing_thing->rect.w / 2);
  406. const float centery = poofing_thing->rect.y + (poofing_thing->rect.h / 2);
  407. const float z = poofing_thing->z;
  408. 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);
  409. if (thing) {
  410. thing->data.poof.startw = poofing_thing->rect.w;
  411. thing->data.poof.starth = poofing_thing->rect.h;
  412. thing->data.poof.centerx = centerx;
  413. thing->data.poof.centery = centery;
  414. thing->ontick = PoofThing_ontick;
  415. thing->ondrag = PoofThing_ondrag;
  416. }
  417. return thing;
  418. }
  419. static void DestroyThingInPoof(Thing *thing)
  420. {
  421. if (thing) {
  422. if (thing->what != THING_POOF) {
  423. CreatePoofThing(thing);
  424. }
  425. DestroyThing(thing);
  426. }
  427. }
  428. /* this poofs a thing and additionally poofs all things connected to the thing. */
  429. static void TrashThing(Thing *thing)
  430. {
  431. Thing *i, *next;
  432. for (i = things; i; i = next) {
  433. next = i->next;
  434. if (i->line_connected_to == thing) {
  435. TrashThing(i);
  436. next = things; /* start over in case this blew up the list. */
  437. }
  438. }
  439. DestroyThingInPoof(thing);
  440. }
  441. static void StreamThing_ontick(Thing *thing, Uint64 now)
  442. {
  443. if (!thing->line_connected_to) {
  444. return;
  445. }
  446. /* are we playing? See if we're done, or update state. */
  447. if (thing->line_connected_to->what == THING_LOGDEV) {
  448. const int available = SDL_GetAudioStreamAvailable(thing->data.stream.stream);
  449. if (!available) {
  450. DestroyThingInPoof(thing);
  451. return;
  452. } else {
  453. thing->progress = 1.0f - (thing->data.stream.total_bytes ? (((float) (available)) / ((float) thing->data.stream.total_bytes)) : 0.0f);
  454. }
  455. }
  456. if (thing->data.stream.next_level_update <= now) {
  457. Uint64 perf = SDL_GetPerformanceCounter();
  458. int i;
  459. for (i = 0; i < SDL_arraysize(thing->data.stream.levels); i++) {
  460. thing->data.stream.levels[i] = (Uint8) (perf % 6);
  461. perf >>= 3;
  462. }
  463. thing->data.stream.next_level_update += 150;
  464. }
  465. }
  466. static void StreamThing_ondrag(Thing *thing, int button, float x, float y)
  467. {
  468. if (button == SDL_BUTTON_RIGHT) { /* this is kinda hacky, but use this to disconnect from a playing source. */
  469. if (thing->line_connected_to) {
  470. SDL_UnbindAudioStream(thing->data.stream.stream); /* unbind from current device */
  471. if (thing->line_connected_to->what == THING_LOGDEV_CAPTURE) {
  472. SDL_FlushAudioStream(thing->data.stream.stream);
  473. }
  474. thing->line_connected_to = NULL;
  475. }
  476. }
  477. }
  478. static void StreamThing_ondrop(Thing *thing, int button, float x, float y)
  479. {
  480. if (droppable_highlighted_thing) {
  481. if (droppable_highlighted_thing->what == THING_TRASHCAN) {
  482. TrashThing(thing);
  483. } else if (((droppable_highlighted_thing->what == THING_LOGDEV) || (droppable_highlighted_thing->what == THING_LOGDEV_CAPTURE)) && (droppable_highlighted_thing != thing->line_connected_to)) {
  484. /* connect to a logical device! */
  485. SDL_Log("Binding audio stream ('%s') to logical device %u", thing->titlebar, (unsigned int) droppable_highlighted_thing->data.logdev.devid);
  486. if (thing->line_connected_to) {
  487. SDL_UnbindAudioStream(thing->data.stream.stream); /* unbind from current device */
  488. if (thing->line_connected_to->what == THING_LOGDEV_CAPTURE) {
  489. SDL_FlushAudioStream(thing->data.stream.stream);
  490. }
  491. }
  492. SDL_BindAudioStream(droppable_highlighted_thing->data.logdev.devid, thing->data.stream.stream); /* bind to new device! */
  493. thing->data.stream.total_bytes = SDL_GetAudioStreamAvailable(thing->data.stream.stream);
  494. thing->progress = 0.0f; /* ontick will adjust this if we're on an output device.*/
  495. thing->data.stream.next_level_update = SDL_GetTicks() + 100;
  496. thing->line_connected_to = droppable_highlighted_thing;
  497. }
  498. }
  499. }
  500. static void StreamThing_ondraw(Thing *thing, SDL_Renderer *renderer)
  501. {
  502. if (thing->line_connected_to) { /* are we playing? Update progress bar, and bounce the levels a little. */
  503. static const float xlocs[5] = { 18, 39, 59, 79, 99 };
  504. static const float ylocs[5] = { 49, 39, 29, 19, 10 };
  505. const float blockw = soundboard_levels_texture->w;
  506. const float blockh = soundboard_levels_texture->h / 5.0f;
  507. int i, j;
  508. SDL_SetRenderDrawColor(renderer, thing->r, thing->g, thing->b, thing->a);
  509. for (i = 0; i < SDL_arraysize(thing->data.stream.levels); i++) {
  510. const int level = (int) thing->data.stream.levels[i];
  511. const float x = xlocs[i];
  512. for (j = 0; j < level; j++) {
  513. const SDL_FRect src = { 0, soundboard_levels_texture->h - ((j+1) * blockh), blockw, blockh };
  514. const SDL_FRect dst = { thing->rect.x + x, thing->rect.y + ylocs[j], blockw, blockh };
  515. SDL_RenderTexture(renderer, soundboard_levels_texture->texture, &src, &dst);
  516. }
  517. }
  518. }
  519. }
  520. static Thing *CreateStreamThing(const SDL_AudioSpec *spec, const Uint8 *buf, const Uint32 buflen, const char *fname, const float x, const float y)
  521. {
  522. static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_LOGDEV, THING_LOGDEV_CAPTURE, THING_NULL };
  523. Thing *thing = CreateThing(THING_STREAM, x, y, 0, -1, -1, soundboard_texture, fname);
  524. if (thing) {
  525. SDL_Log("Adding audio stream for %s", fname ? fname : "(null)");
  526. thing->data.stream.stream = SDL_CreateAudioStream(spec, spec);
  527. if (buf && buflen) {
  528. SDL_PutAudioStreamData(thing->data.stream.stream, buf, (int) buflen);
  529. SDL_FlushAudioStream(thing->data.stream.stream);
  530. thing->data.stream.total_bytes = SDL_GetAudioStreamAvailable(thing->data.stream.stream);
  531. }
  532. thing->ontick = StreamThing_ontick;
  533. thing->ondrag = StreamThing_ondrag;
  534. thing->ondrop = StreamThing_ondrop;
  535. thing->ondraw = StreamThing_ondraw;
  536. thing->can_be_dropped_onto = can_be_dropped_onto;
  537. }
  538. return thing;
  539. }
  540. static void WavThing_ondrag(Thing *thing, int button, float x, float y)
  541. {
  542. if (button == SDL_BUTTON_RIGHT) { /* drag out a new audio stream. */
  543. 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));
  544. }
  545. }
  546. static void WavThing_ondrop(Thing *thing, int button, float x, float y)
  547. {
  548. if (droppable_highlighted_thing) {
  549. if (droppable_highlighted_thing->what == THING_TRASHCAN) {
  550. TrashThing(thing);
  551. }
  552. }
  553. }
  554. static Thing *LoadWavThing(const char *fname, float x, float y)
  555. {
  556. Thing *thing = NULL;
  557. char *path;
  558. SDL_AudioSpec spec;
  559. Uint8 *buf = NULL;
  560. Uint32 buflen = 0;
  561. path = GetNearbyFilename(fname);
  562. if (path) {
  563. fname = path;
  564. }
  565. if (SDL_LoadWAV(fname, &spec, &buf, &buflen) == 0) {
  566. static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
  567. char *titlebar = NULL;
  568. const char *nodirs = SDL_strrchr(fname, '/');
  569. #ifdef SDL_PLATFORM_WINDOWS
  570. const char *nodirs2 = SDL_strrchr(nodirs ? nodirs : fname, '\\');
  571. if (nodirs2) {
  572. nodirs = nodirs2;
  573. }
  574. #endif
  575. SDL_Log("Adding WAV file '%s'", fname);
  576. if (nodirs) {
  577. nodirs++;
  578. } else {
  579. nodirs = fname;
  580. }
  581. SDL_asprintf(&titlebar, "WAV file (\"%s\", %s, %s, %uHz)", nodirs, AudioFmtToString(spec.format), AudioChansToStr(spec.channels), (unsigned int) spec.freq);
  582. thing = CreateThing(THING_WAV, x - (audio_texture->w / 2), y - (audio_texture->h / 2), 5, -1, -1, audio_texture, titlebar);
  583. if (thing) {
  584. SDL_free(titlebar);
  585. SDL_memcpy(&thing->data.wav.spec, &spec, sizeof (SDL_AudioSpec));
  586. thing->data.wav.buf = buf;
  587. thing->data.wav.buflen = buflen;
  588. thing->can_be_dropped_onto = can_be_dropped_onto;
  589. thing->ondrag = WavThing_ondrag;
  590. thing->ondrop = WavThing_ondrop;
  591. }
  592. }
  593. SDL_free(path);
  594. return thing;
  595. }
  596. static Thing *LoadStockWavThing(const char *fname)
  597. {
  598. char *path = GetNearbyFilename(fname);
  599. Thing *thing = LoadWavThing(path ? path : fname, 0.0f, 0.0f); /* will reposition in a moment. */
  600. SDL_free(path);
  601. return thing;
  602. }
  603. static void LoadStockWavThings(void)
  604. {
  605. LoadStockWavThing("sample.wav");
  606. RepositionRowOfThings(THING_WAV, -10.0f);
  607. }
  608. static void DestroyTexture(Texture *tex)
  609. {
  610. if (tex) {
  611. if (state->renderers[0] != NULL) { /* if the renderer went away, this pointer is already bogus. */
  612. SDL_DestroyTexture(tex->texture);
  613. }
  614. SDL_free(tex);
  615. }
  616. }
  617. static Texture *CreateTexture(const char *fname)
  618. {
  619. Texture *tex = (Texture *) SDL_calloc(1, sizeof (Texture));
  620. if (!tex) {
  621. SDL_Log("Out of memory!");
  622. } else {
  623. int texw, texh;
  624. tex->texture = LoadTexture(state->renderers[0], fname, SDL_TRUE, &texw, &texh);
  625. if (!tex->texture) {
  626. SDL_Log("Failed to load '%s': %s", fname, SDL_GetError());
  627. SDL_free(tex);
  628. return NULL;
  629. }
  630. SDL_SetTextureBlendMode(tex->texture, SDL_BLENDMODE_BLEND);
  631. tex->w = (float) texw;
  632. tex->h = (float) texh;
  633. }
  634. return tex;
  635. }
  636. static Thing *CreateLogicalDeviceThing(Thing *parent, const SDL_AudioDeviceID which, const float x, const float y);
  637. static void DeviceThing_ondrag(Thing *thing, int button, float x, float y)
  638. {
  639. if ((button == SDL_BUTTON_MIDDLE) && (thing->what == THING_LOGDEV_CAPTURE)) { /* drag out a new stream. This is a UX mess. :/ */
  640. dragging_thing = CreateStreamThing(&thing->data.logdev.spec, NULL, 0, NULL, x, y);
  641. if (dragging_thing) {
  642. dragging_thing->data.stream.next_level_update = SDL_GetTicks() + 100;
  643. SDL_BindAudioStream(thing->data.logdev.devid, dragging_thing->data.stream.stream); /* bind to new device! */
  644. dragging_thing->line_connected_to = thing;
  645. }
  646. } else if (button == SDL_BUTTON_RIGHT) { /* drag out a new logical device. */
  647. const SDL_AudioDeviceID which = ((thing->what == THING_LOGDEV) || (thing->what == THING_LOGDEV_CAPTURE)) ? thing->data.logdev.devid : thing->data.physdev.devid;
  648. const SDL_AudioDeviceID devid = SDL_OpenAudioDevice(which, NULL);
  649. dragging_thing = devid ? CreateLogicalDeviceThing(thing, devid, x - (thing->rect.w / 2), y - (thing->rect.h / 2)) : NULL;
  650. }
  651. }
  652. static void SetLogicalDeviceTitlebar(Thing *thing)
  653. {
  654. SDL_AudioSpec *spec = &thing->data.logdev.spec;
  655. int frames = 0;
  656. SDL_GetAudioDeviceFormat(thing->data.logdev.devid, spec, &frames);
  657. SDL_free(thing->titlebar);
  658. SDL_asprintf(&thing->titlebar, "Logical device #%u (%s, %s, %s, %uHz, %d frames)", (unsigned int) thing->data.logdev.devid, thing->data.logdev.iscapture ? "CAPTURE" : "OUTPUT", AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq, frames);
  659. }
  660. static void LogicalDeviceThing_ondrop(Thing *thing, int button, float x, float y)
  661. {
  662. if (droppable_highlighted_thing) {
  663. if (droppable_highlighted_thing->what == THING_TRASHCAN) {
  664. TrashThing(thing);
  665. }
  666. }
  667. }
  668. static void SDLCALL PostmixCallback(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen)
  669. {
  670. Thing *thing = (Thing *) userdata;
  671. SDL_LockMutex(thing->data.logdev.postmix_lock);
  672. if (thing->data.logdev.postmix_allocated < buflen) {
  673. void *ptr = SDL_realloc(thing->data.logdev.postmix_buffer, buflen);
  674. if (!ptr) {
  675. SDL_UnlockMutex(thing->data.logdev.postmix_lock);
  676. return; /* oh well. */
  677. }
  678. thing->data.logdev.postmix_buffer = (float *) ptr;
  679. thing->data.logdev.postmix_allocated = buflen;
  680. }
  681. SDL_copyp(&thing->data.logdev.postmix_spec, spec);
  682. SDL_memcpy(thing->data.logdev.postmix_buffer, buffer, buflen);
  683. thing->data.logdev.postmix_buflen = buflen;
  684. SDL_AtomicSet(&thing->data.logdev.postmix_updated, 1);
  685. SDL_UnlockMutex(thing->data.logdev.postmix_lock);
  686. }
  687. static void UpdateVisualizer(SDL_Renderer *renderer, SDL_Texture *visualizer, const int channels, const float *buffer, const int buflen)
  688. {
  689. static const SDL_Color channel_colors[8] = {
  690. { 255, 255, 255, 255 },
  691. { 255, 0, 0, 255 },
  692. { 0, 255, 0, 255 },
  693. { 0, 0, 255, 255 },
  694. { 255, 255, 0, 255 },
  695. { 0, 255, 255, 255 },
  696. { 255, 0, 255, 255 },
  697. { 127, 127, 127, 255 }
  698. };
  699. SDL_SetRenderTarget(renderer, visualizer);
  700. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  701. SDL_RenderClear(renderer);
  702. if (buffer && buflen) {
  703. const int frames = (buflen / sizeof (float)) / channels;
  704. const int skip = frames / (VISUALIZER_WIDTH * 2);
  705. int i, j;
  706. for (i = channels - 1; i >= 0; i--) {
  707. const SDL_Color *color = &channel_colors[i % SDL_arraysize(channel_colors)];
  708. SDL_FPoint points[VISUALIZER_WIDTH + 2];
  709. float prevx = 0.0f;
  710. int pointidx = 1;
  711. points[0].x = 0.0f;
  712. points[0].y = VISUALIZER_HEIGHT * 0.5f;
  713. for (j = 0; j < (SDL_arraysize(points)-1); j++) {
  714. const float val = buffer[((j * skip) * channels) + i];
  715. const float x = prevx + 2;
  716. const float y = (VISUALIZER_HEIGHT * 0.5f) - (VISUALIZER_HEIGHT * (val * 0.5f));
  717. SDL_assert(pointidx < SDL_arraysize(points));
  718. points[pointidx].x = x;
  719. points[pointidx].y = y;
  720. pointidx++;
  721. prevx = x;
  722. }
  723. SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, 255);
  724. SDL_RenderLines(renderer, points, pointidx);
  725. }
  726. }
  727. SDL_SetRenderTarget(renderer, NULL);
  728. }
  729. static void LogicalDeviceThing_ontick(Thing *thing, Uint64 now)
  730. {
  731. const SDL_bool ismousedover = (thing == mouseover_thing);
  732. if (!thing->data.logdev.visualizer || !thing->data.logdev.postmix_lock) { /* need these to work, skip if they failed. */
  733. return;
  734. }
  735. if (thing->data.logdev.visualizer_enabled != ismousedover) {
  736. thing->data.logdev.visualizer_enabled = ismousedover;
  737. if (!ismousedover) {
  738. SDL_SetAudioPostmixCallback(thing->data.logdev.devid, NULL, NULL);
  739. } else {
  740. if (thing->data.logdev.postmix_buffer) {
  741. SDL_memset(thing->data.logdev.postmix_buffer, '\0', thing->data.logdev.postmix_buflen);
  742. }
  743. SDL_AtomicSet(&thing->data.logdev.postmix_updated, 1); /* so this will at least clear the texture later. */
  744. SDL_SetAudioPostmixCallback(thing->data.logdev.devid, PostmixCallback, thing);
  745. }
  746. }
  747. }
  748. static void LogicalDeviceThing_ondraw(Thing *thing, SDL_Renderer *renderer)
  749. {
  750. if (thing->data.logdev.visualizer_enabled) {
  751. SDL_FRect dst;
  752. dst.w = thing->rect.w;
  753. dst.h = thing->rect.h;
  754. dst.x = thing->rect.x + ((thing->rect.w - dst.w) / 2);
  755. dst.y = thing->rect.y + ((thing->rect.h - dst.h) / 2);
  756. if (SDL_AtomicGet(&thing->data.logdev.postmix_updated)) {
  757. float *buffer;
  758. int channels;
  759. int buflen;
  760. SDL_LockMutex(thing->data.logdev.postmix_lock);
  761. channels = thing->data.logdev.postmix_spec.channels;
  762. buflen = thing->data.logdev.postmix_buflen;
  763. buffer = (float *) SDL_malloc(thing->data.logdev.postmix_buflen);
  764. if (buffer) {
  765. SDL_memcpy(buffer, thing->data.logdev.postmix_buffer, thing->data.logdev.postmix_buflen);
  766. SDL_AtomicSet(&thing->data.logdev.postmix_updated, 0);
  767. }
  768. SDL_UnlockMutex(thing->data.logdev.postmix_lock);
  769. UpdateVisualizer(renderer, thing->data.logdev.visualizer, channels, buffer, buflen);
  770. SDL_free(buffer);
  771. }
  772. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 30);
  773. SDL_RenderTexture(renderer, thing->data.logdev.visualizer, NULL, &dst);
  774. }
  775. }
  776. static Thing *CreateLogicalDeviceThing(Thing *parent, const SDL_AudioDeviceID which, const float x, const float y)
  777. {
  778. static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
  779. Thing *physthing = ((parent->what == THING_LOGDEV) || (parent->what == THING_LOGDEV_CAPTURE)) ? parent->data.logdev.physdev : parent;
  780. const SDL_bool iscapture = physthing->data.physdev.iscapture;
  781. Thing *thing;
  782. SDL_Log("Adding logical audio device %u", (unsigned int) which);
  783. thing = CreateThing(iscapture ? THING_LOGDEV_CAPTURE : THING_LOGDEV, x, y, 5, -1, -1, logdev_texture, NULL);
  784. if (thing) {
  785. thing->data.logdev.devid = which;
  786. thing->data.logdev.iscapture = iscapture;
  787. thing->data.logdev.physdev = physthing;
  788. thing->data.logdev.visualizer = SDL_CreateTexture(state->renderers[0], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, VISUALIZER_WIDTH, VISUALIZER_HEIGHT);
  789. thing->data.logdev.postmix_lock = SDL_CreateMutex();
  790. if (thing->data.logdev.visualizer) {
  791. SDL_SetTextureBlendMode(thing->data.logdev.visualizer, SDL_BLENDMODE_BLEND);
  792. }
  793. thing->line_connected_to = physthing;
  794. thing->ontick = LogicalDeviceThing_ontick;
  795. thing->ondrag = DeviceThing_ondrag;
  796. thing->ondrop = LogicalDeviceThing_ondrop;
  797. thing->ondraw = LogicalDeviceThing_ondraw;
  798. thing->can_be_dropped_onto = can_be_dropped_onto;
  799. SetLogicalDeviceTitlebar(thing);
  800. }
  801. return thing;
  802. }
  803. static void SetPhysicalDeviceTitlebar(Thing *thing)
  804. {
  805. int frames = 0;
  806. SDL_AudioSpec *spec = &thing->data.physdev.spec;
  807. SDL_GetAudioDeviceFormat(thing->data.physdev.devid, spec, &frames);
  808. SDL_free(thing->titlebar);
  809. if (thing->data.physdev.devid == SDL_AUDIO_DEVICE_DEFAULT_CAPTURE) {
  810. SDL_asprintf(&thing->titlebar, "Default system device (CAPTURE, %s, %s, %uHz, %d frames)", AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq, frames);
  811. } else if (thing->data.physdev.devid == SDL_AUDIO_DEVICE_DEFAULT_OUTPUT) {
  812. SDL_asprintf(&thing->titlebar, "Default system device (OUTPUT, %s, %s, %uHz, %d frames)", AudioFmtToString(spec->format), AudioChansToStr(spec->channels), (unsigned int) spec->freq, frames);
  813. } else {
  814. SDL_asprintf(&thing->titlebar, "Physical device #%u (%s, \"%s\", %s, %s, %uHz, %d frames)", (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, frames);
  815. }
  816. }
  817. static void PhysicalDeviceThing_ondrop(Thing *thing, int button, float x, float y)
  818. {
  819. if (droppable_highlighted_thing) {
  820. if (droppable_highlighted_thing->what == THING_TRASHCAN) {
  821. TrashThing(thing);
  822. }
  823. }
  824. }
  825. static void PhysicalDeviceThing_ontick(Thing *thing, Uint64 now)
  826. {
  827. const int lifetime = POOF_LIFETIME;
  828. const int elasped = (int) (now - thing->createticks);
  829. if (elasped > lifetime) {
  830. thing->scale = 1.0f;
  831. thing->a = 255;
  832. thing->ontick = NULL; /* no more ticking. */
  833. } else {
  834. const float pct = ((float) elasped) / ((float) lifetime);
  835. thing->a = (Uint8) (int) (pct * 255.0f);
  836. thing->scale = pct; /* grow to normal size */
  837. }
  838. }
  839. static Thing *CreatePhysicalDeviceThing(const SDL_AudioDeviceID which, const SDL_bool iscapture)
  840. {
  841. static const ThingType can_be_dropped_onto[] = { THING_TRASHCAN, THING_NULL };
  842. static float next_physdev_x = 0;
  843. Thing *thing;
  844. int winw, winh;
  845. SDL_GetWindowSize(state->windows[0], &winw, &winh);
  846. if (next_physdev_x > (winw-physdev_texture->w)) {
  847. next_physdev_x = 0;
  848. }
  849. SDL_Log("Adding physical audio device %u", (unsigned int) which);
  850. thing = CreateThing(iscapture ? THING_PHYSDEV_CAPTURE : THING_PHYSDEV, next_physdev_x, 170, 5, -1, -1, physdev_texture, NULL);
  851. if (thing) {
  852. thing->data.physdev.devid = which;
  853. thing->data.physdev.iscapture = iscapture;
  854. thing->data.physdev.name = SDL_strdup(SDL_GetAudioDeviceName(which));
  855. thing->ondrag = DeviceThing_ondrag;
  856. thing->ondrop = PhysicalDeviceThing_ondrop;
  857. thing->ontick = PhysicalDeviceThing_ontick;
  858. thing->can_be_dropped_onto = can_be_dropped_onto;
  859. SetPhysicalDeviceTitlebar(thing);
  860. if (SDL_GetTicks() <= (app_ready_ticks + 2000)) { /* assume this is the initial batch if it happens in the first two seconds. */
  861. RepositionRowOfThings(THING_PHYSDEV, 10.0f); /* don't rearrange them after the initial add. */
  862. RepositionRowOfThings(THING_PHYSDEV_CAPTURE, 170.0f); /* don't rearrange them after the initial add. */
  863. next_physdev_x = 0.0f;
  864. } else {
  865. next_physdev_x += physdev_texture->w * 1.5f;
  866. }
  867. }
  868. return thing;
  869. }
  870. static Thing *CreateTrashcanThing(void)
  871. {
  872. int winw, winh;
  873. SDL_GetWindowSize(state->windows[0], &winw, &winh);
  874. return CreateThing(THING_TRASHCAN, winw - trashcan_texture->w, winh - trashcan_texture->h, 10, -1, -1, trashcan_texture, "Drag things here to remove them.");
  875. }
  876. static Thing *CreateDefaultPhysicalDevice(const SDL_bool iscapture)
  877. {
  878. return CreatePhysicalDeviceThing(iscapture ? SDL_AUDIO_DEVICE_DEFAULT_CAPTURE : SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, iscapture);
  879. }
  880. static void TickThings(void)
  881. {
  882. Thing *i;
  883. Thing *next;
  884. const Uint64 now = SDL_GetTicks();
  885. for (i = things; i; i = next) {
  886. next = i->next; /* in case this deletes itself. */
  887. if (i->ontick) {
  888. i->ontick(i, now);
  889. }
  890. }
  891. }
  892. static void WindowResized(const int newwinw, const int newwinh)
  893. {
  894. Thing *i;
  895. const float neww = (float) newwinw;
  896. const float newh = (float) newwinh;
  897. const float oldw = (float) state->window_w;
  898. const float oldh = (float) state->window_h;
  899. for (i = things; i; i = i->next) {
  900. const float halfw = i->rect.w / 2.0f;
  901. const float halfh = i->rect.h / 2.0f;
  902. const float x = (i->rect.x + halfw) / oldw;
  903. const float y = (i->rect.y + halfh) / oldh;
  904. i->rect.x = (x * neww) - halfw;
  905. i->rect.y = (y * newh) - halfh;
  906. }
  907. state->window_w = newwinw;
  908. state->window_h = newwinh;
  909. }
  910. int SDL_AppInit(void **appstate, int argc, char *argv[])
  911. {
  912. int i;
  913. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO | SDL_INIT_AUDIO);
  914. if (!state) {
  915. return SDL_APP_FAILURE;
  916. }
  917. state->window_flags |= SDL_WINDOW_RESIZABLE;
  918. for (i = 1; i < argc;) {
  919. int consumed = SDLTest_CommonArg(state, i);
  920. if (consumed == 0) {
  921. consumed = -1;
  922. /* add our own command lines here. */
  923. }
  924. if (consumed < 0) {
  925. static const char *options[] = {
  926. /* add our own command lines here. */
  927. /*"[--blend none|blend|add|mod|mul|sub]",*/
  928. NULL
  929. };
  930. SDLTest_CommonLogUsage(state, argv[0], options);
  931. return SDL_APP_FAILURE;
  932. }
  933. i += consumed;
  934. }
  935. if (!SDLTest_CommonInit(state)) {
  936. return SDL_APP_FAILURE;
  937. }
  938. if (state->audio_id) {
  939. SDL_CloseAudioDevice(state->audio_id);
  940. state->audio_id = 0;
  941. }
  942. SetDefaultTitleBar();
  943. if ((physdev_texture = CreateTexture("physaudiodev.bmp")) == NULL) { return -1; }
  944. if ((logdev_texture = CreateTexture("logaudiodev.bmp")) == NULL) { return -1; }
  945. if ((audio_texture = CreateTexture("audiofile.bmp")) == NULL) { return -1; }
  946. if ((trashcan_texture = CreateTexture("trashcan.bmp")) == NULL) { return -1; }
  947. if ((soundboard_texture = CreateTexture("soundboard.bmp")) == NULL) { return -1; }
  948. if ((soundboard_levels_texture = CreateTexture("soundboard_levels.bmp")) == NULL) { return -1; }
  949. LoadStockWavThings();
  950. CreateTrashcanThing();
  951. CreateDefaultPhysicalDevice(SDL_FALSE);
  952. CreateDefaultPhysicalDevice(SDL_TRUE);
  953. return SDL_APP_CONTINUE;
  954. }
  955. static SDL_bool saw_event = SDL_FALSE;
  956. int SDL_AppEvent(void *appstate, const SDL_Event *event)
  957. {
  958. Thing *thing = NULL;
  959. saw_event = SDL_TRUE;
  960. switch (event->type) {
  961. case SDL_EVENT_MOUSE_MOTION:
  962. thing = UpdateMouseOver(event->motion.x, event->motion.y);
  963. if ((dragging_button == -1) && event->motion.state) {
  964. if (event->motion.state & SDL_BUTTON_LMASK) {
  965. /* for people that don't have all three buttons... */
  966. if (ctrl_held) {
  967. dragging_button = SDL_BUTTON_RIGHT;
  968. } else if (alt_held) {
  969. dragging_button = SDL_BUTTON_MIDDLE;
  970. } else {
  971. dragging_button = SDL_BUTTON_LEFT;
  972. }
  973. dragging_button_real = SDL_BUTTON_LEFT;
  974. } else if (event->motion.state & SDL_BUTTON_RMASK) {
  975. dragging_button = SDL_BUTTON_RIGHT;
  976. dragging_button_real = SDL_BUTTON_RIGHT;
  977. } else if (event->motion.state & SDL_BUTTON_MMASK) {
  978. dragging_button = SDL_BUTTON_MIDDLE;
  979. dragging_button_real = SDL_BUTTON_MIDDLE;
  980. }
  981. if (dragging_button != -1) {
  982. dragging_thing = thing;
  983. if (thing && thing->ondrag) {
  984. thing->ondrag(thing, dragging_button, event->motion.x, event->motion.y);
  985. }
  986. }
  987. }
  988. droppable_highlighted_thing = NULL;
  989. if (dragging_thing) {
  990. dragging_thing->rect.x = event->motion.x - (dragging_thing->rect.w / 2);
  991. dragging_thing->rect.y = event->motion.y - (dragging_thing->rect.h / 2);
  992. if (dragging_thing->can_be_dropped_onto) {
  993. thing = FindThingAtPoint(event->motion.x, event->motion.y);
  994. if (thing) {
  995. int i;
  996. for (i = 0; dragging_thing->can_be_dropped_onto[i]; i++) {
  997. if (dragging_thing->can_be_dropped_onto[i] == thing->what) {
  998. droppable_highlighted_thing = thing;
  999. break;
  1000. }
  1001. }
  1002. }
  1003. }
  1004. }
  1005. break;
  1006. case SDL_EVENT_MOUSE_BUTTON_DOWN:
  1007. thing = UpdateMouseOver(event->button.x, event->button.y);
  1008. break;
  1009. case SDL_EVENT_MOUSE_BUTTON_UP:
  1010. if (dragging_button_real == event->button.button) {
  1011. Thing *dropped_thing = dragging_thing;
  1012. dragging_thing = NULL;
  1013. dragging_button = -1;
  1014. dragging_button_real = -1;
  1015. if (dropped_thing && dropped_thing->ondrop) {
  1016. dropped_thing->ondrop(dropped_thing, event->button.button, event->button.x, event->button.y);
  1017. }
  1018. droppable_highlighted_thing = NULL;
  1019. }
  1020. thing = UpdateMouseOver(event->button.x, event->button.y);
  1021. break;
  1022. case SDL_EVENT_MOUSE_WHEEL:
  1023. UpdateMouseOver(event->wheel.mouse_x, event->wheel.mouse_y);
  1024. break;
  1025. case SDL_EVENT_KEY_DOWN:
  1026. case SDL_EVENT_KEY_UP:
  1027. ctrl_held = ((event->key.keysym.mod & SDL_KMOD_CTRL) != 0);
  1028. alt_held = ((event->key.keysym.mod & SDL_KMOD_ALT) != 0);
  1029. break;
  1030. case SDL_EVENT_DROP_FILE:
  1031. SDL_Log("Drop file! '%s'", event->drop.data);
  1032. LoadWavThing(event->drop.data, event->drop.x, event->drop.y);
  1033. /* SDL frees event->drop.data for you when you use SDL_AppEvent(). */
  1034. break;
  1035. case SDL_EVENT_WINDOW_RESIZED:
  1036. WindowResized(event->window.data1, event->window.data2);
  1037. break;
  1038. case SDL_EVENT_AUDIO_DEVICE_ADDED:
  1039. CreatePhysicalDeviceThing(event->adevice.which, event->adevice.iscapture);
  1040. break;
  1041. case SDL_EVENT_AUDIO_DEVICE_REMOVED: {
  1042. const SDL_AudioDeviceID which = event->adevice.which;
  1043. Thing *i, *next;
  1044. SDL_Log("Removing audio device %u", (unsigned int) which);
  1045. for (i = things; i; i = next) {
  1046. next = i->next;
  1047. if (((i->what == THING_PHYSDEV) || (i->what == THING_PHYSDEV_CAPTURE)) && (i->data.physdev.devid == which)) {
  1048. TrashThing(i);
  1049. next = things; /* in case we mangled the list. */
  1050. } else if (((i->what == THING_LOGDEV) || (i->what == THING_LOGDEV_CAPTURE)) && (i->data.logdev.devid == which)) {
  1051. TrashThing(i);
  1052. next = things; /* in case we mangled the list. */
  1053. }
  1054. }
  1055. break;
  1056. }
  1057. default: break;
  1058. }
  1059. return SDLTest_CommonEventMainCallbacks(state, event);
  1060. }
  1061. int SDL_AppIterate(void *appstate)
  1062. {
  1063. if (app_ready_ticks == 0) {
  1064. app_ready_ticks = SDL_GetTicks();
  1065. }
  1066. TickThings();
  1067. Draw();
  1068. if (saw_event) {
  1069. saw_event = SDL_FALSE; /* reset this so we know when SDL_AppEvent() runs again */
  1070. } else {
  1071. SDL_Delay(10);
  1072. }
  1073. return SDL_APP_CONTINUE;
  1074. }
  1075. void SDL_AppQuit(void *appstate)
  1076. {
  1077. while (things) {
  1078. DestroyThing(things); /* make sure all the audio devices are closed, etc. */
  1079. }
  1080. DestroyTexture(physdev_texture);
  1081. DestroyTexture(logdev_texture);
  1082. DestroyTexture(audio_texture);
  1083. DestroyTexture(trashcan_texture);
  1084. DestroyTexture(soundboard_texture);
  1085. DestroyTexture(soundboard_levels_texture);
  1086. SDLTest_CommonQuit(state);
  1087. }