WiiDuktape/source/grrlib_duk.c

91 lines
2.4 KiB
C
Raw Normal View History

2024-06-20 06:37:41 +00:00
#include "grrlib_duk.h"
#include "duk_config.h"
#include "duktape.h"
2024-06-20 06:37:41 +00:00
#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);
if (texture->data == NULL) {
duk_push_string(ctx, "filename");
duk_get_prop(ctx, 0);
const char * filename = duk_to_string(ctx, -1);
duk_pop(ctx);
return duk_error(ctx, DUK_ERR_ERROR, "An error has occured while trying load the texture for the file %s.", filename);
}
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;
}
2024-06-20 06:37:41 +00:00
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");
2024-06-20 06:37:41 +00:00
}