This commit is contained in:
John Alanbrook 2023-01-10 13:13:00 +00:00
parent 248456fd9e
commit 8e8af65125
10 changed files with 106718 additions and 97273 deletions

3779
source/engine/duk_config.h Normal file

File diff suppressed because it is too large Load diff

101351
source/engine/duktape.c Normal file

File diff suppressed because it is too large Load diff

1456
source/engine/duktape.h Normal file

File diff suppressed because it is too large Load diff

110
source/engine/ffi.c Normal file
View file

@ -0,0 +1,110 @@
#include "ffi.h"
#include "script.h"
cpVect duk2vec2(duk_context *duk, int p) {
cpVect pos;
duk_get_prop_index(duk, p, 0);
duk_get_prop_index(duk, p, 1);
pos.x = duk_to_number(duk, -2);
pos.y = duk_to_number(duk, -1);
return pos;
}
duk_ret_t duk_gui_text(duk_context *duk) {
const char *s = duk_to_string(duk, 0);
cpVect pos = duk2vec2(duk, 1);
duk_get_prop_index(duk, 1, 0);
float fpos[2] = {pos.x, pos.y};
float size = duk_to_number(duk, 2);
renderText(s, fpos, size, white, 1800);
return 0;
}
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;
}
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();
break;
case 2:
sim_stop();
break;
case 3:
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_
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);
go->flipx = duk_to_boolean(5);
go->flipy = duk_to_boolean(6);
gameobject_apply(go);
duk_push_int(g);
return 1;
}
void ffi_load()
{
}

8
source/engine/ffi.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef FFI_H
#define FFI_H
#include "duktape.h"
void ffi_load();
#endif

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -9,7 +9,7 @@
#include "stb_ds.h" #include "stb_ds.h"
s7_scheme *s7 = NULL; duk_context *duk = NULL;
s7_pointer cpvec2s7(cpVect v) { s7_pointer cpvec2s7(cpVect v) {
s7_pointer ret = s7_make_vector(s7, 2); s7_pointer ret = s7_make_vector(s7, 2);
@ -65,9 +65,11 @@ static int load_prefab(const char *fpath, const struct stat *sb, int typeflag) {
} }
void script_init() { void script_init() {
s7 = s7_init(); duk = duk_create_heap_default();
/*
s7_set_current_error_port(s7, s7_open_output_function(s7, my_err)); s7_set_current_error_port(s7, s7_open_output_function(s7, my_err));
s7_set_current_output_port(s7, s7_open_output_function(s7, my_print)); s7_set_current_output_port(s7, s7_open_output_function(s7, my_print));
*/
ffi_load(); ffi_load();
/* Load all prefabs into memory */ /* Load all prefabs into memory */

View file

@ -1,10 +1,10 @@
#ifndef SCRIPT_H #ifndef SCRIPT_H
#define SCRIPT_H #define SCRIPT_H
#include "s7.h" #include "duktape.h"
#include <chipmunk/chipmunk.h> #include <chipmunk/chipmunk.h>
extern s7_scheme *s7; extern duk_context *duk;
void script_init(); void script_init();
void script_run(const char *script); void script_run(const char *script);

View file

@ -12,7 +12,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "duktape.h"
#include "2dphysics.h" #include "2dphysics.h"
@ -119,6 +119,13 @@ int main(int argc, char **args) {
} }
} }
duk_context *duk = duk_create_heap_default();
duk_eval_string(duk, "1+2");
printf("DUK RESULT: %d", duk_get_int(duk, -1));
#if DBG #if DBG
if (logout) { if (logout) {
time_t now = time(NULL); time_t now = time(NULL);