3dtoys/maze.c
2024-08-15 03:53:13 -04:00

158 lines
4.9 KiB
C

#include "haloo3d/haloo3d.h"
#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"
#include <stdlib.h>
// Performance options
#define DOLIGHTING
// #define FASTTRIS
#define DITHERSTART -1
#define DITHEREND 8
#define TARGETFPS 60
#define SECPERFRAME 1.0 / TARGETFPS
// IDK you probably have to change this based on your display.
// Maybe there's a way to fix this?
#define UNIGIBITDEPTH 32
#define WIDTH 640
#define HEIGHT 480
#define ASPECT ((float)WIDTH / HEIGHT)
#define FOV 90.0
#define NEARCLIP 0.01
#define FARCLIP 100.0
#define LIGHTANG -MPI / 4.0
#define MINLIGHT 0.25
#define AVGWEIGHT 0.85
// Game options
#define MAZESIZE 31
int main() { // int argc, char **argv) {
haloo3d_easystore storage;
haloo3d_easystore_init(&storage);
haloo3d_easyrender render;
haloo3d_easyrender_init(&render, WIDTH, HEIGHT);
haloo3d_perspective(render.perspective, FOV, ASPECT, NEARCLIP, FARCLIP);
render.camera.pos.y = 0.5;
#ifdef FASTTRIS
render.trifunc = H3D_EASYRENDER_FASTFUNC;
#endif
eprintf("Initialized renderer\n");
haloo3d_easytimer frametimer, sdltimer;
haloo3d_easytimer_init(&frametimer, AVGWEIGHT);
haloo3d_easytimer_init(&sdltimer, AVGWEIGHT);
// Load the junk + generate stuff
haloo3d_obj *flooro = haloo3d_easystore_addobj(&storage, "floor");
haloo3d_obj *ceilo = haloo3d_easystore_addobj(&storage, "ceiling");
haloo3d_fb *floort = haloo3d_easystore_addtex(&storage, "floor");
haloo3d_fb *ceilt = haloo3d_easystore_addtex(&storage, "ceiling");
haloo3d_gen_plane(flooro, MAZESIZE);
haloo3d_gen_plane(ceilo, MAZESIZE);
uint16_t cols[4] = {0xFD93, 0xFB83, 0xFEEE, 0xFDDD};
haloo3d_gen_checkerboard(floort, cols, 2, 32);
haloo3d_gen_checkerboard(ceilt, cols + 2, 2, 32);
eprintf("Initialized models and textures\n");
// 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);
haloo3d_obj_instance *ceili =
haloo3d_easyrender_addinstance(&render, ceilo, ceilt);
floori->cullbackface = 0;
ceili->cullbackface = 0;
ceili->pos.y = 1;
eprintf("Setup all object instances\n");
unigi_type_event event;
unigi_type_resolution res;
res.width = WIDTH;
res.height = HEIGHT;
res.depth = UNIGIBITDEPTH;
int totaldrawn = 0;
eprintf("Scene has %d tris, %d verts\n", render.totalfaces,
render.totalverts);
// Init unigi system
// sprintf(render.printbuf, "maze.exe - %s %s %s", argv[1], argv[2], argv[3]);
unigi_graphics_init();
unigi_window_create(res, "maze.exe"); // render.printbuf);
// -----------------------------------
// Actual rendering
// -----------------------------------
while (1) {
haloo3d_easytimer_start(&frametimer);
render.camera.yaw += 0.005;
haloo3d_easyrender_beginframe(&render);
memset(render.window.buffer, 0,
sizeof(uint16_t) * render.window.width * render.window.height);
unigi_event_get(&event);
if (event.type == unigi_enum_event_input_keyboard) {
exit(0);
}
totaldrawn = 0;
haloo3d_obj_instance *object = NULL;
// Iterate over objects
while ((object = haloo3d_easyrender_nextinstance(&render, object)) !=
NULL) {
// 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++) {
totaldrawn += haloo3d_easyrender_renderface(
&render, object, fi, DITHERSTART, DITHEREND, MINLIGHT);
}
}
haloo3d_print(&render.tprint,
"Last frame: %05.2f (%05.2f)\nLast SDLFl: %05.2f "
"(%05.2f)\nTris: %d / %d\nVerts: %d\n",
frametimer.last * 1000, frametimer.sum * 1000,
sdltimer.last * 1000, sdltimer.sum * 1000, totaldrawn,
render.totalfaces, render.totalverts);
unigi_graphics_blit(0, (unigi_type_color *)render.window.buffer,
res.width * res.height);
haloo3d_easytimer_start(&sdltimer);
unigi_graphics_flush();
haloo3d_easytimer_end(&sdltimer);
haloo3d_easytimer_end(&frametimer);
float waittime = SECPERFRAME - frametimer.last;
if (waittime > 0) {
unigi_time_sleep(waittime * unigi_time_clocks_per_s);
}
}
haloo3d_easystore_deleteallobj(&storage, haloo3d_obj_free);
haloo3d_easystore_deletealltex(&storage, haloo3d_fb_free);
}