Refactor engine and component management; add resource handling and file utilities

This commit is contained in:
Daniel Ramírez 2025-04-22 00:04:17 +02:00
parent ec21fcea59
commit f7da063cfe
22 changed files with 194 additions and 75 deletions

View File

@ -19,6 +19,10 @@ list(APPEND CMAKE_PREFIX_PATH "build")
find_package(glm REQUIRED)
target_link_libraries(xrbds PRIVATE glm::glm)
target_link_libraries(xrbds
PRIVATE glm::glm
PRIVATE -lfilesystem
PRIVATE -lfat
)
add_subdirectory(samples/hello_world)

View File

@ -30,7 +30,7 @@ public:
}
template <typename ComponentType>
void addComponent(FEntity entityID, const ComponentType &component) {
void addComponent(FEntity entityID, const ComponentType component) {
auto &componentMap = getComponentMap<ComponentType>();
componentMap[entityID] =
std::make_shared<ComponentType>(std::move(component));

View File

@ -3,14 +3,15 @@
#include <memory>
#include <nds.h>
#include <stdio.h>
#include <filesystem.h>
#include "input/input.h"
#include "component_manager.h"
#include "object.h"
#include "components/transform.h"
#include "components/mesh_filter.h"
#include "graphics/renderer.h"
#include "scene/3d/node_3d.h"
#include "utils/file.h"
/////////////////////////////////////////////////////////
// Main function
@ -47,20 +48,15 @@ void Engine::run() {
irqSet(IRQ_VBLANK, Engine::VblankCallback);
VblankCallback();
// TEST - Create an entity and add a component
FEntity entity = ComponentManager::GetInstance()->newEntity();
Transform transform({0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f},
{1.0f, 1.0f, 1.0f});
ComponentManager::GetInstance()->addComponent(entity, transform);
MeshFilter meshFilter("example_mesh");
ComponentManager::GetInstance()->addComponent(entity, meshFilter);
// TEST ------------------------
// TEST - Loop trough selected components
auto view = ComponentManager::GetInstance()->view<MeshFilter, Transform>();
PtrShr<Node3D> node = NewNode<Node3D>();
node->getComponent<Transform>()->position = {1.0f, 2.0f, 3.0f};
for (auto &[entity, mesh, transform] : view) {
printf("Entity: %d, Mesh: %p, Transform: %p\n", entity, mesh, transform);
}
FString ahuevo = Utils::File::ReadTextFile("hola.txt");
iprintf("ahuevo: %s\n", ahuevo.c_str());
// -----------------------------
while (pmMainLoop()) {
processInput();
@ -78,4 +74,11 @@ void Engine::render() { Renderer::GetInstance()->render(); }
void Engine::VblankCallback() { Engine::GetInstance()->render(); }
Engine::Engine() {}
Engine::Engine() {
// Initialize nitroFS
char nitroFSPath[32];
strcpy(nitroFSPath, "assets");
char *nitroFSPathptr = nitroFSPath;
if (!nitroFSInit(&nitroFSPathptr))
exit(-1);
}

View File

View File

@ -1,18 +0,0 @@
#ifndef OBJECT_H
#define OBJECT_H
class Object {
public:
Object();
virtual ~Object();
virtual void update();
virtual void render();
virtual void test() { iprintf("Object::test\n"); };
protected:
int id;
};
#endif // OBJECT_H

View File

@ -0,0 +1,6 @@
#include "resources/resource.h"
Resource::Resource(const FString &name, const FString &path)
: name(name), path(path) {
// Constructor implementation
}

View File

@ -1 +1,8 @@
#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/mesh_instance_3d.h"
#include "components/mesh_filter.h"
MeshInstance3D::MeshInstance3D() {
ComponentManager::GetInstance()->addComponent<MeshFilter>(id,
MeshFilter("test"));
}

View File

@ -1 +1,10 @@
#include "scene/3d/node_3d.h"
#include "scene/3d/node_3d.h"
#include "core/component_manager.h"
#include "components/transform.h"
Node3D::Node3D() {
Transform transform{FVector3(0.f, 0.f, 0.f), FVector3(0.f, 0.f, 0.f),
FVector3(0.f, 0.f, 0.f)};
ComponentManager::GetInstance()->addComponent(id, transform);
}

View File

@ -1 +1,5 @@
#include "scene/node.h"
#include "scene/node.h"
#include "core/component_manager.h"
Node::Node() { id = ComponentManager::GetInstance()->newEntity(); }

30
engine/utils/file.cpp Normal file
View File

@ -0,0 +1,30 @@
#include "utils/file.h"
namespace Utils::File {
FString AssetPath(const FString &path) { return "nitro:/" + path; }
FString ReadTextFile(const FString &path) {
FString result;
FILE *f = fopen(AssetPath(path).c_str(), "rb");
if (!f) {
iprintf("No se pudo abrir el archivo\n");
} else {
iprintf("Archivo abierto\n");
fseek(f, 0, SEEK_END);
size_t size = ftell(f);
rewind(f);
TVector<uint8_t> buffer(size);
fread(buffer.data(), 1, size, f);
fclose(f);
for (auto &byte : buffer)
result += byte;
}
return std::move(result);
}
} // namespace Utils::File

View File

@ -0,0 +1,7 @@
#include "mesh_filter.h"
#include "resources/resource.h"
MeshFilter::MeshFilter(const FString &name) : meshName(name) {
mesh = LoadResource<Mesh>(name);
}

View File

@ -1,12 +1,18 @@
#ifndef MESH_FILTER_H
#define MESH_FILTER_H
#include "component.h"
#include "core/types.h"
struct MeshFilter {
FString meshName;
class Mesh;
MeshFilter(const FString &name) : meshName(name) {}
struct MeshFilter : public Component {
public:
MeshFilter(const FString &name);
private:
FString meshName;
PtrShr<Mesh> mesh;
};
#endif // MESH_FILTER_H

View File

@ -21,6 +21,7 @@
#include <calico/types.h>
#include <array>
#include <vector>
#include <string>
/////////////////////////////////////
// MEMORY

View File

@ -1,6 +1,7 @@
#ifndef XRBDS_RESOURCES_MESH_H
#define XRBDS_RESOURCES_MESH_H
#include "resource.h"
#include "core/types.h"
#include <vector>
@ -14,15 +15,15 @@ struct Vertex {
: position(pos), normal(norm), texCoords(tex) {}
};
class Mesh {
class Mesh : public Resource {
public:
static PtrShr<Mesh> Load(std::string path);
static PtrShr<Mesh> Load(FString path);
const TVector<Vertex> &getVertices() const;
const TVector<unsigned int> &getIndices() const;
void setVertices(const std::vector<Vertex> &vertices);
void setIndices(const std::vector<unsigned int> &indices);
void setVertices(const TVector<Vertex> &vertices);
void setIndices(const TVector<unsigned int> &indices);
private:
TVector<Vertex> m_vertices;

View File

@ -0,0 +1,22 @@
#ifndef RESOURCE_H
#define RESOURCE_H
#include "core/types.h"
#include <stdio.h>
template <typename T, typename... Args> PtrShr<T> LoadResource(Args &&...args) {
auto ptr = std::make_shared<T>(std::forward<Args>(args)...);
return ptr;
}
class Resource {
public:
Resource(const FString &name, const FString &path);
private:
FString name;
FString path;
};
#endif // RESOURCE_H

View File

@ -5,16 +5,11 @@
#include "resources/mesh.h"
class MeshInstance3D : public Node3D {
public:
using Super = Node3D;
public:
MeshInstance3D();
virtual ~MeshInstance3D();
// Add methods to set/get mesh, materials, etc.
void set_mesh(const PtrShr<Mesh> &mesh);
PtrShr<Mesh> get_mesh() const;
private:
PtrShr<Mesh> mesh_;
};
#endif // XRBDS_SCENE_3D_MESH_INSTANCE_H

View File

@ -1,27 +1,13 @@
#ifndef XRBDS_SCENE_3D_NODE_3D_H
#define XRBDS_SCENE_3D_NODE_3D_H
class Node3D {
#include "scene/node.h"
struct Node3D : public Node {
using Super = Node;
public:
Node3D();
virtual ~Node3D();
// Set and get position
void setPosition(float x, float y, float z);
void getPosition(float &x, float &y, float &z) const;
// Set and get rotation
void setRotation(float pitch, float yaw, float roll);
void getRotation(float &pitch, float &yaw, float &roll) const;
// Set and get scale
void setScale(float x, float y, float z);
void getScale(float &x, float &y, float &z) const;
private:
float position[3];
float rotation[3];
float scale[3];
};
#endif // XRBDS_SCENE_3D_NODE_3D_H

View File

@ -0,0 +1,40 @@
#ifndef XRBDS_SCENE_NODE_H
#define XRBDS_SCENE_NODE_H
#include "core/types.h"
#include "core/component_manager.h"
/**
* @brief Creates a new shared pointer to a node of type T, forwarding the
* provided arguments to the constructor.
*
* @tparam T The type of the node to be created.
* @tparam Args The types of the arguments to be forwarded to the constructor of
* T.
* @param args The arguments to be forwarded to the constructor of T.
* @return PtrShr<T> A shared pointer to the newly created node of type T.
*/
template <typename T, typename... Args> PtrShr<T> NewNode(Args &&...args) {
auto ptr = std::make_shared<T>(std::forward<Args>(args)...);
return ptr;
}
/**
* @brief Represents a node in the scene graph.
*
* The Node class serves as a base structure for elements in the scene graph.
* It contains an identifier for the entity it represents.
*/
struct Node {
public:
Node();
template <typename T> PtrShr<T> getComponent() {
return ComponentManager::GetInstance()->getComponent<T>(id);
}
protected:
FEntity id;
};
#endif // XRBDS_SCENE_NODE_H

View File

@ -0,0 +1,12 @@
#ifndef XRBDS_UTILS_FILE_H
#define XRBDS_UTILS_FILE_H
#include "core/types.h"
namespace Utils::File {
FString ReadTextFile(const FString &path);
} // namespace Utils::File
#endif // XRBDS_UTILS_FILE_H

View File

@ -10,13 +10,16 @@ target_link_libraries(hello_world
)
# Project specific settings
set(TITLE "Hello World")
set(PUBLISHER "Built with xrbDS")
set(DEVELOPER "Xarblanca")
set(TITLE "Hello World") # Title of the game
set(PUBLISHER "Built with xrbDS") # Publisher of the game
set(DEVELOPER "Xarblanca") # Developer of the game
set(ASSETS "assets") # Path to the assets folder
nds_create_rom(
hello_world
NAME ${TITLE}
SUBTITLE1 ${PUBLISHER}
SUBTITLE2 ${DEVELOPER}
NITROFS ${ASSETS}
ICON "${CMAKE_CURRENT_SOURCE_DIR}/icon.bmp"
)

View File

@ -0,0 +1 @@
ahuevo

Binary file not shown.

After

Width:  |  Height:  |  Size: 594 B