From d38deeef3af2316a666f8fc0173940bd769b748e Mon Sep 17 00:00:00 2001 From: onelin Date: Sat, 1 Nov 2025 00:55:42 +0100 Subject: Flatten project structure This will make it easier to break up the code into smaller chunks again later. One would think doing this seems fun to me at this point. --- src/resources/CMakeLists.txt | 19 --- src/resources/include/engine/resources.h | 138 -------------------- src/resources/include/engine/resources/model.h | 30 ----- src/resources/include/engine/resources/texture.h | 17 --- src/resources/src/model.c | 68 ---------- src/resources/src/resources.c | 154 ----------------------- src/resources/src/textures.c | 41 ------ 7 files changed, 467 deletions(-) delete mode 100644 src/resources/CMakeLists.txt delete mode 100644 src/resources/include/engine/resources.h delete mode 100644 src/resources/include/engine/resources/model.h delete mode 100644 src/resources/include/engine/resources/texture.h delete mode 100644 src/resources/src/model.c delete mode 100644 src/resources/src/resources.c delete mode 100644 src/resources/src/textures.c (limited to 'src/resources') diff --git a/src/resources/CMakeLists.txt b/src/resources/CMakeLists.txt deleted file mode 100644 index 4d3ee22..0000000 --- a/src/resources/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -## Uncomment the following to fetch and build assimp. -#FetchContent_Declare(assimp -# GIT_REPOSITORY https://github.com/assimp/assimp.git -# GIT_TAG v5.3.1 -#) -#FetchContent_MakeAvailable(assimp) - -add_library(daw_resources - src/resources.c - src/textures.c - src/model.c - ) - -target_compile_options(daw_resources PUBLIC ${BUILD_OPTS}) -target_include_directories(daw_resources PRIVATE ${DAW_INCLUDE_DIRS} stb) -target_link_libraries(daw_resources PRIVATE - assimp - cglm - ) diff --git a/src/resources/include/engine/resources.h b/src/resources/include/engine/resources.h deleted file mode 100644 index d967403..0000000 --- a/src/resources/include/engine/resources.h +++ /dev/null @@ -1,138 +0,0 @@ -#ifndef ENGINE_RESOURCES_H -#define ENGINE_RESOURCES_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -// TODO -/* We need some "global resources", available to all states. - * These are resources used throughout the applications lifetime, such as common - * fonts, GUI frames, button background images. - * - * We need to define state-specific resources as well. - * - Can both be defined alike? - * If we lazy-load all resources we can get away with a lot. - * Maybe use fall-back resources? like the missing source texture, and an ugly - * font? - * - Then declare to the engine, in the main function for the game, that these - * resources are to be made available throughout? - * - Shaders, oh boy oh shaders. - * We need to make some meta-shader declaration, so we can declare a set of - * shaders, that are used to link with other shaders, s.t. we can free up the - * shaders after compilation. - * - Make all resource specifications a union. - * */ - -enum Asset { - Asset_error, - Asset_audio, - Asset_font, - Asset_shader, - Asset_shaderprog, - Asset_texture, - Asset_model, -}; - -typedef struct { - enum Asset type; - const char* path; -} Asset_AudioSpec; - -typedef struct { - enum Asset type; - const char* path; -} Asset_FontSpec; - -// if a shader is declared GLOBALLY, we should not destroy it when done with the -// "main" shader compilations. -typedef struct { - enum Asset type; - // Assume shader type from filename - const char* path; -} Asset_ShaderSpec; - -// Use list of gluint shader program ids to link against. This translates to a -// call to compose_shader. -typedef struct { - enum Asset type; - const u32* shader; - const usize shader_len; -} Asset_ShaderProgramSpec; - -typedef struct { - enum Asset type; - const char* path; - /* Bits per component, set to zero if you don't want to change the format. */ - i32 bpc; -} Asset_TextureSpec; - -typedef struct { - enum Asset type; - const char* path; -} Asset_ModelSpec; - -typedef union { - enum Asset type; - Asset_AudioSpec audio; - Asset_FontSpec font; - Asset_ShaderSpec shader; - Asset_ShaderProgramSpec shaderprog; - Asset_TextureSpec texture; - Asset_ModelSpec model; -} asset_t; - -#include - -// The resource spec -typedef struct { - /* Assorted asset specification, makes reloading them easier. */ - usize assets_len; - asset_t* assets; - - /* Translation from `assets`'s indices to type-specific loaded assets: */ - usize* get; // Let r=Resources, then use as: `r.shader[ r.get[ MyShader ] ]` - - /* Loaded assets */ - usize shader_len; - Shader* shader; - - usize texture_len; - Texture* texture; - - usize model_len; - Model* model; -} Resources; - -#define TextureDefinition(_path, ...) unimplemented -#define Resource_AudioDefinition(_path, ...) unimplemented -#define Declare_Shader(PATH) (const asset_t){.shader = {.type = Asset_shader, .path=PATH}} -#define Declare_ShaderProgram(SHADERS, NUMSHADERS) (const asset_t){.shaderprog = {.type = Asset_shaderprog, .shader=SHADERS, .shader_len=NUMSHADERS}} -#define Declare_Texture(PATH) (const asset_t){.texture = {.type = Asset_texture, .path=PATH, .bpc=0}} - -void* get_asset(Resources* r, u32 idx); - -/* Each of resource_load_font, resource_load_texture, and resource_load_audio - * loads a given resource into the engines memory and returns an identifier. - */ -//isize resource_load_font(Asset_FontSpec font_def); -//isize resource_load_texture(Asset_TextureSpec texture_def); -//isize resource_load_audio(Asset_AudioSpec audio_def); - -// Loads all resources specified in `assets` and populates corresponding -// resources members. -i32 resources_load(Resources *resources); - -/* Makes a resource globally available. This must be called **BEFORE** any call - * to `engine_run` */ -isize resource_make_global(isize resource_id); - -#include - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/resources/include/engine/resources/model.h b/src/resources/include/engine/resources/model.h deleted file mode 100644 index a2fb29c..0000000 --- a/src/resources/include/engine/resources/model.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef ENGINE_RESOURCES_MODEL_H -#define ENGINE_RESOURCES_MODEL_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -typedef enum { - Model_error, - Model_obj, -} ModelType; - -typedef struct { - ModelType format; - - u32 m_uiVAO; - u32 m_uiVBO; - u32 m_uiIBO; - unsigned m_uiNumIndices; -} Model; - -#include -Model load_model(const Asset_ModelSpec *restrict ms); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/resources/include/engine/resources/texture.h b/src/resources/include/engine/resources/texture.h deleted file mode 100644 index 9f533d6..0000000 --- a/src/resources/include/engine/resources/texture.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef TEXTURE_H -#define TEXTURE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include - -Texture load_texture(const Asset_TextureSpec *restrict ts); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/resources/src/model.c b/src/resources/src/model.c deleted file mode 100644 index 7244e63..0000000 --- a/src/resources/src/model.c +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#include -#include -#include -#include - -Model load_model(const Asset_ModelSpec *restrict ms) { - ModelType format = Model_error; - - /* - isize vertices_len = 0; - vec3 *vertices = malloc(sizeof(vec3) * 512); - usize *vertices_idx = malloc(sizeof(usize) * 512); - - isize uvs_len = 0; - vec2 *uvs = malloc(sizeof(vec2) * 512); - usize *uvs_idx = malloc(sizeof(usize) * 512); - - isize normals_len = 0; - vec3 *normals = malloc(sizeof(vec3) * 512); - usize *normals_idx = malloc(sizeof(usize) * 512); - */ - - // For now, just default to obj - format = Model_obj; - - FILE* f = fopen(ms->path, "r"); - if (f == NULL) { - ERROR("Failed to load file " TERM_COLOR_YELLOW "%s" TERM_COLOR_RESET, ms->path); - return (Model){.format = format}; - } - const usize filesz = f_get_sz(f); - - char* filecontets = calloc(filesz, sizeof(char)); - fread(filecontets, sizeof(char), filesz, f); - - // Start the import on the given file with some example postprocessing - // Usually - if speed is not the most important aspect for you - you'll t - // probably to request more postprocessing than we do in this example. - const struct aiScene* scene = aiImportFile( filecontets, - aiProcess_CalcTangentSpace | - aiProcess_Triangulate | - aiProcess_JoinIdenticalVertices | - aiProcess_SortByPType); - - free(filecontets); - // If the import failed, report it - if( NULL == scene) { - ERROR("Failed to import %s: %s", ms->path, aiGetErrorString()); - return (Model){.format = format}; - } - - //// Now we can access the file's contents - //DoTheSceneProcessing(scene); - - //// We're done. Release all resources associated with this import - aiReleaseImport(scene); - - { - Model m = {}; - m.format = format; - - // TODO add index array - - return m; - } -} diff --git a/src/resources/src/resources.c b/src/resources/src/resources.c deleted file mode 100644 index 6e35a4f..0000000 --- a/src/resources/src/resources.c +++ /dev/null @@ -1,154 +0,0 @@ -#include - -#include -#include -#include -#include - -extern Instance* GLOBAL_PLATFORM; - -extern const char* ShaderType_str[]; - -void* get_asset(Resources* r, u32 idx) { - switch (r->assets[idx].type) { - case Asset_shader: - // The shaders used to compile shader programs are not preserved. - WARN("We don't store pure shaders"); - break; - - case Asset_shaderprog: - LOG("Idx: r->get[%d] = %d", idx, r->get[idx]); - //LOG("Ptr: &r->shader[r->get[idx]] = %z", &r->shader[r->get[idx]]); - return &r->shader[r->get[idx]]; - - case Asset_texture: - return &r->texture[r->get[idx]]; - - case Asset_error: - case Asset_audio: - case Asset_font: - default: - ERROR("Asset type Not implemented"); - break; - } - return NULL; -} - -i32 resources_load(Resources *resources) { - resources->get = calloc(resources->assets_len, sizeof(usize)); - usize audio_len = 0; - usize font_len = 0; - usize shader_len = 0; - usize shaderprog_len = 0; - usize texture_len = 0; - - usize i = 0; - - // Count each type of resource so we can allocate appropriate sizes. - for (i = 0; i < resources->assets_len; i++) { - usize idx = 0; - - switch (resources->assets[i].type) { - case Asset_audio: idx = audio_len++; WARN("Audio resource type not implemented!"); break; - case Asset_font: idx = font_len++; WARN("Font resource type not implemented!"); break; - case Asset_shader: idx = shader_len++; break; - case Asset_shaderprog: idx = shaderprog_len++; break; - case Asset_texture: idx = texture_len++; break; - - case Asset_error: break; - default: - ERROR("Unknown resource type!"); - exit(EXIT_FAILURE); - break; - } - - // Update index translation table. - resources->get[i] = idx; - } - - //resources->audio = calloc(audio_len, sizeof()); - //resources->font = calloc(font_len, sizeof()); - resources->shader = calloc(shaderprog_len, sizeof(Shader)); - resources->texture = calloc(texture_len, sizeof(Texture)); - - Shader* imm_shader = calloc(shader_len, sizeof(Shader)); - - audio_len = 0; - font_len = 0; - shader_len = 0; - shaderprog_len = 0; - texture_len = 0; - - for (i = 0; i < resources->assets_len; i++) { - switch (resources->assets[i].type) { - case Asset_audio: - //resources->audio_len++; - WARN("Audio resource type not implemented!"); - break; - case Asset_font: - //resources->font_len++; - WARN("Font resource type not implemented!"); - break; - case Asset_shader: { - ShaderType t = - guess_shadertype_from_filename(resources->assets[i].shader.path); - const Shader s = compile_shader(resources->assets[i].shader.path, t); - LOG("Compiled %s! (%s)", resources->assets[i].shader.path, ShaderType_str[t]); - - imm_shader[shader_len] = s; - shader_len++; - } break; - case Asset_shaderprog: { - const usize sz = resources->assets[i].shaderprog.shader_len; - const u32* shader_ids = resources->assets[i].shaderprog.shader; - Shader* shaders = calloc(sz, sizeof(Shader)); - - for (usize j = 0; j < sz; j++) { - //DEBUG("shader[%d] = %d\n", j, imm_shader[resources->assets[i].shaderprog.shader[j]].program); - //shaders[j] = imm_shader[resources->assets[i].shaderprog.shader[j]]; - u32 shader_idx = shader_ids[j]; - //DEBUG("Idx: get[%d] = %d\n", shader_idx, imm_shader[resources->get[shader_idx]].program); - shaders[j] = imm_shader[resources->get[shader_idx]]; - } - - const Shader s = compose_shader(shaders, sz); - - resources->shader[resources->shader_len] = s; - resources->shader_len++; - } break; - case Asset_texture: - resources->texture[resources->texture_len] = load_texture(&resources->assets[i].texture); - resources->texture_len++; - LOG("Loaded texture \"%s\"!", resources->assets[i].texture.path); - break; - - case Asset_error: - break; - default: - ERROR("Unknown resource type!"); - exit(EXIT_FAILURE); - break; - } - - //resources->get[i] = idx; - } - - LOG("Total assets: %lu", resources->assets_len); - LOG(" Shaders: %lu", resources->shader_len); - LOG(" Textures: %lu", resources->texture_len); - - // Delete the immediate shaders - for (i = 0; i < shader_len; i++) { - // glDeleteShader() - } - - shaders_delete(imm_shader, shader_len); - free(imm_shader); - - return 0; -} - -isize resource_make_global(isize resource_id) { - ERROR("`resource_make_global` Not implemented"); - return -1; -} diff --git a/src/resources/src/textures.c b/src/resources/src/textures.c deleted file mode 100644 index 913c931..0000000 --- a/src/resources/src/textures.c +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -#include - -/* Uses stb_image to load an image, and passes it to the renderer for - * backend-specific texture creation. */ -Texture load_texture(const Asset_TextureSpec *restrict ts) { - int width; - int height; - int components_per_pixel; - unsigned char* img; - Texture t; - const Texture err = (Texture){.id = 0, .width = 0, .height = 0}; - - if (ts == NULL) { - ERROR("Invalid Asset_TextureSpec\n"); - return err; - } - - if (ts->path == NULL) { - ERROR("Missing path in Asset_TextureSpec\n"); - return err; - } - - img = stbi_load(ts->path, &width, &height, &components_per_pixel, 0); - - if (img == NULL) { - ERROR("Failed to load image %s", ts->path); - return err; - } else { - t = createTextureFromImageData(img, width, height, (u8)components_per_pixel); - stbi_image_free(img); - } - - if (t.id == 0) { - ERROR("Failed to create texture %s!", ts->path); - return err; - } - - return t; -} -- cgit v1.3