summaryrefslogtreecommitdiff
path: root/src/resources
diff options
context:
space:
mode:
Diffstat (limited to 'src/resources')
-rw-r--r--src/resources/CMakeLists.txt1
-rw-r--r--src/resources/include/engine/resources.h95
-rw-r--r--src/resources/src/resources.c135
3 files changed, 187 insertions, 44 deletions
diff --git a/src/resources/CMakeLists.txt b/src/resources/CMakeLists.txt
index 0c14524..6cb68d3 100644
--- a/src/resources/CMakeLists.txt
+++ b/src/resources/CMakeLists.txt
@@ -6,6 +6,7 @@
#FetchContent_MakeAvailable(assimp)
add_library(daw_resources
+ src/resources.c
src/fonts.c
src/scripts.c
src/textures.c
diff --git a/src/resources/include/engine/resources.h b/src/resources/include/engine/resources.h
index e151d7e..cefae24 100644
--- a/src/resources/include/engine/resources.h
+++ b/src/resources/include/engine/resources.h
@@ -6,8 +6,8 @@
// TODO
/* We need some "global resources", available to all states.
- * These are resources such as common fonts, GUI frames, button background
- * images.
+ * 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?
@@ -16,81 +16,84 @@
* font?
* - Then declare to the engine, in the main function for the game, that these
* resources are to be made available throughout?
- * - Make all resource specifications A UNION?! 🤯
+ * - 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_audio,
};
typedef struct {
enum Asset type;
- const char* font_path;
- i32 ptsize;
+ 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;
- i32 width;
- i32 height;
-} Asset_TextureSpec;
+} 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;
-} Asset_AudioSpec;
+ i32 width;
+ i32 height;
+} Asset_TextureSpec;
typedef union {
enum Asset type;
+ Asset_AudioSpec audio;
Asset_FontSpec font;
+ Asset_ShaderSpec shader;
+ Asset_ShaderProgramSpec shaderprog;
Asset_TextureSpec texture;
- Asset_AudioSpec audio;
} asset_t;
// The resource spec
typedef struct {
- // Was:
-// usize textures_len;
-// usize textures_size;
-// usize fonts_len;
-//
-// usize texturepaths_len;
-// usize fontpaths_len;
-//
-// /* Paths for our sources, kept in case the user wants to reload them */
-// Asset_TextureSpec** texture_paths;
-// Asset_FontSpec** font_paths;
-//
-// /* Our actual sources */
-// Texture** textures;
-// //TTF_Font** fonts;
+ /* Assorted asset specification, makes reloading them easier. */
+ usize assets_len;
+ asset_t* assets;
- // But with the new way:
- // usize assets_len;
- // asset_t assets*;
- Shader* shaders;
- usize shaders_len;
-} Resources;
-
-#define Resource_FontDefinition(_path, _fontsize) \
- (const Asset_FontSpec) { \
- .type = Asset_font, .font_path = _path, .ptsize = _fontsize \
- }
+ /* Translation from `assets`'s indices to type-specific loaded assets: */
+ usize* get; // Let r=Resources, then use as: `r.shader[ r.get[ MyShader ] ]`
-#define Resource_TextureAtlasDefinition(_path, _subtexture_width, \
- _subtexture_height) \
- (const Asset_TextureSpec) { \
- .type = Asset_texture, .width = _subtexture_width, \
- .height = _subtexture_height, .path = _path \
- }
+ /* Loaded assets */
+ usize shader_len;
+ Shader* shader;
+} 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}}
+
+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.
@@ -99,6 +102,10 @@ 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);
diff --git a/src/resources/src/resources.c b/src/resources/src/resources.c
new file mode 100644
index 0000000..99f6f99
--- /dev/null
+++ b/src/resources/src/resources.c
@@ -0,0 +1,135 @@
+#include <string.h>
+
+#include <engine/core/types.h>
+#include <engine/core/logging.h>
+#include <engine/core/platform.h>
+#include <engine/resources.h>
+
+extern Platform* 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[idx] = %d", 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:
+ 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));
+ isize audio_len = 0;
+ isize font_len = 0;
+ isize shader_len = 0;
+ isize shaderprog_len = 0;
+ isize texture_len = 0;
+
+ isize i = 0;
+
+ for (i = 0; i < resources->assets_len; i++) {
+ isize 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++; WARN("Texture resource type not implemented!");break;
+
+ case Asset_error:
+ default:
+ ERROR("Unknown resource type!");
+ exit(EXIT_FAILURE);
+ break;
+ }
+
+ 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());
+
+ 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++) {
+ isize idx = 0;
+
+ 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;
+ } break;
+ case Asset_shaderprog: {
+ const isize sz = resources->assets[i].shaderprog.shader_len;
+ Shader* shaders = calloc(sz, sizeof(Shader));
+
+ for (int 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]];
+ shaders[j] = imm_shader[j];
+ DEBUG("shader[%d] = %d -- %s\n", j, shaders[j].program, ShaderType_str[shaders[j].type]);
+ }
+ const Shader s = compose_shader(shaders, sz);
+ DEBUG("shader = %d -- %s\n", s.program, ShaderType_str[s.type]);
+
+ resources->shader[resources->shader_len++] = s;
+ } break;
+ case Asset_texture:
+ texture_len++;
+ WARN("Texture resource type not implemented!");
+ break;
+
+ case Asset_error:
+ default:
+ ERROR("Unknown resource type!");
+ exit(EXIT_FAILURE);
+ break;
+ }
+
+ resources->get[i] = idx;
+ }
+
+ free(imm_shader);
+
+ return 0;
+}
+
+isize resource_make_global(isize resource_id) {
+ ERROR("`resource_make_global` Not implemented");
+ return -1;
+}