Added texture loading.
This commit is contained in:
@ -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;
|
||||
}
|
@ -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);
|
Reference in New Issue
Block a user