From 5ae72062a23adc2500a90239763ebf3852458504 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 20 Dec 2022 00:15:38 +0000 Subject: [PATCH] Added pawn system for input via scheme --- source/engine/debug/debugdraw.c | 2 +- source/engine/editor/editor.c | 3 +- source/engine/gameobject.c | 24 +++++++++++++- source/engine/gameobject.h | 7 +++- source/engine/input.c | 11 ++++++- source/engine/input.h | 5 ++- source/engine/mrbffi.c | 57 ++++++++++++++++++++++++--------- source/engine/script.c | 6 ++++ source/scripts/engine.scm | 26 ++++++++------- source/shaders/circlevert.glsl | 4 +-- 10 files changed, 108 insertions(+), 37 deletions(-) diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index 0deb801..8bd7825 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -75,7 +75,7 @@ void draw_circle(int x, int y, float radius, int pixels) }; glBindBuffer(GL_ARRAY_BUFFER, circleVBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(verts), &verts, GL_DYNAMIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_DYNAMIC_DRAW); shader_setfloat(circleShader, "radius", radius); shader_setint(circleShader, "thickness", pixels); diff --git a/source/engine/editor/editor.c b/source/engine/editor/editor.c index f40ae8e..b59a77a 100644 --- a/source/engine/editor/editor.c +++ b/source/engine/editor/editor.c @@ -686,8 +686,7 @@ void editor_project_gui() { startobjectgui: if (selectedobject) { - draw_point(selectedobject->transform.position[0], - selectedobject->transform.position[1], 5); + draw_point(selectedobject->transform.position[0], selectedobject->transform.position[1], 5); NK_FORCE(gameobject) diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index 7169480..d0d2d04 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -25,6 +25,14 @@ struct gameobject *get_gameobject_from_id(int id) return &gameobjects[id]; } +int id_from_gameobject(struct gameobject *go) { + for (int i = 0; i < arrlen(gameobjects); i++) { + if (&gameobjects[i] == i) return i; + } + + return -1; +} + static void gameobject_setpickcolor(struct gameobject *go) { float r = ((go->editor.id & 0x000000FF) >> 0) / 255.f; @@ -114,7 +122,7 @@ void gameobject_save(struct gameobject *go, FILE * file) } } -void gameobject_makefromprefab(char *path) +int gameobject_makefromprefab(char *path) { FILE *fprefab = fopen(path, "rb"); if (fprefab == NULL) { @@ -129,6 +137,8 @@ void gameobject_makefromprefab(char *path) gameobject_init(new, fprefab); fclose(fprefab); + + return arrlen(gameobjects)-1; } void gameobject_init(struct gameobject *go, FILE * fprefab) @@ -228,6 +238,18 @@ void gameobject_rotate(struct gameobject *go, float as) cpBodySetAngle(go->body, a); } +void gameobject_setangle(struct gameobject *go, float angle) { + cpBodySetAngle(go->body, angle); +} + +void gameobject_setpos(struct gameobject *go, float x, float y) { + if (!go || !go->body) return; + cpVect p; + p.x = x; + p.y = y; + cpBodySetPosition(go->body, p); +} + void update_gameobjects() { for (int i = 0; i < arrlen(gameobjects); i++) gameobject_update(&gameobjects[i]); diff --git a/source/engine/gameobject.h b/source/engine/gameobject.h index 4a1b21e..56568d6 100644 --- a/source/engine/gameobject.h +++ b/source/engine/gameobject.h @@ -46,14 +46,17 @@ int number_of_gameobjects(); void set_n_gameobjects(int n); void setup_model_transform(struct mTransform *t, struct shader *s, float scale); void toggleprefab(struct gameobject *go); + struct gameobject *get_gameobject_from_id(int id); +int id_from_gameobject(struct gameobject *go); + void gameobject_save(struct gameobject *go, FILE * file); void gameobject_addcomponent(struct gameobject *go, struct component *c); void gameobject_delcomponent(struct gameobject *go, int n); void gameobject_loadcomponent(struct gameobject *go, int id); void gameobject_saveprefab(struct gameobject *go); -void gameobject_makefromprefab(char *path); +int gameobject_makefromprefab(char *path); void gameobject_syncprefabs(char *revertPath); void gameobject_revertprefab(struct gameobject *go); @@ -64,6 +67,8 @@ void update_gameobjects(); void gameobject_move(struct gameobject *go, float xs, float ys); void gameobject_rotate(struct gameobject *go, float as); +void gameobject_setangle(struct gameobject *go, float angle); +void gameobject_setpos(struct gameobject *go, float x, float y); void object_gui(struct gameobject *go); diff --git a/source/engine/input.c b/source/engine/input.c index 6768b6b..1620a8f 100644 --- a/source/engine/input.c +++ b/source/engine/input.c @@ -19,6 +19,13 @@ static int *downkeys = NULL; static int mquit = 0; +static s7_pointer *pawns = NULL; + +void set_pawn(s7_pointer menv) { + arrput(pawns, menv); + YughInfo("Now controling %d pawns.", arrlen(pawns)); +} + static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos) { xchange = (int)xpos - c_xpos; @@ -174,7 +181,9 @@ void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods } SCRIPTCALL: - script_call(keystr); + for (int i = 0; i < arrlen(pawns); i++) + script_eval_w_env(keystr, pawns[i]); + } void cursor_hide() diff --git a/source/engine/input.h b/source/engine/input.h index 77858db..b2d08b6 100644 --- a/source/engine/input.h +++ b/source/engine/input.h @@ -4,6 +4,8 @@ #include #include "window.h" +#include "script.h" + extern int32_t mouseWheelX; extern int32_t mouseWheelY; extern int ychange; @@ -26,7 +28,8 @@ void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods struct inputaction { int scancode; - }; +void set_pawn(s7_pointer env); + #endif diff --git a/source/engine/mrbffi.c b/source/engine/mrbffi.c index d09fcb4..8e21e26 100644 --- a/source/engine/mrbffi.c +++ b/source/engine/mrbffi.c @@ -10,6 +10,7 @@ #include "engine.h" #include "log.h" #include "input.h" +#include "gameobject.h" #include "s7.h" @@ -135,17 +136,19 @@ s7_pointer s7_gen_cmd(s7_scheme *sc, s7_pointer args) { /* 0 : load level */ /* 1: load prefab */ + int response = 0; + switch (cmd) { case 0: load_level(s); break; case 1: - gameobject_makefromprefab(s); + response = gameobject_makefromprefab(s); break; } - return args; + return s7_make_integer(sc, response); } s7_pointer s7_sys_cmd(s7_scheme *sc, s7_pointer args) { @@ -156,6 +159,8 @@ s7_pointer s7_sys_cmd(s7_scheme *sc, s7_pointer args) { quit(); break; } + + return args; } s7_pointer s7_sound_cmd(s7_scheme *sc, s7_pointer args) { @@ -195,24 +200,41 @@ s7_pointer s7_register(s7_scheme *sc, s7_pointer args) { return expr; } -/* -mrb_value mrb_nuke_cb(mrb_state *mrb, mrb_value self) { - mrb_float win; - mrb_sym cb; - mrb_get_args(mrb, "fn", &win, &cb); - window_i((int)win)->nuke_cb = cb; - return self; +s7_pointer s7_set_pawn(s7_scheme *sc, s7_pointer args) { + s7_pointer pawn = s7_car(args); + set_pawn(pawn); + return args; } -mrb_value mrb_gui_cb(mrb_state *mrb, mrb_value self) { - mrb_float win; - mrb_sym cb; - mrb_get_args(mrb, "fn", &win, &cb); - window_i((int)win)->gui_cb = cb; - return self; +s7_pointer s7_set_body(s7_scheme *sc, s7_pointer args) { + int id = s7_integer(s7_car(args)); + int cmd = s7_integer(s7_cadr(args)); + double val1; + double val2; + + switch (cmd) { + case 0: + val1 = s7_real(s7_caddr(args)); + gameobject_setangle(get_gameobject_from_id(id), val1); + break; + + case 1: + val1 = s7_real(s7_car(s7_caddr(args))); + val2 = s7_real(s7_cadr(s7_caddr(args))); + gameobject_setpos(get_gameobject_from_id(id), val1, val2); + break; + } + + return args; } -*/ +s7_pointer s7_set_body_pos(s7_scheme *sc, s7_pointer args) { + int id = s7_integer(s7_car(args)); + double x = s7_real(s7_cadr(args)); + double y = s7_real(s7_caddr(args)); + gameobject_setpos(get_gameobject_from_id(id), x, y); + return args; +} #define S7_FUNC(NAME, ARGS) s7_define_function(s7, #NAME, s7_ ##NAME, ARGS, 0, 0, "") @@ -232,5 +254,8 @@ void ffi_load() { S7_FUNC(sound_cmd, 2); S7_FUNC(gui_hook, 1); S7_FUNC(register, 2); + S7_FUNC(set_pawn, 1); + S7_FUNC(set_body, 3); + S7_FUNC(set_body_pos, 3); } diff --git a/source/engine/script.c b/source/engine/script.c index 5f1e9e1..6ca85c9 100644 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -86,6 +86,12 @@ void script_call(const char *f) { s7_call(s7, s7_name_to_value(s7, f), s7_nil(s7)); } +void script_eval_w_env(const char *s, s7_pointer env) { + char buffer[512]; + snprintf(buffer, 512-1, "(%s)", s); + s7_eval_c_string_with_environment(s7, buffer, env); +} + void script_call_sym(s7_pointer sym) { s7_call(s7, sym, s7_nil(s7)); diff --git a/source/scripts/engine.scm b/source/scripts/engine.scm index e1572b8..6879901 100644 --- a/source/scripts/engine.scm +++ b/source/scripts/engine.scm @@ -13,26 +13,16 @@ (define-macro (funcsrc func) `(funcinfo ,func 'source)) -;(define-macro (glog data lvl) -; (let ((z (gensym))) -; `(begin -; (define (,z) ()) -; (log ,lvl ,data (funcinfo ,z 'file) (funcinfo ,z 'line))))) - (define-macro (glog data lvl) `(log ,lvl ,data (port-filename (current-input-port)) (port-line-number (current-input-port)))) (define-macro (flog data lvl) `(log ,lvl ,data (funcinfo (*function*) 'file) (funcinfo (*function*) 'line))) -;(define-macro (glog data lvl) -; `(log ,lvl ,data (pair-line-number (cddr (procedure-source - (define (loginfo data) (glog data 0)) (define (logwarn data) (glog data 1)) (define (logerr data) (glog data 2)) (define (logcrit data) (glog data 3)) - (define (set_fps fps) (settings_cmd 0 (/ 1 fps))) (define (set_update fps) (settings_cmd 1 (/ 1 fps))) @@ -58,10 +48,24 @@ `(begin (define (,f) (begin . ,expr)) (register 0 ,f)))) - + (define-macro (while condition . body) (let ((loop (gensym))) `(let ,loop () (cond (,condition (begin . ,body) (,loop)))))) + +(define (clamp val min max) + (cond ((< val min) min) + ((> val max) max) + (else val))) + +(define (lerp s f dt) + (+ s (* (clamp dt 0 1) (- f s)))) + +(define (deg2rad deg) + (* deg 0.01745329252)) + +(define (body_angle! body angle) (set_body body 0 angle)) +(define (body_pos! body x y) (set_body_pos body x y)) diff --git a/source/shaders/circlevert.glsl b/source/shaders/circlevert.glsl index ffba2ab..c530cd6 100644 --- a/source/shaders/circlevert.glsl +++ b/source/shaders/circlevert.glsl @@ -1,5 +1,5 @@ #version 330 core -layout (location = 0) in vec4 vertex; +layout (location = 0) in vec2 vertex; out vec2 coords; layout (std140) uniform Projection @@ -9,7 +9,5 @@ layout (std140) uniform Projection void main() { - coords = vertex.zw; - gl_Position = projection * vec4(vertex.xy, 0.0, 1.0); } \ No newline at end of file