From 1fb9485ee8922b95f7b2218ed48dfa389c97b18a Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Fri, 22 Aug 2025 20:52:59 -0500 Subject: [PATCH] Fixed PSP compiling --- CMakeLists.txt | 15 ++++++- src/display/CMakeLists.txt | 9 ++++ src/display/display.c | 2 +- src/display/mesh/mesh.h | 8 ++-- src/display/mesh/meshrenderer.c | 5 +++ src/display/renderer.c | 6 ++- src/dusk.h | 1 + src/engine/engine.h | 1 + src/main.c | 3 ++ src/scene/node.c | 76 +++++++++++++++++++++++++++++++++ src/scene/node.h | 39 ++++++++++++----- src/thread/CMakeLists.txt | 5 +++ 12 files changed, 154 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d9576e..b9596f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,8 @@ set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules) if(NOT DEFINED DUSK_TARGET_SYSTEM) - set(DUSK_TARGET_SYSTEM "linux") + # set(DUSK_TARGET_SYSTEM "linux") + set(DUSK_TARGET_SYSTEM "psp") endif() # Prep cache @@ -60,6 +61,18 @@ if(DUSK_TARGET_SYSTEM STREQUAL "linux") OpenGL::GL GL ) + +elseif(DUSK_TARGET_SYSTEM STREQUAL "psp") + find_package(SDL2 REQUIRED) + target_link_libraries(${DUSK_TARGET_NAME} + PRIVATE + # pspsdk + ${SDL2_LIBRARIES} + ) + target_include_directories(${DUSK_TARGET_NAME} + PRIVATE + ${SDL2_INCLUDE_DIRS} + ) endif() # Add code diff --git a/src/display/CMakeLists.txt b/src/display/CMakeLists.txt index ba78673..92621df 100644 --- a/src/display/CMakeLists.txt +++ b/src/display/CMakeLists.txt @@ -21,4 +21,13 @@ if(DUSK_TARGET_SYSTEM STREQUAL "linux") DISPLAY_WINDOW_WIDTH_DEFAULT=960 DISPLAY_WINDOW_HEIGHT_DEFAULT=720 ) +elseif(DUSK_TARGET_SYSTEM STREQUAL "psp") + target_compile_definitions(${DUSK_TARGET_NAME} + PRIVATE + DUSK_DISPLAY_SDL2=1 + DISPLAY_WINDOW_WIDTH_DEFAULT=480 + DISPLAY_WINDOW_HEIGHT_DEFAULT=272 + DISPLAY_WIDTH=480 + DISPLAY_HEIGHT=272 + ) endif() \ No newline at end of file diff --git a/src/display/display.c b/src/display/display.c index 3ba5f02..79fb176 100644 --- a/src/display/display.c +++ b/src/display/display.c @@ -75,7 +75,7 @@ errorret_t displayUpdate(void) { // Set viewport size. int32_t windowWidth, windowHeight; - SDL_GetWindowSize(DISPLAY.window, &windowWidth, &windowHeight); + // SDL_GetWindowSize(DISPLAY.window, &windowWidth, &windowHeight); glViewport(0, 0, windowWidth, windowHeight); #endif diff --git a/src/display/mesh/mesh.h b/src/display/mesh/mesh.h index d851e58..a9a4439 100644 --- a/src/display/mesh/mesh.h +++ b/src/display/mesh/mesh.h @@ -19,9 +19,11 @@ typedef enum { #define MESH_VERTEX_POS_SIZE 3 typedef struct { - GLubyte color[MESH_VERTEX_COLOR_SIZE]; - GLfloat uv[MESH_VERTEX_UV_SIZE]; - GLfloat pos[MESH_VERTEX_POS_SIZE]; + #if DUSK_DISPLAY_SDL2 + GLubyte color[MESH_VERTEX_COLOR_SIZE]; + GLfloat uv[MESH_VERTEX_UV_SIZE]; + GLfloat pos[MESH_VERTEX_POS_SIZE]; + #endif } meshvertex_t; typedef struct { diff --git a/src/display/mesh/meshrenderer.c b/src/display/mesh/meshrenderer.c index 1c14301..912374c 100644 --- a/src/display/mesh/meshrenderer.c +++ b/src/display/mesh/meshrenderer.c @@ -6,6 +6,7 @@ */ #include "meshrenderer.h" +#include "scene/node.h" meshrenderer_t MESH_RENDERER_DATA[ECS_ENTITY_COUNT_MAX] = { 0 }; ecscomponent_t MESH_RENDERER_COMPONENT = ecsComponentInit( @@ -22,5 +23,9 @@ void meshRendererDraw(const ecsid_t id) { meshrenderer_t *renderer = &MESH_RENDERER_DATA[id]; if(!renderer->mesh) return; + + node_t *node = nodeGet(id); + nodeMatrixUpdate(id); + meshDraw(renderer->mesh, 0, -1); } \ No newline at end of file diff --git a/src/display/renderer.c b/src/display/renderer.c index e4e265b..7588a87 100644 --- a/src/display/renderer.c +++ b/src/display/renderer.c @@ -7,6 +7,7 @@ #include "renderer.h" #include "display/mesh/meshrenderer.h" +#include "scene/node.h" void rendererRender(const ecsid_t camera) { if(camera == -1) return; @@ -14,11 +15,14 @@ void rendererRender(const ecsid_t camera) { // Get the meshes. uint32_t meshCount; ecsid_t meshes[ECS_ENTITY_COUNT_MAX]; + ecsid_t id; meshCount = meshRendererGetAll(meshes); cameraPush(camera); for(uint32_t i = 0; i < meshCount; i++) { - meshRendererDraw(meshes[i]); + id = meshes[i]; + nodeMatrixPush(id); + meshRendererDraw(id); } cameraPop(); } \ No newline at end of file diff --git a/src/dusk.h b/src/dusk.h index 0e1bfed..578ed6a 100644 --- a/src/dusk.h +++ b/src/dusk.h @@ -17,6 +17,7 @@ #include #include #include +#include typedef bool bool_t; typedef int int_t; diff --git a/src/engine/engine.h b/src/engine/engine.h index 29f9052..42db298 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -6,6 +6,7 @@ */ #pragma once +#include "display/display.h"// Important to be included first. #include "error/error.h" typedef struct { diff --git a/src/main.c b/src/main.c index 8e0bae0..e710323 100644 --- a/src/main.c +++ b/src/main.c @@ -7,6 +7,9 @@ #include "engine/engine.h" +// PSP_MODULE_INFO("Dusk", 0, 1, 0); +// PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER | THREAD_ATTR_VFPU); + int main(int argc, char **argv) { errorret_t ret; ret = engineInit(); diff --git a/src/scene/node.c b/src/scene/node.c index c49b037..9e35b1f 100644 --- a/src/scene/node.c +++ b/src/scene/node.c @@ -6,6 +6,7 @@ #include "node.h" #include "util/memory.h" #include "assert/assert.h" +#include "display/display.h" node_t NODE_DATA[ECS_ENTITY_COUNT_MAX] = { 0 }; ecscomponent_t NODE_COMPONENT = ecsComponentInit( @@ -50,4 +51,79 @@ void nodeMatrixSet(const ecsid_t id, mat4 in) { } glm_mat4_copy(in, node->transform); + + // Extract position, scale, rotation from the matrix. + node->position[0] = in[3][0]; + node->position[1] = in[3][1]; + node->position[2] = in[3][2]; + + node->scale[0] = glm_vec3_norm((vec3){ in[0][0], in[0][1], in[0][2] }); + node->scale[1] = glm_vec3_norm((vec3){ in[1][0], in[1][1], in[1][2] }); + node->scale[2] = glm_vec3_norm((vec3){ in[2][0], in[2][1], in[2][2] }); + + // Remove scale from the matrix to extract rotation. + if(node->scale[0] != 0.0f) { + in[0][0] /= node->scale[0]; + in[0][1] /= node->scale[0]; + in[0][2] /= node->scale[0]; + glm_vec3_copy(in[0], node->rotation); + glm_vec3_copy(in[1], node->rotation); + glm_vec3_copy(in[2], node->rotation); + node->rotation[1] = asinf(-in[0][2]); + if (cosf(node->rotation[1]) != 0.0f) { + node->rotation[0] = atan2f(in[1][2], in[2][2]); + node->rotation[2] = atan2f(in[0][1], in[0][0]); + } else { + node->rotation[0] = 0.0f; + node->rotation[2] = atan2f(-in[1][0], in[1][1]); + } + } else { + node->rotation[0] = 0.0f; + node->rotation[1] = 0.0f; + node->rotation[2] = 0.0f; + } +} + +void nodePositionGet(const ecsid_t id, vec3 out) { + node_t *node; + + if(nodeHas(id)) { + node = &NODE_DATA[id]; + } else { + node = nodeAdd(id); + } + + glm_vec3_copy(node->position, out); +} + +void nodeMatrixUpdate(const ecsid_t id) { + node_t *node; + + if(nodeHas(id)) { + node = &NODE_DATA[id]; + } else { + node = nodeAdd(id); + } + + glm_mat4_identity(node->transform); + mat4 rot; + glm_euler(node->rotation, rot); + glm_mat4_mul(node->transform, rot, node->transform); + // glm_scale(node->transform, node->scale); +} + +void nodeMatrixPush(const ecsid_t id) { + assertTrue(nodeHas(id), "Not a node component"); + node_t *node = nodeGet(id); + + #if DUSK_DISPLAY_SDL2 + glPushMatrix(); + glMultMatrixf((const GLfloat*)node->transform); + #endif +} + +void nodeMatrixPop(void) { + #if DUSK_DISPLAY_SDL2 + glPopMatrix(); + #endif } \ No newline at end of file diff --git a/src/scene/node.h b/src/scene/node.h index 2b5f503..b7929e4 100644 --- a/src/scene/node.h +++ b/src/scene/node.h @@ -12,6 +12,10 @@ typedef struct { mat4 transform; + + vec3 position; + vec3 scale; + vec3 rotation; // Euler angles in radians } node_t; extern node_t NODE_DATA[ECS_ENTITY_COUNT_MAX]; @@ -25,15 +29,12 @@ extern ecscomponent_t NODE_COMPONENT; #define nodeRemove(id) ecsComponentDataRemove(&NODE_COMPONENT, id) /** - * Initialize the scene tree. - * - * This will initialize the ECS system and prepare the scene tree for use. + * Initialize the node component. */ void nodeInit(void); /** * Callback for when an entity is added to the ECS. - * This will initialize the scene tree data for the entity. * * @param id The ID of the entity being added. */ @@ -41,24 +42,42 @@ void nodeEntityAdded(const ecsid_t id); /** * Callback for when an entity is removed from the ECS. - * This will clean up any scene tree data associated with the entity. * * @param id The ID of the entity being removed. */ void nodeEntityRemoved(const ecsid_t id); /** - * Get the local transformation matrix of a scene item. + * Get the local transformation matrix of a node. * - * @param id The ID of the scene item. + * @param id The ID of the node. * @param out Pointer to a mat4 where the local matrix will be stored. */ void nodeMatrixGet(const ecsid_t id, mat4 out); /** - * Set the local transformation matrix of a scene item. + * Set the local transformation matrix of a node. * - * @param id The ID of the scene item. + * @param id The ID of the node. * @param in Pointer to a mat4 containing the new local matrix. */ -void nodeMatrixSet(const ecsid_t id, mat4 in); \ No newline at end of file +void nodeMatrixSet(const ecsid_t id, mat4 in); + +/** + * Get the local position of a node. + * + * @param id The ID of the node. + */ +void nodeMatrixUpdate(const ecsid_t id); + +/** + * Push the node's transformation matrix onto the OpenGL matrix stack. + * + * @param id The ID of the node. + */ +void nodeMatrixPush(const ecsid_t id); + +/** + * Pop the last transformation matrix from the OpenGL matrix stack. + */ +void nodeMatrixPop(void); \ No newline at end of file diff --git a/src/thread/CMakeLists.txt b/src/thread/CMakeLists.txt index c566069..9287173 100644 --- a/src/thread/CMakeLists.txt +++ b/src/thread/CMakeLists.txt @@ -16,4 +16,9 @@ if(DUSK_TARGET_SYSTEM STREQUAL "linux") PRIVATE DUSK_THREAD_PTHREAD=1 ) +elseif(DUSK_TARGET_SYSTEM STREQUAL "psp") + target_compile_definitions(${DUSK_TARGET_NAME} + PRIVATE + DUSK_THREAD_PTHREAD=1 + ) endif() \ No newline at end of file