Add gameobject flipping; prefabs autoload

This commit is contained in:
John Alanbrook 2023-01-05 21:34:15 +00:00
parent 1521593269
commit e24eef6c8f
11 changed files with 42 additions and 21 deletions

View file

@ -17,7 +17,7 @@
#define INITIAL_HEAP_SIZE 4096 #define INITIAL_HEAP_SIZE 4096
#define INITIAL_STACK_SIZE 4096 #define INITIAL_STACK_SIZE 4096
#define DEFAULT_BIGNUM_PRECISION 128 #define DEFAULT_BIGNUM_PRECISION 128
#define WITH_PURE_S7 1 #define WITH_PURE_S7 0
#define WITH_SYSTEM_EXTRAS 0 #define WITH_SYSTEM_EXTRAS 0
#define WITH_C_LOADER 0 #define WITH_C_LOADER 0
#define WITH_NUMBER_SEPARATOR 1 #define WITH_NUMBER_SEPARATOR 1

View file

@ -66,5 +66,5 @@ void engine_init()
phys2d_init(); phys2d_init();
YughInfo("Starting sound ..."); YughInfo("Starting sound ...");
//sound_init(); sound_init();
} }

View file

@ -54,9 +54,8 @@ static void gameobject_setpickcolor(struct gameobject *go)
int MakeGameobject() int MakeGameobject()
{ {
if (gameobjects == NULL) arrsetcap(gameobjects, 100); if (gameobjects == NULL) arrsetcap(gameobjects, 5000);
YughInfo("Making new gameobject");
struct gameobject go = { struct gameobject go = {
.editor.id = arrlen(gameobjects), .editor.id = arrlen(gameobjects),
.scale = 1.f, .scale = 1.f,

View file

@ -34,6 +34,8 @@ struct gameobject {
float mass; float mass;
float f; /* friction */ float f; /* friction */
float e; /* elasticity */ float e; /* elasticity */
int flipx; /* 1 or -1 */
int flipy;
cpBody *body; cpBody *body;
struct component *components; struct component *components;
struct phys_cbs *cbs; struct phys_cbs *cbs;

View file

@ -344,6 +344,14 @@ s7_pointer s7_set_body(s7_scheme *sc, s7_pointer args) {
case 4: case 4:
cpBodyApplyImpulseAtWorldPoint(go->body, s7tovec2(sc, s7_caddr(args)), cpBodyGetPosition(go->body)); cpBodyApplyImpulseAtWorldPoint(go->body, s7tovec2(sc, s7_caddr(args)), cpBodyGetPosition(go->body));
break; break;
case 5:
go->flipx = s7_boolean(sc, s7_caddr(args)) ? -1 : 1;
break;
case 6:
go->flipy = s7_boolean(sc, s7_caddr(args)) ? -1 : 1;
break;
} }
return args; return args;
@ -499,8 +507,8 @@ s7_pointer s7_make_gameobject(s7_scheme *sc, s7_pointer args) {
go->mass = s7_real(s7_caddr(args)); go->mass = s7_real(s7_caddr(args));
go->f = s7_real(s7_cadddr(args)); go->f = s7_real(s7_cadddr(args));
go->e = s7_real(s7_list_ref(sc, args, 4)); go->e = s7_real(s7_list_ref(sc, args, 4));
go->flipx = s7_boolean(sc, s7_list_ref(sc, args, 5)) ? -1 : 1;
YughInfo("static %d, dynamic %d, kinematic %d", CP_BODY_TYPE_STATIC, CP_BODY_TYPE_DYNAMIC, CP_BODY_TYPE_KINEMATIC); go->flipy = s7_boolean(sc, s7_list_ref(sc, args, 6)) ? -1 : 1;
gameobject_apply(go); gameobject_apply(go);
@ -512,8 +520,6 @@ s7_pointer s7_make_sprite(s7_scheme *sc, s7_pointer args) {
const char *path = s7_string(s7_cadr(args)); const char *path = s7_string(s7_cadr(args));
cpVect pos = s7tovec2(sc, s7_caddr(args)); cpVect pos = s7tovec2(sc, s7_caddr(args));
YughInfo("Using gameid %d.", go);
struct sprite *sp = make_sprite(get_gameobject_from_id(go)); struct sprite *sp = make_sprite(get_gameobject_from_id(go));
sprite_loadtex(sp, path); sprite_loadtex(sp, path);
@ -595,7 +601,7 @@ void ffi_load() {
S7_FUNC(anim, 2); S7_FUNC(anim, 2);
S7_FUNC(anim_cmd, 3); S7_FUNC(anim_cmd, 3);
S7_FUNC(make_gameobject, 5); S7_FUNC(make_gameobject, 7);
S7_FUNC(make_sprite, 3); S7_FUNC(make_sprite, 3);
S7_FUNC(make_box2d, 3); S7_FUNC(make_box2d, 3);
S7_FUNC(make_circ2d, 3); S7_FUNC(make_circ2d, 3);

View file

@ -92,7 +92,7 @@ void openglInit()
glClearColor(editorClearColor[0], editorClearColor[1], editorClearColor[2], editorClearColor[3]); glClearColor(editorClearColor[0], editorClearColor[1], editorClearColor[2], editorClearColor[3]);
glEnable(GL_CULL_FACE); //glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); glCullFace(GL_BACK);
glEnable(GL_BLEND); glEnable(GL_BLEND);

View file

@ -5,6 +5,8 @@
#include "mrbffi.h" #include "mrbffi.h"
#include "ftw.h"
#include "stb_ds.h" #include "stb_ds.h"
s7_scheme *s7 = NULL; s7_scheme *s7 = NULL;
@ -52,11 +54,26 @@ static void my_print(s7_scheme *sc, uint8_t c, s7_pointer port) {
} }
} }
static int load_prefab(const char *fpath, const struct stat *sb, int typeflag) {
if (typeflag != FTW_F)
return 0;
if (!strcmp(".prefab", strrchr(fpath, '.')))
s7_load(s7, fpath);
return 0;
}
void script_init() { void script_init() {
s7 = s7_init(); s7 = s7_init();
s7_set_current_error_port(s7, s7_open_output_function(s7, my_err)); s7_set_current_error_port(s7, s7_open_output_function(s7, my_err));
s7_set_current_output_port(s7, s7_open_output_function(s7, my_print)); s7_set_current_output_port(s7, s7_open_output_function(s7, my_print));
ffi_load(); ffi_load();
/* Load all prefabs into memory */
script_dofile("scripts/engine.scm");
script_dofile("config.scm");
ftw(".", load_prefab, 10);
} }
void script_run(const char *script) { void script_run(const char *script) {

View file

@ -36,8 +36,6 @@ struct sprite *make_sprite(struct gameobject *go)
void sprite_init(struct sprite *sprite, struct gameobject *go) void sprite_init(struct sprite *sprite, struct gameobject *go)
{ {
sprite->go = go; sprite->go = go;
YughInfo("Added sprite address %p to sprite array %p.", sprite, &arrlast(sprites));
} }
void sprite_io(struct sprite *sprite, FILE *f, int read) void sprite_io(struct sprite *sprite, FILE *f, int read)
@ -120,7 +118,7 @@ void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], flo
memcpy(r_model, UNITMAT4, sizeof(UNITMAT4)); memcpy(r_model, UNITMAT4, sizeof(UNITMAT4));
memcpy(s_model, UNITMAT4, sizeof(UNITMAT4)); memcpy(s_model, UNITMAT4, sizeof(UNITMAT4));
mfloat_t t_scale[2] = { size[0] * tex->width, size[1] * tex->height }; mfloat_t t_scale[2] = { tex->width, tex->height };
mfloat_t t_offset[2] = { offset[0] * t_scale[0], offset[1] * t_scale[1] }; mfloat_t t_offset[2] = { offset[0] * t_scale[0], offset[1] * t_scale[1] };
mat4_translate_vec2(model, t_offset); mat4_translate_vec2(model, t_offset);
@ -142,9 +140,9 @@ void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], flo
float vertices[] = { float vertices[] = {
0.f, 0.f, r.s0, r.t1, 0.f, 0.f, r.s0, r.t1,
1.f, 0.f, r.s1, r.t1, size[0], 0.f, r.s1, r.t1,
0.f, 1.f, r.s0, r.t0, 0.f, size[1], r.s0, r.t0,
1.f, 1.f, r.s1, r.t0 size[0], size[1], r.s1, r.t0
}; };
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW);
@ -167,7 +165,7 @@ void sprite_draw(struct sprite *sprite)
//size[1] = sprite->tex->anim.dimensions[1] * sprite->go->scale; //size[1] = sprite->tex->anim.dimensions[1] * sprite->go->scale;
tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, anim_get_rect(&sprite->anim)); tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, anim_get_rect(&sprite->anim));
} else { } else {
float size[2] = { sprite->size[0] * sprite->go->scale, sprite->size[1] * sprite->go->scale }; float size[2] = { sprite->size[0] * sprite->go->scale * sprite->go->flipx, sprite->size[1] * sprite->go->scale * sprite->go->flipy };
tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, tex_get_rect(sprite->tex)); tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, tex_get_rect(sprite->tex));
} }

View file

@ -59,7 +59,7 @@ struct Texture *texture_pullfromfile(const char *path)
break; break;
} }
glTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);

View file

@ -157,9 +157,6 @@ int main(int argc, char **args) {
renderMS = 1.0/vidmode->refreshRate; renderMS = 1.0/vidmode->refreshRate;
script_dofile("scripts/engine.scm");
script_dofile("config.scm");
window_set_icon("icon.png"); window_set_icon("icon.png");
if (ed) { if (ed) {

View file

@ -109,6 +109,8 @@
(define (body_angle! body angle) (set_body body 0 angle)) (define (body_angle! body angle) (set_body body 0 angle))
(define (body_pos! body pos) (set_body body 2 pos)) (define (body_pos! body pos) (set_body body 2 pos))
(define (body_move! body vec) (set_body body 3 vec)) (define (body_move! body vec) (set_body body 3 vec))
(define (body_flipx! body flip) (set_body body 5 flip))
(define (body_flipy! body flip) (set_body body 6 flip))
(define (gravity! x y) (phys_set 0 x y)) (define (gravity! x y) (phys_set 0 x y))