cleaned more stuff

This commit is contained in:
2025-08-22 12:40:18 -05:00
parent b1be1deb79
commit 94ad64675d
15 changed files with 467 additions and 211 deletions

View File

@@ -6,6 +6,78 @@
*/
#include "camera.h"
#include "display/display.h"
#include "assert/assert.h"
camera_t CAMERA_DATA[ECS_ENTITY_COUNT_MAX] = { 0 };
ecscomponent_t CAMERA_COMPONENT = ecsComponentInit(CAMERA_DATA, NULL);
ecscomponent_t CAMERA_COMPONENT = ecsComponentInit(
CAMERA_DATA,
((ecscomponentcallbacks_t){
.init = NULL,
.entityAdd = cameraEntityAdded
})
);
ecsid_t CAMERA_MAIN = -1;
void cameraEntityAdded(const ecsid_t id) {
if(CAMERA_MAIN == -1) CAMERA_MAIN = id;
camera_t *cam = cameraGet(id);
cam->type = CAMERA_TYPE_ORTHOGRAPHIC;
cam->perspective.fov = glm_rad(75.0f);
cam->nearClip = 0.1f;
cam->farClip = 1000.0f;
cam->clearColor = COLOR_CORNFLOWER_BLUE;
}
void cameraPush(const ecsid_t id) {
assertTrue(cameraHas(id), "Not a camera component");
camera_t *cam = cameraGet(id);
mat4 projection;
switch(cam->type) {
case CAMERA_TYPE_ORTHOGRAPHIC:
glm_ortho(
cam->orthographic.left,
cam->orthographic.right,
cam->orthographic.bottom,
cam->orthographic.top,
cam->nearClip,
cam->farClip,
projection
);
break;
case CAMERA_TYPE_PERSPECTIVE:
glm_perspective(
cam->perspective.fov,
4.0f / 3.0f,
cam->nearClip,
cam->farClip,
projection
);
}
#if DUSK_DISPLAY_SDL2
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glLoadMatrixf((const GLfloat*)projection);
glClearColor(
cam->clearColor.r / 255.0f,
cam->clearColor.g / 255.0f,
cam->clearColor.b / 255.0f,
cam->clearColor.a / 255.0f
);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#endif
}
void cameraPop(void) {
#if DUSK_DISPLAY_SDL2
glPopMatrix();
#endif
}

View File

@@ -7,42 +7,59 @@
#pragma once
#include "ecs/ecscomponent.h"
#include "display/color.h"
typedef enum {
CAMERA_TYPE_PERSPECTIVE,
CAMERA_TYPE_ORTHOGRAPHIC
} cameraprojectiontype_t;
typedef struct {
float_t view[16]; // 4x4 matrix for view transformation
cameraprojectiontype_t type;
union {
struct {
float_t fov;
} perspective;
struct {
float_t left;
float_t right;
float_t top;
float_t bottom;
} orthographic;
};
float_t nearClip;
float_t farClip;
color_t clearColor;
} camera_t;
extern camera_t CAMERA_DATA[ECS_ENTITY_COUNT_MAX];
extern ecscomponent_t CAMERA_COMPONENT;
extern ecsid_t CAMERA_MAIN;
/**
* Initializes the camera component.
*
* @param id The ID of the entity to initialize the camera for.
*/
#define cameraAdd(id) ((camera_t*)ecsComponentDataAdd(&CAMERA_COMPONENT, id))
/**
* Gets the camera component data for a specific entity.
*
* @param id The ID of the entity to get the camera data for.
* @return Pointer to the camera data for the entity.
*/
#define cameraGet(id) ((camera_t*)ecsComponentDataGet(&CAMERA_COMPONENT, id))
/**
* Checks if the camera component has data for a specific entity.
*
* @param id The ID of the entity to check.
* @return True if the camera component has data for the entity, false otherwise.
*/
#define cameraHas(id) ecsComponentDataHas(&CAMERA_COMPONENT, id)
#define cameraRemove(id) ecsComponentDataRemove(&CAMERA_COMPONENT, id)
/**
* Removes the camera component data for a specific entity.
* Callback function called when a new entity is added to the camera component.
* Initializes the camera data for the entity.
*
* @param id The ID of the entity to remove the camera data for.
* @param id The ID of the newly added entity.
*/
#define cameraRemove(id) ecsComponentDataRemove(&CAMERA_COMPONENT, id)
void cameraEntityAdded(const ecsid_t id);
/**
* Pushes the camera's view matrix onto the matrix stack.
*
* @param id The ID of the camera entity to use.
*/
void cameraPush(const ecsid_t id);
/**
* Pops the camera's view matrix off the matrix stack.
*/
void cameraPop(void);

106
src/display/color.h Normal file
View File

