Vectorization

This commit is contained in:
John Alanbrook 2022-12-27 02:57:45 +00:00
parent 147cf1b05d
commit 9a77bb75a8
7 changed files with 78 additions and 56 deletions

View file

@ -11,6 +11,8 @@
#include <chipmunk/chipmunk_unsafe.h>
#include "stb_ds.h"
#include "script.h"
#include "log.h"
cpSpace *space = NULL;
@ -453,15 +455,14 @@ void phys2d_reindex_body(cpBody *body) {
static cpBool s7_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) {
struct gameobject *go = data;
script_call_sym(go->cbs->begin);
cpBody *body1;
cpBody *body2;
cpArbiterGetBodies(arb, &body1, &body2);
cpShape *shape1;
cpShape *shape2;
cpArbiterGetShapes(arb, &shape1, &shape2);
struct gameobject *g2 = cpBodyGetUserData(body2);
script_call_sym_args(go->cbs->begin, s7_make_integer(s7, g2->editor.id));
return 1;
}

View file

@ -61,9 +61,10 @@ int MakeGameobject()
strncpy(go.editor.mname, "New object", MAXNAME);
go.body = cpSpaceAddBody(space, cpBodyNew(go.mass, 1.f));
arrput(gameobjects, go);
cpBodySetUserData(go.body, &arrlast(gameobjects));
return arrlen(gameobjects)-1;
}
@ -148,6 +149,7 @@ void gameobject_init(struct gameobject *go, FILE * fprefab)
{
go->body = cpSpaceAddBody(space, cpBodyNew(go->mass, 1.f));
cpBodySetType(go->body, go->bodytype);
cpBodySetUserData(go->body, go);
int comp_n;
fread(&comp_n, sizeof(int), 1, fprefab);
@ -216,11 +218,11 @@ void toggleprefab(struct gameobject *go)
}
}
void gameobject_move(struct gameobject *go, float xs, float ys)
void gameobject_move(struct gameobject *go, cpVect vec)
{
cpVect p = cpBodyGetPosition(go->body);
p.x += xs;
p.y += ys;
p.x += vec.x;
p.y += vec.y;
cpBodySetPosition(go->body, p);
phys2d_reindex_body(go->body);
@ -241,12 +243,9 @@ void gameobject_setangle(struct gameobject *go, float angle) {
phys2d_reindex_body(go->body);
}
void gameobject_setpos(struct gameobject *go, float x, float y) {
void gameobject_setpos(struct gameobject *go, cpVect vec) {
if (!go || !go->body) return;
cpVect p;
p.x = x;
p.y = y;
cpBodySetPosition(go->body, p);
cpBodySetPosition(go->body, vec);
phys2d_reindex_body(go->body);
}

View file

@ -67,10 +67,10 @@ void gameobject_revertprefab(struct gameobject *go);
void gameobject_init(struct gameobject *go, FILE * fprefab);
void gameobject_move(struct gameobject *go, float xs, float ys);
void gameobject_move(struct gameobject *go, cpVect vec);
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 gameobject_setpos(struct gameobject *go, cpVect vec);
void gameobject_draw_debugs();

View file

@ -21,6 +21,15 @@
#include "nuke.h"
cpVect s7tovec2(s7_scheme *sc, s7_pointer s7vec) {
cpVect ret;
ret.x = s7_vector_ref(sc, s7vec, 0);
ret.y = s7_vector_ref(sc, s7vec, 1);
return ret;
}
extern s7_scheme *s7;
/* FFI */
@ -59,11 +68,10 @@ s7_pointer s7_ui_text(s7_scheme *sc, s7_pointer args) {
s7_pointer s7_gui_text(s7_scheme *sc, s7_pointer args) {
const char *s = s7_string(s7_car(args));
float pos[2];
pos[0] = s7_real(s7_cadr(args));
pos[1] = s7_real(s7_caddr(args));
s7_pointer s7pos = s7_cadr(args);
double pos[2] = { s7_vector_ref(sc, s7pos, 0), s7_vector_ref(sc, s7pos, 1) };
float size = s7_real(s7_cadddr(args));
float size = s7_real(s7_caddr(args));
const float white[3] = {1.f, 1.f, 1.f};
renderText(s, pos, size, white, 200);
@ -73,10 +81,10 @@ s7_pointer s7_gui_text(s7_scheme *sc, s7_pointer args) {
s7_pointer s7_gui_img(s7_scheme *sc, s7_pointer args) {
const char *img = s7_string(s7_car(args));
float x = s7_real(s7_cadr(args));
float y = s7_real(s7_caddr(args));
s7_pointer s7pos = s7_cadr(args);
double pos[2] = { s7_vector_ref(sc, s7pos, 0), s7_vector_ref(sc, s7pos, 1) };
gui_draw_img(img, x, y);
gui_draw_img(img, pos[0], pos[1]);
return args;
}
@ -127,9 +135,8 @@ s7_pointer s7_log(s7_scheme *sc, s7_pointer args) {
/* Call like (ui_rendertext "string" (xpos ypos) size) */
s7_pointer s7_ui_rendertext(s7_scheme *sc, s7_pointer args) {
const char *s = s7_string(s7_car(args));
double pos[2];
pos[0] = s7_real(s7_car(s7_cadr(args)));
pos[1] = s7_real(s7_cadr(s7_cadr(args)));
s7_pointer s7pos = s7_cadr(args);
double pos[2] = { s7_vector_ref(sc, s7pos, 0), s7_vector_ref(sc, s7pos, 1) };
double size = s7_real(s7_caddr(args));
double white[3] = {1.f, 1.f, 1.f};
@ -305,34 +312,24 @@ s7_pointer s7_set_pawn(s7_scheme *sc, s7_pointer args) {
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));
struct gameobject *go = get_gameobject_from_id(id);
switch (cmd) {
case 0:
gameobject_setangle(get_gameobject_from_id(id), s7_real(s7_caddr(args)));
gameobject_setangle(go, s7_real(s7_caddr(args)));
break;
case 1:
cpBodySetType(get_gameobject_from_id(id)->body, s7_integer(s7_caddr(args)));
break;
}
return args;
}
s7_pointer s7_set_body_pos(s7_scheme *sc, s7_pointer args) {
int id = s7_integer(s7_car(args));
int cmd = s7_integer(s7_cadr(args));
double x = s7_real(s7_caddr(args));
double y = s7_real(s7_cadddr(args));
switch (cmd) {
case 0:
gameobject_setpos(get_gameobject_from_id(id), x, y);
cpBodySetType(go->body, s7_integer(s7_caddr(args)));
break;
case 1:
gameobject_move(get_gameobject_from_id(id), x, y);
break;
case 2:
cpBodySetPosition(go, s7tovec2(sc, s7_caddr(args)));
break;
case 3:
gameobject_move(go, s7tovec2(sc, s7_caddr(args)));
break;
}
return args;
@ -353,10 +350,26 @@ s7_pointer s7_phys_q(s7_scheme *sc, s7_pointer args) {
struct gameobject * go = get_gameobject_from_id(s7_integer(s7_car(args)));
int q = s7_integer(s7_cadr(args));
s7_pointer ret;
/* Queries about a body
0: body type of static, dynamic, kinematic
1: body position
2: body rotation
*/
switch(q) {
case 0:
return s7_make_integer(sc, cpBodyGetType(go->body));
case 1:
ret = s7_make_vector(sc, 2);
s7_vector_set(sc, ret, 0, s7_make_real(sc, cpBodyGetPosition(go->body).x));
s7_vector_set(sc, ret, 1, s7_make_real(sc, cpBodyGetPosition(go->body).y));
return ret;
case 2:
return s7_make_real(sc, cpBodyGetAngle(go->body));
}
}
@ -406,7 +419,6 @@ void ffi_load() {
S7_FUNC(register, 2);
S7_FUNC(set_pawn, 1);
S7_FUNC(set_body, 3);
S7_FUNC(set_body_pos, 4);
S7_FUNC(phys_cmd, 3);
S7_FUNC(phys_q, 2);
S7_FUNC(phys_set, 3);

View file

@ -95,6 +95,11 @@ void script_call_sym(s7_pointer sym)
s7_call(s7, sym, s7_nil(s7));
}
void script_call_sym_args(s7_pointer sym, s7_pointer args)
{
s7_call(s7, sym, args);
}
int script_has_sym(s7_pointer sym) {
return 1;
}

View file

@ -3,6 +3,8 @@
#include "s7.h"
extern s7_scheme *s7;
void script_init();
void script_run(const char *script);
int script_dofile(const char *file);
@ -11,6 +13,7 @@ void script_draw();
void script_editor();
void script_call(const char *f);
void script_call_sym(s7_pointer sym);
void script_call_sym_args(s7_pointer sym, s7_pointer args);
int script_has_sym(s7_pointer sym);
void script_eval_w_env(const char *s, s7_pointer env);

View file

@ -93,15 +93,17 @@
(define (deg2rad deg)
(* deg 0.01745329252))
(define (body_angle! body angle) (set_body body 0 angle))
(define-macro (body_type! body type)
`(set_body ,body 1 ,(case type
((static) 2)
((dynamic) 0)
((kinematic) 1))))
(define (body_pos! body x y) (set_body_pos body 0 x y))
(define (body_move! body x y) (set_body_pos body 1 x y))
(define (body_angle! body angle) (set_body body 0 angle))
(define (body_pos! body pos) (set_body body 2 pos))
(define (body_move! body vec) (set_body body 3 vec))
(define (gravity! x y) (phys_set 0 x y))
@ -119,19 +121,19 @@
(define (bodytype? body) (phys_q body 0))
(define-macro (register-phys type body . expr)
(define-macro (register-phys type . expr)
(let ((f (gensym)))
`(begin
(define (,f) (begin . ,expr))
(phys_cmd ,body ,(case type
(define (,f hit) (begin . ,expr))
(phys_cmd body ,(case type
((collide) 0)
((separate) 3)) ,f))))
(define-macro (collide . expr)
`(register-phys collide body ,@expr))
`(register-phys collide ,@expr))
(define-macro (separate . expr)
`(register-phys separate body ,@expr))
`(register-phys separate ,@expr))
(define-macro (not! var)
`(set! ,var (not ,var)))
@ -146,10 +148,10 @@
(define-macro (input key . expr)
`(define (,(symbol "input_" (symbol->string key))) (begin . ,expr)))
(define-macro (+= var amt)
(define-macro (+=! var amt)
`(set! ,var (+ ,var ,amt)))
(define-macro (-= var amt)
(define-macro (-=! var amt)
`(set! ,var (- ,var ,amt)))
(define (attach-script script object)