Mercurial > molko
changeset 340:64aff0afc8ac
js: add Texture.fromImage function
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 16 Oct 2021 09:13:40 +0200 |
parents | 979960e65f76 |
children | 0c18acf4517e |
files | src/libmlk-core-js/core/js-texture.c src/libmlk-core/core/image.c src/libmlk-core/core/image.h |
diffstat | 3 files changed, 59 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/libmlk-core-js/core/js-texture.c Sat Oct 16 09:01:20 2021 +0200 +++ b/src/libmlk-core-js/core/js-texture.c Sat Oct 16 09:13:40 2021 +0200 @@ -20,8 +20,11 @@ #include <core/alloc.h> #include <core/error.h> +#include <core/image.h> #include <core/texture.h> +#include <core/vfs.h> +#include "js-core.h" #include "js-texture.h" #define SIGNATURE DUK_HIDDEN_SYMBOL("Mlk.Texture") @@ -150,6 +153,30 @@ return 0; } +static duk_ret_t +Texture_fromImage(duk_context *ctx) +{ + const char *entry = duk_require_string(ctx, 0); + struct texture *tex; + struct vfs_file file; + + if (vfs_open(js_core_global_vfs(ctx), &file, entry, "r") < 0) + duk_error(ctx, DUK_ERR_ERROR, "%s", error()); + + tex = alloc_new0(sizeof (*tex)); + + if (image_openvfs(tex, &file) < 0) { + free(tex); + vfs_file_finish(&file); + duk_error(ctx, DUK_ERR_ERROR, "%s", error()); + } + + vfs_file_finish(&file); + js_texture_push(ctx, tex); + + return 1; +} + static const struct duk_function_list_entry methods[] = { { "setBlendMode", Texture_setBlendMode, 1 }, { "setAlphaMod", Texture_setAlphaMod, 1 }, @@ -159,6 +186,11 @@ { NULL, NULL, 0 } }; +static const struct duk_function_list_entry functions[] = { + { "fromImage", Texture_fromImage, 1 }, + { NULL, NULL, 0 } +}; + static const duk_number_list_entry blend[] = { { "NONE", TEXTURE_BLEND_NONE }, { "BLEND", TEXTURE_BLEND_BLEND }, @@ -172,8 +204,8 @@ { assert(ctx); - duk_push_global_object(ctx); duk_push_c_function(ctx, Texture_constructor, 2); + duk_put_function_list(ctx, -1, functions); duk_push_object(ctx); duk_put_function_list(ctx, -1, methods); duk_push_c_function(ctx, Texture_destructor, 1); @@ -184,7 +216,7 @@ duk_dup(ctx, -1); duk_put_global_string(ctx, PROTOTYPE); duk_put_prop_string(ctx, -2, "prototype"); - duk_pop(ctx); + duk_put_global_string(ctx, "Texture"); } struct texture *
--- a/src/libmlk-core/core/image.c Sat Oct 16 09:01:20 2021 +0200 +++ b/src/libmlk-core/core/image.c Sat Oct 16 09:13:40 2021 +0200 @@ -22,6 +22,8 @@ #include "error.h" #include "texture.h" +#include "vfs.h" +#include "vfs_p.h" #include "window.h" #include "window_p.h" @@ -66,3 +68,21 @@ return 0; } + +int +image_openvfs(struct texture *tex, struct vfs_file *file) +{ + assert(tex); + assert(vfs_file_ok(file)); + + SDL_RWops *ops; + + if (!(ops = vfs_to_rw(file))) + return -1; + if (!(tex->handle = IMG_LoadTexture_RW(RENDERER(), ops, 1))) + return errorf("%s", SDL_GetError()); + + dimensions(tex); + + return 0; +}
--- a/src/libmlk-core/core/image.h Sat Oct 16 09:01:20 2021 +0200 +++ b/src/libmlk-core/core/image.h Sat Oct 16 09:13:40 2021 +0200 @@ -24,6 +24,7 @@ #include "core.h" struct texture; +struct vfs_file; CORE_BEGIN_DECLS @@ -33,6 +34,10 @@ int image_openmem(struct texture *, const void *, size_t); +int +image_openvfs(struct texture *, struct vfs_file *); + + CORE_END_DECLS #endif /* !MOLKO_CORE_IMAGE_H */