diff --git a/terrain.c b/terrain.c index e5249ac..62f8764 100644 --- a/terrain.c +++ b/terrain.c @@ -21,6 +21,8 @@ #define SWIDTH (WIDTH * SCREENSCALE) #define SHEIGHT (HEIGHT * SCREENSCALE) #define AVGWEIGHT 0.85 +#define DEFAULTUP \ + { .x = 0, .y = 1, .z = 0 } // These are initial values but there may be ways to change it #define CAM_INITPITCH MPI_2 @@ -34,7 +36,7 @@ { .x = 0, .y = 0, .z = -0.5 } // Some globals you can mess around with potentially -int fps = 60; +int fps = 30; // --------------------------------------------------- // The terrain ecs systems @@ -79,7 +81,7 @@ void sys_movement(ecs_placement *p, ecs_movement *m, tecs **ecs) { } // Perform the entire rendering of an object -void sys_renderobject(ecs_placement *p, ecs_object *o) { +void sys_renderobject(ecs_placement *p, ecs_object *o, tecs **ecs) { struct vec3 lighting; struct vec4 precalc_verts[H3D_OBJ_MAXVERTICES]; // The compiler is complaining about lighting beined used unitialized, even @@ -129,7 +131,6 @@ void sys_renderobject(ecs_placement *p, ecs_object *o) { } // Finally, actually render faces // ------------------------------------------------------------------ - int totaldrawn = 0; haloo3d_facef face, baseface; haloo3d_facef outfaces[H3D_FACEF_MAXCLIP]; for (int facei = 0; facei < o->model->numfaces; facei++) { @@ -154,7 +155,7 @@ void sys_renderobject(ecs_placement *p, ecs_object *o) { if (o->cullbackface && backface) { continue; } - totaldrawn++; + (*ecs)->totaldrawn++; // We still have to convert the points into the view haloo3d_facef_viewport_into_fast(outfaces[ti], o->context->precalc_halfwidth, @@ -168,11 +169,32 @@ void sys_renderobject(ecs_placement *p, ecs_object *o) { // return totaldrawn; } +void sys_playerinput(ecs_input *in) { + in->numevents = 0; + unigi_type_event event; + do { + unigi_event_get(&event); + switch (event.type) { + case unigi_enum_event_input_keyboard: + if (event.data.input_keyboard.down) { + switch (event.data.input_keyboard.button) { + // case KEY_SPACE: + // haloo3d_debugconsole_beginprompt(&dc); + // break; + default: + exit(0); + } + } + break; + } + in->numevents++; + } while (event.type != unigi_enum_event_none); +} + int main() { // int argc, char **argv) { srand(clock()); // Init unigi system. Can use anything here that can render to screen - unigi_type_event event; unigi_type_resolution res; res.width = SWIDTH; res.height = SHEIGHT; @@ -197,12 +219,12 @@ int main() { // int argc, char **argv) { haloo3d_print_initdefault(&pt, printbuf, sizeof(printbuf)); pt.fb = &screen; - haloo3d_easytimer frametimer, drawtimer, sdltimer, filltimer, logictimer; + haloo3d_easytimer frametimer, drawtimer, sdltimer; //, filltimer, logictimer; haloo3d_easytimer_init(&frametimer, AVGWEIGHT); haloo3d_easytimer_init(&drawtimer, AVGWEIGHT); haloo3d_easytimer_init(&sdltimer, AVGWEIGHT); - haloo3d_easytimer_init(&filltimer, AVGWEIGHT); - haloo3d_easytimer_init(&logictimer, AVGWEIGHT); + // haloo3d_easytimer_init(&filltimer, AVGWEIGHT); + // haloo3d_easytimer_init(&logictimer, AVGWEIGHT); render_context context; context.windowclear = 0xF000; @@ -218,72 +240,50 @@ int main() { // int argc, char **argv) { tecs_init(&ecs); int tecs_size = sizeof(tecs); - struct vec3 defup; - vec3(defup.v, 0, 1, 0); - ecs_eid playerid = tecs_newentity(&ecs, 0); ECS_SETCOMPONENT(&ecs, playerid, ecs_placement){ - .pos = {.x = 0, .y = 10, .z = 0}, .up = defup}; + .pos = {.x = 0, .y = 10, .z = 0}, .up = DEFAULTUP}; ECS_SETCOMPONENT(&ecs, playerid, ecs_movement){.pos = INIT_PLAYERSPEED}; ECS_SETCOMPONENT(&ecs, playerid, ecs_rotation){.yaw = 0, .pitch = CAM_INITPITCH}; ECS_SETCOMPONENT(&ecs, playerid, ecs_rendercontext) & context; + ECS_SETCOMPONENT(&ecs, playerid, ecs_input){}; ecs_placement *playerpos = &ECS_GETCOMPONENT(&ecs, playerid, ecs_placement); eprintf("Setup ECS system (%d bytes)\n", tecs_size); - // clock_t previous_clock = 0, this_clock; - // MAIN LOOP while (1) { haloo3d_easytimer_start(&frametimer); haloo3d_print_refresh(&pt); - - do { - unigi_event_get(&event); - switch (event.type) { - case unigi_enum_event_input_keyboard: - if (event.data.input_keyboard.down) { - switch (event.data.input_keyboard.button) { - // case KEY_SPACE: - // haloo3d_debugconsole_beginprompt(&dc); - // break; - default: - exit(0); - } - } - break; - } - } while (event.type != unigi_enum_event_none); - - // this_clock = clock(); + ecs.totaldrawn = 0; // ECS logic (which includes rendering) if (ecs.delta_s) { - // ecs.delta_s = (mfloat_t)(this_clock - previous_clock) / CLOCKS_PER_SEC; + ECS_RUNSYSTEM1(&ecs, sys_playerinput, ecs_input); ECS_RUNSYSTEM2(&ecs, sys_rotation, ecs_placement, ecs_rotation); ECS_RUNSYSTEM3(&ecs, sys_movement, ecs_placement, ecs_movement, tecs); ECS_RUNSYSTEM2(&ecs, sys_rendercontext, ecs_rendercontext, ecs_placement); - ECS_RUNSYSTEM2(&ecs, sys_renderobject, ecs_placement, ecs_object); + haloo3d_easytimer_start(&drawtimer); + ECS_RUNSYSTEM3(&ecs, sys_renderobject, ecs_placement, ecs_object, tecs); + haloo3d_easytimer_end(&drawtimer); } - // previous_clock = this_clock; - // Scale 3D into final buffer - haloo3d_easytimer_start(&filltimer); haloo3d_fb_fill(&screen, &context.window); - haloo3d_easytimer_end(&filltimer); // clang-format off haloo3d_print(&pt, "Pframe: %05.2f (%05.2f) DT: %0.3f\n" "PSDLFl: %05.2f (%05.2f)\n" - "Fill: %05.2f (%05.2f)\n" - "PlPos: %05.2f (%05.2f)\n", + "Render: %05.2f (%05.2f)\n" + "PlPos: %05.2f (%05.2f)\n" + "Tris: %d\n", frametimer.last * 1000, frametimer.sum * 1000, ecs.delta_s, sdltimer.last * 1000, sdltimer.sum * 1000, - filltimer.last * 1000, filltimer.sum * 1000, - playerpos->pos.x, playerpos->pos.z); + drawtimer.last * 1000, drawtimer.sum * 1000, + playerpos->pos.x, playerpos->pos.z, + ecs.totaldrawn); // clang-format on // Finally, actually put buffer onto screen @@ -300,7 +300,7 @@ int main() { // int argc, char **argv) { if (waittime > 0) { unigi_time_sleep(waittime * unigi_time_clocks_per_s); } - ecs.delta_s = frametimer.last + waittime; + ecs.delta_s = frametimer.last + MAX(waittime, 0); } haloo3d_fb_free(&screen); diff --git a/terrain_ecstypes.h b/terrain_ecstypes.h index d79c515..397f88e 100644 --- a/terrain_ecstypes.h +++ b/terrain_ecstypes.h @@ -76,6 +76,11 @@ typedef struct { mfloat_t pitch; } ecs_rotation; +// Type to track and mark some entity as receiving input events. +typedef struct { + int numevents; +} ecs_input; + // typedef haloo3d_camera *ecs_camera; // typedef haloo3d_obj_instance *ecs_object; @@ -100,12 +105,14 @@ typedef struct { // Setup ECS system for our game ECS_START(tecs) -float delta_s; // Store delta time in ecs +float delta_s; // Store delta time in ecs +int totaldrawn; // A place to store total drawn tris ECS_COMPONENT(ecs_rendercontext); ECS_COMPONENT(ecs_object); ECS_COMPONENT(ecs_placement); ECS_COMPONENT(ecs_rotation); ECS_COMPONENT(ecs_movement); +ECS_COMPONENT(ecs_input); ECS_END(tecs) ECS_FN_INIT(tecs) @@ -117,5 +124,6 @@ ECS_CID(ecs_object, 1); ECS_CID(ecs_placement, 2); ECS_CID(ecs_rotation, 3); ECS_CID(ecs_movement, 4); +ECS_CID(ecs_input, 5); #endif