Mercurial > molko
changeset 341:0c18acf4517e
js: add music bindings
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 16 Oct 2021 09:45:45 +0200 |
parents | 64aff0afc8ac |
children | 17569bc205fa |
files | src/libmlk-core-js/CMakeLists.txt src/libmlk-core-js/core/js-music.c src/libmlk-core-js/core/js-music.h src/libmlk-core/core/music.c src/libmlk-core/core/music.h src/mlk-run/main.c |
diffstat | 6 files changed, 229 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/libmlk-core-js/CMakeLists.txt Sat Oct 16 09:13:40 2021 +0200 +++ b/src/libmlk-core-js/CMakeLists.txt Sat Oct 16 09:45:45 2021 +0200 @@ -28,6 +28,8 @@ ${libmlk-core-js_SOURCE_DIR}/core/js-event.h ${libmlk-core-js_SOURCE_DIR}/core/js-font.c ${libmlk-core-js_SOURCE_DIR}/core/js-font.h + ${libmlk-core-js_SOURCE_DIR}/core/js-music.c + ${libmlk-core-js_SOURCE_DIR}/core/js-music.h ${libmlk-core-js_SOURCE_DIR}/core/js-painter.c ${libmlk-core-js_SOURCE_DIR}/core/js-painter.h ${libmlk-core-js_SOURCE_DIR}/core/js-texture.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/libmlk-core-js/core/js-music.c Sat Oct 16 09:45:45 2021 +0200 @@ -0,0 +1,175 @@ +/* + * js-music.c -- core music binding + * + * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <assert.h> + +#include <core/alloc.h> +#include <core/error.h> +#include <core/music.h> +#include <core/vfs.h> + +#include "js-core.h" +#include "js-music.h" + +#define SIGNATURE DUK_HIDDEN_SYMBOL("Mlk.Music") + +static inline struct music * +self(duk_context *ctx) +{ + struct music *mus; + + duk_push_this(ctx); + duk_get_prop_string(ctx, -1, SIGNATURE); + mus = duk_to_pointer(ctx, -1); + duk_pop_2(ctx); + + if (!mus) + duk_error(ctx, DUK_ERR_TYPE_ERROR, "not a Music object"); + + return mus; +} + +static duk_ret_t +Music_new(duk_context *ctx) +{ + const char *entry = duk_require_string(ctx, 0); + struct vfs_file file; + struct music *mus; + + if (vfs_open(js_core_global_vfs(ctx), &file, entry, "r") < 0) + duk_error(ctx, DUK_ERR_ERROR, "%s", error()); + + mus = alloc_new0(sizeof (*mus)); + + if (music_openvfs(mus, &file) < 0) { + free(mus); + vfs_file_finish(&file); + duk_error(ctx, DUK_ERR_ERROR, "%s", error()); + } + + vfs_file_finish(&file); + + duk_push_this(ctx); + duk_push_pointer(ctx, mus); + duk_put_prop_string(ctx, -2, SIGNATURE); + duk_pop(ctx); + + return 0; +} + +static duk_ret_t +Music_play(duk_context *ctx) +{ + const int flags = duk_get_int_default(ctx, 0, MUSIC_NONE); + const unsigned int fadein = duk_get_uint_default(ctx, 0, 0); + + if (music_play(self(ctx), flags, fadein) < 0) + duk_error(ctx, DUK_ERR_ERROR, "%s", error()); + + return 0; +} + +static duk_ret_t +Music_playing(duk_context *ctx) +{ + duk_push_int(ctx, music_playing()); + + return 0; +} + +static duk_ret_t +Music_pause(duk_context *ctx) +{ + (void)ctx; + + music_pause(); + + return 0; +} + +static duk_ret_t +Music_resume(duk_context *ctx) +{ + (void)ctx; + + music_resume(); + + return 0; +} + +static duk_ret_t +Music_stop(duk_context *ctx) +{ + music_stop(duk_get_uint_default(ctx, 0, 0)); + + return 0; +} + +static duk_ret_t +Music_destructor(duk_context *ctx) +{ + struct music *mus; + + duk_get_prop_string(ctx, 0, SIGNATURE); + + if ((mus = duk_to_pointer(ctx, -1))) + music_finish(mus); + + duk_pop(ctx); + duk_del_prop_string(ctx, 0, SIGNATURE); + + return 0; +} + +static const duk_function_list_entry methods[] = { + { "play", Music_play, DUK_VARARGS }, + { NULL, NULL, 0 } +}; + +static const duk_function_list_entry functions[] = { + { "playing", Music_playing, 0 }, + { "pause", Music_pause, 0 }, + { "resume", Music_resume, 0 }, + { "stop", Music_stop, DUK_VARARGS }, + { NULL, NULL, 0 } +}; + +static const duk_number_list_entry flags[] = { + { "NONE", MUSIC_NONE }, + { "LOOP", MUSIC_LOOP }, + { NULL, 0 } +}; + +void +js_music_bind(duk_context *ctx) +{ + assert(ctx); + + duk_push_c_function(ctx, Music_new, 1); + duk_put_function_list(ctx, -1, functions); + duk_push_object(ctx); + duk_put_number_list(ctx, -1, flags); + duk_put_prop_string(ctx, -2, "Flags"); + duk_push_object(ctx); + duk_put_function_list(ctx, -1, functions); + duk_put_function_list(ctx, -1, methods); + duk_push_c_function(ctx, Music_destructor, 1); + duk_set_finalizer(ctx, -2); + duk_put_prop_string(ctx, -2, "prototype"); + duk_put_global_string(ctx, "Music"); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/libmlk-core-js/core/js-music.h Sat Oct 16 09:45:45 2021 +0200 @@ -0,0 +1,27 @@ +/* + * js-music.h -- core music binding + * + * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MLK_CORE_JS_MUSIC_H +#define MLK_CORE_JS_MUSIC_H + +#include <duktape.h> + +void +js_music_bind(duk_context *); + +#endif /* !MLK_CORE_JS_MUSIC_H */
--- a/src/libmlk-core/core/music.c Sat Oct 16 09:13:40 2021 +0200 +++ b/src/libmlk-core/core/music.c Sat Oct 16 09:45:45 2021 +0200 @@ -23,6 +23,8 @@ #include "error.h" #include "music.h" +#include "vfs.h" +#include "vfs_p.h" int music_open(struct music *mus, const char *path) @@ -52,6 +54,22 @@ } int +music_openvfs(struct music *mus, struct vfs_file *file) +{ + assert(mus); + assert(vfs_file_ok(file)); + + SDL_RWops *ops; + + if (!(ops = vfs_to_rw(file))) + return -1; + if (!(mus->handle = Mix_LoadMUS_RW(ops, 1))) + return errorf("%s", SDL_GetError()); + + return 0; +} + +int music_ok(const struct music *mus) { return mus && mus->handle;
--- a/src/libmlk-core/core/music.h Sat Oct 16 09:13:40 2021 +0200 +++ b/src/libmlk-core/core/music.h Sat Oct 16 09:45:45 2021 +0200 @@ -23,6 +23,8 @@ #include "core.h" +struct vfs_file; + enum music_flags { MUSIC_NONE = 0, MUSIC_LOOP = (1 << 0) @@ -41,6 +43,9 @@ music_openmem(struct music *, const void *, size_t); int +music_openvfs(struct music *, struct vfs_file *); + +int music_ok(const struct music *); int
--- a/src/mlk-run/main.c Sat Oct 16 09:13:40 2021 +0200 +++ b/src/mlk-run/main.c Sat Oct 16 09:45:45 2021 +0200 @@ -30,6 +30,7 @@ #include <core/js-core.h> #include <core/js-event.h> #include <core/js-font.h> +#include <core/js-music.h> #include <core/js-painter.h> #include <core/js-texture.h> #include <core/js-window.h> @@ -55,6 +56,7 @@ js_core_bind(ctx, &vfs); js_font_bind(ctx); js_event_bind(ctx); + js_music_bind(ctx); js_painter_bind(ctx); js_texture_bind(ctx); js_window_bind(ctx);