Added texture loading.

This commit is contained in:
2021-04-03 19:49:33 +11:00
parent 73decbfe39
commit 7e62552b86
7 changed files with 64 additions and 36 deletions

View File

@ -7,6 +7,11 @@
#include "asset.h"
#ifndef STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#endif
char * assetStringLoad(char *assetName) {
// Open a buffer.
FILE *fptr = assetBufferOpen(assetName);
@ -88,4 +93,37 @@ shader_t * assetShaderLoad(char *fileVertex, char *fileFragment) {
free(fragmentShader);
return shader;//shader may be NULL if loading failed, but not our problem.
}
texture_t * assetTextureLoad(char *fileName) {
FILE *buffer;
texture_t *texture;
int channels, width, height;
pixel_t *data;
stbi_io_callbacks OPENGL_STBI_CALLBACKS;
buffer = assetBufferOpen(fileName);
if(buffer == NULL) return NULL;
// Setup the interface for STBI
OPENGL_STBI_CALLBACKS.read = &assetBufferRead;
OPENGL_STBI_CALLBACKS.skip = &assetBufferSkip;
OPENGL_STBI_CALLBACKS.eof = &assetBufferEnd;
// Buffer the image
channels = 0;
data = (pixel_t *)stbi_load_from_callbacks(
&OPENGL_STBI_CALLBACKS, buffer,
&width, &height,
&channels, STBI_rgb_alpha
);
// Close the buffer
assetBufferClose(buffer);
if(data == NULL) return NULL;
// Turn into a texture.
texture = textureCreate(width, height, data);
stbi_image_free(data);
return texture;
}

View File

@ -75,4 +75,9 @@ void assetBufferSkip(FILE *buffer, int32_t n);
* @param fileFragment The file path of the fragment shader
* @return The loaded shader_t instance (From shaderCompile)
*/
shader_t * assetShaderLoad(char *fileVertex, char *fileFragment);
shader_t * assetShaderLoad(char *fileVertex, char *fileFragment);
/**
* Load a texture from a PNG file.
*/
texture_t * assetTextureLoad(char *fileName);