From 622775492562fb3503c5647109f0d1cdeab9a283 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 16 Dec 2022 17:54:05 +0000 Subject: [PATCH] Added stringifying escape & F keys; add sound, sys FFI; unconflict time.h and timer.h --- source/engine/editor/editor.c | 2 +- source/engine/input.c | 60 ++++++++++++++++- source/engine/input.h | 4 +- source/engine/mrbffi.c | 119 ++++++++++++++++++---------------- source/engine/shader.c | 6 ++ source/engine/texture.c | 2 +- source/engine/timer.c | 4 +- source/engine/timer.h | 2 +- source/engine/window.c | 3 +- source/engine/yugine.c | 2 +- source/scripts/engine.scm | 11 ++++ 11 files changed, 149 insertions(+), 66 deletions(-) diff --git a/source/engine/editor/editor.c b/source/engine/editor/editor.c index 68e5c47..57a4802 100644 --- a/source/engine/editor/editor.c +++ b/source/engine/editor/editor.c @@ -280,7 +280,7 @@ static void edit_input_cb(GLFWwindow *w, int key, int scancode, int action, int switch (key) { case GLFW_KEY_ESCAPE: - quit = true; + quit(); //editor_save_projects(); editor_save(); break; diff --git a/source/engine/input.c b/source/engine/input.c index 665da49..a226a3a 100644 --- a/source/engine/input.c +++ b/source/engine/input.c @@ -3,19 +3,22 @@ #include #include "script.h" #include "stb_ds.h" +#include "log.h" int32_t mouseWheelX = 0; int32_t mouseWheelY = 0; int ychange = 0; int xchange = 0; float deltaT = 0; -int quit = 0; + static double c_xpos; static double c_ypos; static int *downkeys = NULL; +static int mquit = 0; + static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos) { xchange = (int)xpos - c_xpos; @@ -66,7 +69,51 @@ void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods { char keystr[50] = {'\0'}; strcat(keystr, "input_"); - strcat(keystr, glfwGetKeyName(key, 0)); + const char *kkey = glfwGetKeyName(key, scancode); + + if (!kkey) { + char keybuf[10]; + if (key > 289 && key < 302) { + sprintf(keybuf, "f%d", key-289); + } else { + switch(key) { + case GLFW_KEY_ENTER: + kkey = "enter"; + break; + + case GLFW_KEY_ESCAPE: + kkey = "escape"; + 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; + } + + strcat(keystr, kkey); + } + + + } else { + strcat(keystr, kkey); + } + switch (action) { case GLFW_PRESS: strcat(keystr, "_pressed"); @@ -135,3 +182,12 @@ int action_up(int scancode) return !found; } + +int want_quit() { + return mquit; +} + +void quit() { + YughInfo("Exiting game."); + mquit = 1; +} \ No newline at end of file diff --git a/source/engine/input.h b/source/engine/input.h index 5db5c36..77858db 100644 --- a/source/engine/input.h +++ b/source/engine/input.h @@ -9,7 +9,6 @@ extern int32_t mouseWheelY; extern int ychange; extern int xchange; extern float deltaT; -extern int quit; void input_init(); void input_poll(double wait); @@ -19,6 +18,9 @@ void cursor_show(); int action_down(int scancode); +int want_quit(); +void quit(); + void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods); struct inputaction diff --git a/source/engine/mrbffi.c b/source/engine/mrbffi.c index 56ffeac..3ec513d 100644 --- a/source/engine/mrbffi.c +++ b/source/engine/mrbffi.c @@ -9,6 +9,7 @@ #include "editor.h" #include "engine.h" #include "log.h" +#include "input.h" #include "s7.h" @@ -56,15 +57,15 @@ s7_pointer s7_settings_cmd(s7_scheme *sc, s7_pointer args) { YughInfo("Changing a setting."); switch(cmd) { case 0: // render fps - renderMS = (double)val; + renderMS = val; break; case 1: - updateMS = (double)val; + updateMS = val; break; case 2: - physMS = (double)val; + physMS = val; break; } @@ -118,27 +119,67 @@ s7_pointer s7_win_cmd(s7_scheme *sc, s7_pointer args) { return args; } - -/* - -mrb_value mrb_ui_begin(mrb_state *mrb, mrb_value self) { - char *title; - mrb_float w, h; - - mrb_get_args(mrb, "zff", &title, &w, &h); - - return mrb_bool_value(nk_begin(ctx, title, nk_rect(0,0,w,h), NK_WINDOW_TITLE|NK_WINDOW_BORDER|NK_WINDOW_SCALABLE|NK_WINDOW_MOVABLE|NK_WINDOW_NO_SCROLLBAR)); +s7_pointer s7_win_make(s7_scheme *sc, s7_pointer args) { + const char *title = s7_string(s7_car(args)); + int w = s7_integer(s7_cadr(args)); + int h = s7_integer(s7_caddr(args)); + struct window *win = MakeSDLWindow(title, w, h, 0); + return s7_make_integer(sc, win->id); } -*/ +s7_pointer s7_gen_cmd(s7_scheme *sc, s7_pointer args) { + int cmd = s7_integer(s7_car(args)); + const char *s = s7_string(s7_cadr(args)); -/* -mrb_value mrb_win_make(mrb_state *mrb, mrb_value self) { - char name[50] = "New Window"; - struct window *new = MakeSDLWindow(name, 500, 500, 0); - return mrb_float_value(mrb, new->id); + /* Branch table for general commands from scheme */ + /* 0 : load level */ + /* 1: load prefab */ + + switch (cmd) { + case 0: + load_level(s); + break; + + case 1: + gameobject_makefromprefab(s); + break; + } + + return args; } +s7_pointer s7_sys_cmd(s7_scheme *sc, s7_pointer args) { + int cmd = s7_integer(s7_car(args)); + + switch (cmd) { + case 0: + quit(); + break; + } +} + +s7_pointer s7_sound_cmd(s7_scheme *sc, s7_pointer args) { + int sound = s7_integer(s7_car(args)); + int cmd = s7_integer(s7_cadr(args)); + + switch (cmd) { + case 0: // play + break; + + case 1: // pause + break; + + case 2: // stop + break; + + case 3: // play from beginning + break; + } + + return args; +} + +/* mrb_value mrb_nuke_cb(mrb_state *mrb, mrb_value self) { mrb_float win; mrb_sym cb; @@ -155,42 +196,6 @@ mrb_value mrb_gui_cb(mrb_state *mrb, mrb_value self) { return self; } -mrb_value mrb_sound_make(mrb_state *mrb, mrb_value self) { - mrb_value vals; - mrb_get_args(mrb, "H", &vals); - char *name = mrb_str_to_cstr(mrb, mrb_hash_fetch(mrb, vals, mrb_symbol_value(mrb_intern_cstr(mrb, "name")), mrb_str_new_cstr(mrb, "New Window"))); - YughInfo("Window name is %s.", name); - return self; -} - -mrb_value mrb_sound_cmd(mrb_state *mrb, mrb_value self) { - mrb_float sound, cmd; - mrb_get_args(mrb, "ff", &sound, &cmd); - - - switch ((int)cmd) - { - case 0: // play - break; - - case 1: // pause - break; - - case 2: // stop - break; - - case 3: // play from beginning - break; - } - - return self; -} - - -mrb_value mrb_editor_render(mrb_state *mrb, mrb_value self) { - editor_render(); - return self; -} */ #define S7_FUNC(NAME, ARGS) s7_define_function(s7, #NAME, s7_ ##NAME, ARGS, 0, 0, "") @@ -205,5 +210,9 @@ void ffi_load() { S7_FUNC(win_cmd, 2); S7_FUNC(ui_rendertext, 3); S7_FUNC(log, 4); + S7_FUNC(win_make, 3); + S7_FUNC(gen_cmd, 2); + S7_FUNC(sys_cmd, 1); + S7_FUNC(sound_cmd, 2); } diff --git a/source/engine/shader.c b/source/engine/shader.c index 27ff786..f7767a1 100644 --- a/source/engine/shader.c +++ b/source/engine/shader.c @@ -10,6 +10,8 @@ #include "stb_ds.h" #include "timer.h" +#include "time.h" + #define SHADER_BUF 10000 static struct shader *shaders; @@ -89,6 +91,7 @@ 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); @@ -101,6 +104,9 @@ 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/texture.c b/source/engine/texture.c index bbb3716..8a6ff75 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -215,7 +215,7 @@ void anim_play(struct TexAnimation *anim) if (anim->timer == NULL) anim->timer = timer_make(1.f / anim->tex->anim.ms, tex_incr_anim, anim); else - timer_settime(anim->timer, 1.f/anim->tex->anim.ms); + timerr_settime(anim->timer, 1.f/anim->tex->anim.ms); timer_start(anim->timer); } diff --git a/source/engine/timer.c b/source/engine/timer.c index a9a1253..040daa9 100644 --- a/source/engine/timer.c +++ b/source/engine/timer.c @@ -77,12 +77,12 @@ void timer_remove(struct timer *t) { timers[i].timerid = i; } -void timer_settime(struct timer *t, double interval) { +void timerr_settime(struct timer *t, double interval) { //double elapsed = time - (t->fire_time - t->interval); t->interval = interval; t->remain_time = t->interval; - // TODO: timer_settime reacts to elapsed time + // TODO: timerr_settime reacts to elapsed time } void *arrfind(void *arr, int (*valid)(void *arr, void *cmp), void *cmp) diff --git a/source/engine/timer.h b/source/engine/timer.h index 72264a2..71364a1 100644 --- a/source/engine/timer.h +++ b/source/engine/timer.h @@ -19,7 +19,7 @@ void timer_start(struct timer *t); void timer_pause(struct timer *t); void timer_stop(struct timer *t); void timer_update(double s); -void timer_settime(struct timer *t, double interval); +void timerr_settime(struct timer *t, double interval); diff --git a/source/engine/window.c b/source/engine/window.c index ea42ff4..58f1f16 100644 --- a/source/engine/window.c +++ b/source/engine/window.c @@ -66,7 +66,7 @@ void window_framebuffer_size_cb(GLFWwindow *w, int width, int height) void window_close_callback(GLFWwindow *w) { - quit = 1; + quit(); } @@ -267,7 +267,6 @@ void window_seticon(struct window *w, struct Texture *icon) images[0].height = icon->height; images[0].pixels = icon->data; glfwSetWindowIcon(w->window, 1, images); - } int window_hasfocus(struct window *w) diff --git a/source/engine/yugine.c b/source/engine/yugine.c index fc49109..4c2b4bf 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -154,7 +154,7 @@ int main(int argc, char **args) { double lastTick; - while (!quit) { + while (!want_quit()) { double elapsed; elapsed = glfwGetTime() - lastTick; lastTick = glfwGetTime(); diff --git a/source/scripts/engine.scm b/source/scripts/engine.scm index 42cb601..53800ca 100644 --- a/source/scripts/engine.scm +++ b/source/scripts/engine.scm @@ -32,4 +32,15 @@ (define (win_fulltoggle w) (win_cmd w 0)) (define (win_fullscreen w) (win_cmd w 1)) (define (win_unfullscreen w) (win_cmd w 2)) +(define (win_title s) (win_cmd 0 3 s)) +(define (load_level s) (gen_cmd 0 s)) +(define (load_prefab s) (gen_cmd 1 s)) +(define (quit) (sys_cmd 0)) +(define (exit) (quit)) + + +(define (sound_play sound) (sound_cmd sound 0)) +(define (sound_pause sound) (sound_cmd sound 1)) +(define (sound_stop sound) (sound_cmd sound 2)) +(define (sound_restart sound) (sound_cmd sound 3))