From 6e4e529541760319a123bcb04ec8bcd68361499e Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 31 Aug 2023 07:05:06 +0000 Subject: [PATCH] Clean up window and input, integrate sokol [2deca9ddb9] --- Makefile | 2 +- source/engine/debug/debugdraw.c | 6 +- source/engine/debug/debugdraw.h | 1 + source/engine/engine.c | 10 - source/engine/ffi.c | 9 +- source/engine/input.c | 624 +++++++++++--------------------- source/engine/input.h | 26 +- source/engine/nuke.c | 2 - source/engine/openglrender.c | 19 +- source/engine/script.c | 2 + source/engine/window.c | 233 ++---------- source/engine/window.h | 46 ++- source/engine/yugine.c | 74 ++-- source/engine/yugine.h | 2 +- 14 files changed, 332 insertions(+), 724 deletions(-) diff --git a/Makefile b/Makefile index 2436462..bda1de0 100755 --- a/Makefile +++ b/Makefile @@ -83,7 +83,7 @@ ifeq ($(OS), WIN32) EXT = .exe else LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -pthread -rdynamic - ELIBS = engine pthread yughc quickjs glfw3 GL c m dl + ELIBS = engine pthread yughc quickjs GL c m dl X11 Xi Xcursor EGL CLIBS = endif diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index 9c87e9a..09806f5 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -112,7 +112,7 @@ void debug_flush(HMM_Mat4 *view) sg_apply_pipeline(line_pipe); sg_apply_bindings(&line_bind); sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(*view)); - float time = lastTick; + float time = appTime; sg_range tr = { .ptr = &time, .size = sizeof(float) @@ -571,8 +571,8 @@ void draw_grid(int width, int span, struct rgba color) { cpVect offset = cam_pos(); offset = cpvmult(offset, 1/cam_zoom()); - offset.x -= mainwin->width/2; - offset.y -= mainwin->height/2; + offset.x -= mainwin.width/2; + offset.y -= mainwin.height/2; sg_apply_pipeline(grid_pipe); sg_apply_bindings(&grid_bind); diff --git a/source/engine/debug/debugdraw.h b/source/engine/debug/debugdraw.h index 1b32e0e..d1c46c3 100644 --- a/source/engine/debug/debugdraw.h +++ b/source/engine/debug/debugdraw.h @@ -22,6 +22,7 @@ void draw_grid(int width, int span, struct rgba color); void debug_flush(HMM_Mat4 *view); void debug_newframe(); +void debug_nextpass(); cpVect inflatepoint(cpVect a, cpVect b, cpVect c, float d); void inflatepoints(cpVect *r, cpVect *p, float d, int n); diff --git a/source/engine/engine.c b/source/engine/engine.c index 3ab4d5f..9395347 100644 --- a/source/engine/engine.c +++ b/source/engine/engine.c @@ -38,16 +38,6 @@ void error_callback(int error, const char *description) { } void engine_init() { - - glfwSetErrorCallback(error_callback); - /* Initialize GLFW */ - if (!glfwInit()) { - YughError("Could not init GLFW. Exiting."); - exit(1); - } else { - YughInfo("Initted GLFW."); - } - resources_init(); YughInfo("Starting physics ..."); diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 2c00f85..759aed1 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -402,6 +402,7 @@ JSValue duk_nuke(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) } JSValue duk_win_make(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { +/* const char *title = JS_ToCString(js, argv[0]); int w = js2int(argv[1]); int h = js2int(argv[2]); @@ -409,6 +410,8 @@ JSValue duk_win_make(JSContext *js, JSValueConst this, int argc, JSValueConst *a JS_FreeCString(js, title); return JS_NewInt64(js, win->id); +*/ + return JS_NULL; } JSValue duk_spline_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) { @@ -776,10 +779,10 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) return JS_NULL; case 48: - return JS_NewInt64(js, mainwin->width); + return JS_NewInt64(js, mainwin.width); case 49: - return JS_NewInt64(js, mainwin->height); + return JS_NewInt64(js, mainwin.height); case 50: return JS_NewBool(js, action_down(js2int(argv[1]))); @@ -884,11 +887,9 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) return JS_NULL; case 77: - input_to_game(); break; case 78: - input_to_nuke(); break; case 79: diff --git a/source/engine/input.c b/source/engine/input.c index 16f4866..80b891e 100644 --- a/source/engine/input.c +++ b/source/engine/input.c @@ -14,6 +14,9 @@ int32_t mouseWheelX = 0; int32_t mouseWheelY = 0; float deltaT = 0; +static int mouse_states[3]; +static int key_states[512]; + JSValue jsinput; JSValue jsnum; JSValue jsgamepadstr[15]; @@ -29,7 +32,7 @@ cpVect mouse_delta = {0, 0}; struct joystick { int id; - GLFWgamepadstate state; + //GLFWgamepadstate state; }; static int *downkeys = NULL; @@ -59,51 +62,6 @@ JSValue input2js(const char *input) { return n; } -const char *gamepad2str(int btn) { - switch (btn) { - case GLFW_GAMEPAD_BUTTON_CROSS: - return "cross"; - case GLFW_GAMEPAD_BUTTON_CIRCLE: - return "circle"; - case GLFW_GAMEPAD_BUTTON_SQUARE: - return "square"; - case GLFW_GAMEPAD_BUTTON_TRIANGLE: - return "triangle"; - case GLFW_GAMEPAD_BUTTON_START: - return "start"; - case GLFW_GAMEPAD_BUTTON_LEFT_BUMPER: - return "lbump"; - case GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER: - return "rbump"; - case GLFW_GAMEPAD_BUTTON_GUIDE: - return "guide"; - case GLFW_GAMEPAD_BUTTON_BACK: - return "back"; - case GLFW_GAMEPAD_BUTTON_DPAD_UP: - return "dup"; - case GLFW_GAMEPAD_BUTTON_DPAD_DOWN: - return "ddown"; - case GLFW_GAMEPAD_BUTTON_DPAD_LEFT: - return "dleft"; - case GLFW_GAMEPAD_BUTTON_DPAD_RIGHT: - return "dright"; - case GLFW_GAMEPAD_BUTTON_LEFT_THUMB: - return "lthumb"; - case GLFW_GAMEPAD_BUTTON_RIGHT_THUMB: - return "rthumb"; - } - - return "NOBTN"; -} - -void register_pawn(struct callee c) { - pawn_callee = c; -} - -void register_gamepad(struct callee c) { - gamepad_callee = c; -} - void add_downkey(int key) { for (int i = 0; i < arrlen(downkeys); i++) if (downkeys[i] == key) return; @@ -119,132 +77,123 @@ void rm_downkey(int key) { } } -static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos) { - mouse_delta.x = xpos - mouse_pos.x; - mouse_delta.y = ypos - mouse_pos.y; - - mouse_pos.x = xpos; - mouse_pos.y = ypos; - - 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]); +char *mb2str(int btn) +{ + switch(btn) { + case 0: + return "lmouse"; + break; + case 1: + return "rmouse"; + break; + case 2: + return "mmouse"; + break; + } + return "NULLMOUSE"; } +void input_mouse(int btn, int state) +{ + JSValue argv[2]; + argv[0] = input2js(mb2str(btn)); + argv[1] = jsinputstate[btn]; + script_callee(pawn_callee, 2, argv); +} + +void input_mouse_move(float x, float y, float dx, float dy) +{ + mouse_pos.x = x; + mouse_pos.y = y; + mouse_delta.x = dx; + mouse_delta.y = dy; + + JSValue argv[3]; + argv[0] = jsmouse; + argv[1] = jspos; + argv[2] = vec2js(mouse_pos); + script_callee(pawn_callee, 3, argv); + JS_FreeValue(js, argv[2]); +} + +void input_mouse_scroll(float x, float y) +{ + mouseWheelY = y; + mouseWheelX = x; +} + +void input_btn(int btn, int state, int mod) +{ + char *keystr = keyname_extd(btn); + + JSValue argv[2]; + argv[0] = JS_NewString(js, keystr); + argv[1] = jsinputstate[state]; + script_callee(pawn_callee, 2, argv); + JS_FreeValue(js, argv[0]); + + if (state == INPUT_DOWN) { + key_states[btn] = INPUT_DOWN; + add_downkey(btn); + } + else if (state == INPUT_UP) { + key_states[btn] = INPUT_UP; + rm_downkey(btn); + } +} + +void input_key(int key) +{ + char out[2] = {0}; + out[0] = (char)key; + + JSValue argv[3]; + argv[0] = JS_NewString(js, "char"); + argv[1] = JS_NewString(js, out); + script_callee(pawn_callee, 2, argv); + + JS_FreeValue(js, argv[0]); + JS_FreeValue(js, argv[1]); +} + +void register_pawn(struct callee c) { + pawn_callee = c; +} + +void register_gamepad(struct callee c) { + gamepad_callee = c; +} + + static void pawn_call_keydown(int key) { JSValue argv[4]; argv[0] = jsinput; argv[1] = jsnum; - argv[2] = jsinputstate[2]; + argv[2] = jsinputstate[INPUT_DOWN]; /* 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) { - mouseWheelY = yoffset; - mouseWheelX = xoffset; -} - -static void mb_cb(GLFWwindow *w, int button, int action, int mods) { - JSValue argv[3]; - argv[0] = jsinput; - switch (action) { - case GLFW_PRESS: - argv[2] = jsinputstate[2]; - add_downkey(button); - break; - - case GLFW_RELEASE: - rm_downkey(button); - argv[2] = jsinputstate[0]; - argv[1] = jsany; - script_callee(pawn_callee, 3, argv); - break; - - case GLFW_REPEAT: - argv[2] = jsinputstate[1]; - break; - } - - argv[1] = input2js(keyname_extd(button, button)); - script_callee(pawn_callee, 3, argv); -} - -void set_mouse_mode(int mousemode) { - glfwSetInputMode(mainwin->window, GLFW_CURSOR, mousemode); -} - -void char_cb(GLFWwindow *w, unsigned int codepoint) { - static char out[2] = {0}; - static JSValue argv[2]; - - out[0] = (char)codepoint; - argv[0] = JS_NewString(js, "input_text"); - argv[1] = JS_NewString(js, out); - script_callee(pawn_callee, 2, argv); - - JS_FreeValue(js, argv[0]); - JS_FreeValue(js, argv[1]); -} - -static GLFWcharfun nukechar; - -void joystick_add(int id) { - struct joystick joy = {0}; - joy.id = id; - arrpush(joysticks, joy); -} - -void joystick_cb(int jid, int event) { - YughWarn("IN joystick cb"); - if (event == GLFW_CONNECTED) { - for (int i = 0; i < arrlen(joysticks); i++) - if (joysticks[i].id == jid) return; - - joystick_add(jid); - } else if (event == GLFW_DISCONNECTED) { - for (int i = 0; i < arrlen(joysticks); i++) { - if (joysticks[i].id == jid) { - arrdelswap(joysticks, i); - return; - } - } - } -} +/* + 0 free + 1 lock +*/ +void set_mouse_mode(int mousemode) { sapp_lock_mouse(mousemode); } void input_init() { - glfwSetCursorPosCallback(mainwin->window, cursor_pos_cb); - glfwSetScrollCallback(mainwin->window, scroll_cb); - glfwSetMouseButtonCallback(mainwin->window, mb_cb); - glfwSetJoystickCallback(joystick_cb); - nukechar = glfwSetCharCallback(mainwin->window, char_cb); - char *paddb = slurp_text("data/gamecontrollerdb.txt"); - glfwUpdateGamepadMappings(paddb); free(paddb); - for (int b = 0; b < 15; b++) - jsgamepadstr[b] = str2js(gamepad2str(b)); - jsaxesstr[0] = str2js("ljoy"); jsaxesstr[1] = str2js("rjoy"); jsaxesstr[2] = str2js("ltrigger"); jsaxesstr[3] = str2js("rtrigger"); jsaxis = str2js("axis"); - - /* 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[INPUT_UP] = str2js("released"); + jsinputstate[INPUT_REPEAT] = str2js("rep"); + jsinputstate[INPUT_DOWN] = str2js("pressed"); jsinputstate[3] = str2js("pressrep"); jsinputstate[4] = str2js("down"); jsinput = str2js("input"); @@ -254,170 +203,124 @@ void input_init() { jspos = str2js("pos"); } -void input_to_nuke() { - glfwSetCharCallback(mainwin->window, nukechar); -} - -void input_to_game() { - glfwSetCharCallback(mainwin->window, char_cb); -} - -void call_input_signal(char *signal) { - JSValue s = JS_NewString(js, signal); - JS_Call(js, pawn_callee.fn, pawn_callee.obj, 1, &s); - JS_FreeValue(js, s); -} - char keybuf[50]; -const char *keyname_extd(int key, int scancode) { - const char *kkey = NULL; +const char *keyname_extd(int key) { + if (key >= 32 && key <=90) { + keybuf[0] = key; + keybuf[1] = '\0'; + return keybuf; + } if (key > 289 && key < 302) { int num = key - 289; sprintf(keybuf, "f%d", num); return keybuf; - } else if (key >= 320 && key <= 329) { + } + + if (key >= 320 && key <= 329) { int num = key - 320; sprintf(keybuf, "kp%d", num); return keybuf; - } else { - switch (key) { - case GLFW_KEY_ENTER: - kkey = "enter"; - break; - - case GLFW_KEY_ESCAPE: - kkey = "escape"; - break; - - case GLFW_KEY_DELETE: - kkey = "delete"; - break; - - case GLFW_KEY_INSERT: - kkey = "insert"; - break; - - case GLFW_KEY_TAB: - kkey = "tab"; - break; - - case GLFW_KEY_RIGHT: - kkey = "right"; - break; - - case GLFW_KEY_LEFT: - kkey = "left"; - break; - - case GLFW_KEY_UP: - kkey = "up"; - break; - - case GLFW_KEY_DOWN: - kkey = "down"; - break; - - case GLFW_KEY_LEFT_SHIFT: - kkey = "lshift"; - break; - - case GLFW_KEY_RIGHT_SHIFT: - kkey = "rshift"; - break; - - case GLFW_KEY_LEFT_CONTROL: - kkey = "lctrl"; - break; - - case GLFW_KEY_LEFT_ALT: - kkey = "lalt"; - break; - - case GLFW_KEY_RIGHT_CONTROL: - kkey = "rctrl"; - break; - - case GLFW_KEY_RIGHT_ALT: - kkey = "ralt"; - break; - - case GLFW_KEY_SPACE: - kkey = "space"; - break; - - case GLFW_MOUSE_BUTTON_RIGHT: - kkey = "rmouse"; - break; - - case GLFW_MOUSE_BUTTON_LEFT: - kkey = "lmouse"; - break; - - case GLFW_MOUSE_BUTTON_MIDDLE: - kkey = "mmouse"; - break; - - case GLFW_KEY_KP_ADD: - kkey = "plus"; - break; - - case GLFW_KEY_KP_SUBTRACT: - kkey = "minus"; - break; - case GLFW_KEY_GRAVE_ACCENT: - kkey = "backtick"; - break; - - case GLFW_KEY_LEFT_BRACKET: - kkey = "lbracket"; - break; - - case GLFW_KEY_RIGHT_BRACKET: - kkey = "rbracket"; - break; - - case GLFW_KEY_BACKSPACE: - kkey = "backspace"; - break; - } - - if (kkey) return kkey; } - kkey = glfwGetKeyName(key, scancode); + switch (key) { + case SAPP_KEYCODE_ENTER: + return "enter"; + break; - if (kkey) return kkey; + case SAPP_KEYCODE_ESCAPE: + return "escape"; + break; + + case SAPP_KEYCODE_DELETE: + return "delete"; + break; + + case SAPP_KEYCODE_INSERT: + return "insert"; + break; + + case SAPP_KEYCODE_TAB: + return "tab"; + break; + + case SAPP_KEYCODE_RIGHT: + return "right"; + break; + + case SAPP_KEYCODE_LEFT: + return "left"; + break; + + case SAPP_KEYCODE_UP: + return "up"; + break; + + case SAPP_KEYCODE_DOWN: + return "down"; + break; + + case SAPP_KEYCODE_LEFT_SHIFT: + return "lshift"; + break; + + case SAPP_KEYCODE_RIGHT_SHIFT: + return "rshift"; + break; + + case SAPP_KEYCODE_LEFT_CONTROL: + return "lctrl"; + break; + + case SAPP_KEYCODE_LEFT_ALT: + return "lalt"; + break; + + case SAPP_KEYCODE_RIGHT_CONTROL: + return "rctrl"; + break; + + case SAPP_KEYCODE_RIGHT_ALT: + return "ralt"; + break; + + case SAPP_KEYCODE_SPACE: + return "space"; + break; + + case SAPP_KEYCODE_KP_ADD: + return "plus"; + break; + + case SAPP_KEYCODE_KP_SUBTRACT: + return "minus"; + break; + case SAPP_KEYCODE_GRAVE_ACCENT: + return "backtick"; + break; + + case SAPP_KEYCODE_LEFT_BRACKET: + return "lbracket"; + break; + + case SAPP_KEYCODE_RIGHT_BRACKET: + return "rbracket"; + break; + + case SAPP_KEYCODE_BACKSPACE: + return "backspace"; + break; + } return "NULL"; } void call_input_down(int *key) { - JSValue argv[3]; - argv[0] = jsinput; - argv[1] = input2js(keyname_extd(*key, *key)); - argv[2] = jsinputstate[4]; - script_callee(pawn_callee, 3, argv); -} - -const char *axis2str(int axis) { - switch (axis) { - case GLFW_GAMEPAD_AXIS_LEFT_X: - return "lx"; - case GLFW_GAMEPAD_AXIS_LEFT_Y: - return "ly"; - case GLFW_GAMEPAD_AXIS_RIGHT_X: - return "rx"; - case GLFW_GAMEPAD_AXIS_RIGHT_Y: - return "ry"; - case GLFW_GAMEPAD_AXIS_LEFT_TRIGGER: - return "ltrigger"; - case GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER: - return "rtrigger"; - } - - return "NOAXIS"; + JSValue argv[2]; + argv[0] = input2js(keyname_extd(*key)); + argv[1] = jsinputstate[4]; + script_callee(pawn_callee, 2, argv); } /* This is called once every frame - or more if we want it more! */ @@ -426,140 +329,19 @@ void input_poll(double wait) { mouseWheelX = 0; mouseWheelY = 0; - glfwWaitEventsTimeout(wait); - for (int i = 0; i < arrlen(downkeys); i++) call_input_down(&downkeys[i]); - - for (int i = 0; i < arrlen(joysticks); i++) { - GLFWgamepadstate state; - if (!glfwGetGamepadState(joysticks[i].id, &state)) continue; - - JSValue argv[4]; - argv[0] = num_cache[joysticks[i].id]; - for (int b = 0; b < 15; b++) { - argv[1] = jsgamepadstr[b]; - - if (state.buttons[b]) { - argv[2] = num_cache[0]; - script_callee(gamepad_callee, 3, argv); - - if (!joysticks[i].state.buttons[b]) { - argv[2] = num_cache[1]; - script_callee(gamepad_callee, 3, argv); - } - } else if (!state.buttons[b] && joysticks[i].state.buttons[b]) { - argv[2] = num_cache[2]; - script_callee(gamepad_callee, 3, argv); - } - } - - argv[2] = jsaxis; - - float deadzone = 0.05; - - for (int i = 0; i < 4; i++) - state.axes[i] = fabs(state.axes[i]) > deadzone ? state.axes[i] : 0; - - argv[1] = jsaxesstr[0]; - cpVect v; - v.x = state.axes[0]; - v.y = -state.axes[1]; - argv[3] = vec2js(v); - script_callee(gamepad_callee, 4, argv); - JS_FreeValue(js, argv[3]); - - argv[1] = jsaxesstr[1]; - v.x = state.axes[2]; - v.y = -state.axes[3]; - argv[3] = vec2js(v); - script_callee(gamepad_callee, 4, argv); - JS_FreeValue(js, argv[3]); - - argv[1] = jsaxesstr[2]; - argv[3] = num2js((state.axes[4] + 1) / 2); - script_callee(gamepad_callee, 4, argv); - JS_FreeValue(js, argv[3]); - - argv[1] = jsaxesstr[3]; - argv[3] = num2js((state.axes[5] + 1) / 2); - script_callee(gamepad_callee, 4, argv); - JS_FreeValue(js, argv[3]); - - joysticks[i].state = state; - } } int key_is_num(int key) { return key <= 57 && key >= 48; } -void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods) { - JSValue argv[3]; - argv[0] = jsinput; - argv[1] = input2js(keyname_extd(key, scancode)); +void cursor_hide() { sapp_show_mouse(0); } +void cursor_show() { sapp_show_mouse(1); } - switch (action) { - case GLFW_PRESS: - argv[2] = jsinputstate[2]; - script_callee(pawn_callee, 3, argv); - argv[2] = jsinputstate[3]; - script_callee(pawn_callee, 3, argv); - add_downkey(key); - argv[1] = jsany; - argv[2] = jsinputstate[2]; - script_callee(pawn_callee, 3, argv); - - if (key_is_num(key)) - pawn_call_keydown(key - 48); - - break; - - case GLFW_RELEASE: - argv[2] = jsinputstate[0]; - script_callee(pawn_callee, 3, argv); - rm_downkey(key); - argv[1] = jsany; - script_callee(pawn_callee, 3, argv); - break; - - case GLFW_REPEAT: - argv[2] = jsinputstate[1]; - script_callee(pawn_callee, 3, argv); - argv[2] = jsinputstate[3]; - script_callee(pawn_callee, 3, argv); - break; - } -} - -void cursor_hide() { - glfwSetInputMode(mainwin->window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); -} - -void cursor_show() { - glfwSetInputMode(mainwin->window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); -} - -int action_down(int scancode) { - for (int i = 0; i < arrlen(downkeys); i++) { - if (downkeys[i] == scancode) - return 1; - } - - return 0; -} - -int action_up(int scancode) { - int found = 0; - for (int i = 0; i < arrlen(downkeys); i++) { - if (downkeys[i] == scancode) { - found = 1; - break; - } - } - - return !found; -} +int action_down(int key) { return key_states[key] == INPUT_DOWN; } +int action_up(int key) { return key_states[key] == INPUT_UP; } int want_quit() { return mquit; diff --git a/source/engine/input.h b/source/engine/input.h index fd25563..4083c21 100644 --- a/source/engine/input.h +++ b/source/engine/input.h @@ -1,6 +1,8 @@ #ifndef INPUT_H #define INPUT_H +#include "sokol/sokol_app.h" + #include "script.h" #include "window.h" #include @@ -12,6 +14,10 @@ extern int32_t mouseWheelY; extern cpVect mouse_pos; extern cpVect mouse_delta; +#define INPUT_DOWN 0 +#define INPUT_UP 1 +#define INPUT_REPEAT 2 + extern float deltaT; void input_init(); @@ -21,9 +27,14 @@ void cursor_hide(); void cursor_show(); void set_mouse_mode(int mousemode); -void call_input_signal(char *signal); -const char *keyname_extd(int key, int scancode); -int action_down(int scancode); +void input_mouse(int btn, int state); +void input_mouse_move(float x, float y, float dx, float dy); +void input_mouse_scroll(float x, float y); +void input_btn(int btn, int state, int mod); +void input_key(int key); + +const char *keyname_extd(int key); +int action_down(int key); void register_pawn(struct callee c); void register_gamepad(struct callee c); @@ -31,13 +42,4 @@ void register_gamepad(struct callee c); int want_quit(); void quit(); -void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods); - -struct inputaction { - int scancode; -}; - -void input_to_nuke(); -void input_to_game(); - #endif diff --git a/source/engine/nuke.c b/source/engine/nuke.c index 70efd69..bad7a99 100644 --- a/source/engine/nuke.c +++ b/source/engine/nuke.c @@ -28,8 +28,6 @@ struct nk_context *ctx; //static struct nk_glfw nkglfw = {0}; void nuke_init(struct window *win) { - window_makecurrent(win); - snk_setup(&(snk_desc_t){ .no_default_font = false }); diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index 92c923b..a1cbb37 100644 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -116,17 +116,12 @@ static sg_trace_hooks hooks = { void openglInit() { - if (!mainwin) { - YughError("No window to init OpenGL on.", 1); - exit(1); - } - sg_trace_hooks hh = sg_install_trace_hooks(&hooks); font_init(NULL); debugdraw_init(); sprite_initialize(); - nuke_init(mainwin); + nuke_init(&mainwin); model_init(); sg_color c; @@ -245,14 +240,14 @@ void render_winsize() crt_post.img = sg_make_image(&(sg_image_desc){ .render_target = true, - .width = mainwin->width, - .height = mainwin->height + .width = mainwin.width, + .height = mainwin.height }); crt_post.depth_img = sg_make_image(&(sg_image_desc){ .render_target = true, - .width = mainwin->width, - .height = mainwin->height, + .width = mainwin.width, + .height = mainwin.height, .pixel_format = SG_PIXELFORMAT_DEPTH_STENCIL }); @@ -315,7 +310,7 @@ void openglRender(struct window *window) { sg_end_pass(); draw_model(duck,model, lsm); -*/ +*/ // sg_begin_default_pass(&pass_action, window->width, window->height); sg_begin_pass(crt_post.pass, &pass_action); @@ -330,7 +325,7 @@ void openglRender(struct window *window) { pos.y + zoom * window->height / 2, -1.f, 1.f); hudproj = HMM_Orthographic_RH_NO(0, window->width, 0, window->height, -1.f, 1.f); - + sprite_draw_all(); sprite_flush(); call_draw(); diff --git a/source/engine/script.c b/source/engine/script.c index 2657a58..dbfe3bc 100644 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -14,6 +14,8 @@ #include "sys/types.h" #include "time.h" +#include + JSContext *js = NULL; JSRuntime *rt = NULL; diff --git a/source/engine/window.c b/source/engine/window.c index 26a4ed7..1c77dec 100644 --- a/source/engine/window.c +++ b/source/engine/window.c @@ -13,203 +13,46 @@ #include "stb_ds.h" -struct window *mainwin; +struct window mainwin; static struct window *windows = NULL; struct Texture *icon = NULL; -int is_win(struct window *s, GLFWwindow *w) { - return s->window == w; -} - -void window_size_callback(GLFWwindow *w, int width, int height) { +void window_resize(int width, int height) +{ + mainwin.width = width; + mainwin.height = height; render_winsize(); -} - -struct window *winfind(GLFWwindow *w) { - for (int i = 0; i < arrlen(windows); i++) { - if (windows[i].window == w) - return &windows[i]; - } - - return NULL; -} - -void window_iconify_callback(GLFWwindow *w, int iconified) { - struct window *win = winfind(w); - win->iconified = iconified; -} - -void window_focus_callback(GLFWwindow *w, int focused) { - struct window *win = winfind(w); - win->mouseFocus = focused; -} - -void window_maximize_callback(GLFWwindow *w, int maximized) { - struct window *win = winfind(w); - win->minimized = !maximized; -} - -void window_framebuffer_size_cb(GLFWwindow *w, int width, int height) { - struct window *win = mainwin; - win->width = width; - win->height = height; - window_makecurrent(win); - win->render = 1; JSValue vals[2] = { int2js(width), int2js(height) }; send_signal("window_resize", 2, vals); } -void window_close_callback(GLFWwindow *w) { +void window_quit() +{ quit(); } -struct window *MakeSDLWindow(const char *name, int width, int height, uint32_t flags) { - if (arrcap(windows) == 0) - arrsetcap(windows, 5); +void window_focused(int focus) +{ + mainwin.focus = focus; +} - if (mainwin != NULL) - return mainwin; +void window_iconified(int s) +{ + mainwin.iconified = s; +} - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_SAMPLES,1); - struct window w = { - .width = width, - .height = height, - .id = arrlen(windows), - .window = glfwCreateWindow(width, height, name, NULL, mainwin)}; - - if (!w.window) { - YughError("Couldn't make GLFW window\n", 1); - return NULL; - } +void window_suspended(int s) +{ - mainwin = malloc(sizeof(struct window)); - *mainwin = w; - - glfwMakeContextCurrent(w.window); - YughInfo("Loaded OpenGL %d.%d", 3, 3); - glfwSwapInterval(1); - - // Set callbacks - glfwSetWindowCloseCallback(w.window, window_close_callback); - glfwSetWindowSizeCallback(w.window, window_size_callback); - glfwSetFramebufferSizeCallback(w.window, window_framebuffer_size_cb); - glfwSetWindowFocusCallback(w.window, window_focus_callback); - glfwSetKeyCallback(w.window, win_key_callback); - - arrput(windows, w); - - if (arrlen(windows) == 1) - mainwin = &windows[0]; - - window_framebuffer_size_cb(mainwin, width, height); - - return &arrlast(windows); } void window_set_icon(const char *png) { icon = texture_pullfromfile(png); - window_seticon(mainwin, icon); -} - -void window_destroy(struct window *w) { - glfwDestroyWindow(w->window); - arrdelswap(windows, w->id); -} - -struct window *window_i(int index) { - return &windows[index]; -} - -void window_handle_event(struct window *w) { - /* - if (e->type == SDL_WINDOWEVENT && e->window.windowID == w->id) { // TODO: Check ptr direct? - switch (e->window.event) { - case SDL_WINDOWEVENT_SHOWN: - YughLog(0, SDL_LOG_PRIORITY_INFO, "Showed window %d.", w->id); - w->shown = true; - break; - - case SDL_WINDOWEVENT_HIDDEN: - w->shown = false; - YughLog(0, SDL_LOG_PRIORITY_INFO, "Hid window %d.", w->id); - break; - - case SDL_WINDOWEVENT_SIZE_CHANGED: - - w->width = e->window.data1; - w->height = e->window.data2; - YughLog(0, SDL_LOG_PRIORITY_INFO, - "Changed size of window %d: width %d, height %d.", - w->id, w->width, w->height); - window_makecurrent(w); - w->render = true; - break; - - case SDL_WINDOWEVENT_EXPOSED: - YughLog(0, SDL_LOG_PRIORITY_INFO, "Exposed window %d.", w->id); - w->render = true; - break; - - case SDL_WINDOWEVENT_ENTER: - YughLog(0, SDL_LOG_PRIORITY_INFO, "Entered window %d.", w->id); - w->mouseFocus = true; - SDL_RaiseWindow(w->window); - break; - - case SDL_WINDOWEVENT_LEAVE: - YughLog(0, SDL_LOG_PRIORITY_INFO, "Left window %d.", w->id); - w->mouseFocus = false; - break; - - case SDL_WINDOWEVENT_FOCUS_LOST: - YughLog(0, SDL_LOG_PRIORITY_INFO, - "Lost focus on window %d.", w->id); - w->keyboardFocus = false; - break; - - case SDL_WINDOWEVENT_FOCUS_GAINED: - YughLog(0, SDL_LOG_PRIORITY_INFO, - "Gained focus on window %d.", w->id); - w->keyboardFocus = true; - break; - - case SDL_WINDOWEVENT_MINIMIZED: - YughLog(0, SDL_LOG_PRIORITY_INFO, - "Minimized window %d.", w->id); - w->minimized = true; - break; - - case SDL_WINDOWEVENT_MAXIMIZED: - YughLog(0, SDL_LOG_PRIORITY_INFO, - "Maximized window %d.", w->id); - w->minimized = false; - break; - - case SDL_WINDOWEVENT_RESTORED: - YughLog(0, SDL_LOG_PRIORITY_INFO, - "Restored window %d.", w->id); - w->minimized = false; - break; - - case SDL_WINDOWEVENT_CLOSE: - YughLog(0, SDL_LOG_PRIORITY_INFO, "Closed window %d.", w->id); - break; - } - } - */ -} - -void window_all_handle_events() { - for (int i = 0; i < arrlen(windows); i++) - window_handle_event(&windows[i]); + window_seticon(&mainwin, icon); } void window_makefullscreen(struct window *w) { @@ -223,48 +66,14 @@ void window_unfullscreen(struct window *w) { } void window_togglefullscreen(struct window *w) { - w->fullscreen = !w->fullscreen; - - if (w->fullscreen) { - glfwMaximizeWindow(w->window); - } else { - glfwRestoreWindow(w->window); - } -} - -void window_makecurrent(struct window *w) { - - if (w->window != glfwGetCurrentContext()) - glfwMakeContextCurrent(w->window); - - glViewport(0, 0, w->width, w->height); -} - -void window_swap(struct window *w) { - glfwSwapBuffers(w->window); + sapp_toggle_fullscreen(); + mainwin.fullscreen = sapp_is_fullscreen(); } void window_seticon(struct window *w, struct Texture *icon) { - - static GLFWimage images[1]; - images[0].width = icon->width; - images[0].height = icon->height; - images[0].pixels = icon->data; - glfwSetWindowIcon(w->window, 1, images); -} - -int window_hasfocus(struct window *w) { - return glfwGetWindowAttrib(w->window, GLFW_FOCUSED); + } void window_render(struct window *w) { - window_makecurrent(w); openglRender(w); - window_swap(w); -} - -void window_renderall() { - // arrwalk(windows, window_render); - for (int i = 0; i < arrlen(windows); i++) - window_render(&windows[i]); } diff --git a/source/engine/window.h b/source/engine/window.h index 68a03c5..a28c835 100644 --- a/source/engine/window.h +++ b/source/engine/window.h @@ -1,46 +1,42 @@ #ifndef WINDOW_H #define WINDOW_H -/* + #include "render.h" #include #include struct window { - GLFWwindow *window; - int id; - int width; - int height; - bool render; - bool mouseFocus; - bool keyboardFocus; - bool fullscreen; - bool minimized; - bool iconified; - bool shown; - void (*nuke_gui)(); + int id; + int width; + int height; + bool render; + bool mouseFocus; + bool keyboardFocus; + bool fullscreen; + bool minimized; + bool iconified; + bool focus; + bool shown; }; struct Texture; -extern struct window *mainwin; -*/ +extern struct window mainwin; + +void window_resize(int width, int height); +void window_quit(); +void window_focused(int focus); +void window_iconified(int s); +void window_suspended(int s); -//struct window *MakeSDLWindow(const char *name, int width, int height, uint32_t flags); -void window_set_icon(const char *png); -void window_destroy(struct window *w); -void window_handle_event(struct window *w); -void window_all_handle_events(); -void window_makecurrent(struct window *w); void window_makefullscreen(struct window *w); void window_togglefullscreen(struct window *w); void window_unfullscreen(struct window *w); -void window_swap(struct window *w); + +void window_set_icon(const char *png); void window_seticon(struct window *w, struct Texture *icon); -int window_hasfocus(struct window *w); -struct window *window_i(int index); void window_render(struct window *w); -void window_renderall(); #endif diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 9092c7c..8a51c6b 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -34,12 +34,10 @@ #include "string.h" #define SOKOL_TRACE_HOOKS -#define SOKOL_GFX_IMPL -#define SOKOL_GLES3 -#include "sokol/sokol_gfx.h" +#define SOKOL_IMPL +#define SOKOL_GLCORE33 -#define SOKOL_APP_IMPL -#define SOKOL_DEBUG +#include "sokol/sokol_gfx.h" #include "sokol/sokol_app.h" int physOn = 0; @@ -53,9 +51,10 @@ double physMS = 1 / 165.f; double updateMS = 1 / 165.f; static int sim_play = 0; -double lastTick = 0.0; static int phys_step = 0; +double appTime = 0; + static float timescale = 1.f; #define SIM_STOP 0 @@ -159,7 +158,7 @@ void c_init() { } } */ - +int logout = 0; #if DBG if (logout) { time_t now = time(NULL); @@ -187,6 +186,9 @@ void c_init() { engine_init(); + mainwin.width = sapp_width(); + mainwin.height = sapp_height(); + sg_setup(&(sg_desc){ .logger = { .func = sg_logging, @@ -207,18 +209,13 @@ int frame_fps() { void c_frame() { - double elapsed = glfwGetTime() - lastTick; - deltaT = elapsed; - lastTick = glfwGetTime(); - //double wait = fmax(0, renderMS - elapsed); + double elapsed = sapp_frame_duration(); + appTime += elapsed; + if (sim_playing()) input_poll(fmax(0, renderMS-elapsed)); else input_poll(1000); - window_all_handle_events(); - framems[framei++] = elapsed; - - if (framei == FPSBUF) framei = 0; if (sim_play == SIM_PLAY || sim_play == SIM_STEP) { timer_update(elapsed * timescale); @@ -239,7 +236,7 @@ void c_frame() if (renderlag >= renderMS) { renderlag -= renderMS; - window_renderall(); + window_render(&mainwin); } gameobjects_cleanup(); @@ -252,27 +249,62 @@ void c_clean() void c_event(const sapp_event *e) { - switch (e->type) { case SAPP_EVENTTYPE_MOUSE_MOVE: - input_mouse_moved(e->mouse_x, e->mouse_y); + input_mouse_move(e->mouse_x, e->mouse_y, e->mouse_dx, e->mouse_dy); break; case SAPP_EVENTTYPE_MOUSE_SCROLL: - input_scroll(e->scroll_x, e->scroll_y); + input_mouse_scroll(e->scroll_x, e->scroll_y); break; case SAPP_EVENTTYPE_KEY_DOWN: - input_key(e->key_code, e->key_repeat ? 2 : 0); + input_btn(e->key_code, e->key_repeat ? INPUT_REPEAT : INPUT_DOWN, e->modifiers); break; case SAPP_EVENTTYPE_KEY_UP: - input_key(e->key_code, 1); + input_btn(e->key_code, INPUT_UP, e->modifiers); + break; + + case SAPP_EVENTTYPE_MOUSE_UP: + input_mouse(e->mouse_button, INPUT_UP); + break; + + case SAPP_EVENTTYPE_MOUSE_DOWN: + input_mouse(e->mouse_button, INPUT_DOWN); break; case SAPP_EVENTTYPE_CHAR: input_key(e->char_code); break; + + case SAPP_EVENTTYPE_RESIZED: + window_resize(e->window_width, e->window_height); + break; + + case SAPP_EVENTTYPE_ICONIFIED: + window_iconified(1); + break; + + case SAPP_EVENTTYPE_RESTORED: + window_iconified(0); + break; + + case SAPP_EVENTTYPE_FOCUSED: + window_focused(1); + break; + + case SAPP_EVENTTYPE_UNFOCUSED: + window_focused(0); + break; + + case SAPP_EVENTTYPE_SUSPENDED: + window_suspended(1); + break; + + case SAPP_EVENTTYPE_QUIT_REQUESTED: + window_quit(); + break; } } diff --git a/source/engine/yugine.h b/source/engine/yugine.h index e5af950..e02a884 100644 --- a/source/engine/yugine.h +++ b/source/engine/yugine.h @@ -15,7 +15,7 @@ const char *engine_info(); int frame_fps(); -extern double lastTick; +extern double appTime; #endif