Logging and much better debugging info

This commit is contained in:
John Alanbrook 2023-01-14 04:08:39 +00:00
parent d6797a7f24
commit b8f67a8f9e
6 changed files with 60 additions and 28 deletions

View file

@ -64,5 +64,5 @@ void engine_init()
phys2d_init(); phys2d_init();
YughInfo("Starting sound ..."); YughInfo("Starting sound ...");
// sound_init(); sound_init();
} }

View file

@ -110,7 +110,12 @@ duk_ret_t duk_cmd(duk_context *duk) {
break; break;
case 12: case 12:
anim2d_delete(duk_to_int(duk, 1)); //anim2d_delete(duk_to_int(duk, 1));
break;
case 13:
play_song(duk_to_string(duk, 1), duk_to_string(duk, 2));
break; break;
} }
@ -149,7 +154,6 @@ duk_ret_t duk_register_collide(duk_context *duk) {
int go = duk_get_int(duk, 3); int go = duk_get_int(duk, 3);
struct callee c = {fn, obj}; struct callee c = {fn, obj};
YughInfo("Registering ...");
phys2d_add_handler_type(0, go, c); phys2d_add_handler_type(0, go, c);
@ -223,10 +227,13 @@ duk_ret_t duk_make_gameobject(duk_context *duk) {
return 1; return 1;
} }
duk_ret_t duk_loginfo(duk_context *duk) { duk_ret_t duk_yughlog(duk_context *duk) {
const char *s = duk_to_string(duk,0); 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);
YughInfo("%s", s); mYughLog(1, cmd, line, f, s);
return 0; return 0;
} }
@ -342,7 +349,7 @@ duk_ret_t duk_make_anim2d(duk_context *duk) {
const char *path = duk_to_string(duk, 1); const char *path = duk_to_string(duk, 1);
cpVect pos = duk2vec2(duk, 2); cpVect pos = duk2vec2(duk, 2);
return 0;
} }
duk_ret_t duk_make_box2d(duk_context *duk) { duk_ret_t duk_make_box2d(duk_context *duk) {
@ -421,13 +428,11 @@ duk_ret_t duk_timer_cmd(duk_context *duk) {
void ffi_load() void ffi_load()
{ {
DUK_FUNC(loginfo, 1); DUK_FUNC(yughlog, 4);
DUK_FUNC(make_gameobject, 7); DUK_FUNC(make_gameobject, 7);
DUK_FUNC(set_body, 3); DUK_FUNC(set_body, 3);
DUK_FUNC(q_body, 2); DUK_FUNC(q_body, 2);
DUK_FUNC(sprite, 3);
DUK_FUNC(sys_cmd, 1); DUK_FUNC(sys_cmd, 1);
DUK_FUNC(win_make, 3); DUK_FUNC(win_make, 3);
@ -435,7 +440,7 @@ void ffi_load()
DUK_FUNC(make_anim2d, 3); DUK_FUNC(make_anim2d, 3);
DUK_FUNC(make_box2d, 3); DUK_FUNC(make_box2d, 3);
DUK_FUNC(make_circle2d, 3); DUK_FUNC(make_circle2d, 3);
DUK_FUNC(cmd, 2); DUK_FUNC(cmd, DUK_VARARGS);
DUK_FUNC(register, 3); DUK_FUNC(register, 3);
DUK_FUNC(register_collide, 4); DUK_FUNC(register_collide, 4);

View file

@ -57,6 +57,15 @@ time_t file_mod_secs(const char *file) {
return attr.st_mtime; return attr.st_mtime;
} }
void duk_run_err() {
duk_get_prop_string(duk, -1, "lineNumber");
duk_get_prop_string(duk, -2, "fileName");
duk_get_prop_string(duk, -3, "stack");
mYughLog(1, 2, duk_get_int(duk, -3), duk_safe_to_string(duk, -2), "%s\n%s", duk_safe_to_string(duk, -4), duk_safe_to_string(duk, -1));
duk_pop_3(duk);
}
int script_dofile(const char *file) { int script_dofile(const char *file) {
const char *script = slurp_text(file); const char *script = slurp_text(file);
if (!script) { if (!script) {
@ -65,14 +74,18 @@ int script_dofile(const char *file) {
} }
duk_push_string(duk, script); duk_push_string(duk, script);
free(script); free(script);
if (duk_peval(duk) != 0) { duk_push_string(duk, file);
printf("ERROR: %s\n", duk_safe_to_string(duk, -1));
if (duk_pcompile(duk, 0) != 0) {
duk_run_err();
return 1; return 1;
} }
if (duk_pcall(duk, 0))
duk_run_err();
duk_pop(duk); duk_pop(duk);
return file_mod_secs(file); return file_mod_secs(file);
} }
@ -105,15 +118,18 @@ void script_eval_w_env(const char *s, void *env) {
return; return;
} }
duk_push_string(duk, s); duk_push_string(duk, s);
duk_call_prop(duk, -2, 0); if (duk_pcall_prop(duk, -2, 0))
duk_pop(duk); duk_run_err();
duk_pop(duk);
duk_pop_2(duk);
} }
void script_call_sym(void *sym) void script_call_sym(void *sym)
{ {
duk_push_heapptr(duk, sym); duk_push_heapptr(duk, sym);
duk_call(duk, 0); if (duk_pcall(duk, 0))
duk_run_err();
duk_pop(duk); duk_pop(duk);
} }
@ -143,7 +159,10 @@ void register_physics(struct callee c) {
void call_callee(struct callee c) { void call_callee(struct callee c) {
duk_push_heapptr(duk, c.fn); duk_push_heapptr(duk, c.fn);
duk_push_heapptr(duk, c.obj); duk_push_heapptr(duk, c.obj);
duk_call_method(duk, 0);
if (duk_pcall_method(duk, 0))
duk_run_err();
duk_pop(duk); duk_pop(duk);
} }
@ -151,7 +170,10 @@ void callee_dbl(struct callee c, double d) {
duk_push_heapptr(duk, c.fn); duk_push_heapptr(duk, c.fn);
duk_push_heapptr(duk, c.obj); duk_push_heapptr(duk, c.obj);
duk_push_number(duk, d); duk_push_number(duk, d);
duk_call_method(duk, 1);
if (duk_pcall_method(duk, 1))
duk_run_err();
duk_pop(duk); duk_pop(duk);
} }

View file

@ -73,13 +73,8 @@ void sound_pause(struct sound *s);
void sound_resume(struct sound *s); void sound_resume(struct sound *s);
void sound_stop(struct sound *s); void sound_stop(struct sound *s);
struct music make_music(const char *ogg); struct music make_music(const char *ogg);
const char *get_audio_driver(); const char *get_audio_driver();
void soundstream_fillbuf(struct soundstream *stream, short *buf, int n); void soundstream_fillbuf(struct soundstream *stream, short *buf, int n);

View file

@ -7,6 +7,7 @@
#include "log.h" #include "log.h"
#include <math.h> #include <math.h>
#include "util.h" #include "util.h"
#include "parson.h"
static struct { static struct {
char *key; char *key;
@ -22,7 +23,14 @@ struct Texture *texture_pullfromfile(const char *path)
return texhash[index].value; return texhash[index].value;
struct Texture *tex = calloc(1, sizeof(*tex)); struct Texture *tex = calloc(1, sizeof(*tex));
tex->opts.sprite = 1;
/* Find texture's asset; otherwise load default asset */
JSON_Value *rv = json_parse_file("texture.asset");
JSON_Object *ro = json_value_get_object(rv);
tex->opts.sprite = json_object_get_boolean(ro, "sprite");
tex->opts.mips = json_object_get_boolean(ro, "mips");
json_value_free(rv);
tex->opts.gamma = 0; tex->opts.gamma = 0;
tex->anim.ms = 1; tex->anim.ms = 1;
@ -61,6 +69,7 @@ struct Texture *texture_pullfromfile(const char *path)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
if (tex->opts.mips)
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
if (tex->opts.sprite) { if (tex->opts.sprite) {

View file

@ -48,6 +48,7 @@ struct TexAnim {
struct TextureOptions { struct TextureOptions {
int sprite; int sprite;
int mips;
unsigned int gamma:1; unsigned int gamma:1;
int animation; int animation;
}; };