new ui stuff

This commit is contained in:
2026-02-08 00:19:25 +01:00
parent e71ba138c0
commit fceac125c5
16 changed files with 687 additions and 503 deletions

26
src/world/camera.c Normal file
View File

@@ -0,0 +1,26 @@
#include "camera.h"
#include "../lib/raymath.h"
Camera createCamera(int32 width, int32 height) {
return (Camera){
.view = (Matrix){0},
.proj = MatrixPerspective(DEG2RAD * 45.0f, (real32)width/(real32)height, 0.1f, 100.0f),
.pos = (RLVector3){0},
.up = (RLVector3){0,1,0},
};
}
void cameraSetAspect(Camera *c, int32 width, int32 height) {
real32 aspectRatio = (real32)width/(real32)height;
c->proj = MatrixPerspective(DEG2RAD * 45.0f, aspectRatio, 0.1f, 100.0f);
}
void cameraLookAt(Camera *c, float x, float y, float z) {
c->target = (RLVector3){x, y, z};
c->view = MatrixLookAt(c->pos, c->target, c->up);
}
void cameraSetUp(Camera *c, real32 up_x, real32 up_y, real32 up_z) {
c->up = (RLVector3){up_x, up_y, up_z};
}

21
src/world/camera.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef CAMERA_H
#define CAMERA_H
#include "../lib/djstdlib/core.h"
#include "../lib/raymath.h"
typedef struct Camera Camera;
struct Camera {
Matrix view;
Matrix proj;
RLVector3 pos;
RLVector3 up;
RLVector3 target;
};
Camera createCamera(int32 width, int32 height);
void cameraSetAspect(Camera *c, int32 width, int32 height);
void cameraLookAt(Camera *c, float x, float y, float z);
void cameraSetUp(Camera *c, real32 up_x, real32 up_y, real32 up_z);
#endif

View File

