WiiDuktape/source/grrlib_duk.c
Fries 9f1b00d39d add file support
file loading is implemented with an asset generator script that reads
all the files in the data folder and converts them into c arrays and
puts them inside a c source file.
2024-06-20 18:41:49 -07:00

81 lines
2.1 KiB
C

#include "grrlib_duk.h"
#include "duk_config.h"
#include "duktape.h"
#include <grrlib.h>
static unsigned int get_color(duk_context *ctx, duk_idx_t index) {
size_t rgba_size;
unsigned char * rgba = duk_get_buffer(ctx, index, &rgba_size);
return rgba[0] << 24 | rgba[1] << 16 | rgba[2] << 8 | rgba[3];
}
static duk_ret_t grrlib_fill_screen(duk_context *ctx) {
unsigned int color = get_color(ctx, 0);
GRRLIB_FillScreen(color);
return 0;
}
static duk_ret_t grrlib_rectangle(duk_context *ctx) {
float x = duk_to_number(ctx, 0);
float y = duk_to_number(ctx, 1);
float width = duk_to_number(ctx, 2);
float height = duk_to_number(ctx, 3);
unsigned int color = get_color(ctx, 4);
bool filled = duk_to_boolean(ctx, 5);
GRRLIB_Rectangle(x, y, width, height, color, filled);
return 0;
}
static duk_ret_t grrlib_load_texture(duk_context *ctx) {
void* file_ptr;
duk_push_string(ctx, "file_ptr");
duk_get_prop(ctx, 0);
file_ptr = duk_get_pointer(ctx, -1);
duk_pop(ctx);
size_t size;
duk_push_string(ctx, "size");
duk_get_prop(ctx, 0);
size = duk_get_number(ctx, -1);
duk_pop(ctx);
GRRLIB_texImg* texture = GRRLIB_LoadTexture(file_ptr);
duk_push_pointer(ctx, texture);
return 1;
}
static duk_ret_t grrlib_draw_img(duk_context *ctx) {
float xPos = duk_to_number(ctx, 0);
float yPos = duk_to_number(ctx, 1);
void * texture = duk_get_pointer(ctx, 2);
float degrees = duk_to_number(ctx, 3);
float scaleX = duk_to_number(ctx, 4);
float scaleY = duk_to_number(ctx, 5);
unsigned int color = get_color(ctx, 6);
GRRLIB_DrawImg(xPos, yPos, texture, degrees, scaleX, scaleY, color);
return 0;
}
void define_grrlib_object(duk_context *ctx) {
duk_idx_t grrlib_obj = duk_push_object(ctx);
duk_push_c_function(ctx, grrlib_fill_screen, 1);
duk_put_prop_string(ctx, grrlib_obj, "fill_screen");
duk_push_c_function(ctx, grrlib_rectangle, 6);
duk_put_prop_string(ctx, grrlib_obj, "rectangle");
duk_push_c_function(ctx, grrlib_load_texture, 1);
duk_put_prop_string(ctx, grrlib_obj, "load_texture");
duk_push_c_function(ctx, grrlib_draw_img, 7);
duk_put_prop_string(ctx, grrlib_obj, "draw_img");
}