3dtoys/maze.c

507 lines
17 KiB
C
Raw Normal View History

2024-08-15 06:28:37 +00:00
#include "haloo3d/haloo3d.h"
2024-08-17 03:32:24 +00:00
#include "haloo3d/haloo3dex_console.h"
2024-08-15 06:28:37 +00:00
#include "haloo3d/haloo3dex_easy.h"
#include "haloo3d/haloo3dex_gen.h"
// #include "haloo3d/haloo3dex_img.h"
#include "haloo3d/haloo3dex_obj.h"
#include "haloo3d/haloo3dex_print.h"
#include "unigi/unigi.headers/src/main.h"
#include "unigi/unigi.platform.sdl1/src/main.c"
2024-08-18 04:30:29 +00:00
#include "ecs.h"
#include "ecs_comps.h"
2024-08-17 03:32:24 +00:00
#include "keys.h"
2024-08-15 06:28:37 +00:00
#include <stdlib.h>
2024-08-17 22:11:00 +00:00
// INteresting flags for performance comparisons
#define FASTFILL
2024-08-18 05:26:07 +00:00
#define WIDTH 320
#define HEIGHT 200
2024-08-15 06:28:37 +00:00
#define ASPECT ((float)WIDTH / HEIGHT)
2024-08-18 05:26:07 +00:00
#define SCREENSCALE 3
2024-08-17 10:06:02 +00:00
#define SWIDTH (WIDTH * SCREENSCALE)
#define SHEIGHT (HEIGHT * SCREENSCALE)
2024-08-15 06:28:37 +00:00
#define NEARCLIP 0.01
#define FARCLIP 100.0
#define LIGHTANG -MPI / 4.0
#define AVGWEIGHT 0.85
// Game options
2024-08-18 05:26:07 +00:00
#define MAZESIZE 15
2024-08-18 08:33:01 +00:00
#define HSCALE 1.0
2024-08-15 06:28:37 +00:00
2024-08-18 05:26:07 +00:00
// Maze grows in the positive direction
#define MAZENORTH 1
2024-08-17 10:06:02 +00:00
#define MAZEEAST 2
#define MAZEVISIT 4
2024-08-18 08:33:01 +00:00
// When you rightshift these values, you "turn right".
// NOTE: north in this case is "towards the screen" because it moves in the
// positive direction. In this case, it's actually wound in the opposite
// direction of what you'd expect
2024-08-18 05:26:07 +00:00
#define DIRNORTH 8
2024-08-18 08:33:01 +00:00
#define DIRWEST 4
2024-08-18 05:26:07 +00:00
#define DIRSOUTH 2
2024-08-18 08:33:01 +00:00
#define DIREAST 1
2024-08-18 05:26:07 +00:00
2024-08-18 08:33:01 +00:00
#define TURNRIGHT(d) (d == 1 ? 8 : (d >> 1))
#define TURNLEFT(d) (d == 8 ? 1 : (d << 1))
2024-08-17 10:06:02 +00:00
#define STACKPUSH(s, t, v) s[t++] = v;
2024-08-18 08:33:01 +00:00
// Store all the values users can change at the beginning
float ditherstart = -1;
float ditherend = 8;
float fov = 90.0;
float minlight = 0.25;
int fps = 30;
uint16_t sky = 0xF000;
// A general position within the maze, and a pointer to the maze itself.
// Can be used to traverse the maze
typedef struct {
uint8_t *maze;
struct vec2i pos; // tile position within maze
int size;
uint8_t dir; // facing dir (see DIREAST/DIRSOUTH/etc);
} ecs_maze;
// State for tracking ai moving through the maze.
typedef struct {
uint8_t state;
} ecs_smartai;
2024-08-18 05:26:07 +00:00
struct vec2i dirtovec(uint8_t dir) {
struct vec2i result;
switch (dir) {
case DIREAST:
vec2i(result.v, 1, 0);
break;
case DIRWEST:
vec2i(result.v, -1, 0);
break;
case DIRNORTH:
vec2i(result.v, 0, 1);
break;
case DIRSOUTH:
vec2i(result.v, 0, -1);
break;
default:
vec2i(result.v, 0, 0);
}
return result;
}
2024-08-18 04:30:29 +00:00
2024-08-17 10:06:02 +00:00
int maze_visited(uint8_t *maze, int x, int y, int size) {
return (maze[x + y * size] & MAZEVISIT) > 0;
}
2024-08-18 05:26:07 +00:00
int maze_connected(uint8_t *maze, int x, int y, int size, uint8_t move) {
2024-08-18 08:33:01 +00:00
eprintf("CHECKING DIR %d at (%d,%d), it is %d\n", move, x, y,
maze[x + y * size]);
2024-08-18 05:26:07 +00:00
if (move == DIREAST) {
2024-08-17 10:06:02 +00:00
return (maze[x + y * size] & MAZEEAST) == 0;
2024-08-18 05:26:07 +00:00
} else if (move == DIRWEST) {
2024-08-17 10:06:02 +00:00
return (x > 0) && ((maze[x - 1 + y * size] & MAZEEAST) == 0);
2024-08-18 05:26:07 +00:00
} else if (move == DIRNORTH) {
return (maze[x + y * size] & MAZENORTH) == 0;
} else if (move == DIRSOUTH) {
return (y > 0) && ((maze[x + (y - 1) * size] & MAZENORTH) == 0);
2024-08-17 10:06:02 +00:00
}
return 0;
}
// Generate a (square) maze. Utilize one bit of the maze (#2) to
// indicate whether it is visited
void maze_generate(uint8_t *maze, int size) {
const int mazesquare = (size) * (size);
for (int i = 0; i < mazesquare; i++) {
2024-08-18 05:26:07 +00:00
maze[i] = MAZENORTH | MAZEEAST;
2024-08-17 10:06:02 +00:00
}
2024-08-16 08:30:28 +00:00
int *mazestack;
2024-08-17 10:06:02 +00:00
mallocordie(mazestack, sizeof(int) * mazesquare);
2024-08-16 08:30:28 +00:00
for (int i = 0; i < size * size; i++) {
mazestack[i] = -1;
}
// Push current cell onto stack, mark as visited
int x = size / 2;
int y = size / 2;
2024-08-17 10:06:02 +00:00
int mazetop = 0;
STACKPUSH(mazestack, mazetop, x + y * size);
maze[x + y * size] |= MAZEVISIT;
2024-08-18 05:26:07 +00:00
// struct vec2i visitable[4];
uint8_t visitable[4];
2024-08-17 10:06:02 +00:00
int visittop = 0;
2024-08-16 08:30:28 +00:00
// Now let's make a maze!
while (mazetop) {
mazetop--;
2024-08-17 10:06:02 +00:00
visittop = 0;
2024-08-16 08:30:28 +00:00
x = mazestack[mazetop] % size;
y = mazestack[mazetop] / size;
2024-08-17 10:06:02 +00:00
if (x > 0 && !maze_visited(maze, x - 1, y, size)) {
2024-08-18 05:26:07 +00:00
visitable[visittop++] = DIRWEST;
2024-08-16 08:30:28 +00:00
}
2024-08-17 10:06:02 +00:00
if (x < size - 1 && !maze_visited(maze, x + 1, y, size)) {
2024-08-18 05:26:07 +00:00
visitable[visittop++] = DIREAST;
2024-08-16 08:30:28 +00:00
}
2024-08-17 10:06:02 +00:00
if (y > 0 && !maze_visited(maze, x, y - 1, size)) {
2024-08-18 05:26:07 +00:00
visitable[visittop++] = DIRSOUTH;
2024-08-16 08:30:28 +00:00
}
2024-08-17 10:06:02 +00:00
if (y < size - 1 && !maze_visited(maze, x, y + 1, size)) {
2024-08-18 05:26:07 +00:00
visitable[visittop++] = DIRNORTH;
2024-08-16 08:30:28 +00:00
}
// You can generate a random location!
2024-08-17 10:06:02 +00:00
if (visittop) {
// Readd ourselves, we're moving
STACKPUSH(mazestack, mazetop, x + y * size);
2024-08-18 05:26:07 +00:00
uint8_t dir = visitable[rand() % visittop];
struct vec2i movedir = dirtovec(dir);
2024-08-16 08:30:28 +00:00
int nx = x + movedir.x;
int ny = y + movedir.y;
2024-08-17 10:06:02 +00:00
// Trust that the visitable array is always valid
2024-08-18 05:26:07 +00:00
if (dir == DIREAST) { // Tear down east wall
2024-08-17 10:06:02 +00:00
maze[x + y * size] &= ~MAZEEAST;
2024-08-18 05:26:07 +00:00
} else if (dir == DIRWEST) { // Move left and tear down east wall
2024-08-17 10:06:02 +00:00
maze[(x - 1) + y * size] &= ~MAZEEAST;
2024-08-18 05:26:07 +00:00
} else if (dir == DIRNORTH) { // tear down north wall
maze[x + y * size] &= ~MAZENORTH;
} else if (dir == DIRSOUTH) { // move down and tear down north wall
maze[x + (y - 1) * size] &= ~MAZENORTH;
2024-08-17 10:06:02 +00:00
}
2024-08-16 08:30:28 +00:00
// Push onto stack and set visited
2024-08-17 10:06:02 +00:00
STACKPUSH(mazestack, mazetop, nx + ny * size);
maze[nx + ny * size] |= MAZEVISIT;
2024-08-16 08:30:28 +00:00
}
}
free(mazestack);
}
2024-08-17 10:06:02 +00:00
void maze_wall_generate(uint8_t *maze, int size, haloo3d_obj *obj) {
2024-08-18 05:26:07 +00:00
// Simple: for each cell, we check if north or east is a wall. If so,
2024-08-18 08:33:01 +00:00
// generate it. Also, generate walls for the south and west global wall
2024-08-17 10:06:02 +00:00
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
2024-08-18 05:26:07 +00:00
if (!maze_connected(maze, x, y, size, DIREAST)) {
haloo3d_gen_grid_quad(obj, x, y, dirtovec(DIREAST));
2024-08-17 10:06:02 +00:00
}
2024-08-18 05:26:07 +00:00
if (!maze_connected(maze, x, y, size, DIRNORTH)) {
haloo3d_gen_grid_quad(obj, x, y, dirtovec(DIRNORTH));
2024-08-17 10:06:02 +00:00
}
}
}
2024-08-17 10:40:23 +00:00
for (int i = 0; i < size; i++) {
2024-08-18 05:26:07 +00:00
haloo3d_gen_grid_quad(obj, i, 0, dirtovec(DIRSOUTH));
haloo3d_gen_grid_quad(obj, 0, i, dirtovec(DIRWEST));
2024-08-17 10:40:23 +00:00
}
2024-08-17 10:06:02 +00:00
}
2024-08-18 08:33:01 +00:00
// everything in the maze is controlled by the CPU. As such, movement
// is as simple as "go here by this time". No need to complicate the
// components?
static void sys_ecs_smartai(haloo_ecs *ecs, hecs_eidt id, hecs_cidt said,
hecs_cidt mzid, hecs_cidt mtid, hecs_cidt rtid) {
ecs_smartai *smartai = HECS_ENTITYCOMPONENT(ecs_smartai *, id, said, ecs);
ecs_maze *maze = HECS_ENTITYCOMPONENT(ecs_maze *, id, mzid, ecs);
ecs_moveto *mt = HECS_ENTITYCOMPONENT(ecs_moveto *, id, mtid, ecs);
ecs_rotateto *rt = HECS_ENTITYCOMPONENT(ecs_rotateto *, id, rtid, ecs);
if (mt->timer == 0) { // && rt->timer == 0) {
eprintf("SMARTAI: %d DIR: %d POS: (%f, %f)\n", smartai->state, maze->dir,
mt->pos.x, mt->pos.z);
// We can only do things if we're not moving and not rotating
// First, we see if we can turn right. If so, go for it.
uint8_t newdir = TURNRIGHT(maze->dir);
if (smartai->state == 0 && rt->timer == 0 &&
maze_connected(maze->maze, maze->pos.x, maze->pos.y, maze->size,
newdir)) {
rt->dstrot.x = rt->rot.x + MPI_2;
rt->timer = fps / 2;
maze->dir = newdir;
smartai->state = 2;
// We are turning and want to move forward, do not turn right again
eprintf("TURN RIGHT TO: %d\n", maze->dir);
return;
}
// -=------------ SEPARATE
// Now, we see if we can move in the direction we're now facing. If so,
// begin an animation to move there. Otherwise, turn left
if (!maze_connected(maze->maze, maze->pos.x, maze->pos.y, maze->size,
maze->dir)) {
if (rt->timer == 0) {
rt->dstrot.x = rt->rot.x - MPI_2;
rt->timer = fps / 2;
maze->dir = TURNLEFT(maze->dir);
eprintf("TURN LEFT (stuck): %d\n", maze->dir);
smartai->state = 1; // We are stuck, do not turn right
}
} else {
// Move in the direction
struct vec2i movement = dirtovec(maze->dir);
maze->pos.x += movement.x;
maze->pos.y += movement.y;
mt->timer = fps / 2;
mt->dst.x += movement.x;
mt->dst.z += movement.y;
smartai->state = 0; // We are no longer stuck, you can turn right
}
}
}
2024-08-15 06:28:37 +00:00
2024-08-18 08:33:01 +00:00
int main() { // int argc, char **argv) {
2024-08-17 03:32:24 +00:00
2024-08-15 06:28:37 +00:00
haloo3d_easystore storage;
haloo3d_easystore_init(&storage);
2024-08-17 03:32:24 +00:00
haloo3d_debugconsole dc;
haloo3d_debugconsole_init(&dc);
2024-08-16 07:10:49 +00:00
haloo3d_fb screen;
haloo3d_fb_init(&screen, SWIDTH, SHEIGHT);
2024-08-15 06:28:37 +00:00
haloo3d_easyrender render;
haloo3d_easyrender_init(&render, WIDTH, HEIGHT);
2024-08-15 07:33:11 +00:00
render.camera.pos.y = 0.5;
eprintf("Initialized renderer\n");
2024-08-15 06:28:37 +00:00
2024-08-17 03:32:24 +00:00
haloo3d_debugconsole_set(&dc, "render/fps.i", &fps);
haloo3d_debugconsole_set(&dc, "render/fov.f", &fov);
2024-08-18 08:33:01 +00:00
haloo3d_debugconsole_set(&dc, "render/trifunc.i", &render.trifunc);
2024-08-17 03:32:24 +00:00
haloo3d_debugconsole_set(&dc, "render/ditherstart.f", &ditherstart);
haloo3d_debugconsole_set(&dc, "render/ditherend.f", &ditherend);
haloo3d_debugconsole_set(&dc, "render/sky.u16x", &sky);
2024-08-17 10:06:02 +00:00
haloo3d_debugconsole_set(&dc, "camera/pos_y.f", &render.camera.pos.y);
haloo3d_debugconsole_set(&dc, "camera/pitch.f", &render.camera.pitch);
2024-08-17 03:32:24 +00:00
2024-08-16 07:10:49 +00:00
render.tprint.fb = &screen;
2024-08-17 22:11:00 +00:00
haloo3d_easytimer frametimer, sdltimer, filltimer;
2024-08-15 07:33:11 +00:00
haloo3d_easytimer_init(&frametimer, AVGWEIGHT);
2024-08-15 07:53:13 +00:00
haloo3d_easytimer_init(&sdltimer, AVGWEIGHT);
2024-08-17 22:11:00 +00:00
haloo3d_easytimer_init(&filltimer, AVGWEIGHT);
2024-08-15 06:28:37 +00:00
// Load the junk + generate stuff
haloo3d_obj *flooro = haloo3d_easystore_addobj(&storage, "floor");
haloo3d_obj *ceilo = haloo3d_easystore_addobj(&storage, "ceiling");
2024-08-16 07:10:49 +00:00
haloo3d_obj *wallo = haloo3d_easystore_addobj(&storage, "walls");
2024-08-15 06:28:37 +00:00
haloo3d_fb *floort = haloo3d_easystore_addtex(&storage, "floor");
haloo3d_fb *ceilt = haloo3d_easystore_addtex(&storage, "ceiling");
2024-08-16 07:10:49 +00:00
haloo3d_fb *wallt = haloo3d_easystore_addtex(&storage, "walls");
2024-08-15 06:28:37 +00:00
haloo3d_gen_plane(flooro, MAZESIZE);
haloo3d_gen_plane(ceilo, MAZESIZE);
2024-08-17 10:06:02 +00:00
haloo3d_gen_grid(wallo, MAZESIZE, 0);
// no generic maze generator, we just do it raw. Each cell has a byte which
// indicates if the wall to the NORTH (#0 bit) and the wall to the WEST (#1
// bit) are solid. Because of this, we need one additional row and column
uint8_t maze[MAZESIZE * MAZESIZE];
maze_generate(maze, MAZESIZE);
maze_wall_generate(maze, MAZESIZE, wallo);
2024-08-16 07:10:49 +00:00
2024-08-15 06:28:37 +00:00
uint16_t cols[4] = {0xFD93, 0xFB83, 0xFEEE, 0xFDDD};
2024-08-16 07:10:49 +00:00
haloo3d_fb_init_tex(floort, 64, 64);
haloo3d_apply_alternating(floort, cols, 1);
haloo3d_apply_noise(floort, NULL, 1.0 / 6);
// haloo3d_apply_alternating(floort, cols, 2);
haloo3d_fb_init_tex(ceilt, 16, 16);
haloo3d_apply_alternating(ceilt, cols + 2, 2);
haloo3d_fb_init_tex(wallt, 64, 64);
uint16_t wallcols[] = {0xFA22};
haloo3d_apply_alternating(wallt, wallcols, 1);
haloo3d_apply_noise(wallt, NULL, 1.0 / 8);
haloo3d_apply_brick(wallt, 16, 11, 0xFEEE);
// haloo3d_apply_brick(wallt, 14, 8, 0xFD94);
haloo3d_apply_noise(wallt, NULL, 1.0 / 8);
2024-08-15 07:33:11 +00:00
eprintf("Initialized models and textures\n");
2024-08-15 06:28:37 +00:00
// Lighting. Note that for performance, the lighting is always calculated
// against the base model, and is thus not realistic if the object rotates in
// the world. This can be fixed easily, since each object gets its own
// lighting vector, which can easily be rotated in the opposite direction of
// the model
struct vec3 light;
vec3(light.v, 0, -MCOS(LIGHTANG), MSIN(LIGHTANG));
haloo3d_obj_instance *floori =
haloo3d_easyrender_addinstance(&render, flooro, floort);
2024-08-16 07:10:49 +00:00
haloo3d_obj_instance *walli =
haloo3d_easyrender_addinstance(&render, wallo, wallt);
2024-08-15 06:28:37 +00:00
haloo3d_obj_instance *ceili =
haloo3d_easyrender_addinstance(&render, ceilo, ceilt);
2024-08-15 07:33:11 +00:00
floori->cullbackface = 0;
ceili->cullbackface = 0;
2024-08-16 07:10:49 +00:00
walli->cullbackface = 0;
vec3(floori->scale.v, HSCALE, 1, HSCALE);
vec3(ceili->scale.v, HSCALE, 1, HSCALE);
2024-08-18 08:33:01 +00:00
vec3(walli->scale.v, HSCALE, 1, HSCALE);
// vec3(walli->scale.v, HSCALE, 0, HSCALE);
2024-08-16 08:30:28 +00:00
ceili->pos.y = 1; //-1;
2024-08-15 07:33:11 +00:00
eprintf("Setup all object instances\n");
2024-08-15 06:28:37 +00:00
unigi_type_event event;
unigi_type_resolution res;
2024-08-16 07:10:49 +00:00
res.width = SWIDTH;
res.height = SHEIGHT;
2024-08-15 08:32:40 +00:00
res.depth = 0;
2024-08-15 06:28:37 +00:00
int totaldrawn = 0;
2024-08-15 07:33:11 +00:00
eprintf("Scene has %d tris, %d verts\n", render.totalfaces,
render.totalverts);
2024-08-15 06:28:37 +00:00
// Init unigi system
2024-08-15 07:33:11 +00:00
// sprintf(render.printbuf, "maze.exe - %s %s %s", argv[1], argv[2], argv[3]);
2024-08-15 06:28:37 +00:00
unigi_graphics_init();
2024-08-15 07:33:11 +00:00
unigi_window_create(res, "maze.exe"); // render.printbuf);
2024-08-15 06:28:37 +00:00
2024-08-18 08:33:01 +00:00
// render.camera.pos.y = 4; // 5;
// render.camera.pitch = MPI - 0.1; // 2.2;
// ceili->pos.y = -10;
2024-08-17 10:06:02 +00:00
haloo3d_debugconsole_set(&dc, "obj/ceil/pos_y.f", &ceili->pos.y);
2024-08-16 08:30:28 +00:00
2024-08-18 04:30:29 +00:00
// Set up the various systems
haloo_ecs ecs;
haloo_ecs_init(&ecs);
HECS_ADDNEWCOMPONENT(ecs_moveto, &ecs);
2024-08-18 08:33:01 +00:00
HECS_ADDNEWCOMPONENT(ecs_rotateto, &ecs);
2024-08-18 04:30:29 +00:00
HECS_ADDNEWCOMPONENT(ecs_maze, &ecs);
2024-08-18 08:33:01 +00:00
// HECS_ADDNEWCOMPONENT(ecs_objin, &ecs);
2024-08-18 04:30:29 +00:00
HECS_ADDNEWCOMPONENT(ecs_camera, &ecs);
2024-08-18 08:33:01 +00:00
HECS_ADDNEWCOMPONENT(ecs_smartai, &ecs);
2024-08-18 04:30:29 +00:00
hecs_eidt playerid = haloo_ecs_newentity(&ecs, 0);
2024-08-18 08:33:01 +00:00
if (playerid == -1) {
dieerr("WHY IS PLAYERID -1???\n");
}
eprintf("Player eid: %d\n", playerid);
2024-08-18 04:30:29 +00:00
2024-08-18 08:33:01 +00:00
// This is VERY critical: the player start is at 0 0 but in the maze that's
// the center of the maze! This should probably be fixed or something!
struct vec2i playerstart = {.x = MAZESIZE / 2, .y = MAZESIZE / 2};
struct vec2 playerrotation = {.x = render.camera.yaw,
.y = render.camera.pitch};
2024-08-18 04:30:29 +00:00
HECS_SETCOMPONENT(ecs_moveto, &ecs, playerid){
.pos = render.camera.pos, .dst = render.camera.pos, .timer = 0};
2024-08-18 08:33:01 +00:00
HECS_SETCOMPONENT(ecs_rotateto, &ecs, playerid){
.rot = playerrotation, .dstrot = playerrotation, .timer = 0};
HECS_SETCOMPONENT(ecs_maze, &ecs, playerid){
.maze = maze, .pos = playerstart, .size = MAZESIZE, .dir = DIRSOUTH};
2024-08-18 04:30:29 +00:00
HECS_SETCOMPONENT(ecs_camera, &ecs, playerid) & render.camera;
2024-08-18 08:33:01 +00:00
HECS_SETCOMPONENT(ecs_smartai, &ecs, playerid){.state = 0};
eprintf("Player component mask: %lx\n", ecs.e_components[playerid]);
2024-08-18 04:30:29 +00:00
2024-08-15 06:28:37 +00:00
// -----------------------------------
// Actual rendering
// -----------------------------------
2024-08-16 07:10:49 +00:00
// ceili->texture = &render.window;
2024-08-15 06:28:37 +00:00
while (1) {
haloo3d_easytimer_start(&frametimer);
2024-08-18 08:33:01 +00:00
// render.camera.yaw += 0.008;
2024-08-17 03:32:24 +00:00
haloo3d_perspective(render.perspective, fov, ASPECT, NEARCLIP, FARCLIP);
2024-08-15 06:28:37 +00:00
haloo3d_easyrender_beginframe(&render);
2024-08-17 03:32:24 +00:00
haloo3d_fb_clear(&render.window, sky);
2024-08-16 07:10:49 +00:00
2024-08-18 08:33:01 +00:00
// walli->scale.y = fabs(sin(3 * render.camera.yaw));
// render.camera.up.x = sin(render.camera.yaw);
// render.camera.up.y = cos(render.camera.yaw);
// walli->up.x = sin(3 * render.camera.yaw);
// walli->up.y = cos(4 * render.camera.yaw);
2024-08-15 06:28:37 +00:00
unigi_event_get(&event);
if (event.type == unigi_enum_event_input_keyboard) {
2024-08-17 03:32:24 +00:00
if (event.data.input_keyboard.down) {
switch (event.data.input_keyboard.button) {
case KEY_SPACE:
haloo3d_debugconsole_beginprompt(&dc);
break;
default:
exit(0);
}
}
2024-08-15 06:28:37 +00:00
}
2024-08-18 04:30:29 +00:00
// ---------------------------
// Game logic?
// ---------------------------
for (int i = 0; i < HECS_MAXENTITIES; i++) {
2024-08-18 08:33:01 +00:00
// eprintf("CHECKING EID %d\n", i);
// HECS_RUNSYS(&ecs, i, sys_ecs_objin_moveto, ecs_objin_id,
// ecs_moveto_id);
HECS_RUNSYS(&ecs, i, sys_ecs_camera_rotateto, ecs_camera_id,
ecs_rotateto_id);
HECS_RUNSYS(&ecs, i, sys_ecs_camera_moveto, ecs_camera_id, ecs_moveto_id);
HECS_RUNSYS(&ecs, i, sys_ecs_smartai, ecs_smartai_id, ecs_maze_id,
ecs_moveto_id, ecs_rotateto_id);
HECS_RUNSYS(&ecs, i, sys_ecs_moveto, ecs_moveto_id);
HECS_RUNSYS(&ecs, i, sys_ecs_rotateto, ecs_rotateto_id);
HECS_RUNSYS(&ecs, i, sys_ecs_moveto_camera, ecs_moveto_id, ecs_camera_id);
HECS_RUNSYS(&ecs, i, sys_ecs_rotateto_camera, ecs_rotateto_id,
ecs_camera_id);
// HECS_RUNSYS(&ecs, i, sys_ecs_moveto_objin, ecs_moveto_id,
// ecs_objin_id);
2024-08-18 04:30:29 +00:00
}
2024-08-15 06:28:37 +00:00
totaldrawn = 0;
haloo3d_obj_instance *object = NULL;
// Iterate over objects
2024-08-15 07:33:11 +00:00
while ((object = haloo3d_easyrender_nextinstance(&render, object)) !=
NULL) {
2024-08-15 06:28:37 +00:00
// Setup final model matrix and the precalced vertices
haloo3d_easyrender_beginmodel(&render, object);
// Iterate over object faces
for (int fi = 0; fi < object->model->numfaces; fi++) {
2024-08-15 07:33:11 +00:00
totaldrawn += haloo3d_easyrender_renderface(
2024-08-17 03:32:24 +00:00
&render, object, fi, ditherstart, ditherend, minlight);
2024-08-15 06:28:37 +00:00
}
}
2024-08-17 22:11:00 +00:00
haloo3d_easytimer_start(&filltimer);
#ifdef FASTFILL
haloo3d_fb_fill(&screen, &render.window);
#else
haloo3d_recti texrect = {.x1 = 0, .y1 = 0, .x2 = WIDTH, .y2 = HEIGHT};
haloo3d_recti screenrect = {.x1 = 0, .y1 = 0, .x2 = SWIDTH, .y2 = SHEIGHT};
2024-08-16 07:10:49 +00:00
haloo3d_sprite(&screen, &render.window, texrect, screenrect);
2024-08-17 22:11:00 +00:00
#endif
haloo3d_easytimer_end(&filltimer);
2024-08-16 07:10:49 +00:00
2024-08-15 06:28:37 +00:00
haloo3d_print(&render.tprint,
2024-08-17 22:11:00 +00:00
"Last frame: %05.2f (%05.2f)\nLast fill: %05.2f "
"(%05.2f)\nLast SDLFl: %05.2f "
2024-08-15 07:53:13 +00:00
"(%05.2f)\nTris: %d / %d\nVerts: %d\n",
frametimer.last * 1000, frametimer.sum * 1000,
2024-08-17 22:11:00 +00:00
filltimer.last * 1000, filltimer.sum * 1000,
2024-08-15 07:53:13 +00:00
sdltimer.last * 1000, sdltimer.sum * 1000, totaldrawn,
2024-08-15 07:33:11 +00:00
render.totalfaces, render.totalverts);
2024-08-15 06:28:37 +00:00
2024-08-17 22:11:00 +00:00
haloo3d_easytimer_start(&sdltimer);
2024-08-16 07:10:49 +00:00
unigi_graphics_blit(0, (unigi_type_color *)screen.buffer,
2024-08-15 06:28:37 +00:00
res.width * res.height);
unigi_graphics_flush();
2024-08-15 07:53:13 +00:00
haloo3d_easytimer_end(&sdltimer);
2024-08-15 06:28:37 +00:00
haloo3d_easytimer_end(&frametimer);
2024-08-15 07:53:13 +00:00
2024-08-17 03:32:24 +00:00
float waittime = (1.0 / fps) - frametimer.last;
2024-08-15 06:28:37 +00:00
if (waittime > 0) {
unigi_time_sleep(waittime * unigi_time_clocks_per_s);
}
}
2024-08-18 05:26:07 +00:00
// Just to get the compiler to STOP COMPLAINING about unused
haloo_ecs_removeentity(&ecs, playerid); //, int eid)
2024-08-15 06:28:37 +00:00
haloo3d_easystore_deleteallobj(&storage, haloo3d_obj_free);
haloo3d_easystore_deletealltex(&storage, haloo3d_fb_free);
}