Skip to content

Commit

Permalink
added a method of loading sprite shaders from an in-memory buffer in …
Browse files Browse the repository at this point in the history
…order to support cases where the licensing terms of a given piece of artwork requires embedding or otherwise obscuring the piece

Signed-off-by: Addison Schuhardt <addison@schuhardt.net>
  • Loading branch information
aschuhardt committed Jun 1, 2021
1 parent bbe01e3 commit 0d096e2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
13 changes: 11 additions & 2 deletions include/shader/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ typedef struct procy_sprite_shader_program_t {
} procy_sprite_shader_program_t;

/*
* Builds and compiles a shader program with information for rendering text
* sprites from a bitmap font.
* Builds and compiles a shader program capable of rendering sprites based on a
* texture derived from an image file stored in-memory
*
*/
procy_sprite_shader_program_t *procy_create_sprite_shader_mem(
unsigned char *contents, size_t length);

/*
* Builds and compiles a shader program capable of rendering sprites based on a
* texture derived from an on-disk image file
*/

procy_sprite_shader_program_t *procy_create_sprite_shader(const char *path);

/*
Expand Down
46 changes: 37 additions & 9 deletions src/shader/sprite.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "shader/sprite.h"

#include <errno.h>
#include <log.h>
#include <string.h>

Expand Down Expand Up @@ -74,27 +75,25 @@ static void disable_shader_attributes() {
}

static bool load_sprite_texture(sprite_shader_program_t *shader,
const char *path) {
unsigned char *contents, size_t length) {
if (glIsTexture(shader->texture)) {
GL_CHECK(glDeleteTextures(1, &shader->texture));
}

int components, width, height;
unsigned char *bitmap =
stbi_load(path, &shader->texture_w, &shader->texture_h, &components, 0);
unsigned char *bitmap = stbi_load_from_memory(
contents, length, &shader->texture_w, &shader->texture_h, &components, 0);

if (bitmap == NULL || shader->texture_w < 0 || shader->texture_h < 0) {
log_error("Failed to read sprite texture from %s", path);

const char *msg = stbi_failure_reason();
if (msg != NULL) {
log_error("STBI failure message: %s", msg);
}

return false;
} else {
log_debug("Loaded texture from \"%s\" (size: %dx%d; comp: %d)", path,
shader->texture_w, shader->texture_h, components);
log_debug("Loaded texture (size: %dx%d; comp: %d)", shader->texture_w,
shader->texture_h, components);
// copy both bitmap buffers into a single location
const size_t bitmap_size = (size_t)shader->texture_w * shader->texture_h;
if (bitmap_size != 0) {
Expand Down Expand Up @@ -169,7 +168,8 @@ static void draw_sprite_batch(shader_program_t *const program,
/* Public interface definition */
/* --------------------------- */

sprite_shader_program_t *procy_create_sprite_shader(const char *path) {
sprite_shader_program_t *procy_create_sprite_shader_mem(unsigned char *contents,
size_t length) {
sprite_shader_program_t *sprite_shader =
malloc(sizeof(sprite_shader_program_t));

Expand All @@ -193,7 +193,7 @@ sprite_shader_program_t *procy_create_sprite_shader(const char *path) {
procy_compile_frag_shader((char *)embed_sprite_frag,
&program->fragment))) {
// load font texture and codepoints
if (!load_sprite_texture(sprite_shader, path)) {
if (!load_sprite_texture(sprite_shader, contents, length)) {
procy_destroy_sprite_shader(sprite_shader);
return NULL;
}
Expand All @@ -219,6 +219,34 @@ sprite_shader_program_t *procy_create_sprite_shader(const char *path) {
return sprite_shader;
}

sprite_shader_program_t *procy_create_sprite_shader(const char *path) {
FILE *file = fopen(path, "rb");
if (file == NULL) {
log_error("Failed to open sprite texture file at \"%s\": %s", path,
strerror(errno));
return NULL;
}

fseek(file, 0, SEEK_END);
size_t len = ftell(file);
unsigned char *buffer = malloc(sizeof(unsigned char) * len);
if (buffer == NULL) {
log_error("Failed to allocate %zu bytes of memory for \"%s\"", len, path);
fclose(file);
return NULL;
}

fseek(file, 0, SEEK_SET);
fread(buffer, sizeof(unsigned char), len, file);
fclose(file);

sprite_shader_program_t *shader = procy_create_sprite_shader_mem(buffer, len);

free(buffer);

return shader;
}

static void compute_sprite_vertices(sprite_shader_program_t *shader,
draw_op_t *const op,
sprite_vertex_t *const vertices,
Expand Down

0 comments on commit 0d096e2

Please sign in to comment.