Browse Source

Minor cleanup of audio, update cakelisp

RelativeDependencies
Macoy Madson 9 months ago
parent
commit
24d5935967
  1. 2
      Dependencies/cakelisp
  2. 9
      src/Math.cake
  3. 45
      src/SDL.cake
  4. 40
      test/src/VocalGame.cake

2
Dependencies/cakelisp

@ -1 +1 @@
Subproject commit ed10693994d3121bd58ec018ebb07f11352af706
Subproject commit caa3113695c43dd6e5ca8f8828f9eae39caaa03f

9
src/Math.cake

@ -16,7 +16,7 @@
;; TODO: Remove once importing Math.cake no longer changes all compile commands
(add-build-config-label "HandmadeMath")
(c-define HANDMADE_MATH_IMPLEMENTATION)
(c-define-local HANDMADE_MATH_IMPLEMENTATION)
(c-import &with-decls "HandmadeMath.h"
;; Must also include in source file so HANDMADE_MATH_IMPLEMENTATION is output here
&with-defs "HandmadeMath.h")
@ -24,12 +24,14 @@
;;
;; Fundamental
;;
(defun interpolate-range (start-A float end-A float
start-B float end-B float B-value float
&return float)
(var interpolate-to float (/ (- B-value start-B) (- end-B start-B)))
(return (+ (* interpolate-to (- end-A start-A)) start-A)))
;; TODO: abs(), min(), max(), clamp()
;;
;; Trigonometry
@ -42,9 +44,6 @@
(defun radians-to-degrees (angle-radians float &return float)
(return (/ (* angle-radians 180.f) g-pi)))
;; TODO: Linear interpolation
;; TODO: abs(), min(), max(), clamp()
;;
;; Vectors
;;
@ -171,7 +170,7 @@
;;
;; Macros/helpers (TODO move)
;;
(defgenerator c-define (define-name symbol)
(defgenerator c-define-local (define-name symbol)
(var define-statement (const ([] CStatementOperation))
(array
(array Keyword "#define" -1)

45
src/SDL.cake

@ -7,11 +7,12 @@
"SDL.h"
"SDL_syswm.h")
(forward-declare (struct SDL_Window)
(struct SDL_AudioSpec))
(defun sdl-print-error ()
(printf "SDL_Error: %s\n" (SDL_GetError)))
(forward-declare (struct SDL_Window))
;; This supports drawing using SDL functions
(defun sdl-initialize-for-2d (window-out (* (* SDL_Window)) &return bool)
(when (< (SDL_Init (bit-or SDL_INIT_VIDEO SDL_INIT_AUDIO SDL_INIT_TIMER)) 0)
@ -58,6 +59,46 @@
(SDL_DestroyWindow window)
(SDL_Quit))
;;
;; Audio
;;
;; Allocates both names and array
;; Returns number of devices in device-names-out
(defun sdl-audio-get-devices (device-names-out (* (* (* (const char))))
is-capture bool &return int)
(var num-devices int (SDL_GetNumAudioDevices is-capture))
(set (deref device-names-out) (type-cast
(calloc (sizeof (type (* (const char)))) num-devices)
(* (* (const char)))))
(printf "Available %s devices:\n" (? is-capture "recording" "playback"))
(var i int 0)
(while (< i num-devices)
(var device-name (* (const char)) (SDL_GetAudioDeviceName i is-capture))
(when device-name
(printf "\t[%d] %s\n" i device-name)
(set (at i (deref device-names-out)) (strdup device-name)))
(incr i))
(return num-devices))
(defun sdl-audio-list-specification (spec (* SDL_AudioSpec))
(printf "freq: %d\n" (path spec > freq))
(printf "format: %d\n" (path spec > format))
(printf "channels: %d\n" (path spec > channels))
(printf "samples: %d\n" (path spec > samples)))
(defun sdl-audio-free-device-list (device-names (* (* (const char))) num-devices int)
(var i int 0)
(while (< i num-devices)
(free (type-cast (at i device-names) (* void)))
(incr i))
(free device-names))
;;
;; Test
;;
(defun test--sdl-main (&return int)
(printf "Hello, SDL!\n")
(var window (* SDL_Window) null)

40
test/src/VocalGame.cake

@ -177,38 +177,6 @@
(break))
(incr i)))
;; Allocates both names and array
;; Returns number of devices in device-names-out
(defun-local sdl-audio-get-devices (device-names-out (* (* (* (const char))))
is-capture bool &return int)
(ZoneScopedN "Audio get devices")
(var num-devices int (SDL_GetNumAudioDevices is-capture))
(set (deref device-names-out) (type-cast
(calloc (sizeof (type (* (const char)))) num-devices)
(* (* (const char)))))
(printf "Available %s devices:\n" (? is-capture "recording" "playback"))
(var i int 0)
(while (< i num-devices)
(var device-name (* (const char)) (SDL_GetAudioDeviceName i is-capture))
(when device-name
(printf "\t[%d] %s\n" i device-name)
(set (at i (deref device-names-out)) (strdup device-name)))
(incr i))
(return num-devices))
(defun-local sdl-audio-list-specification (spec (* SDL_AudioSpec))
(printf "freq: %d\n" (path spec > freq))
(printf "format: %d\n" (path spec > format))
(printf "channels: %d\n" (path spec > channels))
(printf "samples: %d\n" (path spec > samples)))
(defun-local sdl-audio-free-device-list (device-names (* (* (const char))) num-devices int)
(var i int 0)
(while (< i num-devices)
(free (type-cast (at i device-names) (* void)))
(incr i))
(free device-names))
(defun-local initialize-audio (output-device-out (* SDL_AudioDeviceID)
input-device-out (* SDL_AudioDeviceID)
output-device-spec-out (* SDL_AudioSpec)
@ -350,7 +318,7 @@
(return (and output-device-id input-device-id)))
(defun-local sdl-audio-close (output-device SDL_AudioDeviceID
(defun-local audio-close (output-device SDL_AudioDeviceID
input-device SDL_AudioDeviceID)
(when (>= output-device 2) (SDL_CloseAudioDevice output-device))
(when (>= input-device 2) (SDL_CloseAudioDevice input-device)))
@ -384,7 +352,7 @@
(defun-local audio-shutdown-thread (user-data (* void) &return int)
(ZoneScopedN "SDL audio shutdown")
(sdl-audio-close g-audio-output-device
(audio-close g-audio-output-device
g-audio-input-device)
(return 0))
@ -422,7 +390,7 @@
(unless (initialize-audio (addr g-audio-output-device) (addr g-audio-input-device)
(addr g-audio-output-device-spec) (addr g-audio-input-device-spec))
(sdl-shutdown g-window)
(sdl-audio-close g-audio-output-device g-audio-input-device)
(audio-close g-audio-output-device g-audio-input-device)
(TracyCZoneEnd startup-zone)
(return 1))))
@ -489,7 +457,7 @@
;; Our audio callbacks are going away!
;; The SDL_QueueAudio() API would make this easier to deal with, but then I need to manage
;; an audio thread on my own
(sdl-audio-close g-audio-output-device
(audio-close g-audio-output-device
g-audio-input-device)
(return g-reload-sentinel))

Loading…
Cancel
Save