diff options
| author | 0scar <qgt268@alumni.ku.dk> | 2024-02-19 07:14:06 +0000 |
|---|---|---|
| committer | 0scar <qgt268@alumni.ku.dk> | 2024-02-19 07:18:45 +0000 |
| commit | b0bfd3cf610fdf243503447f6ad9fa19de502365 (patch) | |
| tree | a519569bb12219f685c32427972da67ee345ff82 /src | |
| parent | bfe5cc29b1f31fe4921264a96a58607c836ee516 (diff) | |
Add texture loading
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/src/loop.c | 28 | ||||
| -rw-r--r-- | src/rendering/include/engine/rendering/rendering.h | 11 | ||||
| -rw-r--r-- | src/rendering/src/rendering.c | 25 | ||||
| -rw-r--r-- | src/resources/include/engine/resources.h | 16 | ||||
| -rw-r--r-- | src/resources/include/engine/resources/texture.h | 10 | ||||
| -rw-r--r-- | src/resources/src/resources.c | 10 | ||||
| -rw-r--r-- | src/resources/src/textures.c | 41 |
7 files changed, 85 insertions, 56 deletions
diff --git a/src/core/src/loop.c b/src/core/src/loop.c index 534acce..d083e6f 100644 --- a/src/core/src/loop.c +++ b/src/core/src/loop.c @@ -44,12 +44,6 @@ static Camera default_camera = { input_callback_t* callbacks[128]; usize callbacks_len; -/* TODO: MOVE ME */ -#include <glad/gl.h> -char *img_filename = "test.png"; -unsigned char *test_image = NULL; -GLuint img_texture; - i32 nproc(void) { return get_nprocs(); } @@ -247,28 +241,6 @@ Platform* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight p->cam = &default_camera; glm_ortho_default(45.f, p->cam->per); - { - int width; - int height; - int components_per_pixel; - test_image = - stbi_load(img_filename, &width, &height, &components_per_pixel, 0); - if (test_image == NULL) { - ERROR("Failed to load image %s", img_filename); - } else { - const GladGLContext* gl = w->context; - gl->GenTextures(1, &img_texture); - gl->BindTexture(GL_TEXTURE_2D, img_texture); - - gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, - GL_UNSIGNED_BYTE, test_image); - - gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - - stbi_image_free(test_image); - } - } // TODO: Add global bindings INFO("Available cores: %d", nproc()); diff --git a/src/rendering/include/engine/rendering/rendering.h b/src/rendering/include/engine/rendering/rendering.h index 87de0b2..1412cb4 100644 --- a/src/rendering/include/engine/rendering/rendering.h +++ b/src/rendering/include/engine/rendering/rendering.h @@ -20,6 +20,7 @@ extern "C" { #define RGB(_r, _g, _b) RGBA(_r, _g, _b, 0xFF) /* Types */ +/* TODO: Cleanup these types. */ typedef struct { u8 r; u8 g; @@ -28,6 +29,14 @@ typedef struct { } Engine_color; typedef struct { + /* Maybe implement types, such as `atlas` (default), `standalone`, or + * something idk. */ + u32 id; + i32 width; + i32 height; +} Texture; + +typedef struct { u32 texture_id; v2_i32 coord; } Sprite; @@ -145,6 +154,8 @@ u32 ComposeShader(u32 *shaders, usize shaders_len); ShaderType guess_shadertype_from_filename(const char *restrict fname); +Texture createTextureFromImageData(unsigned char* image_data, i32 width, i32 height); + #ifdef __cplusplus } #endif diff --git a/src/rendering/src/rendering.c b/src/rendering/src/rendering.c index aa9306e..d475ee2 100644 --- a/src/rendering/src/rendering.c +++ b/src/rendering/src/rendering.c @@ -252,3 +252,28 @@ Sprite sprite_new(u64 tid, u8 coord) { .y = ts * ((coord & 0xF0) >> 4), }}; } + +Texture createTextureFromImageData(unsigned char* image_data, i32 width, i32 height) { + Window* restrict w = GLOBAL_PLATFORM->window; + Texture t; + t.width = width; + t.height = height; + + if (w->renderer != WINDOW_RENDERER_OPENGL) { + ERROR("createTextureFromImageData not implemented for chosen renderer!"); + return (Texture){.id = 0, .width = 0, .height = 0}; + } + + const GladGLContext* gl = w->context; + + gl->GenTextures(1, &t.id); + gl->BindTexture(GL_TEXTURE_2D, t.id); + + gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, + GL_UNSIGNED_BYTE, image_data); + + gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + return t; +} diff --git a/src/resources/include/engine/resources.h b/src/resources/include/engine/resources.h index fe78a7d..707bbde 100644 --- a/src/resources/include/engine/resources.h +++ b/src/resources/include/engine/resources.h @@ -65,8 +65,8 @@ typedef struct { typedef struct { enum Asset type; const char* path; - i32 width; - i32 height; + /* Bits per component, set to zero if you don't want to change the format. */ + i32 bpc; } Asset_TextureSpec; typedef union { @@ -90,21 +90,25 @@ typedef struct { /* Loaded assets */ usize shader_len; Shader* shader; + + usize texture_len; + Texture* texture; } 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_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); +//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. diff --git a/src/resources/include/engine/resources/texture.h b/src/resources/include/engine/resources/texture.h index 979dd25..9f533d6 100644 --- a/src/resources/include/engine/resources/texture.h +++ b/src/resources/include/engine/resources/texture.h @@ -6,14 +6,10 @@ extern "C" { #endif #include <engine/core/types.h> +#include <engine/resources.h> +#include <engine/rendering/rendering.h> -typedef struct { - const i32 tilesize; - const i32 width; - const i32 height; -} Texture; - -Texture* load_texture(void* render, const Asset_TextureSpec* ts); +Texture load_texture(const Asset_TextureSpec *restrict ts); #ifdef __cplusplus } diff --git a/src/resources/src/resources.c b/src/resources/src/resources.c index 99f6f99..a42a864 100644 --- a/src/resources/src/resources.c +++ b/src/resources/src/resources.c @@ -42,6 +42,7 @@ i32 resources_load(Resources *resources) { isize i = 0; + // Count each type of resource so we can allocate appropriate sizes. for (i = 0; i < resources->assets_len; i++) { isize idx = 0; @@ -50,7 +51,7 @@ i32 resources_load(Resources *resources) { 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_texture: idx = texture_len++; break; case Asset_error: default: @@ -59,13 +60,14 @@ i32 resources_load(Resources *resources) { 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()); + resources->texture = calloc(texture_len, sizeof(Texture)); Shader* imm_shader = calloc(shader_len, sizeof(Shader)); @@ -110,8 +112,8 @@ i32 resources_load(Resources *resources) { resources->shader[resources->shader_len++] = s; } break; case Asset_texture: - texture_len++; - WARN("Texture resource type not implemented!"); + resources->texture[resources->texture_len++] = load_texture(&resources->assets[i].texture); + resources->texture_len++; break; case Asset_error: diff --git a/src/resources/src/textures.c b/src/resources/src/textures.c index cde56b7..7c205ef 100644 --- a/src/resources/src/textures.c +++ b/src/resources/src/textures.c @@ -1,22 +1,41 @@ #include <engine/core/logging.h> #include <engine/resources.h> +#include <stb/stb_image.h> -// Use the engine/render.h backend to create a renderer-specific texture - -Texture* load_texture(void* render, const Asset_TextureSpec* ts) { - Texture* t = NULL; +/* 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 NULL; + return err; + } + + if (ts->path == NULL) { + ERROR("Missing path in Asset_TextureSpec\n"); + return err; } - //t = (Texture*)malloc(sizeof(Texture)); - //t->texture = new_texture; - ///* Assigning const value */ - //*(i32*)&t->tilesize = tw; - //*(i32*)&t->width = ts->width; - //*(i32*)&t->height = ts->height; + 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); + stbi_image_free(img); + } + + if (t.id == 0) { + ERROR("Failed to create texture %s!", ts->path); + return err; + } return t; } |
