2023-01-10 07:13:00 -06:00
|
|
|
#include "ffi.h"
|
|
|
|
|
|
|
|
#include "script.h"
|
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
#include "string.h"
|
2023-03-24 14:01:01 -05:00
|
|
|
#include "debug.h"
|
2023-01-10 14:02:24 -06:00
|
|
|
#include "window.h"
|
|
|
|
#include "editor.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "gameobject.h"
|
|
|
|
#include "openglrender.h"
|
|
|
|
#include "2dphysics.h"
|
|
|
|
#include "sprite.h"
|
|
|
|
#include "anim.h"
|
|
|
|
#include "yugine.h"
|
|
|
|
#include "nuke.h"
|
2023-01-15 09:53:50 -06:00
|
|
|
#include "sound.h"
|
2023-01-10 15:41:43 -06:00
|
|
|
#include "font.h"
|
2023-01-16 13:20:07 -06:00
|
|
|
#include "sound.h"
|
|
|
|
#include "music.h"
|
|
|
|
#include "level.h"
|
2023-01-27 12:06:16 -06:00
|
|
|
#include "tinyspline.h"
|
2023-02-02 17:52:15 -06:00
|
|
|
#include "mix.h"
|
2023-02-05 17:42:36 -06:00
|
|
|
#include "debugdraw.h"
|
|
|
|
#include "stb_ds.h"
|
2023-02-13 21:02:14 -06:00
|
|
|
#include <ftw.h>
|
2023-02-02 17:52:15 -06:00
|
|
|
|
2023-03-24 14:01:01 -05:00
|
|
|
#include "miniaudio.h"
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
|
|
|
|
#define BYTE_TO_BINARY(byte) \
|
|
|
|
(byte & 0x80 ? '1' : '0'), \
|
|
|
|
(byte & 0x40 ? '1' : '0'), \
|
|
|
|
(byte & 0x20 ? '1' : '0'), \
|
|
|
|
(byte & 0x10 ? '1' : '0'), \
|
|
|
|
(byte & 0x08 ? '1' : '0'), \
|
|
|
|
(byte & 0x04 ? '1' : '0'), \
|
|
|
|
(byte & 0x02 ? '1' : '0'), \
|
|
|
|
(byte & 0x01 ? '1' : '0')
|
2023-01-25 21:32:58 -06:00
|
|
|
|
2023-02-06 16:41:47 -06:00
|
|
|
struct gameobject *duk2go(duk_context *duk, int p)
|
|
|
|
{
|
|
|
|
return id2go(duk_to_int(duk, p));
|
|
|
|
}
|
|
|
|
|
2023-03-05 22:05:22 -06:00
|
|
|
struct timer *duk2timer(duk_context *duk, int p)
|
|
|
|
{
|
|
|
|
return id2timer(duk_to_int(duk, p));
|
|
|
|
}
|
|
|
|
|
2023-01-16 02:16:39 -06:00
|
|
|
struct color duk2color(duk_context *duk, int p)
|
|
|
|
{
|
|
|
|
struct color color;
|
|
|
|
|
|
|
|
duk_get_prop_index(duk, p, 0);
|
2023-03-03 13:07:59 -06:00
|
|
|
color.r = duk_to_int(duk, -1);
|
|
|
|
duk_get_prop_index(duk, p, 1);
|
|
|
|
color.g = duk_to_int(duk, -1);
|
|
|
|
duk_get_prop_index(duk, p, 2);
|
|
|
|
color.b = duk_to_int(duk, -1);
|
2023-01-16 02:16:39 -06:00
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2023-03-05 22:05:22 -06:00
|
|
|
float duk_get_prop_number(duk_context *duk, int p, const char *str)
|
|
|
|
{
|
|
|
|
duk_get_prop_string(duk,p,str);
|
|
|
|
return duk_to_number(duk,-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct glrect duk_to_glrect(duk_context *duk, int p)
|
|
|
|
{
|
|
|
|
struct glrect rect;
|
|
|
|
rect.s0 = duk_get_prop_number(duk,p,"s0");
|
|
|
|
rect.s1 = duk_get_prop_number(duk,p,"s1");
|
|
|
|
rect.t0 = duk_get_prop_number(duk,p,"t0");
|
|
|
|
rect.t1 = duk_get_prop_number(duk,p,"t1");
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
cpVect duk2vec2(duk_context *duk, int p)
|
|
|
|
{
|
2023-01-10 07:13:00 -06:00
|
|
|
cpVect pos;
|
2023-02-02 17:52:15 -06:00
|
|
|
|
2023-01-25 21:32:58 -06:00
|
|
|
if (p < 0) p = duk_get_top_index(duk) + p + 1;
|
|
|
|
|
2023-01-10 07:13:00 -06:00
|
|
|
duk_get_prop_index(duk, p, 0);
|
2023-01-10 14:02:24 -06:00
|
|
|
pos.x = duk_to_number(duk, -1);
|
2023-01-27 12:06:16 -06:00
|
|
|
duk_pop(duk);
|
|
|
|
|
2023-01-10 07:13:00 -06:00
|
|
|
duk_get_prop_index(duk, p, 1);
|
|
|
|
pos.y = duk_to_number(duk, -1);
|
|
|
|
|
2023-01-27 12:06:16 -06:00
|
|
|
duk_pop(duk);
|
|
|
|
|
2023-01-10 07:13:00 -06:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
cpBitmask duk2bitmask(duk_context *duk, int p)
|
|
|
|
{
|
|
|
|
cpBitmask mask = 0;
|
|
|
|
|
|
|
|
if (p < 0) p = duk_get_top_index(duk)+p+1;
|
|
|
|
|
|
|
|
int len = duk_get_length(duk, p);
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
duk_get_prop_index(duk, p, i);
|
2023-03-10 13:13:48 -06:00
|
|
|
int val = duk_to_boolean(duk, -1);
|
2023-02-02 17:52:15 -06:00
|
|
|
duk_pop(duk);
|
2023-03-10 13:13:48 -06:00
|
|
|
if (!val) continue;
|
2023-02-02 17:52:15 -06:00
|
|
|
|
2023-03-10 13:13:48 -06:00
|
|
|
mask |= 1<<i;
|
2023-02-02 17:52:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2023-02-08 15:30:12 -06:00
|
|
|
cpVect *duk2cpvec2arr(duk_context *duk, int p)
|
|
|
|
{
|
|
|
|
int n = duk_get_length(duk, p);
|
|
|
|
cpVect *points = NULL;
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
duk_get_prop_index(duk, p, i);
|
|
|
|
|
|
|
|
arrput(points, duk2vec2(duk, -1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
void bitmask2duk(duk_context *duk, cpBitmask mask)
|
|
|
|
{
|
|
|
|
int arr = duk_push_array(duk);
|
|
|
|
int arridx = 0;
|
|
|
|
for (int i = 0; i < 11; i++) {
|
|
|
|
int on = mask & 1<<i;
|
|
|
|
if (on) {
|
|
|
|
duk_push_int(duk, i);
|
|
|
|
duk_put_prop_index(duk, arr, arridx++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 18:30:23 -06:00
|
|
|
void vec2float(cpVect v, float *f)
|
|
|
|
{
|
|
|
|
f[0] = v.x;
|
|
|
|
f[1] = v.y;
|
|
|
|
}
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
|
|
|
|
duk_idx_t vect2duk(cpVect v) {
|
|
|
|
duk_idx_t arr = duk_push_array(duk);
|
|
|
|
duk_push_number(duk, v.x);
|
|
|
|
duk_put_prop_index(duk, arr, 0);
|
|
|
|
duk_push_number(duk, v.y);
|
|
|
|
duk_put_prop_index(duk, arr, 1);
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2023-03-19 20:33:05 -05:00
|
|
|
duk_idx_t vecarr2duk(duk_context *duk, cpVect *points, int n)
|
|
|
|
{
|
|
|
|
duk_idx_t arr = duk_push_array(duk);
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-03-24 14:01:01 -05:00
|
|
|
vect2duk(points[i]);
|
2023-03-19 20:33:05 -05:00
|
|
|
duk_put_prop_index(duk, arr, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
void duk_dump_stack(duk_context *duk)
|
|
|
|
{
|
|
|
|
YughInfo("DUK CALLSTACK");
|
2023-03-17 10:25:35 -05:00
|
|
|
for (int i = -1; ; i--) { /* Start at -2 to skip the invoked C function */
|
2023-02-02 17:52:15 -06:00
|
|
|
duk_inspect_callstack_entry(duk, i);
|
|
|
|
if (duk_is_undefined(duk, -1)) break;
|
|
|
|
|
|
|
|
duk_get_prop_string(duk, -1, "lineNumber");
|
|
|
|
long ln = duk_to_int(duk, -1);
|
|
|
|
duk_pop(duk);
|
|
|
|
|
|
|
|
duk_get_prop_string(duk, -1, "function");
|
|
|
|
duk_get_prop_string(duk, -1, "name");
|
|
|
|
const char *fn = duk_to_string(duk, -1);
|
|
|
|
duk_pop(duk);
|
|
|
|
|
|
|
|
duk_get_prop_string(duk, -1, "fileName");
|
|
|
|
const char *file = duk_to_string(duk, -1);
|
|
|
|
duk_pop(duk);
|
|
|
|
|
|
|
|
mYughLog(1, 3, ln, file, "function: %s", fn);
|
|
|
|
|
|
|
|
duk_pop_2(duk);
|
|
|
|
}
|
|
|
|
|
|
|
|
duk_push_context_dump(duk);
|
|
|
|
YughInfo("DUK STACK\n%s", duk_to_string(duk, -1));
|
|
|
|
duk_pop(duk);
|
|
|
|
}
|
|
|
|
|
2023-01-10 07:13:00 -06:00
|
|
|
duk_ret_t duk_gui_text(duk_context *duk) {
|
|
|
|
const char *s = duk_to_string(duk, 0);
|
|
|
|
cpVect pos = duk2vec2(duk, 1);
|
|
|
|
|
|
|
|
float size = duk_to_number(duk, 2);
|
2023-01-10 14:02:24 -06:00
|
|
|
const float white[3] = {1.f, 1.f, 1.f};
|
2023-03-10 13:13:48 -06:00
|
|
|
renderText(s, &pos, size, white, 500,-1);
|
2023-01-10 07:13:00 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-03 13:07:59 -06:00
|
|
|
duk_ret_t duk_ui_text(duk_context *duk)
|
|
|
|
{
|
|
|
|
const char *s = duk_to_string(duk, 0);
|
|
|
|
cpVect pos = duk2vec2(duk, 1);
|
|
|
|
|
|
|
|
float size = duk_to_number(duk, 2);
|
|
|
|
struct color c = duk2color(duk,3);
|
|
|
|
const float col[3] = {(float)c.r/255, (float)c.g/255, (float)c.b/255};
|
2023-04-07 12:52:35 -05:00
|
|
|
int wrap = duk_to_int(duk,4);
|
|
|
|
duk_push_int(duk,renderText(s, &pos, size, col, wrap,-1));
|
|
|
|
return 1;
|
2023-03-10 13:13:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
duk_ret_t duk_cursor_text(duk_context *duk)
|
|
|
|
{
|
|
|
|
const char *s = duk_to_string(duk, 0);
|
|
|
|
cpVect pos = duk2vec2(duk, 1);
|
2023-03-03 13:07:59 -06:00
|
|
|
|
2023-03-10 13:13:48 -06:00
|
|
|
float size = duk_to_number(duk, 2);
|
|
|
|
struct color c = duk2color(duk,3);
|
|
|
|
const float col[3] = {(float)c.r/255, (float)c.g/255, (float)c.b/255};
|
|
|
|
int cursor = duk_to_int(duk,4);
|
2023-04-07 12:52:35 -05:00
|
|
|
int wrap = duk_to_int(duk,5);
|
|
|
|
renderText(s, &pos, size, col, wrap,cursor);
|
2023-03-10 13:13:48 -06:00
|
|
|
return 0;
|
2023-03-03 13:07:59 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 17:23:11 -06:00
|
|
|
duk_ret_t duk_gui_img(duk_context *duk) {
|
|
|
|
const char *img = duk_to_string(duk, 0);
|
|
|
|
cpVect pos = duk2vec2(duk, 1);
|
|
|
|
gui_draw_img(img, pos.x, pos.y);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-19 13:06:32 -06:00
|
|
|
duk_ret_t duk_nuke(duk_context *duk)
|
|
|
|
{
|
|
|
|
int cmd = duk_to_int(duk, 0);
|
|
|
|
float editnum;
|
2023-01-19 16:43:29 -06:00
|
|
|
int editint;
|
2023-02-05 11:40:59 -06:00
|
|
|
char textbox[130];
|
2023-02-26 10:24:21 -06:00
|
|
|
struct nk_rect rect = nk_rect(0,0,0,0);
|
2023-01-19 13:06:32 -06:00
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case 0:
|
2023-02-26 10:24:21 -06:00
|
|
|
duk_get_prop_string(duk, 2, "x");
|
|
|
|
rect.x = duk_to_number(duk,-1);
|
|
|
|
duk_get_prop_string(duk, 2, "y");
|
|
|
|
rect.y = duk_to_number(duk,-1);
|
|
|
|
duk_get_prop_string(duk,2,"w");
|
|
|
|
rect.w = duk_to_number(duk,-1);
|
|
|
|
duk_get_prop_string(duk,2,"h");
|
|
|
|
rect.h = duk_to_number(duk,-1);
|
|
|
|
nuke_begin(duk_to_string(duk, 1),rect, NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE | NK_WINDOW_TITLE);
|
2023-01-19 13:06:32 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
nuke_stop();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
editnum = duk_to_number(duk, 2);
|
2023-01-19 18:30:23 -06:00
|
|
|
nuke_property_float(duk_to_string(duk, 1), duk_to_number(duk, 3), &editnum, duk_to_number(duk, 4), duk_to_number(duk, 5), duk_to_number(duk, 5));
|
2023-01-19 13:06:32 -06:00
|
|
|
duk_push_number(duk, editnum);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
nuke_nel(duk_to_number(duk, 1));
|
|
|
|
return 0;
|
2023-01-19 16:43:29 -06:00
|
|
|
|
|
|
|
case 4:
|
|
|
|
editint = duk_to_boolean(duk, 2);
|
|
|
|
nuke_checkbox(duk_to_string(duk, 1), &editint);
|
|
|
|
duk_push_boolean(duk, editint);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
nuke_label(duk_to_string(duk, 1));
|
|
|
|
return 0;
|
2023-02-03 13:41:53 -06:00
|
|
|
|
|
|
|
case 6:
|
|
|
|
duk_push_boolean(duk, nuke_btn(duk_to_string(duk, 1)));
|
|
|
|
return 1;
|
2023-02-05 11:40:59 -06:00
|
|
|
|
|
|
|
case 7:
|
|
|
|
strncpy(textbox, duk_to_string(duk, 1), 130);
|
|
|
|
nuke_edit_str(textbox);
|
|
|
|
duk_push_string(duk, textbox);
|
|
|
|
return 1;
|
2023-02-15 17:54:05 -06:00
|
|
|
|
|
|
|
case 8:
|
|
|
|
nuke_img(duk_to_string(duk, 1));
|
|
|
|
break;
|
2023-02-23 17:03:58 -06:00
|
|
|
|
|
|
|
case 9:
|
|
|
|
editint = duk_to_int(duk,2);
|
|
|
|
nuke_radio_btn(duk_to_string(duk,1), &editint, duk_to_int(duk, 3));
|
|
|
|
duk_push_int(duk, editint);
|
|
|
|
return 1;
|
2023-02-26 10:24:21 -06:00
|
|
|
|
|
|
|
case 10:
|
|
|
|
rect = nuke_win_get_bounds();
|
|
|
|
duk_push_object(duk);
|
|
|
|
duk_push_number(duk, rect.x);
|
|
|
|
duk_put_prop_string(duk, -2, "x");
|
|
|
|
duk_push_number(duk,rect.y);
|
|
|
|
duk_put_prop_string(duk, -2, "y");
|
|
|
|
duk_push_number(duk,rect.w);
|
|
|
|
duk_put_prop_string(duk,-2,"w");
|
|
|
|
duk_push_number(duk,rect.h);
|
|
|
|
duk_put_prop_string(duk,-2,"h");
|
|
|
|
return 1;
|
2023-03-01 14:42:44 -06:00
|
|
|
|
|
|
|
case 11:
|
|
|
|
duk_push_boolean(duk, nuke_push_tree_id(duk_to_string(duk, 1), duk_to_int(duk,2)));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 12:
|
|
|
|
nuke_tree_pop();
|
|
|
|
return 0;
|
2023-03-10 13:13:48 -06:00
|
|
|
|
|
|
|
case 13:
|
|
|
|
nuke_row(duk_to_int(duk,1));
|
|
|
|
break;
|
2023-03-17 10:25:35 -05:00
|
|
|
|
|
|
|
case 14:
|
|
|
|
nuke_scrolltext(duk_to_string(duk,1));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 15:
|
|
|
|
nuke_nel_h(duk_to_int(duk,1), duk_to_int(duk,2));
|
|
|
|
break;
|
2023-01-19 13:06:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-10 07:13:00 -06:00
|
|
|
duk_ret_t duk_win_make(duk_context *duk) {
|
|
|
|
const char *title = duk_to_string(duk, 0);
|
|
|
|
int w = duk_to_int(duk, 1);
|
|
|
|
int h = duk_to_int(duk, 2);
|
|
|
|
struct window *win = MakeSDLWindow(title, w, h, 0);
|
|
|
|
|
|
|
|
duk_push_int(duk, win->id);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-27 12:06:16 -06:00
|
|
|
duk_ret_t duk_spline_cmd(duk_context *duk)
|
|
|
|
{
|
|
|
|
tsBSpline spline;
|
|
|
|
|
|
|
|
int n = duk_get_length(duk, 4);
|
|
|
|
int d = duk_to_int(duk, 2);
|
2023-02-08 15:30:12 -06:00
|
|
|
cpVect points[n*d];
|
2023-01-27 12:06:16 -06:00
|
|
|
|
2023-02-10 14:31:59 -06:00
|
|
|
tsStatus status;
|
|
|
|
ts_bspline_new(n, d, duk_to_int(duk, 1), duk_to_int(duk, 3), &spline, &status);
|
|
|
|
|
|
|
|
if (status.code) {
|
|
|
|
YughCritical("Spline creation error %d: %s", status.code, status.message);
|
|
|
|
}
|
2023-01-27 12:06:16 -06:00
|
|
|
|
2023-03-24 14:01:01 -05:00
|
|
|
for (int i = 0; i < n; i++) {
|
2023-01-27 12:06:16 -06:00
|
|
|
duk_get_prop_index(duk, 4, i);
|
|
|
|
points[i] = duk2vec2(duk, -1);
|
|
|
|
duk_pop(duk);
|
2023-03-24 14:01:01 -05:00
|
|
|
}
|
2023-01-27 12:06:16 -06:00
|
|
|
|
|
|
|
ts_bspline_set_control_points(&spline, points, NULL);
|
|
|
|
|
|
|
|
|
2023-03-24 14:01:01 -05:00
|
|
|
size_t nsamples = duk_to_int(duk, 5);
|
2023-01-27 12:06:16 -06:00
|
|
|
cpVect samples[nsamples];
|
2023-03-24 14:01:01 -05:00
|
|
|
static_assert(sizeof(tsReal)*2 == sizeof(cpVect));
|
|
|
|
size_t rsamples;
|
2023-01-27 12:06:16 -06:00
|
|
|
ts_bspline_sample(&spline, nsamples, &samples, &rsamples, NULL);
|
|
|
|
|
2023-03-24 14:01:01 -05:00
|
|
|
int arridx = duk_push_array(duk);
|
2023-01-27 12:06:16 -06:00
|
|
|
|
2023-03-24 14:01:01 -05:00
|
|
|
duk_require_stack(duk, nsamples*3);
|
2023-01-27 12:06:16 -06:00
|
|
|
|
2023-03-24 14:01:01 -05:00
|
|
|
for (int i = 0; i < nsamples; i++) {
|
|
|
|
int pidx = duk_push_array(duk);
|
|
|
|
duk_push_number(duk, samples[i].x);
|
|
|
|
duk_put_prop_index(duk, pidx, 0);
|
|
|
|
duk_push_number(duk, samples[i].y);
|
|
|
|
duk_put_prop_index(duk, pidx, 1);
|
|
|
|
duk_put_prop_index(duk, arridx, i);
|
|
|
|
}
|
2023-01-27 12:06:16 -06:00
|
|
|
|
2023-03-24 14:01:01 -05:00
|
|
|
ts_bspline_free(&spline);
|
2023-01-27 12:06:16 -06:00
|
|
|
|
2023-03-24 14:01:01 -05:00
|
|
|
return 1;
|
2023-01-27 12:06:16 -06:00
|
|
|
}
|
|
|
|
|
2023-02-05 17:42:36 -06:00
|
|
|
void ints2duk(int *ints)
|
|
|
|
{
|
|
|
|
int idx = duk_push_array(duk);
|
|
|
|
|
|
|
|
for (int i = 0; i < arrlen(ints); i++) {
|
|
|
|
duk_push_int(duk, ints[i]);
|
|
|
|
duk_put_prop_index(duk, idx, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:31:58 -06:00
|
|
|
int vec_between(cpVect p, cpVect a, cpVect b)
|
|
|
|
{
|
|
|
|
cpVect n;
|
|
|
|
n.x = b.x - a.x;
|
|
|
|
n.y = b.y - a.y;
|
|
|
|
n = cpvnormalize(n);
|
|
|
|
|
|
|
|
return (cpvdot(n, cpvsub(p, a)) > 0 && cpvdot(cpvneg(n), cpvsub(p, b)) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determines between which two points in 'segs' point 'p' falls.
|
|
|
|
0 indicates 'p' comes before the first point.
|
|
|
|
arrlen(segs) indicates it comes after the last point.
|
|
|
|
*/
|
2023-02-08 15:30:12 -06:00
|
|
|
int point2segindex(cpVect p, cpVect *segs, double slop)
|
|
|
|
{
|
2023-02-10 14:31:58 -06:00
|
|
|
float shortest = slop < 0 ? INFINITY : slop;
|
2023-02-08 15:30:12 -06:00
|
|
|
int best = -1;
|
|
|
|
|
|
|
|
for (int i = 0; i < arrlen(segs)-1; i++)
|
|
|
|
{
|
|
|
|
float a = (segs[i+1].y - segs[i].y) / (segs[i+1].x - segs[i].x);
|
|
|
|
float c = segs[i].y - (a * segs[i].x);
|
|
|
|
float b = -1;
|
|
|
|
|
|
|
|
float dist = abs(a*p.x + b*p.y + c) / sqrt(pow(a,2) + 1);
|
|
|
|
|
2023-02-10 14:31:58 -06:00
|
|
|
if (dist > shortest) continue;
|
|
|
|
|
|
|
|
int between = vec_between(p, segs[i], segs[i+1]);
|
|
|
|
|
|
|
|
if (between) {
|
2023-02-08 15:30:12 -06:00
|
|
|
shortest = dist;
|
2023-02-10 14:31:58 -06:00
|
|
|
best = i+1;
|
|
|
|
} else {
|
|
|
|
if (i == 0 && cpvdist(p, segs[0]) < slop) {
|
|
|
|
shortest = dist;
|
|
|
|
best = i;
|
|
|
|
} else if (i == arrlen(segs)-2 && cpvdist(p, arrlast(segs)) < slop) {
|
|
|
|
shortest = dist;
|
|
|
|
best = arrlen(segs);
|
|
|
|
}
|
2023-02-08 15:30:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:31:58 -06:00
|
|
|
if (best == 1) {
|
2023-02-08 15:30:12 -06:00
|
|
|
cpVect n;
|
|
|
|
n.x = segs[1].x-segs[0].x;
|
|
|
|
n.y = segs[1].y-segs[0].y;
|
|
|
|
n = cpvnormalize(n);
|
2023-02-10 14:31:58 -06:00
|
|
|
if (cpvdot(n, cpvsub(p, segs[0])) < 0)
|
|
|
|
if (cpvdist(p, segs[0]) >= slop)
|
|
|
|
best = -1;
|
|
|
|
else
|
|
|
|
best = 0;
|
2023-02-08 15:30:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (best == arrlen(segs)-1) {
|
|
|
|
cpVect n;
|
|
|
|
n.x = segs[best-1].x-segs[best].x;
|
|
|
|
n.y = segs[best-1].y-segs[best-1].y;
|
|
|
|
n = cpvnormalize(n);
|
|
|
|
|
2023-02-10 14:31:58 -06:00
|
|
|
if (cpvdot(n, cpvsub(p, segs[best])) < 0)
|
|
|
|
if (cpvdist(p, segs[best]) >= slop)
|
|
|
|
best = -1;
|
|
|
|
else
|
|
|
|
best = arrlen(segs);
|
2023-02-08 15:30:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2023-02-13 21:02:14 -06:00
|
|
|
int file_exists(char *path)
|
|
|
|
{
|
|
|
|
FILE *o = fopen(path, "r");
|
|
|
|
if (o) {
|
|
|
|
fclose(o);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *dukext;
|
|
|
|
static int dukarr;
|
|
|
|
static int dukidx;
|
|
|
|
|
|
|
|
static int duk2path(const char *path, const struct stat *sb, int typeflag)
|
|
|
|
{
|
|
|
|
if (typeflag == FTW_F) {
|
|
|
|
char *ext = strrchr(path, '.');
|
|
|
|
if (ext && !strcmp(ext, dukext)) {
|
2023-02-15 17:54:05 -06:00
|
|
|
duk_push_string(duk, &path[2]);
|
2023-02-13 21:02:14 -06:00
|
|
|
duk_put_prop_index(duk, dukarr, dukidx++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dukext2paths(char *ext)
|
|
|
|
{
|
|
|
|
dukext = ext;
|
|
|
|
dukarr = duk_push_array(duk);
|
|
|
|
dukidx = 0;
|
|
|
|
ftw(".", duk2path, 10);
|
|
|
|
}
|
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
duk_ret_t duk_cmd(duk_context *duk) {
|
|
|
|
int cmd = duk_to_int(duk, 0);
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
switch(cmd) {
|
|
|
|
case 0:
|
|
|
|
duk_push_int(duk, script_dofile(duk_to_string(duk, 1)));
|
|
|
|
return 1;
|
2023-01-10 14:02:24 -06:00
|
|
|
|
|
|
|
case 1:
|
|
|
|
set_pawn(duk_get_heapptr(duk, 1));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2023-01-12 17:41:54 -06:00
|
|
|
gameobject_delete(duk_get_int(duk, 1));
|
2023-01-10 14:02:24 -06:00
|
|
|
break;
|
2023-01-10 17:23:11 -06:00
|
|
|
|
|
|
|
case 3:
|
|
|
|
set_timescale(duk_get_number(duk,1));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
debug_draw_phys(duk_get_boolean(duk, 1));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
renderMS = duk_get_number(duk, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
updateMS = duk_get_number(duk, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
physMS = duk_get_number(duk, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
phys2d_set_gravity(duk2vec2(duk, 1));
|
|
|
|
break;
|
|
|
|
|
2023-01-11 16:57:34 -06:00
|
|
|
case 9:
|
2023-01-12 17:41:54 -06:00
|
|
|
sprite_delete(duk_to_int(duk, 1));
|
2023-01-11 16:57:34 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 10:
|
2023-01-13 08:05:36 -06:00
|
|
|
remove_pawn(duk_get_heapptr(duk, 1));
|
2023-01-11 16:57:34 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 11:
|
2023-01-13 13:07:44 -06:00
|
|
|
duk_push_int(duk, file_mod_secs(duk_to_string(duk, 1)));
|
|
|
|
return 1;
|
|
|
|
break;
|
2023-01-11 16:57:34 -06:00
|
|
|
|
2023-01-13 13:07:44 -06:00
|
|
|
case 12:
|
2023-03-05 22:05:22 -06:00
|
|
|
sprite_loadtex(id2sprite(duk_to_int(duk, 1)), duk_to_string(duk, 2), duk_to_glrect(duk, 3));
|
2023-01-13 22:08:39 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 13:
|
|
|
|
play_song(duk_to_string(duk, 1), duk_to_string(duk, 2));
|
2023-01-11 16:57:34 -06:00
|
|
|
break;
|
|
|
|
|
2023-01-15 09:53:50 -06:00
|
|
|
case 14:
|
2023-03-24 14:01:01 -05:00
|
|
|
mini_sound(duk_to_string(duk,1));
|
2023-01-15 09:53:50 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 15:
|
2023-01-15 23:27:28 -06:00
|
|
|
music_stop();
|
2023-01-15 09:53:50 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
2023-01-18 10:45:43 -06:00
|
|
|
color2float(duk2color(duk, 1), dbg_color);
|
2023-01-15 09:53:50 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 17:
|
2023-01-18 10:45:43 -06:00
|
|
|
color2float(duk2color(duk, 1), trigger_color);
|
2023-01-15 09:53:50 -06:00
|
|
|
break;
|
|
|
|
|
2023-01-16 17:18:09 -06:00
|
|
|
case 18:
|
2023-01-17 15:09:14 -06:00
|
|
|
shape_set_sensor(duk_to_pointer(duk, 1), duk_to_boolean(duk, 2));
|
2023-01-16 17:18:09 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 19:
|
2023-03-24 14:01:01 -05:00
|
|
|
mini_master(duk_to_number(duk,1));
|
2023-01-17 15:09:14 -06:00
|
|
|
break;
|
2023-01-17 13:04:08 -06:00
|
|
|
|
|
|
|
case 20:
|
|
|
|
sprite_enabled(duk_to_int(duk, 1), duk_to_boolean(duk, 2));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 21:
|
2023-01-18 10:45:43 -06:00
|
|
|
duk_push_boolean(duk, shape_get_sensor(duk_to_pointer(duk, 1)));
|
|
|
|
return 1;
|
2023-01-17 13:04:08 -06:00
|
|
|
|
|
|
|
case 22:
|
2023-01-18 14:43:07 -06:00
|
|
|
shape_enabled(duk_to_pointer(duk, 1), duk_to_boolean(duk, 2));
|
2023-01-17 15:09:14 -06:00
|
|
|
break;
|
2023-01-18 10:45:43 -06:00
|
|
|
|
|
|
|
case 23:
|
|
|
|
duk_push_boolean(duk, shape_is_enabled(duk_to_pointer(duk, 1)));
|
|
|
|
return 1;
|
2023-01-18 17:15:36 -06:00
|
|
|
|
|
|
|
case 24:
|
2023-03-05 22:05:22 -06:00
|
|
|
timer_pause(duk2timer(duk,1));
|
2023-01-18 17:15:36 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 25:
|
2023-03-05 22:05:22 -06:00
|
|
|
timer_stop(duk2timer(duk,1));
|
2023-01-18 17:15:36 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 26:
|
2023-03-05 22:05:22 -06:00
|
|
|
timer_start(duk2timer(duk,1));
|
2023-01-18 17:15:36 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 27:
|
2023-03-17 10:25:35 -05:00
|
|
|
timer_remove(duk_to_int(duk,1));
|
2023-01-18 17:15:36 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 28:
|
2023-03-05 22:05:22 -06:00
|
|
|
timerr_settime(duk2timer(duk,1), duk_to_number(duk, 2));
|
2023-01-18 17:15:36 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 29:
|
2023-03-05 22:05:22 -06:00
|
|
|
duk_push_number(duk, duk2timer(duk,1)->interval);
|
2023-01-18 17:15:36 -06:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 30:
|
|
|
|
sprite_setanim(id2sprite(duk_to_int(duk, 1)), duk_to_pointer(duk, 2), duk_to_int(duk, 3));
|
|
|
|
return 0;
|
2023-01-19 10:44:29 -06:00
|
|
|
|
|
|
|
case 31:
|
|
|
|
free(duk_to_pointer(duk, 1));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 32:
|
2023-03-05 22:05:22 -06:00
|
|
|
duk_push_number(duk, duk2timer(duk,1)->remain_time);
|
2023-01-19 10:44:29 -06:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 33:
|
2023-03-05 22:05:22 -06:00
|
|
|
duk_push_boolean(duk, duk2timer(duk, 1)->on);
|
2023-01-19 10:44:29 -06:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 34:
|
2023-03-05 22:05:22 -06:00
|
|
|
duk_push_boolean(duk, duk2timer(duk,1)->repeat);
|
2023-01-19 10:44:29 -06:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 35:
|
2023-03-05 22:05:22 -06:00
|
|
|
duk2timer(duk,1)->repeat = duk_to_boolean(duk, 2);
|
2023-01-19 10:44:29 -06:00
|
|
|
return 0;
|
2023-01-19 18:30:23 -06:00
|
|
|
|
|
|
|
case 36:
|
|
|
|
id2go(duk_to_int(duk, 1))->scale = duk_to_number(duk, 2);
|
2023-01-25 21:32:58 -06:00
|
|
|
cpSpaceReindexShapesForBody(space, id2go(duk_to_int(duk, 1))->body);
|
2023-01-19 18:30:23 -06:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 37:
|
|
|
|
if (!id2sprite(duk_to_int(duk, 1))) return 0;
|
|
|
|
vec2float(duk2vec2(duk, 2), id2sprite(duk_to_int(duk, 1))->pos);
|
|
|
|
break;
|
2023-01-25 21:32:58 -06:00
|
|
|
|
|
|
|
case 38:
|
|
|
|
duk_push_string(duk, slurp_text(duk_to_string(duk, 1)));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 39:
|
|
|
|
duk_push_int(duk, slurp_write(duk_to_string(duk, 1), duk_to_string(duk, 2)));
|
|
|
|
return 1;
|
2023-02-02 17:52:15 -06:00
|
|
|
|
|
|
|
case 40:
|
|
|
|
id2go(duk_to_int(duk, 1))->filter.categories = duk2bitmask(duk, 2);
|
|
|
|
gameobject_apply(id2go(duk_to_int(duk, 1)));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 41:
|
|
|
|
id2go(duk_to_int(duk, 1))->filter.mask = duk2bitmask(duk, 2);
|
|
|
|
gameobject_apply(id2go(duk_to_int(duk, 1)));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 42:
|
|
|
|
bitmask2duk(duk, id2go(duk_to_int(duk, 1))->filter.categories);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 43:
|
|
|
|
bitmask2duk(duk, id2go(duk_to_int(duk, 1))->filter.mask);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 44:
|
|
|
|
duk_push_int(duk, pos2gameobject(duk2vec2(duk, 1)));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 45:
|
|
|
|
vect2duk(mouse_pos);
|
|
|
|
return 1;
|
2023-02-04 22:53:54 -06:00
|
|
|
|
|
|
|
case 46:
|
|
|
|
set_mouse_mode(duk_to_int(duk, 1));
|
|
|
|
return 0;
|
2023-02-05 11:40:59 -06:00
|
|
|
|
|
|
|
case 47:
|
|
|
|
draw_grid(duk_to_int(duk, 1), duk_to_int(duk, 2));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 48:
|
|
|
|
duk_push_int(duk, mainwin->width);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 49:
|
|
|
|
duk_push_int(duk, mainwin->height);
|
|
|
|
return 1;
|
2023-02-05 17:42:36 -06:00
|
|
|
|
|
|
|
case 50:
|
|
|
|
duk_push_boolean(duk, action_down(duk_to_int(duk, 1)));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 51:
|
2023-03-10 13:13:48 -06:00
|
|
|
draw_cppoint(duk2vec2(duk, 1), duk_to_number(duk, 2), duk2color(duk,3));
|
2023-02-05 17:42:36 -06:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 52:
|
|
|
|
ints2duk(phys2d_query_box(duk2vec2(duk, 1), duk2vec2(duk, 2)));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 53:
|
2023-03-03 13:07:59 -06:00
|
|
|
draw_box(duk2vec2(duk, 1), duk2vec2(duk, 2), duk2color(duk,3));
|
2023-02-05 17:42:36 -06:00
|
|
|
return 0;
|
2023-02-06 16:41:47 -06:00
|
|
|
|
|
|
|
case 54:
|
|
|
|
gameobject_apply(id2go(duk_to_int(duk, 1)));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 55:
|
|
|
|
duk2go(duk, 1)->flipx = duk_to_boolean(duk, 2) ? -1 : 1;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 56:
|
|
|
|
duk2go(duk, 1)->flipy = duk_to_boolean(duk, 2) ? -1 : 1;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 57:
|
|
|
|
duk_push_boolean(duk, duk2go(duk, 1)->flipx == -1 ? 1 : 0);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 58:
|
|
|
|
duk_push_boolean(duk, duk2go(duk, 1)->flipy == -1 ? 1 : 0);
|
|
|
|
return 1;
|
2023-02-08 15:30:12 -06:00
|
|
|
|
|
|
|
case 59:
|
|
|
|
duk_push_int(duk, point2segindex(duk2vec2(duk, 1), duk2cpvec2arr(duk, 2), duk_to_number(duk, 3)));
|
|
|
|
return 1;
|
2023-02-10 14:31:59 -06:00
|
|
|
|
|
|
|
case 60:
|
|
|
|
if (!id2sprite(duk_to_int(duk, 1))) return 0;
|
|
|
|
id2sprite(duk_to_int(duk, 1))->layer = duk_to_int(duk, 2);
|
|
|
|
break;
|
2023-02-13 08:30:35 -06:00
|
|
|
|
|
|
|
case 61:
|
|
|
|
set_cam_body(id2body(duk_to_int(duk, 1)));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 62:
|
|
|
|
add_zoom(duk_to_number(duk, 1));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 63:
|
|
|
|
duk_push_number(duk, deltaT);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 64:
|
|
|
|
vect2duk(tex_get_dimensions(texture_pullfromfile(duk_to_string(duk, 1))));
|
|
|
|
return 1;
|
|
|
|
|
2023-02-13 21:02:14 -06:00
|
|
|
case 65:
|
|
|
|
duk_push_boolean(duk, file_exists(duk_to_string(duk, 1)));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 66:
|
|
|
|
dukext2paths(duk_to_string(duk, 1));
|
|
|
|
return 1;
|
2023-02-16 16:13:07 -06:00
|
|
|
|
|
|
|
case 67:
|
|
|
|
opengl_rendermode(LIT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 68:
|
|
|
|
opengl_rendermode(WIREFRAME);
|
|
|
|
break;
|
2023-02-17 13:15:56 -06:00
|
|
|
|
|
|
|
case 69:
|
|
|
|
gameobject_set_sensor(duk_to_int(duk, 1), duk_to_boolean(duk,2));
|
|
|
|
break;
|
2023-02-20 11:10:03 -06:00
|
|
|
|
|
|
|
case 70:
|
|
|
|
vect2duk(world2go(id2go(duk_to_int(duk,1)), duk2vec2(duk,2)));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 71:
|
|
|
|
vect2duk(go2world(id2go(duk_to_int(duk,1)),duk2vec2(duk,2)));
|
|
|
|
return 1;
|
2023-02-24 14:13:35 -06:00
|
|
|
|
|
|
|
case 72:
|
|
|
|
vect2duk(cpSpaceGetGravity(space));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 73:
|
|
|
|
cpSpaceSetDamping(space, duk_to_number(duk,1));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 74:
|
|
|
|
duk_push_number(duk, cpSpaceGetDamping(space));
|
|
|
|
return 1;
|
2023-03-10 13:13:48 -06:00
|
|
|
|
|
|
|
case 75:
|
|
|
|
id2go(duk_to_int(duk,1))->layer = duk_to_int(duk,2);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 76:
|
|
|
|
set_cat_mask(duk_to_int(duk,1), duk2bitmask(duk,2));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 77:
|
|
|
|
input_to_game();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 78:
|
|
|
|
input_to_nuke();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 79:
|
|
|
|
duk_push_boolean(duk, phys_stepping());
|
|
|
|
return 1;
|
2023-03-13 09:27:32 -05:00
|
|
|
|
|
|
|
case 80:
|
|
|
|
ints2duk(phys2d_query_shape(duk_get_pointer(duk,1)));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 81:
|
2023-03-17 10:25:35 -05:00
|
|
|
draw_arrow(duk2vec2(duk,1), duk2vec2(duk,2), duk2color(duk,3), duk_to_int(duk,4));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 82:
|
|
|
|
gameobject_draw_debug(duk_to_int(duk,1));
|
2023-03-13 09:27:32 -05:00
|
|
|
return 0;
|
2023-03-17 10:25:35 -05:00
|
|
|
|
|
|
|
case 83:
|
|
|
|
draw_edge(duk2cpvec2arr(duk, 1), 2, duk2color(duk,2), 1);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 84:
|
|
|
|
duk_push_string(duk, consolelog);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 85:
|
|
|
|
vect2duk(cpvproject(duk2vec2(duk,1), duk2vec2(duk,2)));
|
|
|
|
return 1;
|
2023-03-19 20:33:05 -05:00
|
|
|
|
|
|
|
case 86:
|
|
|
|
ints2duk(phys2d_query_box_points(duk2vec2(duk, 1), duk2vec2(duk, 2), duk2cpvec2arr(duk,3), duk_to_int(duk,4)));
|
|
|
|
return 1;
|
2023-03-24 14:01:01 -05:00
|
|
|
|
|
|
|
case 87:
|
|
|
|
mini_music_play(duk_to_string(duk,1));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 88:
|
|
|
|
mini_music_pause();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 89:
|
|
|
|
mini_music_stop();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 90:
|
|
|
|
window_set_icon(duk_to_string(duk,1));
|
|
|
|
break;
|
2023-01-10 14:02:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-11 16:57:34 -06:00
|
|
|
duk_ret_t duk_register(duk_context *duk) {
|
|
|
|
int cmd = duk_to_int(duk, 0);
|
|
|
|
void *fn = duk_get_heapptr(duk, 1);
|
|
|
|
void *obj = duk_get_heapptr(duk, 2);
|
|
|
|
|
|
|
|
struct callee c = {fn, obj};
|
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case 0:
|
|
|
|
register_update(c);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
register_physics(c);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
register_gui(c);
|
|
|
|
break;
|
2023-01-19 13:06:32 -06:00
|
|
|
|
|
|
|
case 3:
|
|
|
|
register_nk_gui(c);
|
|
|
|
break;
|
2023-02-24 12:11:36 -06:00
|
|
|
|
|
|
|
case 4:
|
|
|
|
unregister_obj(obj);
|
|
|
|
break;
|
|
|
|
|
2023-02-28 17:03:28 -06:00
|
|
|
case 5:
|
|
|
|
unregister_gui(c);
|
|
|
|
break;
|
|
|
|
|
2023-03-17 10:25:35 -05:00
|
|
|
case 6:
|
|
|
|
register_debug(c);
|
|
|
|
break;
|
2023-01-11 16:57:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-17 01:16:52 -06:00
|
|
|
void gameobject_add_shape_collider(int go, struct callee c, struct phys2d_shape *shape)
|
|
|
|
{
|
|
|
|
struct shape_cb shapecb;
|
2023-03-10 13:13:48 -06:00
|
|
|
shapecb.shape = shape;
|
2023-02-17 01:16:52 -06:00
|
|
|
shapecb.cbs.begin = c;
|
|
|
|
arrpush(id2go(go)->shape_cbs, shapecb);
|
|
|
|
}
|
|
|
|
|
2023-01-11 16:57:34 -06:00
|
|
|
duk_ret_t duk_register_collide(duk_context *duk) {
|
|
|
|
int cmd = duk_to_int(duk, 0);
|
|
|
|
void *fn = duk_get_heapptr(duk, 1);
|
|
|
|
void *obj = duk_get_heapptr(duk, 2);
|
|
|
|
int go = duk_get_int(duk, 3);
|
|
|
|
struct callee c = {fn, obj};
|
2023-02-17 01:16:52 -06:00
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case 0:
|
2023-03-10 13:13:48 -06:00
|
|
|
id2go(go)->cbs.begin = c;
|
2023-02-17 01:16:52 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
gameobject_add_shape_collider(go, c, duk_get_pointer(duk,4));
|
|
|
|
break;
|
2023-02-24 12:11:36 -06:00
|
|
|
|
|
|
|
case 2:
|
|
|
|
phys2d_rm_go_handlers(go);
|
2023-03-17 10:25:35 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
id2go(go)->cbs.separate = c;
|
|
|
|
break;
|
2023-02-17 01:16:52 -06:00
|
|
|
}
|
2023-01-11 16:57:34 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-10 07:13:00 -06:00
|
|
|
duk_ret_t duk_sys_cmd(duk_context *duk) {
|
|
|
|
int cmd = duk_to_int(duk, 0);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case 0:
|
|
|
|
quit();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
sim_start();
|
2023-01-25 21:32:58 -06:00
|
|
|
cpSpaceReindexStatic(space);
|
2023-01-10 07:13:00 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
sim_stop();
|
|
|
|
break;
|
|
|
|
|
2023-02-28 17:03:28 -06:00
|
|
|
case 3:
|
2023-01-10 07:13:00 -06:00
|
|
|
sim_pause();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
sim_step();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
duk_push_boolean(duk, sim_playing());
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
duk_push_boolean(duk, sim_paused());
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
duk_push_int(duk, MakeGameobject());
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
duk_push_int(duk, frame_fps());
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 9: /* Clear the level out */
|
|
|
|
new_level();
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
duk_ret_t duk_make_gameobject(duk_context *duk) {
|
|
|
|
int g = MakeGameobject();
|
|
|
|
struct gameobject *go = get_gameobject_from_id(g);
|
|
|
|
|
|
|
|
go->scale = duk_to_number(duk, 0);
|
|
|
|
go->bodytype = duk_to_int(duk, 1);
|
|
|
|
go->mass = duk_to_number(duk, 2);
|
|
|
|
go->f = duk_to_number(duk, 3);
|
|
|
|
go->e = duk_to_number(duk, 4);
|
2023-02-06 16:41:47 -06:00
|
|
|
go->flipx = 1.f;
|
|
|
|
go->flipy = 1.f;
|
2023-01-10 07:13:00 -06:00
|
|
|
|
|
|
|
gameobject_apply(go);
|
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
duk_push_int(duk, g);
|
2023-01-10 07:13:00 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-13 22:08:39 -06:00
|
|
|
duk_ret_t duk_yughlog(duk_context *duk) {
|
|
|
|
int cmd = duk_to_int(duk, 0);
|
|
|
|
const char *s = duk_to_string(duk,1);
|
|
|
|
const char *f = duk_to_string(duk, 2);
|
|
|
|
int line = duk_to_int(duk, 3);
|
2023-01-10 14:02:24 -06:00
|
|
|
|
2023-01-13 22:08:39 -06:00
|
|
|
mYughLog(1, cmd, line, f, s);
|
2023-01-10 14:02:24 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
duk_ret_t duk_set_body(duk_context *duk) {
|
|
|
|
int cmd = duk_to_int(duk, 0);
|
|
|
|
int id = duk_to_int(duk, 1);
|
|
|
|
struct gameobject *go = get_gameobject_from_id(id);
|
2023-02-24 14:13:35 -06:00
|
|
|
if (!go) return 0;
|
2023-01-10 14:02:24 -06:00
|
|
|
|
2023-01-13 13:07:44 -06:00
|
|
|
/* TODO: Possible that reindexing shapes only needs done for static shapes? */
|
2023-01-10 14:02:24 -06:00
|
|
|
switch (cmd) {
|
|
|
|
case 0:
|
|
|
|
gameobject_setangle(go, duk_to_number(duk, 2));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2023-02-24 12:11:36 -06:00
|
|
|
go->bodytype = duk_to_int(duk,2);
|
2023-01-10 14:02:24 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
cpBodySetPosition(go->body, duk2vec2(duk, 2));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
gameobject_move(go, duk2vec2(duk, 2));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
cpBodyApplyImpulseAtWorldPoint(go->body, duk2vec2(duk, 2), cpBodyGetPosition(go->body));
|
2023-02-06 16:41:47 -06:00
|
|
|
return 0;
|
2023-01-10 14:02:24 -06:00
|
|
|
|
|
|
|
case 5:
|
|
|
|
go->flipx = duk_to_boolean(duk, 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
go->flipy = duk_to_boolean(duk, 2);
|
|
|
|
break;
|
|
|
|
|
2023-01-12 17:41:54 -06:00
|
|
|
case 7:
|
|
|
|
cpBodySetMass(go->body, duk_to_number(duk, 2));
|
|
|
|
break;
|
|
|
|
|
2023-01-13 13:07:44 -06:00
|
|
|
case 8:
|
|
|
|
cpBodySetAngularVelocity(go->body, duk_to_number(duk, 2));
|
2023-03-03 13:07:59 -06:00
|
|
|
return 0;
|
2023-01-13 13:07:44 -06:00
|
|
|
|
|
|
|
case 9:
|
|
|
|
cpBodySetVelocity(go->body, duk2vec2(duk, 2));
|
2023-03-03 13:07:59 -06:00
|
|
|
return 0;
|
2023-01-13 13:07:44 -06:00
|
|
|
|
2023-02-24 14:13:35 -06:00
|
|
|
case 10:
|
|
|
|
go->e = fmax(duk_to_number(duk,2), 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 11:
|
|
|
|
go->f = fmax(duk_to_number(duk,2),0);
|
|
|
|
break;
|
2023-03-17 10:25:35 -05:00
|
|
|
|
|
|
|
case 12:
|
|
|
|
cpBodyApplyForceAtWorldPoint(go->body, duk2vec2(duk, 2), cpBodyGetPosition(go->body));
|
|
|
|
return 0;
|
2023-01-10 14:02:24 -06:00
|
|
|
}
|
|
|
|
|
2023-01-25 21:32:58 -06:00
|
|
|
cpSpaceReindexShapesForBody(space, go->body);
|
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
duk_ret_t duk_q_body(duk_context *duk) {
|
|
|
|
int q = duk_to_int(duk, 0);
|
|
|
|
struct gameobject *go = get_gameobject_from_id(duk_to_int(duk, 1));
|
|
|
|
|
2023-01-11 16:57:34 -06:00
|
|
|
if (!go) return 0;
|
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
switch(q) {
|
|
|
|
case 0:
|
|
|
|
duk_push_int(duk, cpBodyGetType(go->body));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
vect2duk(cpBodyGetPosition(go->body));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
duk_push_number(duk, cpBodyGetAngle(go->body));
|
|
|
|
return 1;
|
2023-01-11 16:57:34 -06:00
|
|
|
|
|
|
|
case 3:
|
|
|
|
vect2duk(cpBodyGetVelocity(go->body));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
duk_push_number(duk, cpBodyGetAngularVelocity(go->body));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
duk_push_number(duk, cpBodyGetMass(go->body));
|
|
|
|
return 1;
|
2023-03-10 13:13:48 -06:00
|
|
|
|
|
|
|
case 6:
|
|
|
|
duk_push_number(duk, cpBodyGetMoment(go->body));
|
|
|
|
return 1;
|
2023-03-17 10:25:35 -05:00
|
|
|
|
|
|
|
case 7:
|
|
|
|
duk_push_boolean(duk, phys2d_in_air(go->body));
|
|
|
|
return 1;
|
2023-01-10 14:02:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-12 17:41:54 -06:00
|
|
|
duk_ret_t duk_make_sprite(duk_context *duk) {
|
|
|
|
int go = duk_to_int(duk, 0);
|
|
|
|
const char *path = duk_to_string(duk, 1);
|
|
|
|
cpVect pos = duk2vec2(duk, 2);
|
|
|
|
int sprite = make_sprite(go);
|
|
|
|
struct sprite *sp = id2sprite(sprite);
|
2023-03-05 22:05:22 -06:00
|
|
|
sprite_loadtex(sp, path, ST_UNIT);
|
2023-01-12 17:41:54 -06:00
|
|
|
sp->pos[0] = pos.x;
|
|
|
|
sp->pos[1] = pos.y;
|
|
|
|
|
2023-02-10 14:31:59 -06:00
|
|
|
duk_push_int(duk, sprite);
|
2023-01-12 17:41:54 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:43:07 -06:00
|
|
|
/* Make anim from texture */
|
2023-01-13 13:07:44 -06:00
|
|
|
duk_ret_t duk_make_anim2d(duk_context *duk) {
|
2023-01-18 17:15:36 -06:00
|
|
|
const char *path = duk_to_string(duk, 0);
|
|
|
|
int frames = duk_to_int(duk, 1);
|
|
|
|
int fps = duk_to_int(duk, 2);
|
2023-01-15 09:53:50 -06:00
|
|
|
|
2023-01-18 17:15:36 -06:00
|
|
|
struct TexAnim *anim = anim2d_from_tex(path, frames, fps);
|
2023-01-15 09:53:50 -06:00
|
|
|
|
2023-01-18 17:15:36 -06:00
|
|
|
duk_push_pointer(duk, anim);
|
2023-01-16 02:16:39 -06:00
|
|
|
return 1;
|
2023-01-13 13:07:44 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 17:23:11 -06:00
|
|
|
duk_ret_t duk_make_box2d(duk_context *duk) {
|
|
|
|
int go = duk_to_int(duk, 0);
|
|
|
|
cpVect size = duk2vec2(duk, 1);
|
|
|
|
cpVect offset = duk2vec2(duk, 2);
|
|
|
|
|
2023-01-12 17:41:54 -06:00
|
|
|
struct phys2d_box *box = Make2DBox(go);
|
2023-01-10 17:23:11 -06:00
|
|
|
box->w = size.x;
|
|
|
|
box->h = size.y;
|
|
|
|
box->offset[0] = offset.x;
|
|
|
|
box->offset[1] = offset.y;
|
2023-01-12 17:41:54 -06:00
|
|
|
|
|
|
|
phys2d_applybox(box);
|
2023-01-10 17:23:11 -06:00
|
|
|
|
2023-01-19 18:30:23 -06:00
|
|
|
int idx = duk_push_object(duk);
|
|
|
|
duk_push_pointer(duk, box);
|
2023-02-23 17:03:58 -06:00
|
|
|
duk_put_prop_string(duk, idx, "id");
|
|
|
|
duk_push_pointer(duk, &box->shape);
|
2023-01-19 18:30:23 -06:00
|
|
|
duk_put_prop_string(duk, idx, "shape");
|
2023-01-17 13:04:08 -06:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:44:29 -06:00
|
|
|
duk_ret_t duk_cmd_box2d(duk_context *duk)
|
|
|
|
{
|
|
|
|
int cmd = duk_to_int(duk, 0);
|
|
|
|
struct phys2d_box *box = duk_to_pointer(duk, 1);
|
2023-01-25 21:32:58 -06:00
|
|
|
cpVect arg;
|
2023-01-19 18:30:23 -06:00
|
|
|
|
|
|
|
if (!box) return 0;
|
2023-01-19 10:44:29 -06:00
|
|
|
|
2023-01-19 18:30:23 -06:00
|
|
|
switch(cmd) {
|
|
|
|
case 0:
|
2023-01-25 21:32:58 -06:00
|
|
|
arg = duk2vec2(duk, 2);
|
2023-01-19 18:30:23 -06:00
|
|
|
box->w = arg.x;
|
|
|
|
box->h = arg.y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2023-01-25 21:32:58 -06:00
|
|
|
arg = duk2vec2(duk, 2);
|
2023-01-19 18:30:23 -06:00
|
|
|
box->offset[0] = arg.x;
|
|
|
|
box->offset[1] = arg.y;
|
|
|
|
break;
|
2023-01-25 21:32:58 -06:00
|
|
|
|
|
|
|
case 2:
|
|
|
|
box->rotation = duk_to_number(duk, 2);
|
|
|
|
break;
|
2023-01-19 18:30:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
phys2d_applybox(box);
|
2023-01-19 10:44:29 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-10 17:23:11 -06:00
|
|
|
duk_ret_t duk_make_circle2d(duk_context *duk) {
|
2023-01-11 16:57:34 -06:00
|
|
|
int go = duk_to_int(duk, 0);
|
|
|
|
double radius = duk_to_number(duk, 1);
|
2023-01-10 17:23:11 -06:00
|
|
|
|
2023-01-12 17:41:54 -06:00
|
|
|
struct phys2d_circle *circle = Make2DCircle(go);
|
2023-01-10 17:23:11 -06:00
|
|
|
circle->radius = radius;
|
2023-01-19 18:30:23 -06:00
|
|
|
circle->offset = duk2vec2(duk, 2);
|
2023-01-10 17:23:11 -06:00
|
|
|
|
2023-01-12 17:41:54 -06:00
|
|
|
phys2d_applycircle(circle);
|
2023-01-10 17:23:11 -06:00
|
|
|
|
2023-01-19 16:43:29 -06:00
|
|
|
int idx = duk_push_object(duk);
|
|
|
|
duk_push_pointer(duk, circle);
|
2023-02-23 17:03:58 -06:00
|
|
|
duk_put_prop_string(duk, idx, "id");
|
|
|
|
duk_push_pointer(duk, &circle->shape);
|
2023-01-19 16:43:29 -06:00
|
|
|
duk_put_prop_string(duk, idx, "shape");
|
2023-01-17 15:09:14 -06:00
|
|
|
|
|
|
|
return 1;
|
2023-01-10 17:23:11 -06:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:44:29 -06:00
|
|
|
duk_ret_t duk_cmd_circle2d(duk_context *duk)
|
|
|
|
{
|
2023-01-19 13:06:32 -06:00
|
|
|
int cmd = duk_to_int(duk, 0);
|
|
|
|
struct phys2d_circle *circle = duk_to_pointer(duk, 1);
|
|
|
|
|
2023-01-19 16:43:29 -06:00
|
|
|
if (!circle) return 0;
|
2023-01-19 13:06:32 -06:00
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case 0:
|
|
|
|
circle->radius = duk_to_number(duk, 2);
|
2023-01-19 18:30:23 -06:00
|
|
|
break;
|
2023-01-19 13:06:32 -06:00
|
|
|
|
2023-01-19 18:30:23 -06:00
|
|
|
case 1:
|
|
|
|
circle->offset = duk2vec2(duk, 2);
|
|
|
|
break;
|
2023-01-19 13:06:32 -06:00
|
|
|
}
|
|
|
|
|
2023-01-19 18:30:23 -06:00
|
|
|
phys2d_applycircle(circle);
|
2023-01-19 10:44:29 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-25 21:32:58 -06:00
|
|
|
duk_ret_t duk_make_poly2d(duk_context *duk)
|
|
|
|
{
|
|
|
|
int go = duk_to_int(duk, 0);
|
|
|
|
struct phys2d_poly *poly = Make2DPoly(go);
|
2023-02-17 22:32:23 -06:00
|
|
|
phys2d_poly_setverts(poly, duk2cpvec2arr(duk,1));
|
2023-01-25 21:32:58 -06:00
|
|
|
|
2023-02-17 22:32:23 -06:00
|
|
|
int idx = duk_push_object(duk);
|
|
|
|
duk_push_pointer(duk, poly);
|
|
|
|
duk_put_prop_string(duk, idx, "id");
|
|
|
|
duk_push_pointer(duk, &poly->shape);
|
|
|
|
duk_put_prop_string(duk, idx, "shape");
|
2023-01-25 21:32:58 -06:00
|
|
|
|
2023-02-17 22:32:23 -06:00
|
|
|
return 1;
|
2023-01-25 21:32:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
duk_ret_t duk_cmd_poly2d(duk_context *duk)
|
|
|
|
{
|
2023-02-17 22:32:23 -06:00
|
|
|
int cmd = duk_to_int(duk,0);
|
|
|
|
struct phys2d_poly *poly = duk_to_pointer(duk,1);
|
2023-02-24 14:13:35 -06:00
|
|
|
|
|
|
|
if (!poly) return 0;
|
2023-02-17 22:32:23 -06:00
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case 0:
|
|
|
|
phys2d_poly_setverts(poly, duk2cpvec2arr(duk,2));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
return 0;
|
2023-01-25 21:32:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
duk_ret_t duk_make_edge2d(duk_context *duk)
|
|
|
|
{
|
|
|
|
int go = duk_to_int(duk, 0);
|
|
|
|
struct phys2d_edge *edge = Make2DEdge(go);
|
|
|
|
|
|
|
|
int arridx = 1;
|
|
|
|
|
|
|
|
int n = duk_get_length(duk, arridx);
|
|
|
|
cpVect points[n];
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
duk_get_prop_index(duk, arridx, i);
|
|
|
|
|
|
|
|
points[i] = duk2vec2(duk, -1);
|
|
|
|
|
|
|
|
phys2d_edgeaddvert(edge);
|
|
|
|
phys2d_edge_setvert(edge, i, points[i]);
|
|
|
|
}
|
|
|
|
|
2023-02-23 17:03:58 -06:00
|
|
|
int idx = duk_push_object(duk);
|
2023-01-25 21:32:58 -06:00
|
|
|
duk_push_pointer(duk, edge);
|
2023-02-23 17:03:58 -06:00
|
|
|
duk_put_prop_string(duk, idx, "id");
|
|
|
|
duk_push_pointer(duk, &edge->shape);
|
2023-01-25 21:32:58 -06:00
|
|
|
duk_put_prop_string(duk, idx, "shape");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
duk_ret_t duk_cmd_edge2d(duk_context *duk)
|
|
|
|
{
|
2023-02-08 15:30:12 -06:00
|
|
|
int cmd = duk_to_int(duk, 0);
|
|
|
|
struct phys2d_edge *edge = duk_to_pointer(duk, 1);
|
|
|
|
|
2023-02-24 14:13:35 -06:00
|
|
|
if (!edge) return 0;
|
|
|
|
|
2023-02-08 15:30:12 -06:00
|
|
|
switch(cmd) {
|
|
|
|
case 0:
|
|
|
|
phys2d_edge_clearverts(edge);
|
|
|
|
phys2d_edge_addverts(edge, duk2cpvec2arr(duk, 2));
|
|
|
|
break;
|
2023-02-13 08:30:35 -06:00
|
|
|
|
|
|
|
case 1:
|
|
|
|
edge->thickness = duk_to_number(duk, 2);
|
|
|
|
break;
|
2023-02-08 15:30:12 -06:00
|
|
|
}
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
return 0;
|
2023-01-25 21:32:58 -06:00
|
|
|
}
|
|
|
|
|
2023-03-19 20:33:05 -05:00
|
|
|
duk_ret_t duk_inflate_cpv(duk_context *duk)
|
|
|
|
{
|
|
|
|
cpVect *points = duk2cpvec2arr(duk,0);
|
|
|
|
int n = duk_to_int(duk,1);
|
2023-03-24 14:01:01 -05:00
|
|
|
double d = duk_to_number(duk,2);
|
|
|
|
|
2023-03-19 20:33:05 -05:00
|
|
|
cpVect inflate_out[n];
|
|
|
|
cpVect inflate_in[n];
|
|
|
|
|
|
|
|
inflatepoints(inflate_out, points, d, n);
|
|
|
|
inflatepoints(inflate_in, points, -d, n);
|
|
|
|
|
|
|
|
duk_idx_t arr = duk_push_array(duk);
|
|
|
|
duk_idx_t out = vecarr2duk(duk, inflate_out, n);
|
2023-03-24 14:01:01 -05:00
|
|
|
duk_put_prop_index(duk,arr, 0);
|
2023-03-19 20:33:05 -05:00
|
|
|
duk_idx_t in = vecarr2duk(duk, inflate_in, n);
|
2023-03-24 14:01:01 -05:00
|
|
|
duk_put_prop_index(duk,arr,1);
|
2023-03-19 20:33:05 -05:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-18 17:15:36 -06:00
|
|
|
/* These are anims for controlling properties on an object */
|
2023-01-10 17:23:11 -06:00
|
|
|
duk_ret_t duk_anim(duk_context *duk) {
|
|
|
|
void *prop = duk_get_heapptr(duk, 0);
|
|
|
|
int keyframes = duk_get_length(duk, 1);
|
|
|
|
YughInfo("Processing %d keyframes.", keyframes);
|
|
|
|
|
|
|
|
struct anim a = make_anim();
|
|
|
|
|
|
|
|
for (int i = 0; i < keyframes; i++) {
|
|
|
|
struct keyframe k;
|
|
|
|
duk_get_prop_index(duk, 1, i); /* End of stack is now the keyframe */
|
|
|
|
cpVect v = duk2vec2(duk, duk_get_top_index(duk));
|
|
|
|
k.time = v.y;
|
|
|
|
k.val = v.x;
|
|
|
|
a = anim_add_keyframe(a, k);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (double i = 0; i < 3.0; i = i + 0.1) {
|
|
|
|
YughInfo("Val is now %f at time %f", anim_val(a, i), i);
|
|
|
|
duk_push_heapptr(duk, prop);
|
|
|
|
duk_push_number(duk, anim_val(a, i));
|
|
|
|
duk_call(duk, 1);
|
|
|
|
duk_pop(duk);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-18 17:15:36 -06:00
|
|
|
duk_ret_t duk_make_timer(duk_context *duk) {
|
|
|
|
void *sym = duk_get_heapptr(duk, 0);
|
|
|
|
double secs = duk_to_number(duk, 1);
|
|
|
|
void *obj = duk_get_heapptr(duk, 2);
|
|
|
|
struct callee *c = malloc(sizeof(*c));
|
|
|
|
c->fn = sym;
|
|
|
|
c->obj = obj;
|
2023-03-17 10:25:35 -05:00
|
|
|
int id = timer_make(secs, call_callee, c, 1);
|
2023-01-12 17:41:54 -06:00
|
|
|
|
2023-03-17 10:25:35 -05:00
|
|
|
duk_push_int(duk, id);
|
2023-01-18 17:15:36 -06:00
|
|
|
return 1;
|
2023-01-10 17:23:11 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
#define DUK_FUNC(NAME, ARGS) duk_push_c_function(duk, duk_##NAME, ARGS); duk_put_global_string(duk, #NAME);
|
|
|
|
|
2023-01-10 07:13:00 -06:00
|
|
|
void ffi_load()
|
|
|
|
{
|
2023-01-13 22:08:39 -06:00
|
|
|
DUK_FUNC(yughlog, 4);
|
2023-01-19 13:06:32 -06:00
|
|
|
DUK_FUNC(nuke, DUK_VARARGS);
|
2023-01-10 14:02:24 -06:00
|
|
|
DUK_FUNC(make_gameobject, 7);
|
|
|
|
DUK_FUNC(set_body, 3);
|
|
|
|
DUK_FUNC(q_body, 2);
|
2023-01-12 17:41:54 -06:00
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
DUK_FUNC(sys_cmd, 1);
|
|
|
|
DUK_FUNC(win_make, 3);
|
2023-01-10 17:23:11 -06:00
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
DUK_FUNC(make_sprite, 3);
|
2023-01-18 17:15:36 -06:00
|
|
|
DUK_FUNC(make_anim2d, 3);
|
2023-01-27 12:06:16 -06:00
|
|
|
DUK_FUNC(spline_cmd, 6);
|
2023-02-02 17:52:15 -06:00
|
|
|
|
2023-01-10 17:23:11 -06:00
|
|
|
DUK_FUNC(make_box2d, 3);
|
2023-01-19 10:44:29 -06:00
|
|
|
DUK_FUNC(cmd_box2d, DUK_VARARGS);
|
2023-01-10 17:23:11 -06:00
|
|
|
DUK_FUNC(make_circle2d, 3);
|
2023-01-19 10:44:29 -06:00
|
|
|
DUK_FUNC(cmd_circle2d, DUK_VARARGS);
|
2023-01-25 21:32:58 -06:00
|
|
|
DUK_FUNC(make_poly2d, 2);
|
|
|
|
DUK_FUNC(cmd_poly2d, DUK_VARARGS);
|
|
|
|
DUK_FUNC(make_edge2d, 3);
|
|
|
|
DUK_FUNC(cmd_edge2d, DUK_VARARGS);
|
2023-01-18 17:15:36 -06:00
|
|
|
DUK_FUNC(make_timer, 3);
|
|
|
|
|
2023-01-13 22:08:39 -06:00
|
|
|
DUK_FUNC(cmd, DUK_VARARGS);
|
2023-01-11 16:57:34 -06:00
|
|
|
DUK_FUNC(register, 3);
|
2023-02-17 01:16:52 -06:00
|
|
|
DUK_FUNC(register_collide, DUK_VARARGS);
|
2023-01-10 17:23:11 -06:00
|
|
|
|
2023-03-03 13:07:59 -06:00
|
|
|
DUK_FUNC(gui_text, DUK_VARARGS);
|
2023-04-07 12:52:35 -05:00
|
|
|
DUK_FUNC(ui_text, 5);
|
2023-03-10 13:13:48 -06:00
|
|
|
DUK_FUNC(cursor_text,5);
|
2023-01-10 17:23:11 -06:00
|
|
|
DUK_FUNC(gui_img, 2);
|
2023-03-19 20:33:05 -05:00
|
|
|
|
2023-03-24 14:01:01 -05:00
|
|
|
DUK_FUNC(inflate_cpv, 3);
|
2023-01-10 17:23:11 -06:00
|
|
|
|
|
|
|
DUK_FUNC(anim, 2);
|
2023-01-10 15:41:43 -06:00
|
|
|
}
|