Reshuffled
This commit is contained in:
129
src/file/asset.c
Normal file
129
src/file/asset.c
Normal file
@ -0,0 +1,129 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Msters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#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);
|
||||
if(fptr == NULL) return NULL;
|
||||
|
||||
// Read the count of bytes in the file
|
||||
fseek(fptr, 0, SEEK_END);// Seek to the end
|
||||
size_t length = ftell(fptr);// Get our current position (the end)
|
||||
fseek(fptr, 0, SEEK_SET);// Reset the seek
|
||||
|
||||
// Create the string buffer
|
||||
char *str = malloc(length + 1);// Add 1 for the null terminator.
|
||||
if(str == NULL) {
|
||||
assetBufferClose(fptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Read and seal the string.
|
||||
fread(str, 1, length, fptr);// Read all the bytes
|
||||
str[length] = '\0';// Null terminate.
|
||||
assetBufferClose(fptr); // Close the buffer.
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
FILE * assetBufferOpen(char *assetName) {
|
||||
// Get the directory based on the raw input by creating a new string.
|
||||
size_t lenAsset = strlen(assetName);// Get the length of asset
|
||||
size_t lenPrefix = strlen(ASSET_PREFIX);// Get the length of the prefix
|
||||
|
||||
// Create str to house both the prefix and asset, and null terminator
|
||||
char *joined = malloc(lenAsset + lenPrefix + 1);
|
||||
if(joined == NULL) return NULL;// Mem okay?
|
||||
|
||||
joined[0] = '\0';//Start at null
|
||||
strcat(joined, ASSET_PREFIX);//Add prefix
|
||||
strcat(joined, assetName);//Add body
|
||||
|
||||
// Open the file pointer now.
|
||||
FILE *fptr = fopen(joined, "rb");
|
||||
free(joined);// Free the string we just created
|
||||
if(!fptr) return NULL;// File available?
|
||||
return fptr;
|
||||
}
|
||||
|
||||
bool assetBufferClose(FILE *buffer) {
|
||||
return fclose(buffer);
|
||||
}
|
||||
|
||||
int32_t assetBufferRead(FILE *buffer, char *data, int32_t size) {
|
||||
return (int32_t)fread(data, 1, size, buffer);
|
||||
}
|
||||
|
||||
int32_t assetBufferEnd(FILE *buffer) {
|
||||
return feof(buffer);
|
||||
}
|
||||
|
||||
void assetBufferSkip(FILE *buffer, int32_t n) {
|
||||
fseek(buffer, n, SEEK_CUR);
|
||||
}
|
||||
|
||||
shader_t * assetShaderLoad(char *fileVertex, char *fileFragment) {
|
||||
// Load the vertex shader into memory
|
||||
char *vertexShader = assetStringLoad(fileVertex);
|
||||
if(vertexShader == NULL) return NULL;
|
||||
|
||||
// Load the fragment shader into memory
|
||||
char *fragmentShader = assetStringLoad(fileFragment);
|
||||
if(fragmentShader == NULL) {
|
||||
free(vertexShader);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Now attempt to load the shader
|
||||
shader_t *shader = shaderCompile(vertexShader, fragmentShader);
|
||||
|
||||
//Cleanup
|
||||
free(vertexShader);
|
||||
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;
|
||||
}
|
85
src/file/asset.h
Normal file
85
src/file/asset.h
Normal file
@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Msters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include "../display/shader.h"
|
||||
#include "../display/texture.h"
|
||||
|
||||
/** Prefix of all asset load methods, may be customizable in future. */
|
||||
#define ASSET_PREFIX "../assets/"
|
||||
|
||||
/**
|
||||
* Method to load an asset into memory as a raw string.
|
||||
*
|
||||
* @param assetName Path leading to the asset within the root asset directory.
|
||||
* @return Pointer to char array of data from asset, NULL if unsuccesful.
|
||||
*/
|
||||
char * assetStringLoad(char *assetName);
|
||||
|
||||
/**
|
||||
* Platform-centric method to open a file buffer to an asset.
|
||||
*
|
||||
* @param assetName The asset name to open a buffer for.
|
||||
* @return Pointer to a buffer, NULL if unsuccessfuil.
|
||||
*/
|
||||
FILE * assetBufferOpen(char *assetName);
|
||||
|
||||
/**
|
||||
* Closes a previously opened asset buffer.
|
||||
*
|
||||
* @param buffer Buffer to close.
|
||||
* @return True if successful, otherwise false.
|
||||
*/
|
||||
bool assetBufferClose(FILE *buffer);
|
||||
|
||||
/**
|
||||
* Read bytes from buffer.
|
||||
*
|
||||
* @param buffer The buffer pointing to an asset.
|
||||
* @param data Pointer to a ubyte array to buffer data into.
|
||||
* @param size Length of the data buffer. Represents how many bytes can be read.
|
||||
* @return The count of bytes read. Complete when less than data array size.
|
||||
*/
|
||||
int32_t assetBufferRead(FILE *buffer, char *data, int32_t size);
|
||||
|
||||
/**
|
||||
* Skip to the end of the buffer, useful to find the length of the buffer.
|
||||
*
|
||||
* @param Buffer The buffer pointing to an asset.
|
||||
* @return How many bytes were skipped
|
||||
*/
|
||||
int32_t assetBufferEnd(FILE *buffer);
|
||||
|
||||
/**
|
||||
* Method to skip n bytes in the buffer
|
||||
*
|
||||
* @param buffer The buffer pointing to an asset.
|
||||
* @param n Count of bytes to skip.
|
||||
*/
|
||||
void assetBufferSkip(FILE *buffer, int32_t n);
|
||||
|
||||
/**
|
||||
* Load a shader program from a vertex and fragment shader file.
|
||||
*
|
||||
* @param fileVertex The file path of the vertex shader
|
||||
* @param fileFragment The file path of the fragment shader
|
||||
* @return The loaded shader_t instance (From shaderCompile)
|
||||
*/
|
||||
shader_t * assetShaderLoad(char *fileVertex, char *fileFragment);
|
||||
|
||||
/**
|
||||
* Load a texture from a PNG file.
|
||||
*
|
||||
* @param fileName The fike path of the PNG image.
|
||||
* @return The loaded texture object.
|
||||
*/
|
||||
texture_t * assetTextureLoad(char *fileName);
|
Reference in New Issue
Block a user