2024-08-15 06:28:37 +00:00
|
|
|
#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
|
2024-08-15 07:53:13 +00:00
|
|
|
#define DITHERSTART -1
|
2024-08-15 07:33:11 +00:00
|
|
|
#define DITHEREND 8
|
2024-08-16 07:10:49 +00:00
|
|
|
#define TARGETFPS 30
|
2024-08-15 08:32:40 +00:00
|
|
|
#define SECPERFRAME (1.0 / TARGETFPS)
|
2024-08-15 06:28:37 +00:00
|
|
|
|
2024-08-16 07:10:49 +00:00
|
|
|
#define WIDTH 320
|
|
|
|
#define HEIGHT 200
|
|
|
|
#define SWIDTH WIDTH * 3
|
|
|
|
#define SHEIGHT HEIGHT * 3
|
2024-08-15 06:28:37 +00:00
|
|
|
#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
|
2024-08-16 07:10:49 +00:00
|
|
|
#define HSCALE 2.0
|
2024-08-15 06:28:37 +00:00
|
|
|
|
2024-08-16 08:30:28 +00:00
|
|
|
// Generate a (square) maze
|
|
|
|
void gen_maze(haloo3d_obj *mazewalls, int size) {
|
|
|
|
uint8_t *mazevisit;
|
|
|
|
int *mazestack;
|
|
|
|
mallocordie(mazevisit, sizeof(uint8_t) * size * size);
|
|
|
|
mallocordie(mazestack, sizeof(int) * size * size);
|
|
|
|
memset(mazevisit, 0, size * size);
|
|
|
|
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;
|
|
|
|
mazevisit[x + y * size] = 1;
|
|
|
|
mazestack[0] = x + y * size;
|
|
|
|
int mazetop = 1;
|
|
|
|
struct vec2i visitable[4];
|
|
|
|
int visitmax = 0;
|
|
|
|
|
|
|
|
// Now let's make a maze!
|
|
|
|
while (mazetop) {
|
|
|
|
mazetop--;
|
|
|
|
visitmax = 0;
|
|
|
|
x = mazestack[mazetop] % size;
|
|
|
|
y = mazestack[mazetop] / size;
|
|
|
|
if (x > 0 && !mazevisit[x - 1 + y * size]) {
|
|
|
|
visitable[visitmax].x = -1;
|
|
|
|
visitable[visitmax++].y = 0;
|
|
|
|
}
|
|
|
|
if (x < size - 1 && !mazevisit[x + 1 + y * size]) {
|
|
|
|
visitable[visitmax].x = 1;
|
|
|
|
visitable[visitmax++].y = 0;
|
|
|
|
}
|
|
|
|
if (y > 0 && !mazevisit[x + (y - 1) * size]) {
|
|
|
|
visitable[visitmax].x = 0;
|
|
|
|
visitable[visitmax++].y = -1;
|
|
|
|
}
|
|
|
|
if (y < size - 1 && !mazevisit[x + (y + 1) * size]) {
|
|
|
|
visitable[visitmax].x = 0;
|
|
|
|
visitable[visitmax++].y = 1;
|
|
|
|
}
|
|
|
|
// You can generate a random location!
|
|
|
|
if (visitmax) {
|
|
|
|
mazetop++; // mazestack already has our location
|
|
|
|
struct vec2i movedir = visitable[rand() % visitmax];
|
|
|
|
int facebase = 4 * (x + y * size);
|
|
|
|
// Faces are... somewhere.
|
|
|
|
haloo3d_vertexi *face; // This is actually 6 vertices, the quad
|
|
|
|
// Only y == -21 moves in the direction of facebase
|
|
|
|
if (movedir.y == 1) {
|
|
|
|
facebase += 4 * size; // backwards?
|
|
|
|
} else if (movedir.x == -1) {
|
|
|
|
facebase += 2;
|
|
|
|
} else if (movedir.x == 1) {
|
|
|
|
facebase += 6;
|
|
|
|
}
|
|
|
|
face = mazewalls->faces[facebase];
|
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
|
face[i].posi = 0; // this should make the triangles degenerate
|
|
|
|
}
|
|
|
|
int nx = x + movedir.x;
|
|
|
|
int ny = y + movedir.y;
|
|
|
|
// Push onto stack and set visited
|
|
|
|
mazestack[mazetop++] = nx + ny * size;
|
|
|
|
mazevisit[nx + ny * size] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(mazevisit);
|
|
|
|
free(mazestack);
|
|
|
|
}
|
|
|
|
|
2024-08-15 07:33:11 +00:00
|
|
|
int main() { // int argc, char **argv) {
|
2024-08-15 06:28:37 +00:00
|
|
|
|
|
|
|
haloo3d_easystore storage;
|
|
|
|
haloo3d_easystore_init(&storage);
|
|
|
|
|
2024-08-16 07:10:49 +00:00
|
|
|
haloo3d_fb screen;
|
|
|
|
haloo3d_fb_init(&screen, SWIDTH, SHEIGHT);
|
|
|
|
haloo3d_recti texrect = {.x1 = 0, .y1 = 0, .x2 = WIDTH, .y2 = HEIGHT};
|
|
|
|
haloo3d_recti screenrect = {.x1 = 0, .y1 = 0, .x2 = SWIDTH, .y2 = SHEIGHT};
|
|
|
|
|
2024-08-15 06:28:37 +00:00
|
|
|
haloo3d_easyrender render;
|
|
|
|
haloo3d_easyrender_init(&render, WIDTH, HEIGHT);
|
|
|
|
haloo3d_perspective(render.perspective, FOV, ASPECT, NEARCLIP, FARCLIP);
|
2024-08-15 07:33:11 +00:00
|
|
|
render.camera.pos.y = 0.5;
|
|
|
|
#ifdef FASTTRIS
|
|
|
|
render.trifunc = H3D_EASYRENDER_FASTFUNC;
|
|
|
|
#endif
|
|
|
|
eprintf("Initialized renderer\n");
|
2024-08-15 06:28:37 +00:00
|
|
|
|
2024-08-16 07:10:49 +00:00
|
|
|
render.tprint.fb = &screen;
|
|
|
|
|
2024-08-15 07:53:13 +00:00
|
|
|
haloo3d_easytimer frametimer, sdltimer;
|
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-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-16 07:10:49 +00:00
|
|
|
haloo3d_gen_grid(wallo, MAZESIZE);
|
2024-08-16 08:30:28 +00:00
|
|
|
gen_maze(wallo, MAZESIZE);
|
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);
|
|
|
|
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-16 08:30:28 +00:00
|
|
|
// render.camera.pos.y = 5;
|
|
|
|
// render.camera.pitch = 2.2;
|
|
|
|
|
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-16 07:10:49 +00:00
|
|
|
render.camera.yaw += 0.008;
|
2024-08-15 06:28:37 +00:00
|
|
|
haloo3d_easyrender_beginframe(&render);
|
2024-08-16 07:10:49 +00:00
|
|
|
haloo3d_fb_clear(&render.window, 0xF000);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
|
|
|
&render, object, fi, DITHERSTART, DITHEREND, MINLIGHT);
|
2024-08-15 06:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 07:10:49 +00:00
|
|
|
haloo3d_sprite(&screen, &render.window, texrect, screenrect);
|
|
|
|
|
2024-08-15 06:28:37 +00:00
|
|
|
haloo3d_print(&render.tprint,
|
2024-08-15 07:53:13 +00:00
|
|
|
"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,
|
2024-08-15 07:33:11 +00:00
|
|
|
render.totalfaces, render.totalverts);
|
2024-08-15 06:28:37 +00:00
|
|
|
|
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);
|
2024-08-15 07:53:13 +00:00
|
|
|
|
|
|
|
haloo3d_easytimer_start(&sdltimer);
|
2024-08-15 06:28:37 +00:00
|
|
|
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-15 06:28:37 +00:00
|
|
|
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);
|
|
|
|
}
|