From 77895c7a455d9e9de1d98379553b6e08d3fd80c1 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 23 Aug 2023 03:44:09 +0000 Subject: [PATCH] start sokol integration. --- source/engine/render.h | 3 - source/engine/window.h | 6 +- source/engine/yugine.c | 123 +++++++++++++++++++++++++++-------------- 3 files changed, 83 insertions(+), 49 deletions(-) diff --git a/source/engine/render.h b/source/engine/render.h index 977d863..7108f05 100644 --- a/source/engine/render.h +++ b/source/engine/render.h @@ -1,9 +1,6 @@ #ifndef RENDER_H #define RENDER_H -#define GLFW_INCLUDE_NONE -#include - #include "sokol/sokol_gfx.h" #include "HandmadeMath.h" diff --git a/source/engine/window.h b/source/engine/window.h index b07859c..68a03c5 100644 --- a/source/engine/window.h +++ b/source/engine/window.h @@ -1,6 +1,7 @@ #ifndef WINDOW_H #define WINDOW_H +/* #include "render.h" #include #include @@ -10,7 +11,6 @@ struct window { int id; int width; int height; - /* TODO: Bitfield these */ bool render; bool mouseFocus; bool keyboardFocus; @@ -24,9 +24,9 @@ struct window { struct Texture; extern struct window *mainwin; +*/ - -struct window *MakeSDLWindow(const char *name, int width, int height, uint32_t flags); +//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); diff --git a/source/engine/yugine.c b/source/engine/yugine.c index b8be5fb..9092c7c 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -38,6 +38,10 @@ #define SOKOL_GLES3 #include "sokol/sokol_gfx.h" +#define SOKOL_APP_IMPL +#define SOKOL_DEBUG +#include "sokol/sokol_app.h" + int physOn = 0; double renderlag = 0; @@ -54,11 +58,6 @@ static int phys_step = 0; static float timescale = 1.f; -#define FPSBUF 10 -static double framems[FPSBUF]; -int framei = 0; -int fps; - #define SIM_STOP 0 #define SIM_PLAY 1 #define SIM_PAUSE 2 @@ -123,13 +122,10 @@ void sg_logging(const char *tag, uint32_t lvl, uint32_t id, const char *msg, uin mYughLog(0, 1, line, file, "tag: %s, msg: %s", tag, msg); } -int main(int argc, char **args) { - int logout = 1; - - script_startup(); - - logout = 0; +void c_init() { +/* + int logout = 0; for (int i = 1; i < argc; i++) { if (args[i][0] == '-') { switch (args[i][1]) { @@ -162,6 +158,7 @@ int main(int argc, char **args) { } } } +*/ #if DBG if (logout) { @@ -190,11 +187,6 @@ int main(int argc, char **args) { engine_init(); - const GLFWvidmode *vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); - YughInfo("Refresh rate is %d", vidmode->refreshRate); - - renderMS = 1.0 / vidmode->refreshRate; - sg_setup(&(sg_desc){ .logger = { .func = sg_logging, @@ -207,26 +199,14 @@ int main(int argc, char **args) { input_init(); openglInit(); +} - int argsize = 0; - for (int i = 1; i < argc; i++) { - argsize += strlen(args[i]); - if (argc > i+1) argsize++; - } +int frame_fps() { + return 1.0/sapp_frame_duration(); +} - char cmdstr[argsize]; - cmdstr[0] = '\0'; - - YughWarn("num is %d", argc); - - for (int i = 0; i < argc; i++) { - strcat(cmdstr, args[i]); - if (argc > i+1) strcat(cmdstr, " "); - } - - script_evalf("cmd_args('%s');", cmdstr); - - while (!want_quit()) { +void c_frame() +{ double elapsed = glfwGetTime() - lastTick; deltaT = elapsed; lastTick = glfwGetTime(); @@ -263,18 +243,37 @@ int main(int argc, char **args) { } gameobjects_cleanup(); - } - - return 0; } -int frame_fps() { - double fpsms = 0; - for (int i = 0; i < FPSBUF; i++) { - fpsms += framems[i]; - } +void c_clean() +{ - return FPSBUF / fpsms; +} + +void c_event(const sapp_event *e) +{ + + switch (e->type) { + case SAPP_EVENTTYPE_MOUSE_MOVE: + input_mouse_moved(e->mouse_x, e->mouse_y); + break; + + case SAPP_EVENTTYPE_MOUSE_SCROLL: + input_scroll(e->scroll_x, e->scroll_y); + break; + + case SAPP_EVENTTYPE_KEY_DOWN: + input_key(e->key_code, e->key_repeat ? 2 : 0); + break; + + case SAPP_EVENTTYPE_KEY_UP: + input_key(e->key_code, 1); + break; + + case SAPP_EVENTTYPE_CHAR: + input_key(e->char_code); + break; + } } int sim_playing() { return sim_play == SIM_PLAY; } @@ -306,3 +305,41 @@ void sim_step() { void set_timescale(float val) { timescale = val; } + +sapp_desc sokol_main(int argc, char **args) { + script_startup(); + + int argsize = 0; + for (int i = 1; i < argc; i++) { + argsize += strlen(args[i]); + if (argc > i+1) argsize++; + } + + char cmdstr[argsize]; + cmdstr[0] = '\0'; + + for (int i = 0; i < argc; i++) { + strcat(cmdstr, args[i]); + if (argc > i+1) strcat(cmdstr, " "); + } + + script_evalf("cmd_args('%s');", cmdstr); + + return (sapp_desc){ + .width = 720, + .height = 480, + .high_dpi = 0, + .sample_count = 8, + .fullscreen = 0, + .window_title = "Yugine", + .enable_clipboard = false, + .clipboard_size = 0, + .enable_dragndrop = true, + .max_dropped_files = 1, + .max_dropped_file_path_length = 2048, + .init_cb = c_init, + .frame_cb = c_frame, + .cleanup_cb = c_clean, + .event_cb = c_event, + }; +}