@@ -37,7 +37,6 @@ int32 createSceneGraphNode(Scene *s) {
int32 createEntity(Scene *s) {
Entity *newEntity;
int32 newEntityHandle;
println("%d", s->nextFreeEntity);
if (s->nextFreeEntity) {
newEntityHandle = s->nextFreeEntity;
newEntity = getEntity(s, newEntityHandle);
@@ -98,36 +97,36 @@ void recalcScene(Scene *s) {
recalcSceneGraphNode(s, s->sceneRoot);
}
function void removeSceneGraphNodeRecursive(Scene *s, int32 graphNodeHandle, bool deletingParent) {
if (!graphNodeHandle) return;
SceneGraphNode *graphNode = getSceneGraphNode(s, graphNodeHandle);
static void removeSceneGraphNodeRecursive(Scene *s, int32 deletedNodeHandle, bool deletingParent) {
if (!deletedNodeHandle) return;
SceneGraphNode *deletedNode = getSceneGraphNode(s, deletedNodeHandle);
int32 nextChild = graphNode->firstChild;
int32 nextChild = deletedNode->firstChild;
while (nextChild) {
int32 sibling = getSceneGraphNode(s, nextChild)->nextSibling;
removeSceneGraphNodeRecursive(s, nextChild, true);
nextChild = sibling;
}
if (graphNode->entityHandle) {
Entity *entity = getEntity(s, graphNode->entityHandle);
if (deletedNode->entityHandle) {
Entity *entity = getEntity(s, deletedNode->entityHandle);
*entity = (Entity){0};
if (s->nextFreeEntity) {
entity->next = s->nextFreeEntity;
}
s->nextFreeEntity = graphNode->entityHandle;
s->nextFreeEntity = deletedNode->entityHandle;
}
if (s->nextFreeNode) {
graphNode->next = s->nextFreeNode;
deletedNode->next = s->nextFreeNode;
}
s->nextFreeNode = graphNodeHandle;
s->nextFreeNode = deletedNodeHandle;
if (!deletingParent && graphNode->parentHandle) {
SceneGraphNode *parentNode = getSceneGraphNode(s, graphNode->parentHandle);
if (parentNode->firstChild == graphNodeHandle) {
if (graphNode->nextSibling) {
parentNode->firstChild = graphNode->nextSibling;
if (!deletingParent && deletedNode->parentHandle) {
SceneGraphNode *parentNode = getSceneGraphNode(s, deletedNode->parentHandle);
if (parentNode->firstChild == deletedNodeHandle) {
if (deletedNode->nextSibling) {
parentNode->firstChild = deletedNode->nextSibling;
} else {
parentNode->firstChild = 0;
}
@@ -136,20 +135,21 @@ function void removeSceneGraphNodeRecursive(Scene *s, int32 graphNodeHandle, boo
int32 nextSibling = getSceneGraphNode(s, parentNode->firstChild)->nextSibling;
while (nextSibling) {
SceneGraphNode *siblingNode = getSceneGraphNode(s, nextSibling);
if (nextSibling == graphNodeHandle) {
if (nextSibling == deletedNodeHandle) {
SceneGraphNode *prevSiblingNode = getSceneGraphNode(s, prevSibling);
prevSiblingNode->nextSibling = graphNode->nextSibling;
prevSiblingNode->nextSibling = deletedNode->nextSibling;
break;
}
prevSibling = nextSibling;
nextSibling = siblingNode->nextSibling;
}
}
}
graphNode->firstChild = 0;
graphNode->parentHandle = 0;
graphNode->entityHandle = 0;
graphNode->nextSibling = 0;
deletedNode->firstChild = 0;
deletedNode->parentHandle = 0;
deletedNode->entityHandle = 0;
deletedNode->nextSibling = 0;
}
void removeSceneGraphNode(Scene *s, int32 graphNodeHandle) {
@@ -168,3 +168,28 @@ void sceneNodeAddNode(Scene *s, int32 parentHandle, int32 childHandle) {
parentNode->firstChild = childHandle;
childNode->parentHandle = parentHandle;
}
void show(Scene *s, uint32 graphNodeHandle) {
SceneGraphNode *node = getSceneGraphNode(s, graphNodeHandle);
if (node->entityHandle) {
getEntity(s, node->entityHandle)->flags |= EntityFlags_Visible;
}
int32 next = node->firstChild;
while (next) {
show(s, next);
next = getSceneGraphNode(s, next)->nextSibling;
}
}
void hide(Scene *s, uint32 graphNodeHandle) {
SceneGraphNode *node = getSceneGraphNode(s, graphNodeHandle);
if (node->entityHandle) {
getEntity(s, node->entityHandle)->flags &= ~EntityFlags_Visible;
}
int32 next = node->firstChild;
while (next) {
hide(s, next);
next = getSceneGraphNode(s, next)->nextSibling;
}
}

View File

@@ -1,3 +1,6 @@
#ifndef SCENE_H
#define SCENE_H
#include "../gfx/gfx.h"
#include "../lib/raymath.h"
@@ -33,10 +36,10 @@ struct SceneGraphNode {
int32 entityHandle;
int32 parentHandle;
// Free list
/** Next free in the free list */
int32 next;
// Children
/** Next child in child list in scene hierarchy */
int32 nextSibling;
int32 firstChild;
};
@@ -47,6 +50,7 @@ struct Scene {
int32 sceneRoot;
EntityList entities;
int32 nextFreeEntity;
/** @internal */
SceneGraphNodeList graphNodes;
int32 nextFreeNode;
};
@@ -63,3 +67,7 @@ void removeEntity(Scene *s, int32 entityHandle);
void removeSceneGraphNode(Scene *s, int32 graphNodeHandle);
void sceneNodeAddNode(Scene *s, int32 recipientNodeHandle, int32 graphNodeHandle);
SceneGraphNode *getSceneGraphNodeForEntity(Scene *s, int32 entityHandle);
void show(Scene *s, uint32 graphNodeHandle);
void hide(Scene *s, uint32 graphNodeHandle);
#endif

View File

@@ -1 +1,2 @@
#include "scene.c"
#include "camera.c"

8
src/world/world.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef WORLD_H
#define WORLD_H
#include "scene.h"
#include "camera.h"
#endif