Initial terrain stuff

This commit is contained in:
Carlos Sanchez 2024-09-08 18:42:50 -04:00
parent 8bfe2301a7
commit 1fddf39939
4 changed files with 161 additions and 1 deletions

13
ecs2.h
View File

@ -93,6 +93,19 @@ typedef int ecs_eid;
(_ecs)->entities[eid] |= type##_fl; \
(_ecs)->c_##type[eid] = (type)
// A shortcut to run a system against all matching components. You could also
// use the query function for your ecs type if you don't want to use this macro.
// Make sure you pass int the name of YOUR system, not the "run" function. This
// function bypasses
#define ECS_RUNSYSTEM(_ecs, _comps, _system) \
{ \
ecs_cid _realcomps = ECS_SELFFLAG | _comps; \
for (int __i = 0; __i < ECS_MAXENTITIES; __i++) { \
if ((_ecs->entities[__i] & _realcomps) == _realcomps) \
_system##_run(_ecs, __i); \
} \
}
// Add a system function which automatically calls your given function with
// pre-pulled items from the entity component arrays. The new function is
// named the same as the old one, just with _run appeneded

@ -1 +1 @@
Subproject commit 9fbc0948f182cd46c4aaff43b632f92f72fb4fb2
Subproject commit a07b4de2e0753440f0fc664f717e9bc5f46193b9

64
terrain.c Normal file
View File

@ -0,0 +1,64 @@
#include "haloo3d/haloo3d.h"
#include "haloo3d/haloo3dex_easy.h"
#include "haloo3d/haloo3dex_gen.h"
#include "haloo3d/haloo3dex_obj.h"
#include "unigi/unigi.headers/src/main.h"
#include "unigi/unigi.platform.sdl1/src/main.c"
#include "ecs2.h"
#include "terrain_ecstypes.h"
#include <stdlib.h>
#define WIDTH 480
#define HEIGHT 300
#define ASPECT ((float)WIDTH / HEIGHT)
#define SCREENSCALE 2
#define SWIDTH (WIDTH * SCREENSCALE)
#define SHEIGHT (HEIGHT * SCREENSCALE)
#define NEARCLIP 0.01
#define FARCLIP 100.0
#define AVGWEIGHT 0.85
#define CAM_INITPITCH MPI_2
// Try 0.5 and 3.5 or something
#define DITHERSTART 10000
#define DITHEREND 10000
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;
res.depth = 0;
unigi_graphics_init();
unigi_window_create(res, "terrain.exe"); // render.printbuf);
eprintf("Initialized unigi system\n");
haloo3d_easystore storage;
haloo3d_easystore_init(&storage);
haloo3d_fb *palettetex = haloo3d_easystore_addtex(&storage, "palette");
haloo3d_gen_palettetex(palettetex);
eprintf("Initialized storage and default textures/etc\n");
haloo3d_fb screen;
haloo3d_fb_init(&screen, SWIDTH, SHEIGHT);
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_easystore_deleteallobj(&storage, haloo3d_obj_free);
haloo3d_easystore_deletealltex(&storage, haloo3d_fb_free);
}

83
terrain_ecstypes.h Normal file
View File

@ -0,0 +1,83 @@
#ifndef __TERRAIN_ECSTYPES
#define __TERRAIN_ECSTYPES
#include "ecs2.h"
#include "haloo3d/haloo3d.h"
// #include "haloo3d/haloo3dex_easy.h"
// Baseline render context. When a pointer to this is attached as a component,
// it becomes a render target, and that entity will perform the precalcs
// necessary for later object rendering.
typedef struct {
// struct vec4 precalcs[H3D_OBJ_MAXVERTICES];
// haloo3d_facef outfaces[H3D_FACEF_MAXCLIP];
mfloat_t perspective[MAT4_SIZE];
mfloat_t precalc_screen[MAT4_SIZE];
haloo3d_fb window;
// Baseline render settings. Some of these settings will be applied to
// each object associated with this context
haloo3d_trirender rendersettings;
mfloat_t precalc_halfwidth; // Optimization for reducing calcs per tri
mfloat_t precalc_halfheight; // Optimization for reducing calcs per tri
} render_context;
// Associate this to some entity along with a placement and it will be the
// main "renderer" for a viewpoint
typedef render_context *ecs_rendercontext;
// All values which allow rendering of a 3d object
typedef struct {
struct vec3 scale; // how big the thing should be in world
struct vec3 *lighting; // a pointer to lighting, null for none
render_context *context; // What to render into
haloo3d_obj *model;
haloo3d_fb *texture;
uint8_t cullbackface; // Whether to cull backfaces (you probably should)
// Whether to automatically move lighting when models have a
// lookvec. This also normalizes the light
uint8_t autolightfix;
} ecs_object;
// Some placement within the world. Doesn't imply anything other than a
// position and a facing direction
typedef struct {
struct vec3 pos;
struct vec3 up;
struct vec3 lookvec;
} ecs_placement;
// typedef haloo3d_camera *ecs_camera;
// typedef haloo3d_obj_instance *ecs_object;
// // A billboard for OUR system, which does not look up or down at the target.
// As
// // such, no matter what the lookat is set to, y is always going to be equal
// to
// // the y of the instance, so it appears to be looking "straight"
// typedef struct {
// haloo3d_obj_instance *obj;
// struct vec3 *lookat;
// } ecs_billboard;
// // A component which allows an object instance to die
// // when the maze is initiailized.
// typedef struct {
// haloo3d_obj_instance *obj;
// haloo3d_easyrender *render;
// worldstate *ws;
// void (*diefunc)(haloo3d_obj_instance *);
// } ecs_dieoninit;
// Setup ECS system for our game
ECS_START(tecs)
ECS_COMPONENT(ecs_rendercontext);
ECS_COMPONENT(ecs_object);
ECS_COMPONENT(ecs_placement);
ECS_END(tecs)
// And then a copy of the components here... that sucksssss
ECS_CID(ecs_rendercontext, 0);
ECS_CID(ecs_object, 1);
ECS_CID(ecs_placement, 2);
#endif