@@ -0,0 +1,106 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
typedef float_t colorchannelf_t;
typedef uint8_t colorchannelb_t;
typedef struct {
colorchannelf_t r;
colorchannelf_t g;
colorchannelf_t b;
} color3f_t;
typedef struct {
colorchannelf_t r;
colorchannelf_t g;
colorchannelf_t b;
colorchannelf_t a;
} color4f_t;
typedef struct {
colorchannelb_t r;
colorchannelb_t g;
colorchannelb_t b;
} color3b_t;
typedef struct {
colorchannelb_t r;
colorchannelb_t g;
colorchannelb_t b;
colorchannelb_t a;
} color4b_t;
typedef color4b_t color_t;
#define color3f(r, g, b) ((color3f_t){r, g, b})
#define color4f(r, g, b, a) ((color4f_t){r, g, b, a})
#define color3b(r, g, b) ((color3b_t){r, g, b})
#define color4b(r, g, b, a) ((color4b_t){r, g, b, a})
#define color(r, g, b, a) ((color_t){r, g, b, a})
#define COLOR_WHITE_3F color3f(1.0f, 1.0f, 1.0f)
#define COLOR_WHITE_4F color4f(1.0f, 1.0f, 1.0f, 1.0f)
#define COLOR_WHITE_3B color3b(255, 255, 255)
#define COLOR_WHITE_4B color4b(255, 255, 255, 255)
#define COLOR_WHITE color(255, 255, 255, 255)
#define COLOR_BLACK_3F color3f(0.0f, 0.0f, 0.0f)
#define COLOR_BLACK_4F color4f(0.0f, 0.0f, 0.0f, 1.0f)
#define COLOR_BLACK_3B color3b(0, 0, 0)
#define COLOR_BLACK_4B color4b(0, 0, 0, 255)
#define COLOR_BLACK color(0, 0, 0, 255)
#define COLOR_RED_3F color3f(1.0f, 0.0f, 0.0f)
#define COLOR_RED_4F color4f(1.0f, 0.0f, 0.0f, 1.0f)
#define COLOR_RED_3B color3b(255, 0, 0)
#define COLOR_RED_4B color4b(255, 0, 0, 255)
#define COLOR_RED color(255, 0, 0, 255)
#define COLOR_GREEN_3F color3f(0.0f, 1.0f, 0.0f)
#define COLOR_GREEN_4F color4f(0.0f, 1.0f, 0.0f, 1.0f)
#define COLOR_GREEN_3B color3b(0, 255, 0)
#define COLOR_GREEN_4B color4b(0, 255, 0, 255)
#define COLOR_GREEN color(0, 255, 0, 255)
#define COLOR_BLUE_3F color3f(0.0f, 0.0f, 1.0f)
#define COLOR_BLUE_4F color4f(0.0f, 0.0f, 1.0f, 1.0f)
#define COLOR_BLUE_3B color3b(0, 0, 255)
#define COLOR_BLUE_4B color4b(0, 0, 255, 255)
#define COLOR_BLUE color(0, 0, 255, 255)
#define COLOR_YELLOW_3F color3f(1.0f, 1.0f, 0.0f)
#define COLOR_YELLOW_4F color4f(1.0f, 1.0f, 0.0f, 1.0f)
#define COLOR_YELLOW_3B color3b(255, 255, 0)
#define COLOR_YELLOW_4B color4b(255, 255, 0, 255)
#define COLOR_YELLOW color(255, 255, 0, 255)
#define COLOR_CYAN_3F color3f(0.0f, 1.0f, 1.0f)
#define COLOR_CYAN_4F color4f(0.0f, 1.0f, 1.0f, 1.0f)
#define COLOR_CYAN_3B color3b(0, 255, 255)
#define COLOR_CYAN_4B color4b(0, 255, 255, 255)
#define COLOR_CYAN color(0, 255, 255, 255)
#define COLOR_MAGENTA_3F color3f(1.0f, 0.0f, 1.0f)
#define COLOR_MAGENTA_4F color4f(1.0f, 0.0f, 1.0f, 1.0f)
#define COLOR_MAGENTA_3B color3b(255, 0, 255)
#define COLOR_MAGENTA_4B color4b(255, 0, 255, 255)
#define COLOR_MAGENTA color(255, 0, 255, 255)
#define COLOR_TRANSPARENT_3F color3f(0.0f, 0.0f, 0.0f)
#define COLOR_TRANSPARENT_4F color4f(0.0f, 0.0f, 0.0f, 0.0f)
#define COLOR_TRANSPARENT_3B color3b(0, 0, 0)
#define COLOR_TRANSPARENT_4B color4b(0, 0, 0, 0)
#define COLOR_TRANSPARENT color(0, 0, 0, 0)
#define COLOR_CORNFLOWER_BLUE_3F color3f(0.39f, 0.58f, 0.93f)
#define COLOR_CORNFLOWER_BLUE_4F color4f(0.39f, 0.58f, 0.93f, 1.0f)
#define COLOR_CORNFLOWER_BLUE_3B color3b(100, 149, 237)
#define COLOR_CORNFLOWER_BLUE_4B color4b(100, 149, 237, 255)
#define COLOR_CORNFLOWER_BLUE color(100, 149, 237, 255)

View File

@@ -7,7 +7,7 @@
#include "display/display.h"
#include "console/console.h"
#include "display/camera.h"
#include "display/mesh/mesh.h"
display_t DISPLAY;
@@ -82,11 +82,21 @@ errorret_t displayUpdate(void) {
}
}
SDL_GL_SwapWindow(DISPLAY.window);
// Set viewport size.
int32_t windowWidth, windowHeight;
SDL_GetWindowSize(DISPLAY.window, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
#endif
meshDraw(&mesh, 0, -1);
if(CAMERA_MAIN != -1) {
cameraPush(CAMERA_MAIN);
meshDraw(&mesh, 0, -1);
cameraPop();
}
#if DUSK_DISPLAY_SDL2
SDL_GL_SwapWindow(DISPLAY.window);
#endif
// For now, we just return an OK error.
errorOk();