diff --git a/Makefile b/Makefile index 0883793..955e2b4 100755 --- a/Makefile +++ b/Makefile @@ -83,7 +83,7 @@ ifeq ($(OS), WIN32) EXT = .exe else LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -rdynamic - ELIBS = engine pthread yughc glfw3 quickjs tcc c m dl + ELIBS = engine pthread yughc glfw3 tcc quickjs c m dl CLIBS = endif diff --git a/source/engine/debug/log.c b/source/engine/debug/log.c index 40b153d..c21c8de 100644 --- a/source/engine/debug/log.c +++ b/source/engine/debug/log.c @@ -35,9 +35,13 @@ void mYughLog(int category, int priority, int line, const char *file, const char if (priority >= logLevel) { time_t now = time(0); + char timestr[50]; +// strftime(timestr,50," char *dt = ctime(&now); dt[strlen(dt) - 1] = '\0'; // The above time conversion adds a \n; this removes it + double ticks = (double)clock()/CLOCKS_PER_SEC; + va_list args; va_start(args, message); char msgbuffer[ERROR_BUFFER] = { '\0' }; @@ -45,7 +49,7 @@ void mYughLog(int category, int priority, int line, const char *file, const char va_end(args); char buffer[ERROR_BUFFER] = { '\0' }; - snprintf(buffer, ERROR_BUFFER, "%s:%d: %s, %s: %s\n", file, line, logstr[priority], catstr[category], msgbuffer); + snprintf(buffer, ERROR_BUFFER, "%g | %s:%d: %s, %s: %s\n", ticks, file, line, logstr[priority], catstr[category], msgbuffer); log_print(buffer); diff --git a/source/engine/engine.c b/source/engine/engine.c index 0c725af..68b2f37 100644 --- a/source/engine/engine.c +++ b/source/engine/engine.c @@ -52,6 +52,8 @@ void engine_init() if (!glfwInit()) { YughError("Could not init GLFW. Exiting."); exit(1); + } else { + YughInfo("Initted GLFW."); } resources_init(); diff --git a/source/engine/input.c b/source/engine/input.c index 9d04a7c..66fa735 100644 --- a/source/engine/input.c +++ b/source/engine/input.c @@ -8,10 +8,22 @@ #include "time.h" #include "font.h" +#inlcude "stb_ds.h" + int32_t mouseWheelX = 0; int32_t mouseWheelY = 0; float deltaT = 0; +JSValue jsinput; +JSValue jsnum; +JSValue jsgamepadstr[15]; +JSValue jsaxesstr[4]; +JSValue jsinputstate[5]; +JSValue jsaxis; +JSValue jsany; +JSValue jsmouse; +JSValue jspos; + cpVect mouse_pos = {0,0}; cpVect mouse_delta = {0,0}; @@ -28,6 +40,26 @@ static int mquit = 0; static struct callee pawn_callee; static struct callee gamepad_callee; +static struct { + char *key; + JSValue value; +} *jshash = NULL; + +JSValue input2js(const char *input) +{ + int idx = shgeti(jshash, input); + if (idx != -1) + return jshash[idx].value; + + if (shlen(jshash) == 0) + sh_new_arena(jshash); + + JSValue n = str2js(input); + shput(jshash, input, str2js(input)); + + return n; +} + const char *gamepad2str(int btn) { switch(btn) { @@ -84,22 +116,25 @@ static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos) mouse_pos.x = xpos; mouse_pos.y = ypos; - JSValue argv[2]; - argv[0] = JS_NewString(js, "input_mouse_pos"); - argv[1] = vec2js(mouse_pos); - script_callee(pawn_callee, 2, argv); - JS_FreeValue(js, argv[0]); - JS_FreeValue(js, argv[1]); + JSValue argv[4]; + argv[0] = jsinput; + argv[1] = jsmouse; + argv[2] = jspos; + argv[3] = vec2js(mouse_pos); + script_callee(pawn_callee, 4, argv); + JS_FreeValue(js, argv[3]); } static void pawn_call_keydown(int key) { - JSValue argv[2]; - argv[0] = JS_NewString(js, "input_num_pressed"); - argv[1] = JS_NewInt32(js, key); - script_callee(pawn_callee, 2, argv); - JS_FreeValue(js, argv[0]); - JS_FreeValue(js, argv[1]); + JSValue argv[4]; + argv[0] = jsinput; + argv[1] = jsnum; + argv[2] = jsinputstate[2]; + /* TODO: Could cache */ + argv[3] = JS_NewInt32(js, key); + script_callee(pawn_callee, 4, argv); + JS_FreeValue(js, argv[3]); } static void scroll_cb(GLFWwindow *w, double xoffset, double yoffset) @@ -112,33 +147,33 @@ static void mb_cb(GLFWwindow *w, int button, int action, int mods) { const char *act = NULL; const char *btn = NULL; + JSValue argv[3]; + argv[0] = jsinput; switch (action) { case GLFW_PRESS: - act = "pressed"; + argv[3] = jsinputstate[2]; add_downkey(button); break; case GLFW_RELEASE: - act = "released"; rm_downkey(button); - call_input_signal("input_any_released"); + argv[2] = jsinputstate[0]; + argv[1] = jsany; + script_callee(pawn_callee,3,argv); break; case GLFW_REPEAT: - act = "repeat"; + argv[2] = jsinputstate[1]; break; } - btn = keyname_extd(button, button); - if (!act || !btn) { YughError("Tried to call a mouse action that doesn't exist."); return; } - char keystr[50] = {'\0'}; - snprintf(keystr, 50, "input_%s_%s", btn, act); - call_input_signal(keystr); + argv[1] = input2js(keyname_extd(button,button)); + script_callee(pawn_callee,3,argv); } void set_mouse_mode(int mousemode) @@ -187,9 +222,6 @@ void joystick_cb(int jid, int event) } } -JSValue jsgamepadstr[15]; -JSValue jsaxesstr[4]; -JSValue jsaxis; void input_init() { @@ -215,6 +247,17 @@ void input_init() /* Grab all joysticks initially present */ for (int i = 0; i < 16; i++) if (glfwJoystickPresent(i)) joystick_add(i); + + jsinputstate[0] = str2js("released"); + jsinputstate[1] = str2js("rep"); + jsinputstate[2] = str2js("pressed"); + jsinputstate[3] = str2js("pressrep"); + jsinputstate[4] = str2js("down"); + jsinput = str2js("input"); + jsnum = str2js("num"); + jsany = str2js("any"); + jsmouse = str2js("mouse"); + jspos = str2js("pos"); } void input_to_nuke() @@ -360,9 +403,11 @@ const char *keyname_extd(int key, int scancode) { } void call_input_down(int *key) { - char keystr[50] = {'\0'}; - snprintf(keystr, 50, "input_%s_down", keyname_extd(*key, 0)); - call_input_signal(keystr); + JSValue argv[3]; + argv[0] = jsinput; + argv[1] = input2js(keyname_extd(*key, *key)); + argv[2] = jsinputstate[4]; + script_callee(pawn_callee,3,argv); } @@ -461,37 +506,39 @@ int key_is_num(int key) { void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods) { - char keystr[50] = {'\0'}; - char kkey[50] = {'\0'}; - snprintf(kkey, 50, "%s", keyname_extd(key,scancode)); + JSValue argv[3]; + argv[0] = jsinput; + argv[1] = input2js(keyname_extd(key,scancode)); switch (action) { case GLFW_PRESS: - snprintf(keystr, 50, "input_%s_pressed", kkey); - call_input_signal(keystr); - snprintf(keystr,50,"input_%s_pressrep", kkey); - call_input_signal(keystr); + argv[2] = jsinputstate[2]; + script_callee(pawn_callee, 3, argv); + argv[2] = jsinputstate[3]; + script_callee(pawn_callee,3,argv); add_downkey(key); - call_input_signal("input_any_pressed"); + argv[1] = jsany; + argv[2] = jsinputstate[2]; + script_callee(pawn_callee,3,argv); - if (key_is_num(key)) { + if (key_is_num(key)) pawn_call_keydown(key-48); - } break; case GLFW_RELEASE: - snprintf(keystr, 50, "input_%s_released", kkey); - call_input_signal(keystr); + argv[2] = jsinputstate[0]; + script_callee(pawn_callee,3,argv); rm_downkey(key); - call_input_signal("input_any_released"); + argv[1] = jsany; + script_callee(pawn_callee,3,argv); break; case GLFW_REPEAT: - snprintf(keystr, 50, "input_%s_rep", kkey); - call_input_signal(keystr); - snprintf(keystr,50,"input_%s_pressrep", kkey); - call_input_signal(keystr); + argv[2] = jsinputstate[1]; + script_callee(pawn_callee,3,argv); + argv[2] = jsinputstate[3]; + script_callee(pawn_callee,3,argv); break; } } diff --git a/source/engine/shader.c b/source/engine/shader.c index 812056b..8006b0e 100644 --- a/source/engine/shader.c +++ b/source/engine/shader.c @@ -95,7 +95,6 @@ GLuint load_shader_from_file(const char *path, int type) void shader_compile(struct shader *shader) { YughInfo("Making shader with %s and %s.", shader->vertpath, shader->fragpath); - clock_t startt = clock(); GLuint vert = load_shader_from_file(shader->vertpath, GL_VERTEX_SHADER); GLuint frag = load_shader_from_file(shader->fragpath, GL_FRAGMENT_SHADER); @@ -108,9 +107,6 @@ void shader_compile(struct shader *shader) glDeleteShader(vert); glDeleteShader(frag); - - startt = clock() - startt; - YughInfo("Created shader in %d ticks (%f seconds).", startt, ((float)startt/CLOCKS_PER_SEC)); } void shader_use(struct shader *shader) diff --git a/source/engine/yugine.c b/source/engine/yugine.c index db2f210..28e910a 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -12,7 +12,6 @@ #define MINIAUDIO_IMPLEMENTATION #include "miniaudio.h" - #include "timer.h" #include "quickjs/quickjs.h" @@ -58,7 +57,18 @@ int fps; #define SIM_PLAY 1 #define SIM_PAUSE 2 #define SIM_STEP 3 +/* +int __builtin_clz(unsigned int a) +{ + int to; + __asm__ __volatile__( + "bsr %edi, %eax\n\t" + "xor $31, %eax\n\t" + : "=&a"(to)); + return to; +} +*/ void print_stacktrace() { void *ents[512]; @@ -195,6 +205,8 @@ int main(int argc, char **args) { input_init(); openglInit(); +YughWarn("%d", __builtin_clz(500)); + if (ed) script_dofile("scripts/editor.js"); else diff --git a/source/scripts/engine.js b/source/scripts/engine.js index 7acaf4c..306b921 100644 --- a/source/scripts/engine.js +++ b/source/scripts/engine.js @@ -472,8 +472,9 @@ var Register = { this.nk_guis.forEach(x => x[0].call(x[1])); }, - kbm_input(fn, ...args) { - Player.players[0].input(fn, ...args); + kbm_input(src, btn, state, ...args) { + var input = `${src}_${btn}_${state}`; + Player.players[0].input(input, ...args); }, gamepad_playermap: [],