changeset 343:9eb25198d706

js: add sound bindings
author David Demelier <markand@malikania.fr>
date Sat, 16 Oct 2021 22:17:09 +0200
parents 17569bc205fa
children 9641a7eaa4d2
files src/libmlk-core-js/CMakeLists.txt src/libmlk-core-js/core/js-font.c src/libmlk-core-js/core/js-sound.c src/libmlk-core-js/core/js-sound.h src/libmlk-core/core/sound.c src/libmlk-core/core/sound.h src/mlk-run/main.c
diffstat 7 files changed, 249 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/libmlk-core-js/CMakeLists.txt	Sat Oct 16 09:47:10 2021 +0200
+++ b/src/libmlk-core-js/CMakeLists.txt	Sat Oct 16 22:17:09 2021 +0200
@@ -32,6 +32,8 @@
 	${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-sound.c
+	${libmlk-core-js_SOURCE_DIR}/core/js-sound.h
 	${libmlk-core-js_SOURCE_DIR}/core/js-texture.c
 	${libmlk-core-js_SOURCE_DIR}/core/js-texture.h
 	${libmlk-core-js_SOURCE_DIR}/core/js-window.c
@@ -45,3 +47,5 @@
 	INCLUDES
 		PUBLIC $<BUILD_INTERFACE:${libmlk-core-js_SOURCE_DIR}>
 )
+
+source_group(TREE ${libmlk-core-js_SOURCE_DIR} FILES ${SOURCES})
--- a/src/libmlk-core-js/core/js-font.c	Sat Oct 16 09:47:10 2021 +0200
+++ b/src/libmlk-core-js/core/js-font.c	Sat Oct 16 22:17:09 2021 +0200
@@ -188,6 +188,7 @@
 	duk_push_object(ctx);
 	duk_put_function_list(ctx, -1, methods);
 	duk_push_c_function(ctx, Font_destructor, 1);
+	duk_set_finalizer(ctx, -2);
 	duk_put_prop_string(ctx, -2, "prototype");
 	duk_put_global_string(ctx, "Font");
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmlk-core-js/core/js-sound.c	Sat Oct 16 22:17:09 2021 +0200
@@ -0,0 +1,187 @@
+/*
+ * js-sound.c -- core sound 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/sound.h>
+#include <core/vfs.h>
+
+#include "js-core.h"
+#include "js-sound.h"
+
+#define SIGNATURE DUK_HIDDEN_SYMBOL("Mlk.Sound")
+
+static struct sound *
+self(duk_context *ctx)
+{
+	struct sound *snd;
+
+	duk_push_this(ctx);
+	duk_get_prop_string(ctx, -1, SIGNATURE);
+	snd = duk_to_pointer(ctx, -1);
+	duk_pop_2(ctx);
+
+	if (!snd)
+		duk_error(ctx, DUK_ERR_TYPE_ERROR, "not a Sound object");
+
+	return snd;
+}
+
+static duk_ret_t
+Sound_constructor(duk_context *ctx)
+{
+	const char *entry = duk_require_string(ctx, 0);
+	struct vfs_file file;
+	struct sound *snd;
+
+	if (vfs_open(js_core_global_vfs(ctx), &file, entry, "r") < 0)
+		duk_error(ctx, DUK_ERR_ERROR, "%s", error());
+
+	snd = alloc_new0(sizeof (*snd));
+
+	if (sound_openvfs(snd, &file) < 0) {
+		free(snd);
+		vfs_file_finish(&file);
+		duk_error(ctx, DUK_ERR_ERROR, "%s", error());
+	}
+
+	vfs_file_finish(&file);
+
+	duk_push_this(ctx);
+	duk_push_pointer(ctx, snd);
+	duk_put_prop_string(ctx, -2, SIGNATURE);
+	duk_pop(ctx);
+
+	return 0;
+	
+	
+}
+
+static duk_ret_t
+Sound_destructor(duk_context *ctx)
+{
+	struct sound *snd;
+
+	duk_get_prop_string(ctx, 0, SIGNATURE);
+
+	if ((snd = duk_to_pointer(ctx, -1)))
+		sound_finish(snd);
+
+	duk_pop(ctx);
+	duk_del_prop_string(ctx, 0, SIGNATURE);
+
+	return 0;
+}
+
+static duk_ret_t
+Sound_proto_play(duk_context *ctx)
+{
+	const int channel = duk_get_int_default(ctx, 0, 0);
+	const unsigned int fadein = duk_get_uint_default(ctx, 1, 0);
+
+	if (sound_play(self(ctx), channel, fadein) < 0)
+		duk_error(ctx, DUK_ERR_ERROR, "%s", error());
+	
+	return 0;
+}
+
+static duk_ret_t
+Sound_proto_pause(duk_context *ctx)
+{
+	sound_pause(self(ctx));
+
+	return 0;
+}
+
+static duk_ret_t
+Sound_proto_resume(duk_context *ctx)
+{
+	sound_resume(self(ctx));
+
+	return 0;
+}
+
+static duk_ret_t
+Sound_proto_stop(duk_context *ctx)
+{
+	const unsigned int fadeout = duk_get_uint_default(ctx, 0, 0);
+
+	sound_stop(self(ctx), fadeout);
+
+	return 0;
+}
+
+static duk_ret_t
+Sound_pause(duk_context *ctx)
+{
+	(void)ctx;
+
+	sound_pause(NULL);
+
+	return 0;
+}
+
+static duk_ret_t
+Sound_resume(duk_context *ctx)
+{
+	(void)ctx;
+
+	sound_resume(NULL);
+
+	return 0;
+}
+
+static duk_ret_t
+Sound_stop(duk_context *ctx)
+{
+	sound_stop(NULL, duk_get_uint_default(ctx, 0, 0));
+
+	return 0;
+}
+
+static const duk_function_list_entry methods[] = {
+	{ "play",               Sound_proto_play,       DUK_VARARGS     },
+	{ "pause",              Sound_proto_pause,      0               },
+	{ "resume",             Sound_proto_resume,     0               },
+	{ "stop",               Sound_proto_stop,       DUK_VARARGS     },
+	{ NULL,                 NULL,                   0               }
+};
+
+static const duk_function_list_entry functions[] = {
+	{ "pause",              Sound_pause,            0               },
+	{ "resume",             Sound_resume,           0               },
+	{ "stop",               Sound_stop,             DUK_VARARGS     },
+	{ NULL,                 NULL,                   0               }
+};
+
+void
+js_sound_bind(duk_context *ctx)
+{
+	assert(ctx);
+
+	duk_push_c_function(ctx, Sound_constructor, 1);
+	duk_put_function_list(ctx, -1, functions);
+	duk_push_object(ctx);
+	duk_put_function_list(ctx, -1, methods);
+	duk_push_c_function(ctx, Sound_destructor, 1);
+	duk_set_finalizer(ctx, -2);
+	duk_put_prop_string(ctx, -2, "prototype");
+	duk_put_global_string(ctx, "Sound");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmlk-core-js/core/js-sound.h	Sat Oct 16 22:17:09 2021 +0200
@@ -0,0 +1,27 @@
+/*
+ * js-sound.h -- core sound 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_SOUND_H
+#define MLK_CORE_JS_SOUND_H
+
+#include <duktape.h>
+
+void
+js_sound_bind(duk_context *);
+
+#endif /* !MLK_CORE_JS_SOUND_H */
--- a/src/libmlk-core/core/sound.c	Sat Oct 16 09:47:10 2021 +0200
+++ b/src/libmlk-core/core/sound.c	Sat Oct 16 22:17:09 2021 +0200
@@ -23,6 +23,8 @@
 
 #include "error.h"
 #include "sound.h"
