diff options
| author | Drew DeVault <sir@cmpwn.com> | 2021-12-12 13:50:07 +0100 |
|---|---|---|
| committer | Drew DeVault <sir@cmpwn.com> | 2021-12-12 13:50:07 +0100 |
| commit | 1bd67672132302c37db6a676cd3598cd49e23f9b (patch) | |
| tree | 54b0fd7ab1d121f65d7bb4d33b7934f8d47ec343 | |
| parent | Add rwops io::stream implementation (diff) | |
| download | hare-chip8-1bd67672132302c37db6a676cd3598cd49e23f9b.tar.gz | |
sdl2::mixer: implement sample loading
| -rw-r--r-- | COPYING | 4 | ||||
| -rw-r--r-- | cmd/demo/main.ha | 12 | ||||
| -rw-r--r-- | sample.ogg | bin | 0 -> 17276 bytes | |||
| -rw-r--r-- | sdl2/mixer/samples.ha | 33 |
4 files changed, 47 insertions, 2 deletions
@@ -1,3 +1,7 @@ +sample.ogg: lancelottjones CC-BY 3.0 + +hare-sdl2: + Mozilla Public License Version 2.0 ================================== diff --git a/cmd/demo/main.ha b/cmd/demo/main.ha index 2b662a2..a5e9e68 100644 --- a/cmd/demo/main.ha +++ b/cmd/demo/main.ha @@ -1,4 +1,5 @@ use fmt; +use fs; use sdl2; use sdl2::{ controller_axis, event_type, renderer_flags, window_flags }; use sdl2::image; @@ -25,12 +26,16 @@ export fn main() void = { match (run()) { case let err: sdl2::error => fmt::fatal("SDL2 error: {}", sdl2::strerror(err)); + case let err: fs::error => + fmt::fatal("Error: {}", fs::strerror(err)); case void => void; }; }; -fn run() (void | sdl2::error) = { - sdl2::init(sdl2::init_flags::VIDEO | sdl2::init_flags::GAMECONTROLLER)!; +fn run() (void | fs::error | sdl2::error) = { + sdl2::init(sdl2::init_flags::VIDEO + | sdl2::init_flags::AUDIO + | sdl2::init_flags::GAMECONTROLLER)!; defer sdl2::quit(); image::init(image::init_flags::PNG | image::init_flags::JPG)!; defer image::quit(); @@ -67,6 +72,9 @@ fn run() (void | sdl2::error) = { sdl2::game_controller_close(c); }; + let sample = mixer::load_file("sample.ogg")?; + defer mixer::free_chunk(sample); + let state = state { window = win, render = render, diff --git a/sample.ogg b/sample.ogg Binary files differnew file mode 100644 index 0000000..f3decb6 --- /dev/null +++ b/sample.ogg diff --git a/sdl2/mixer/samples.ha b/sdl2/mixer/samples.ha new file mode 100644 index 0000000..49f1e1e --- /dev/null +++ b/sdl2/mixer/samples.ha @@ -0,0 +1,33 @@ +use fs; +use io; +use os; +use sdl2; + +// The internal format for an audio chunk +export type chunk = struct { + allocated: int, + abuf: *u8, + alen: u32, + volume: u8, +}; + +@symbol("Mix_LoadWAV_RW") fn _loadwav_rw(src: *sdl2::rwops, freesrc: int) nullable *chunk; + +// Loads a sample from an [[io::handle]]. The stream will not be closed for you. +export fn load(src: io::handle) (*chunk | sdl2::error) = { + const rw = sdl2::rw_from_handle(src); + defer sdl2::rwclose(rw); + return sdl2::wrapptr(_loadwav_rw(rw, 0))?: *chunk; +}; + +// Loads a sample from a file path. +export fn load_file(src: str) (*chunk | fs::error | sdl2::error) = { + const file = os::open(src)?; + defer io::close(file); + return load(file); +}; + +// Free the memory used in chunk, and free chunk itself as well. Do not use +// chunk after this without loading a new sample to it. Note: It's a bad idea to +// free a chunk that is still being played... +export @symbol("Mix_FreeChunk") fn free_chunk(chunk: *chunk) void; |
