Update what's in global scope

This commit is contained in:
2024-02-25 23:31:48 +00:00
parent 9c8fe27ce4
commit 4351b4bf20
27 changed files with 634 additions and 777 deletions

View File

@@ -421,3 +421,12 @@ void free_drawmodel(struct drawmodel *dm) {
free(dm);
}
void material_free(material *mat)
{
}
void mesh_free(mesh *m)
{
}

View File

@@ -189,8 +189,7 @@ void input_dropped_files(int n)
argv[0] = jstr("emacs");
argv[1] = jstr("drop");
argv[2] = jstr("pressed");
char *path = rebase_path(sapp_get_dropped_file_path(0));
argv[3] = str2js(path+1);
argv[3] = str2js(sapp_get_dropped_file_path(0));
script_callee(pawn_callee, 4, argv);
JS_FreeValue(js,argv[3]);
}
@@ -323,6 +322,15 @@ int key_is_num(int key) {
void cursor_hide() { sapp_show_mouse(0); }
void cursor_show() { sapp_show_mouse(1); }
void cursor_img(const char *path)
{
/* NSString *str = [NSString stringWithUTF8String:path];
NSImage *img = [[NSImage alloc] initWithContentsOfFile:str];
NSCursor *custom = [[NSCursor alloc] initWithImage:img hotSpot:NSMakePoint(0,0)];
[custom set];
*/
}
int action_down(int key) { return key_states[key] == INPUT_DOWN; }
int action_up(int key) { return key_states[key] == INPUT_UP; }

View File

@@ -18,6 +18,7 @@ void input_poll(double wait);
void cursor_hide();
void cursor_show();
void cursor_img(const char *path);
void set_mouse_mode(int mousemode);
void input_mouse(int btn, int state, uint32_t mod);

View File

@@ -25,9 +25,17 @@
#include <assert.h>
#include "resources.h"
#include <sokol/sokol_time.h>
#include <sokol/sokol_app.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <limits.h>
#if (defined(_WIN32) || defined(__WIN32__))
#include <direct.h>
#define mkdir(x,y) _mkdir(x)
#endif
#include "nota.h"
@@ -90,6 +98,11 @@ static JSValue constraint2js(constraint *c)
static JSValue sound_proto;
sound *js2sound(JSValue v) { return js2dsp_node(v)->data; }
#define QJSGLOBALCLASS(NAME) \
JSValue NAME = JS_NewObject(js); \
JS_SetPropertyFunctionList(js, NAME, js_##NAME##_funcs, countof(js_##NAME##_funcs)); \
JS_SetPropertyStr(js, globalThis, #NAME, NAME); \
#define QJSCLASSPREP(TYPE) \
JS_NewClassID(&js_##TYPE##_id);\
JS_NewClass(JS_GetRuntime(js), js_##TYPE##_id, &js_##TYPE##_class);\
@@ -731,6 +744,15 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
gameobject_draw_debug(js2gameobject(argv[1]));
break;
case 16:
str = js2str(argv[1]);
file_eval_env(str,argv[2]);
break;
case 17:
sapp_set_mouse_cursor(js2int(argv[1]));
break;
case 18:
shape_set_sensor(js2ptr(argv[1]), JS_ToBool(js, argv[2]));
break;
@@ -1026,6 +1048,11 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
case 96:
id2sprite(js2int(argv[1]))->color = js2color(argv[2]);
break;
case 97:
str = js2str(argv[1]);
cursor_img(str);
break;
case 103:
ret = vec2js(js2gameobject(argv[1])->scale.XY);
break;
@@ -1178,11 +1205,9 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
case 143:
str = JS_ToCString(js, argv[1]);
system(str);
break;
case 144:
ret = str2js(DATA_PATH);
if (!getenv(str)) ret = JS_UNDEFINED;
else
ret = str2js(getenv(str));
break;
case 145:
@@ -1899,8 +1924,25 @@ JSValue js_emitter_emit(JSContext *js, JSValueConst this, int argc, JSValue *arg
return JS_UNDEFINED;
}
static const JSCFunctionListEntry js_global_funcs[] = {
JSValue js_os_cwd(JSContext *js, JSValueConst this)
{
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
return str2js(cwd);
}
JSValue js_os_env(JSContext *js, JSValueConst this, int argc, JSValue *argv)
{
char *str = js2str(argv[0]);
JSValue ret = JS_UNDEFINED;
if (getenv(str)) ret = str2js(getenv(str));
JS_FreeCString(js,str);
return ret;
}
static const JSCFunctionListEntry js_os_funcs[] = {
MIST_CFUNC_DEF("cwd", 0, js_os_cwd),
MIST_CFUNC_DEF("env", 1, js_os_env),
};
static const JSCFunctionListEntry js_emitter_funcs[] = {
@@ -2045,6 +2087,11 @@ JSValue duk_cmd_points(JSContext *js, JSValueConst this, int argc, JSValueConst
const char *STRTEST = "TEST STRING";
JSValue duk_profile_js2num(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
{
}
JSValue duk_profile(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
{
int cmd = js2int(argv[0]);
@@ -2153,6 +2200,8 @@ void ffi_load() {
QJSCLASSPREP_FUNCS(warp_damp);
QJSCLASSPREP_FUNCS(constraint);
QJSGLOBALCLASS(os);
}
void ffi_stop()

View File

@@ -25,9 +25,6 @@
#include "core.cdb.h"
char *DATA_PATH = NULL; /* The top level asset path, where the executable resides */
char *PREF_PATH = NULL; /* Path to where the program can write data to, usually for save files etc. */
char **prefabs;
static const char *cur_ext = NULL;
@@ -35,18 +32,10 @@ struct dirent *c_dirent = NULL;
char pathbuf[MAXPATH + 1];
const char *DB_NAME = "test.db";
static struct cdb corecdb;
static struct cdb game_cdb;
void resources_init() {
DATA_PATH = malloc(MAXPATH);
getcwd(DATA_PATH, MAXPATH);
if (!PREF_PATH)
PREF_PATH = strdup("./tmp/");
int fd = open("test.cdb", O_RDONLY);
cdb_init(&game_cdb, fd);
cdb_initf(&corecdb, core_cdb, core_cdb_len);
@@ -80,23 +69,6 @@ char *dirname(const char *path)
return dir;
}
char *rebase_path(const char *path)
{
int off = 0;
while (path[off] == DATA_PATH[off]) {
off++;
if (!path[off] || !DATA_PATH[off]) break;
}
return path+off;
}
FILE *res_open(char *path, const char *tag) {
strncpy(pathbuf, DATA_PATH, MAXPATH);
strncat(pathbuf, path, MAXPATH);
FILE *f = fopen(pathbuf, tag);
return f;
}
char *seprint(char *fmt, ...)
{
va_list args;
@@ -115,27 +87,6 @@ char *seprint(char *fmt, ...)
static char *ext_paths = NULL;
#ifndef __EMSCRIPTEN__
static int ext_check(const char *path, const struct stat *sb, int typeflag) {
if (typeflag == FTW_F) {
const char *ext = strrchr(path, '.');
if (ext != NULL && !strcmp(ext, cur_ext)) {
char newstr[255];
strncpy(newstr, path, 255);
arrput(prefabs, newstr);
}
}
return 0;
}
void fill_extensions(char *paths, const char *path, const char *ext) {
cur_ext = ext;
arrfree(paths);
ext_paths = paths;
ftw(".", ext_check, 10);
}
static char **ls_paths = NULL;
static int ls_ftw(const char *path, const struct stat *sb, int typeflag)

View File

@@ -8,7 +8,6 @@
extern char *DATA_PATH;
void resources_init();
void fill_extensions(char *paths, const char *path, const char *ext);
char *get_filename_from_path(char *path, int extension);
char *get_directory_from_path(char *path);
char *str_replace_ext(const char *s, const char *newext);
@@ -16,7 +15,6 @@ FILE *res_open(char *path, const char *tag);
FILE *path_open(const char *tag, const char *fmt, ...);
char **ls(const char *path);
int cp(const char *p1, const char *p2);
char *rebase_path(const char *path); /* given a global path, rebase to the local structure */
int fexists(const char *path);
FILE *fopen_mkdir(const char *path, const char *mode);

View File

@@ -277,7 +277,8 @@ void script_call_fn_arg(JSValue fn, JSValue arg)
void out_memusage(const char *file)
{
FILE *f = fopen_mkdir(file, "w");
FILE *f = fopen(file, "w");
if (!f) return;
JSMemoryUsage jsmem;
JS_ComputeMemoryUsage(rt, &jsmem);
JS_DumpMemoryUsage(f, &jsmem, rt);

View File

@@ -3770,6 +3770,7 @@ _SOKOL_PRIVATE void _sapp_macos_update_cursor(sapp_mouse_cursor cursor, bool sho
[_sapp.macos.cursors[cursor] set];
}
else {
printf("CURSOR\n");
[[NSCursor arrowCursor] set];
}
}

View File

@@ -260,7 +260,7 @@ static sapp_desc start_desc = {
.logger.func = sg_logging,
};
void app_name(const char *name) { start_desc.window_title = strdup(name); }
void app_name(const char *name) { sapp_set_window_title(name); }
int main(int argc, char **argv) {
#ifndef NDEBUG