From 52daa83f2f55b341bb4852d12af4d7c8566cb999 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Fri, 23 Aug 2024 20:11:04 -0400 Subject: [PATCH] Setup tetrahedron spawn (too many) --- haloo3d | 2 +- maze.c | 107 ++++++++++++++++++++++++++------------ maze_ecstypes.h | 1 + resources/tetrahedron.obj | 15 ++++++ 4 files changed, 90 insertions(+), 35 deletions(-) create mode 100644 resources/tetrahedron.obj diff --git a/haloo3d b/haloo3d index 72cdacd..917dfe4 160000 --- a/haloo3d +++ b/haloo3d @@ -1 +1 @@ -Subproject commit 72cdacd864f20c42a0965488eefc598bff650144 +Subproject commit 917dfe41aa118cefe36779c9bba7957160cc52e4 diff --git a/maze.c b/maze.c index 2d62d2b..b266891 100644 --- a/maze.c +++ b/maze.c @@ -46,11 +46,17 @@ // an 8 / 10 chance to go horizontal #define MAZEHBIAS 60 #define PRINTMAZE +// Max amount of flip polys to generate in maze. actual amount can be lower, +// but there will always at least be 1 +#define MAXFLIPPOLYS 17 +// Min space between flip polys +#define FLIPPOLYBUFFER 3 // Maze grows in the positive direction #define MAZENORTH 1 #define MAZEEAST 2 #define MAZEVISIT 4 +#define MAZEFLIP 8 // When you rightshift these values, you "turn right". // NOTE: north in this case is "towards the screen" because it moves in the @@ -68,8 +74,12 @@ #define ENDTEXTURE "resources/mazeend.ppm" #define PAINTINGTEXTURE "resources/specwall.ppm" #define MOUSETEXTURE "resources/mouse.ppm" +#define TETRAHEDRONFILE "resources/tetrahedron.obj" #define PAINTINGNAME "painting" +#define NUMPOLYS 1 +const char POLYNAMES[NUMPOLYS][20] = {"tetrahedron"}; + // Store all the values users can change at the beginning float ditherstart = -1; float ditherend = 8; @@ -222,10 +232,6 @@ void maze_generate(uint8_t *maze, int size, struct vec2i *start, end->y); } -// int rand_painting(int size) { -// return (rand() % ()) == 0; -// } - void create_painting(struct vec2i mazepos, uint8_t dir, haloo3d_easyinstancer *ins, mecs *ecs, ecs_world *world) { // Create the painting render instance, set up the ecs instance, etc. @@ -235,8 +241,10 @@ void create_painting(struct vec2i mazepos, uint8_t dir, .scale = &world->scaleto, .basescale = 1, .timer = &world->scaletimer}; - ECS_SETCOMPONENT(ecs, id, ecs_dieoninit){ - .obj = painting, .render = ins->render, .ws = world->state}; + ECS_SETCOMPONENT(ecs, id, ecs_dieoninit){.obj = painting, + .render = ins->render, + .ws = world->state, + .diefunc = NULL}; // Fix up some things based on dir. switch (dir) { case DIRNORTH: @@ -257,6 +265,28 @@ void create_painting(struct vec2i mazepos, uint8_t dir, // where the painting rotates around. } +void kill_flippoly(haloo3d_obj_instance *obj) { free(obj->lighting); } + +void create_flippoly(struct vec2i mazepos, haloo3d_easyinstancer *ins, + mecs *ecs, ecs_world *world) { + // Create the render instance, set up the ecs instance, etc. + haloo3d_obj_instance *poly = haloo3d_easyinstantiate(ins, POLYNAMES[0]); + vec3(poly->scale.v, 0.15, world->scaleto, 0.15); + vec3(poly->pos.v, (mazepos.x + 0.5) * world->state->cellsize, 0.35, + (mazepos.y + 0.5) * world->state->cellsize); + ecs_eid id = mecs_newentity(ecs, 0); + mallocordie(poly->lighting, sizeof(struct vec3)); + vec3(poly->lighting->v, 0, -MPI / 4, -1); + ECS_SETCOMPONENT(ecs, id, ecs_syncgrow){.obj = poly, + .scale = &world->scaleto, + .basescale = poly->scale.x, + .timer = &world->scaletimer}; + ECS_SETCOMPONENT(ecs, id, ecs_dieoninit){.obj = poly, + .render = ins->render, + .ws = world->state, + .diefunc = kill_flippoly}; +} + // Generate walls AND create paintings. Kind of doing too much void maze_wall_generate(uint8_t *maze, int size, haloo3d_obj *obj, haloo3d_easyinstancer *ins, mecs *ecs, @@ -288,34 +318,6 @@ void maze_wall_generate(uint8_t *maze, int size, haloo3d_obj *obj, } } -// // A simple linked list of objects -// struct objlist { -// haloo3d_obj_instance *obj; -// struct objlist *next; -// }; -// -// // Add a new link at the given elem -// struct objlist *objlist_add(struct objlist *elem, haloo3d_obj_instance *obj) -// { -// mallocordie(elem->next, sizeof(struct objlist *)); -// elem->next->obj = obj; -// elem->next->next = NULL; -// return elem->next; -// } -// -// // Free every single element starting at head -// void objlist_free(struct objlist *head, haloo3d_easyrender *r) { -// // void (*freefunc)(haloo3d_obj_instance *obj)) { -// if (head) { -// objlist_free(head->next, r); -// haloo3d_easyrender_deleteinstance(r, head->obj); -// // if (freefunc) { -// // freefunc(head->obj); -// //} -// free(head); -// } -// } - enum { WSTATE_INIT = 0, WSTATE_SPINUP = 1, @@ -399,6 +401,9 @@ void sys_dieoninit(ecs_dieoninit *die, mecs **ecs) { if (die->ws->state == WSTATE_INIT) { ecs_eid id = mecs_eid(ecs); eprintf("DELETING SELF: %d\n", id); + if (die->diefunc) { + die->diefunc(die->obj); + } // Trivially delete the entity from the renderer haloo3d_easyrender_deleteinstance(die->render, die->obj); // Delete ourselves from existence @@ -416,6 +421,26 @@ void sys_world(ecs_world *w, mecs **ecs) { &w->state->end); maze_wall_generate(w->state->maze, w->state->size, w->wallmodel, w->instancer, *ecs, w); + // SUPER simple flip poly generation + for (int i = 0; i < MAXFLIPPOLYS; i++) { + struct vec2i m = {.x = rand() % w->state->size, + .y = rand() % w->state->size}; + for (int yc = m.y - FLIPPOLYBUFFER; yc < m.y + FLIPPOLYBUFFER; yc++) { + if (yc < 0 || yc >= w->state->size) + continue; + for (int xc = m.x - FLIPPOLYBUFFER; xc < m.x + FLIPPOLYBUFFER; xc++) { + if (xc < 0 || xc >= w->state->size) + continue; + if (w->state->maze[m.x + m.y * w->state->size] & MAZEFLIP) { + goto SKIPFLIPPOLYADD; + } + } + } + eprintf("ADDING FLIPPOLY TO %d,%d\n", m.x, m.y); + create_flippoly(m, w->instancer, *ecs, w); + w->state->maze[m.x + m.y * w->state->size] |= MAZEFLIP; + SKIPFLIPPOLYADD:; + } eprintf("INIT MAZE COMPLETE, spinning up walls\n"); maze_to_pos(&w->state->end, w->endobj->pos.v, w->state->cellsize); w->state->state = WSTATE_SPINUP; @@ -690,6 +715,8 @@ void init_walltexture(haloo3d_fb *wallt) { void init_starttexture(haloo3d_fb *startt) { haloo3d_fb_init_tex(startt, 64, 128); + // Fill buffer with 0 + memset(startt->buffer, 0, startt->width * startt->height * sizeof(uint16_t)); haloo3d_recti rect = {.x1 = 4, .x2 = 60, .y1 = 58, .y2 = 71}; // Create a temporary printing system just to print to this. IDK, maybe I'll // make this betterin the future @@ -847,6 +874,7 @@ int main() { // int argc, char **argv) { render.camera.pos.y = 0.5; render.tprint.fb = &screen; render.trifunc = 2; // 2 is just better overall for now + render.autolightfix = 1; eprintf("Initialized renderer\n"); haloo3d_easyinstancer instancer = {.storage = &storage, .render = &render}; @@ -872,6 +900,17 @@ int main() { // int argc, char **argv) { haloo3d_fb *mouset = haloo3d_easystore_addtex(&storage, "mouse"); haloo3d_fb *paintingt = haloo3d_easystore_addtex(&storage, PAINTINGNAME); + for (int i = 0; i < NUMPOLYS; i++) { + haloo3d_fb *polyt = haloo3d_easystore_addtex(&storage, POLYNAMES[i]); + haloo3d_obj *polyo = haloo3d_easystore_addobj(&storage, "tetrahedron"); + switch (i) { + default: + haloo3d_obj_loadfile(polyo, TETRAHEDRONFILE); + break; + } + haloo3d_gen_solidtex(polyt, 0xFDDD); + } + haloo3d_gen_plane(planeo, MAZESIZE); haloo3d_gen_grid(wallo, MAZESIZE, 0); create_paintingobj(paintingo); diff --git a/maze_ecstypes.h b/maze_ecstypes.h index 17d6e24..c506c53 100644 --- a/maze_ecstypes.h +++ b/maze_ecstypes.h @@ -98,6 +98,7 @@ typedef struct { haloo3d_obj_instance *obj; haloo3d_easyrender *render; worldstate *ws; + void (*diefunc)(haloo3d_obj_instance *); } ecs_dieoninit; // Setup ECS system for our game diff --git a/resources/tetrahedron.obj b/resources/tetrahedron.obj new file mode 100644 index 0000000..83f9c14 --- /dev/null +++ b/resources/tetrahedron.obj @@ -0,0 +1,15 @@ +v -1 1 1 +v 1 1 -1 +v 1 -1 1 +v -1 -1 -1 + +# This is a textureless tetrahedron, but define a +# point anyway just in case +vt 0 0 + +# simple 4 faces. the tetrahedron doesn't face the camera in any +# meaningful way; it'll be up to you to make that work +f 1/1 3/1 2/1 +f 2/1 3/1 4/1 +f 1/1 4/1 3/1 +f 1/1 2/1 4/1