+#include "vfs.h"
+#include "vfs_p.h"
 
 int
 sound_open(struct sound *snd, const char *path)
@@ -52,6 +54,22 @@
 }
 
 int
+sound_openvfs(struct sound *snd, struct vfs_file *file)
+{
+	assert(snd);
+	assert(vfs_file_ok(file));
+
+	SDL_RWops *ops;
+
+	if (!(ops = vfs_to_rw(file)))
+		return -1;
+	if (!(snd->handle = Mix_LoadWAV_RW(ops, 1)))
+		return errorf("%s", SDL_GetError());
+
+	return 0;
+}
+
+int
 sound_ok(const struct sound *snd)
 {
 	return snd && snd->handle;
--- a/src/libmlk-core/core/sound.h	Sat Oct 16 09:47:10 2021 +0200
+++ b/src/libmlk-core/core/sound.h	Sat Oct 16 22:17:09 2021 +0200
@@ -25,6 +25,8 @@
 
 #define SOUND_CHANNELS_MAX (256)
 
+struct vfs_file;
+
 struct sound {
 	void *handle;
 	int channel;
@@ -39,6 +41,9 @@
 sound_openmem(struct sound *, const void *, size_t);
 
 int
+sound_openvfs(struct sound *, struct vfs_file *);
+
+int
 sound_ok(const struct sound *);
 
 int
--- a/src/mlk-run/main.c	Sat Oct 16 09:47:10 2021 +0200
+++ b/src/mlk-run/main.c	Sat Oct 16 22:17:09 2021 +0200
@@ -32,6 +32,7 @@
 #include <core/js-font.h>
 #include <core/js-music.h>
 #include <core/js-painter.h>
+#include <core/js-sound.h>
 #include <core/js-texture.h>
 #include <core/js-window.h>
 
@@ -58,6 +59,7 @@
 	js_event_bind(ctx);
 	js_music_bind(ctx);
 	js_painter_bind(ctx);
+	js_sound_bind(ctx);
 	js_texture_bind(ctx);
 	js_window_bind(ctx);
 }
@@ -93,8 +95,12 @@
 
 	vfs_file_finish(&main);
 
-	if (duk_peval_string(ctx, code))
+	if (duk_peval_string(ctx, code)) {
+		duk_get_prop_string(ctx, -1, "lineNumber");
+		printf("%d\n", duk_to_int(ctx, -1));
+		duk_pop(ctx);
 		panicf("%s", duk_safe_to_string(ctx, -1));
+	}
 
 	free(code);
 }