Skip to content

Instantly share code, notes, and snippets.

@kdrnic
Created September 3, 2017 16:39
Show Gist options
  • Save kdrnic/825a121e93ef5394fafab09c0dd24552 to your computer and use it in GitHub Desktop.
Save kdrnic/825a121e93ef5394fafab09c0dd24552 to your computer and use it in GitHub Desktop.
#define JS_FUNC(name) static duk_ret_t js_ ## name (duk_context *ctx)
JS_FUNC(Key)
{
int sc = duk_to_int(ctx, 0);
duk_push_number(ctx, key[sc]);
return 1;
}
JS_FUNC(Joystick)
{
duk_idx_t obj_idx, arr_idx;
int n = duk_to_int(ctx, 0);
int b;
obj_idx = duk_push_object(ctx);
duk_push_int(ctx, joy[n].flags);
duk_put_prop_string(ctx, obj_idx, "flags");
duk_push_int(ctx, joy[n].num_sticks);
duk_put_prop_string(ctx, obj_idx, "num_sticks");
duk_push_int(ctx, joy[n].num_buttons);
duk_put_prop_string(ctx, obj_idx, "num_buttons");
arr_idx = duk_push_array(ctx);
for(b = 0; b < joy[n].num_buttons; b++)
{
duk_idx_t obj_idx2 = duk_push_object(ctx);
duk_push_int(ctx, joy[n].button[b].b);
duk_put_prop_string(ctx, obj_idx2, "b");
duk_push_string(ctx, joy[n].button[b].name);
duk_put_prop_string(ctx, obj_idx2, "name");
duk_put_prop_index(ctx, arr_idx, b);
}
duk_put_prop_string(ctx, obj_idx, "button");
return 1;
}
JS_FUNC(JoyStick)
{
int j = duk_get_int(ctx, 0);
int s = duk_get_int(ctx, 1);
duk_idx_t arr_idx = duk_push_array(ctx);
if(s < joy[j].num_sticks)
{
int a;
for(a = 0; a < joy[j].stick[s].num_axis; a++)
{
duk_push_number(ctx, ((double) joy[j].stick[s].axis[a].pos) / 128.0);
duk_put_prop_index(ctx, arr_idx, a * 3);
duk_push_number(ctx,joy[j].stick[s].axis[a].d1);
duk_put_prop_index(ctx, arr_idx, 1 + a * 3);
duk_push_number(ctx,joy[j].stick[s].axis[a].d2);
duk_put_prop_index(ctx, arr_idx, 2 + a * 3);
}
}
return 1;
}
JS_FUNC(Cout)
{
int i;
int n = duk_get_top(ctx);
for(i = 0; i < n; i++)
{
switch(duk_get_type(ctx, i))
{
#define CASETYPE(type, str, ...) case DUK_TYPE_ ## type : printf(str, ## __VA_ARGS__); break;
CASETYPE(UNDEFINED, "undefined")
CASETYPE(NULL, "null")
CASETYPE(BOOLEAN, duk_get_boolean(ctx, i) ? "true" : "false")
CASETYPE(NUMBER, "%f", duk_get_number(ctx, i))
CASETYPE(STRING, "%s", duk_get_string(ctx, i))
CASETYPE(OBJECT, "[object]")
CASETYPE(POINTER, "[pointer %x]", (unsigned int) duk_get_pointer)
CASETYPE(BUFFER, "[buffer]")
CASETYPE(LIGHTFUNC, "[lightfunc]")
#undef CASETYPE
default:
printf("[unknown type?]");
break;
}
if(i + 1 < n) printf(" ");
else printf("\n");
}
return 0;
}
JS_FUNC(Quit)
{
quit = 1;
return 0;
}
JS_FUNC(ClearToColor)
{
BITMAP *bmp;
int c;
c = duk_get_int(ctx, 1);
duk_get_prop_string(ctx, 0, "ptr");
bmp = duk_get_pointer(ctx, -1);
clear_to_color(bmp, c);
return 0;
}
JS_FUNC(RectFill)
{
BITMAP *bmp;
int x1, x2, y1, y2, c;
x1 = duk_get_int(ctx, 1);
y1 = duk_get_int(ctx, 2);
x2 = duk_get_int(ctx, 3);
y2 = duk_get_int(ctx, 4);
c = duk_get_int(ctx, 5);
duk_get_prop_string(ctx, 0, "ptr");
bmp = duk_get_pointer(ctx, -1);
rectfill(bmp, x1, y1, x2, y2, c);
return 0;
}
void init_js_funcs()
{
#define PUSHFUNC(a,b) duk_push_c_function(main_ctx, js_ ## a, b); duk_put_global_string(main_ctx, #a);
PUSHFUNC(Key, 1)
PUSHFUNC(Quit, 0)
PUSHFUNC(ClearToColor, 2)
PUSHFUNC(RectFill, 6)
PUSHFUNC(Joystick, 1)
PUSHFUNC(Cout, DUK_VARARGS)
PUSHFUNC(JoyStick, 1)
#undef PUSHFUNC
}
#undef JS_FUNC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment