Added pawn system for input via scheme

This commit is contained in:
John Alanbrook 2022-12-20 00:15:38 +00:00
parent 9bdb7ce9cb
commit 5ae72062a2
10 changed files with 108 additions and 37 deletions

View file

@ -75,7 +75,7 @@ void draw_circle(int x, int y, float radius, int pixels)
}; };
glBindBuffer(GL_ARRAY_BUFFER, circleVBO); 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_setfloat(circleShader, "radius", radius);
shader_setint(circleShader, "thickness", pixels); shader_setint(circleShader, "thickness", pixels);

View file

@ -686,8 +686,7 @@ void editor_project_gui() {
startobjectgui: startobjectgui:
if (selectedobject) { if (selectedobject) {
draw_point(selectedobject->transform.position[0], draw_point(selectedobject->transform.position[0], selectedobject->transform.position[1], 5);
selectedobject->transform.position[1], 5);
NK_FORCE(gameobject) NK_FORCE(gameobject)

View file

@ -25,6 +25,14 @@ struct gameobject *get_gameobject_from_id(int id)
return &gameobjects[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) static void gameobject_setpickcolor(struct gameobject *go)
{ {
float r = ((go->editor.id & 0x000000FF) >> 0) / 255.f; 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"); FILE *fprefab = fopen(path, "rb");
if (fprefab == NULL) { if (fprefab == NULL) {
@ -129,6 +137,8 @@ void gameobject_makefromprefab(char *path)
gameobject_init(new, fprefab); gameobject_init(new, fprefab);
fclose(fprefab); fclose(fprefab);
return arrlen(gameobjects)-1;
} }
void gameobject_init(struct gameobject *go, FILE * fprefab) void gameobject_init(struct gameobject *go, FILE * fprefab)
@ -228,6 +238,18 @@ void gameobject_rotate(struct gameobject *go, float as)
cpBodySetAngle(go->body, a); 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() { void update_gameobjects() {
for (int i = 0; i < arrlen(gameobjects); i++) for (int i = 0; i < arrlen(gameobjects); i++)
gameobject_update(&gameobjects[i]); gameobject_update(&gameobjects[i]);

View file

@ -46,14 +46,17 @@ int number_of_gameobjects();
void set_n_gameobjects(int n); void set_n_gameobjects(int n);
void setup_model_transform(struct mTransform *t, struct shader *s, float scale); void setup_model_transform(struct mTransform *t, struct shader *s, float scale);
void toggleprefab(struct gameobject *go); void toggleprefab(struct gameobject *go);
struct gameobject *get_gameobject_from_id(int id); 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_save(struct gameobject *go, FILE * file);
void gameobject_addcomponent(struct gameobject *go, struct component *c); void gameobject_addcomponent(struct gameobject *go, struct component *c);
void gameobject_delcomponent(struct gameobject *go, int n); void gameobject_delcomponent(struct gameobject *go, int n);
void gameobject_loadcomponent(struct gameobject *go, int id); void gameobject_loadcomponent(struct gameobject *go, int id);
void gameobject_saveprefab(struct gameobject *go); void gameobject_saveprefab(struct gameobject *go);
void gameobject_makefromprefab(char *path); int gameobject_makefromprefab(char *path);
void gameobject_syncprefabs(char *revertPath); void gameobject_syncprefabs(char *revertPath);
void gameobject_revertprefab(struct gameobject *go); 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_move(struct gameobject *go, float xs, float ys);
void gameobject_rotate(struct gameobject *go, float as); 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); void object_gui(struct gameobject *go);

View file

@ -19,6 +19,13 @@ static int *downkeys = NULL;
static int mquit = 0; 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) static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos)
{ {
xchange = (int)xpos - c_xpos; xchange = (int)xpos - c_xpos;
@ -174,7 +181,9 @@ void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods
} }
SCRIPTCALL: SCRIPTCALL:
script_call(keystr); for (int i = 0; i < arrlen(pawns); i++)
script_eval_w_env(keystr, pawns[i]);
} }
void cursor_hide() void cursor_hide()

View file

@ -4,6 +4,8 @@
#include <stdint.h> #include <stdint.h>
#include "window.h" #include "window.h"
#include "script.h"
extern int32_t mouseWheelX; extern int32_t mouseWheelX;
extern int32_t mouseWheelY; extern int32_t mouseWheelY;
extern int ychange; extern int ychange;
@ -26,7 +28,8 @@ void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods
struct inputaction struct inputaction
{ {
int scancode; int scancode;
}; };
void set_pawn(s7_pointer env);
#endif #endif

View file

@ -10,6 +10,7 @@
#include "engine.h" #include "engine.h"
#include "log.h" #include "log.h"
#include "input.h" #include "input.h"
#include "gameobject.h"
#include "s7.h" #include "s7.h"
@ -135,17 +136,19 @@ s7_pointer s7_gen_cmd(s7_scheme *sc, s7_pointer args) {
/* 0 : load level */ /* 0 : load level */
/* 1: load prefab */ /* 1: load prefab */
int response = 0;
switch (cmd) { switch (cmd) {
case 0: case 0:
load_level(s); load_level(s);
break; break;
case 1: case 1:
gameobject_makefromprefab(s); response = gameobject_makefromprefab(s);
break; break;
} }
return args; return s7_make_integer(sc, response);
} }
s7_pointer s7_sys_cmd(s7_scheme *sc, s7_pointer args) { 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(); quit();
break; break;
} }
return args;
} }
s7_pointer s7_sound_cmd(s7_scheme *sc, s7_pointer 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; return expr;
} }
/* s7_pointer s7_set_pawn(s7_scheme *sc, s7_pointer args) {
mrb_value mrb_nuke_cb(mrb_state *mrb, mrb_value self) { s7_pointer pawn = s7_car(args);
mrb_float win; set_pawn(pawn);
mrb_sym cb; return args;
mrb_get_args(mrb, "fn", &win, &cb);
window_i((int)win)->nuke_cb = cb;
return self;
} }
mrb_value mrb_gui_cb(mrb_state *mrb, mrb_value self) { s7_pointer s7_set_body(s7_scheme *sc, s7_pointer args) {
mrb_float win; int id = s7_integer(s7_car(args));
mrb_sym cb; int cmd = s7_integer(s7_cadr(args));
mrb_get_args(mrb, "fn", &win, &cb); double val1;
window_i((int)win)->gui_cb = cb; double val2;
return self;
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, "") #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(sound_cmd, 2);
S7_FUNC(gui_hook, 1); S7_FUNC(gui_hook, 1);
S7_FUNC(register, 2); S7_FUNC(register, 2);
S7_FUNC(set_pawn, 1);
S7_FUNC(set_body, 3);
S7_FUNC(set_body_pos, 3);
} }

View file

@ -86,6 +86,12 @@ void script_call(const char *f) {
s7_call(s7, s7_name_to_value(s7, f), s7_nil(s7)); 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) void script_call_sym(s7_pointer sym)
{ {
s7_call(s7, sym, s7_nil(s7)); s7_call(s7, sym, s7_nil(s7));

View file

@ -13,26 +13,16 @@
(define-macro (funcsrc func) (define-macro (funcsrc func)
`(funcinfo ,func 'source)) `(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) (define-macro (glog data lvl)
`(log ,lvl ,data (port-filename (current-input-port)) (port-line-number (current-input-port)))) `(log ,lvl ,data (port-filename (current-input-port)) (port-line-number (current-input-port))))
(define-macro (flog data lvl) (define-macro (flog data lvl)
`(log ,lvl ,data (funcinfo (*function*) 'file) (funcinfo (*function*) 'line))) `(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 (loginfo data) (glog data 0))
(define (logwarn data) (glog data 1)) (define (logwarn data) (glog data 1))
(define (logerr data) (glog data 2)) (define (logerr data) (glog data 2))
(define (logcrit data) (glog data 3)) (define (logcrit data) (glog data 3))
(define (set_fps fps) (settings_cmd 0 (/ 1 fps))) (define (set_fps fps) (settings_cmd 0 (/ 1 fps)))
(define (set_update fps) (settings_cmd 1 (/ 1 fps))) (define (set_update fps) (settings_cmd 1 (/ 1 fps)))
@ -58,10 +48,24 @@
`(begin `(begin
(define (,f) (begin . ,expr)) (define (,f) (begin . ,expr))
(register 0 ,f)))) (register 0 ,f))))
(define-macro (while condition . body) (define-macro (while condition . body)
(let ((loop (gensym))) (let ((loop (gensym)))
`(let ,loop () `(let ,loop ()
(cond (,condition (cond (,condition
(begin . ,body) (begin . ,body)
(,loop)))))) (,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))

View file

@ -1,5 +1,5 @@
#version 330 core #version 330 core
layout (location = 0) in vec4 vertex; layout (location = 0) in vec2 vertex;
out vec2 coords; out vec2 coords;
layout (std140) uniform Projection layout (std140) uniform Projection
@ -9,7 +9,5 @@ layout (std140) uniform Projection
void main() void main()
{ {
coords = vertex.zw;
gl_Position = projection * vec4(vertex.xy, 0.0, 1.0); gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);
} }