Command line overhaul
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#ifndef CIRCBUF_H
|
||||
#define CIRCBUF_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static inline unsigned int powof2(unsigned int x)
|
||||
|
||||
@@ -26,6 +26,9 @@
|
||||
#include "resources.h"
|
||||
#include <sokol/sokol_time.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "nota.h"
|
||||
|
||||
#include "render.h"
|
||||
@@ -1081,9 +1084,6 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
|
||||
ret = JS_NewInt64(js, file_mod_secs(str));
|
||||
break;
|
||||
|
||||
case 120:
|
||||
ret = str2js(engine_info());
|
||||
break;
|
||||
case 121:
|
||||
ret = number2js(get_timescale());
|
||||
break;
|
||||
@@ -1194,7 +1194,6 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
|
||||
break;
|
||||
|
||||
case 147:
|
||||
YughWarn("EXITING");
|
||||
exit(js2int(argv[1]));
|
||||
break;
|
||||
case 148:
|
||||
@@ -1435,6 +1434,34 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
|
||||
case 254:
|
||||
ret = warp_damp2js(warp_damp_make());
|
||||
break;
|
||||
|
||||
case 255:
|
||||
ret = str2js(VER);
|
||||
break;
|
||||
|
||||
case 256:
|
||||
ret = str2js(COM);
|
||||
break;
|
||||
case 257:
|
||||
engine_start(argv[1]);
|
||||
break;
|
||||
case 258:
|
||||
str = js2str(argv[1]);
|
||||
ret = int2js(mkdir(str, 0777));
|
||||
break;
|
||||
case 259:
|
||||
script_gc();
|
||||
break;
|
||||
case 260:
|
||||
str = js2str(argv[1]);
|
||||
d1 = script_compile(str, &plen);
|
||||
ret = JS_NewArrayBufferCopy(js, d1, plen);
|
||||
break;
|
||||
case 261:
|
||||
str = js2str(argv[1]);
|
||||
d1 = slurp_file(str, &plen);
|
||||
return script_run_bytecode(d1, plen);
|
||||
break;
|
||||
}
|
||||
|
||||
if (str) JS_FreeCString(js, str);
|
||||
@@ -1816,7 +1843,6 @@ GETSET_PAIR(warp_gravity, planar_force, vec3)
|
||||
|
||||
#define MIST_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
|
||||
|
||||
|
||||
#define CGETSET_ADD(ID, ENTRY) MIST_CGETSET_DEF(#ENTRY, ID##_get_##ENTRY, ID##_set_##ENTRY)
|
||||
|
||||
static const JSCFunctionListEntry js_warp_gravity_funcs [] = {
|
||||
@@ -1873,6 +1899,10 @@ JSValue js_emitter_emit(JSContext *js, JSValueConst this, int argc, JSValue *arg
|
||||
return JS_UNDEFINED;
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_global_funcs[] = {
|
||||
|
||||
};
|
||||
|
||||
static const JSCFunctionListEntry js_emitter_funcs[] = {
|
||||
CGETSET_ADD(emitter, life),
|
||||
CGETSET_ADD(emitter, life_var),
|
||||
|
||||
@@ -26,7 +26,7 @@ JSRuntime *rt = NULL;
|
||||
#ifndef NDEBUG
|
||||
#define JS_EVAL_FLAGS JS_EVAL_FLAG_STRICT
|
||||
#else
|
||||
#define JS_EVAL_FLAGS JS_EVAL_FLAG_STRICT// | JS_EVAL_FLAG_STRIP
|
||||
#define JS_EVAL_FLAGS JS_EVAL_FLAG_STRICT | JS_EVAL_FLAG_STRIP
|
||||
#endif
|
||||
|
||||
static struct {
|
||||
@@ -76,8 +76,11 @@ void script_stop()
|
||||
ffi_stop();
|
||||
for (int i = 0; i < shlen(jsstrs); i++)
|
||||
JS_FreeValue(js,jsstrs[i].value);
|
||||
|
||||
#if LEAK
|
||||
JS_FreeContext(js);
|
||||
JS_FreeRuntime(rt);
|
||||
#endif
|
||||
}
|
||||
|
||||
void script_gc()
|
||||
@@ -134,16 +137,25 @@ void script_evalf(const char *format, ...)
|
||||
JS_FreeValue(js,obj);
|
||||
}
|
||||
|
||||
uint8_t *compile_script(const char *file, size_t *len) {
|
||||
char *script = slurp_text(file, len);
|
||||
JSValue obj = JS_Eval(js, script, *len, file, JS_EVAL_FLAG_COMPILE_ONLY | JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAGS);
|
||||
uint8_t *script_compile(const char *file, size_t *len) {
|
||||
size_t file_len;
|
||||
char *script = slurp_text(file, &file_len);
|
||||
JSValue obj = JS_Eval(js, script, file_len, file, JS_EVAL_FLAG_COMPILE_ONLY | JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAGS);
|
||||
free(script);
|
||||
size_t out_len;
|
||||
uint8_t *out = JS_WriteObject(js, &out_len, obj, JS_WRITE_OBJ_BYTECODE);
|
||||
uint8_t *out = JS_WriteObject(js, len, obj, JS_WRITE_OBJ_BYTECODE);
|
||||
JS_FreeValue(js,obj);
|
||||
return out;
|
||||
}
|
||||
|
||||
JSValue script_run_bytecode(uint8_t *code, size_t len)
|
||||
{
|
||||
JSValue b = JS_ReadObject(js, code, len, JS_READ_OBJ_BYTECODE);
|
||||
JSValue ret = JS_EvalFunction(js, b);
|
||||
js_print_exception(ret);
|
||||
JS_FreeValue(js,b);
|
||||
JS_FreeValue(js,ret);
|
||||
}
|
||||
|
||||
struct callee stacktrace_callee;
|
||||
|
||||
time_t file_mod_secs(const char *file) {
|
||||
@@ -265,7 +277,7 @@ void script_call_fn_arg(JSValue fn, JSValue arg)
|
||||
|
||||
void out_memusage(const char *file)
|
||||
{
|
||||
FILE *f = fopen(file, "w");
|
||||
FILE *f = fopen_mkdir(file, "w");
|
||||
JSMemoryUsage jsmem;
|
||||
JS_ComputeMemoryUsage(rt, &jsmem);
|
||||
JS_DumpMemoryUsage(f, &jsmem, rt);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#define SCRIPT_H
|
||||
|
||||
#include "quickjs/quickjs.h"
|
||||
#include <chipmunk/chipmunk.h>
|
||||
#include <time.h>
|
||||
|
||||
extern JSContext *js;
|
||||
@@ -67,12 +66,15 @@ void call_nk_gui();
|
||||
void unregister_obj(JSValue obj);
|
||||
|
||||
void send_signal(const char *signal, int argc, JSValue *argv);
|
||||
void script_gc();
|
||||
|
||||
void register_physics(struct callee c);
|
||||
void call_physics(double dt);
|
||||
|
||||
void register_draw(struct callee c);
|
||||
void call_draw();
|
||||
uint8_t *compile_script(const char *file, size_t *len);
|
||||
|
||||
JSValue script_run_bytecode(uint8_t *code, size_t len);
|
||||
uint8_t *script_compile(const char *file, size_t *len);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -21,7 +21,15 @@ warp_damp *warp_damp_make()
|
||||
}
|
||||
|
||||
void warp_damp_free(warp_damp *d) { free(d); }
|
||||
void warp_gravity_free(warp_gravity *n) { free(n); }
|
||||
void warp_gravity_free(warp_gravity *n) {
|
||||
for (int i = 0; i < arrlen(warps); i++) {
|
||||
if (warps[i] == n) {
|
||||
arrdelswap(warps, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(n);
|
||||
}
|
||||
|
||||
HMM_Vec3 warp_damp_force(warp_damp *d, HMM_Vec3 pos, HMM_Vec3 vel)
|
||||
{
|
||||
|
||||
@@ -81,10 +81,6 @@ static int sim_play = SIM_PLAY;
|
||||
|
||||
int editor_mode = 0;
|
||||
|
||||
#define ENGINEINFO "Yugine version " VER ", " INFO " build.\nCopyright 2022-2024."
|
||||
|
||||
const char *engine_info() { return ENGINEINFO; }
|
||||
|
||||
static int argc;
|
||||
static char **args;
|
||||
|
||||
@@ -94,17 +90,18 @@ void seghandle()
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void c_init() {
|
||||
static JSValue c_init_fn;
|
||||
|
||||
void c_init() {
|
||||
input_init();
|
||||
script_evalf("world_start();");
|
||||
|
||||
render_init();
|
||||
window_set_icon("icons/moon.gif");
|
||||
window_resize(sapp_width(), sapp_height());
|
||||
script_evalf("Game.init();");
|
||||
|
||||
particle_init();
|
||||
|
||||
if (!JS_IsUndefined(c_init_fn))
|
||||
script_call_sym(c_init_fn);
|
||||
}
|
||||
|
||||
int frame_fps() { return 1.0/sapp_frame_duration(); }
|
||||
@@ -158,7 +155,7 @@ void c_frame()
|
||||
|
||||
void c_clean() {
|
||||
gif_rec_end("out.gif");
|
||||
out_memusage("jsmem.txt");
|
||||
out_memusage(".prosperon/jsmem.txt");
|
||||
script_stop();
|
||||
saudio_shutdown();
|
||||
sg_shutdown();
|
||||
@@ -250,7 +247,7 @@ static sapp_desc start_desc = {
|
||||
.high_dpi = 0,
|
||||
.sample_count = 1,
|
||||
.fullscreen = 1,
|
||||
.window_title = "Primum Machinam",
|
||||
.window_title = "Prosperon",
|
||||
.enable_clipboard = false,
|
||||
.clipboard_size = 0,
|
||||
.enable_dragndrop = true,
|
||||
@@ -303,14 +300,10 @@ sprintf(da.details, "COMPetitive");
|
||||
dam->update_activity(dam, &da, NULL, NULL);
|
||||
#endif
|
||||
|
||||
stm_setup(); /* time */
|
||||
start_t = frame_t = stm_now();
|
||||
physlast = updatelast = start_t;
|
||||
sound_init();
|
||||
resources_init();
|
||||
phys2d_init();
|
||||
script_startup();
|
||||
|
||||
script_startup();
|
||||
|
||||
int argsize = 0;
|
||||
for (int i = 0; i < argc; i++) {
|
||||
argsize += strlen(argv[i]);
|
||||
@@ -324,15 +317,27 @@ dam->update_activity(dam, &da, NULL, NULL);
|
||||
strcat(cmdstr, argv[i]);
|
||||
if (argc > i+1) strcat(cmdstr, " ");
|
||||
}
|
||||
|
||||
script_evalf("cmd_args('%s');", cmdstr);
|
||||
|
||||
out_memusage(".prosperon/jsmem.txt");
|
||||
script_stop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void engine_start(JSValue fn)
|
||||
{
|
||||
c_init_fn = fn;
|
||||
stm_setup(); /* time */
|
||||
start_t = frame_t = stm_now();
|
||||
physlast = updatelast = start_t;
|
||||
sound_init();
|
||||
phys2d_init();
|
||||
|
||||
start_desc.width = mainwin.width;
|
||||
start_desc.height = mainwin.height;
|
||||
start_desc.fullscreen = 0;
|
||||
sapp_run(&start_desc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
double apptime() { return stm_sec(stm_diff(stm_now(), start_t)); }
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef YUGINE_H
|
||||
#define YUGINE_H
|
||||
|
||||
#include "script.h"
|
||||
|
||||
int sim_playing();
|
||||
int sim_paused();
|
||||
void sim_start();
|
||||
@@ -10,9 +12,8 @@ void sim_step();
|
||||
int phys_stepping();
|
||||
void set_timescale(float val);
|
||||
void print_stacktrace();
|
||||
void yugine_init();
|
||||
void engine_start(JSValue fn); /* fn runs after the engine starts */
|
||||
|
||||
const char *engine_info();
|
||||
void app_name(const char *name);
|
||||
|
||||
int frame_fps();
|
||||
|
||||
Reference in New Issue
Block a user