From 39310e7e2ec4d47658407d0fb980a874aaac8847 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sun, 23 Apr 2023 10:26:17 +0200 Subject: [PATCH] basic level loading --- src/chaos-drop.h | 42 +++++++++++++++++++++++++---- src/main.cc | 69 ++++++++++++++++++++++++++++++++++++----------- src/raytracing.cc | 16 ++++++----- 3 files changed, 99 insertions(+), 28 deletions(-) diff --git a/src/chaos-drop.h b/src/chaos-drop.h index ffc89e5..0ef7c6f 100644 --- a/src/chaos-drop.h +++ b/src/chaos-drop.h @@ -49,15 +49,47 @@ struct camera { int roll_quadrant; }; +struct element_mirror { + num begin, end; +}; +enum element_plane_type { + ELEMENT_PLANE_DAMAGE, + ELEMENT_PLANE_ARROW, +}; +struct element_plane { + num y; + uint32_t shape; + uint16_t type; + uint16_t data; /* ARROW: direction */ +}; +struct element_text { + num begin, end; + char const *str; +}; + +struct level { + int mirror_count; + struct element_mirror *mirrors; + + int plane_count; + struct element_plane *planes; + + int text_count; + struct element_text *texts; +}; + struct world { num16 neon_position; num16 neon_period; - bool mirror; - num mirror_begin, mirror_end; - - num object_plane_y; - char object_plane_shape[25]; + /* Current objects */ + num depth; + struct element_mirror *mirror; + struct element_plane *plane; + struct element_text *text; + int mirror_index; + int plane_index; + int text_index; }; void camera_set_fov(struct camera *camera, float fov_degrees); diff --git a/src/main.cc b/src/main.cc index 9181d9f..adab3f9 100644 --- a/src/main.cc +++ b/src/main.cc @@ -176,6 +176,7 @@ vec3 operator * (mat3 const &M, vec3 const &u) static struct camera *camera = NULL; static struct world *world = NULL; +static struct level *level = NULL; static bool debug = false; void render(void) @@ -203,6 +204,8 @@ int update(void) struct input input = {}; if(platform_update(&input)) return 1; + if(input.OPTN) + debug = !debug; static num const mv_speed = 0.45; static num const wall = WORLD_SIZE * 0.45; /* margin */ @@ -249,24 +252,59 @@ int update(void) camera_roll(camera, barrel_snap); camera_update_angles(camera); + world->depth += fall_speed; world->neon_position += world->neon_period - num16(fall_speed); world->neon_position %= world->neon_period; - if(world->mirror) { - world->mirror_begin -= fall_speed; - world->mirror_end -= fall_speed; + // Remove old world elements + + struct world *w = world; + + if(w->mirror && w->depth > w->mirror->end) + w->mirror = NULL; + if(w->plane && w->depth > w->plane->y) { + // TODO: Check collision + w->plane = NULL; } - if(world->mirror_end < 0) - world->mirror = false; + if(w->text && w->depth > w->text->end) + w->text = NULL; - if(world->object_plane_y >= 0) - world->object_plane_y -= fall_speed; + // Load incoming world elements + + if(!w->mirror && w->mirror_index < level->mirror_count + && level->mirrors[w->mirror_index].begin < w->depth + num(64)) { + w->mirror = &level->mirrors[w->mirror_index++]; + } + if(!w->plane && w->plane_index < level->plane_count + && level->planes[w->plane_index].y < w->depth + num(64)) { + w->plane = &level->planes[w->plane_index++]; + } + if(!w->text && w->text_index < level->text_count + && level->texts[w->text_index].begin < w->depth + num(64)) { + w->text = &level->texts[w->text_index++]; + } - if(input.OPTN) - debug = !debug; return 0; } +/* Basic basic level */ +struct level lv1 = { + .mirror_count = 1, + .mirrors = (struct element_mirror[]){ + { .begin = 256, .end = 512 }, + }, + .plane_count = 2, + .planes = (struct element_plane[]){ + { .y = 64, .shape = 0b01010'10101'01010'10101'01010, + .type = ELEMENT_PLANE_DAMAGE, .data = 0 }, + { .y = 128, .shape = 0b11111'10001'10001'10001'11111, + .type = ELEMENT_PLANE_DAMAGE, .data = 0 }, + }, + .text_count = 0, + .texts = (struct element_text[]){ + }, +}; + int main(void) { if(azur_init("Chaos Drop!", 3*VWIDTH, 3*VHEIGHT)) @@ -278,16 +316,15 @@ int main(void) camera_set_fov(camera, 80.0); camera_update_angles(camera); + level = &lv1; + struct world w = {}; w.neon_position = 0; + w.depth = 0; w.neon_period = 64; - w.mirror = true; - w.mirror_begin = 64; - w.mirror_end = 256; - w.object_plane_y = 32; - for(int y = 0; y < 5; y++) - for(int x = 0; x < 5; x++) - w.object_plane_shape[5*y+x] = (x & 1) != 0; + w.mirror = NULL; + w.plane = NULL; + w.text = NULL; world = &w; init(); diff --git a/src/raytracing.cc b/src/raytracing.cc index 8c5d88c..8c5f478 100644 --- a/src/raytracing.cc +++ b/src/raytracing.cc @@ -189,9 +189,10 @@ static int cast_ray(struct world const *world, svec3 const &origin, /* Compute intersection with object plane */ *collision_y = num(origin.y) + num(*t) * num(rayDir.y); - if(__builtin_expect(world->object_plane_y >= 0 && - (*collision_y > world->object_plane_y), 0)) { - snum plane_t = (snum(world->object_plane_y) - origin.y) / rayDir.y; + if(__builtin_expect(world->plane && + (*collision_y > world->plane->y - world->depth), 0)) { + snum plane_y = snum(world->plane->y - world->depth); + snum plane_t = (plane_y - origin.y) / rayDir.y; snum plane_x = origin.x + plane_t * rayDir.x; snum plane_z = origin.z + plane_t * rayDir.z; @@ -201,9 +202,9 @@ static int cast_ray(struct world const *world, svec3 const &origin, int cell_z = plane_z.ifloor(); if((unsigned)cell_x < 5 && (unsigned)cell_z < 5) { - if(world->object_plane_shape[5*cell_z + cell_x]) { + if((world->plane->shape >> (5*cell_z + cell_x)) & 1) { *t = plane_t; - *collision_y = world->object_plane_y; + *collision_y = world->plane->y - world->depth; return OBJECT_PLANE; } } @@ -249,8 +250,9 @@ void render_fragment(struct camera const *camera, struct world const *world, bool ok_z = collision.z >= snum(-WORLD_SIZE / 4) && collision.z < snum(WORLD_SIZE / 4); /* Mirror also does not go on forever */ - bool ok_y = coll_y >= world->mirror_begin - && coll_y < world->mirror_end; + bool ok_y = world->mirror + && coll_y >= (world->mirror->begin - world->depth) + && coll_y < (world->mirror->end - world->depth); if(ok_z && ok_y) { collision.x = origin.x + t * rayDir.x;