Dengine/src/AssetManager.c

94 lines
2.3 KiB
C
Raw Normal View History

#define STBI_FAILURE_USERMSG
#define STB_IMAGE_IMPLEMENTATION
#include <STB/stb_image.h>
#include <Dengine/Platform.h>
#include <Dengine/AssetManager.h>
GLOBAL_VAR AssetManager assetManager;
Texture *asset_getTexture(const enum TexList type)
2016-06-08 07:29:16 +00:00
{
// NOTE(doyle): Since we're using a map, the count of an object can
// only be 1 or 0
if (type < texlist_count)
return &assetManager.textures[type];
2016-06-08 07:29:16 +00:00
return NULL;
2016-06-08 07:29:16 +00:00
}
const i32 asset_loadTextureImage(const char *const path, const enum TexList type)
2016-06-08 07:29:16 +00:00
{
/* Open the texture image */
i32 imgWidth, imgHeight, bytesPerPixel;
stbi_set_flip_vertically_on_load(TRUE);
2016-06-17 14:40:40 +00:00
u8 *image =
stbi_load(path, &imgWidth, &imgHeight, &bytesPerPixel, 0);
2016-06-08 07:29:16 +00:00
if (!image)
{
printf("stdbi_load() failed: %s\n", stbi_failure_reason());
2016-06-08 07:29:16 +00:00
return -1;
}
2016-06-17 14:40:40 +00:00
Texture tex = genTexture(CAST(GLuint)(imgWidth), CAST(GLuint)(imgHeight),
CAST(GLint)(bytesPerPixel), image);
glCheckError();
2016-06-08 07:29:16 +00:00
stbi_image_free(image);
assetManager.textures[type] = tex;
2016-06-08 07:29:16 +00:00
return 0;
}
Shader *asset_getShader(const enum ShaderList type)
2016-06-08 07:29:16 +00:00
{
if (type < shaderlist_count)
return &assetManager.shaders[type];
2016-06-08 07:29:16 +00:00
return NULL;
2016-06-08 07:29:16 +00:00
}
INTERNAL GLuint createShaderFromPath(const char *const path, GLuint shadertype)
2016-06-08 07:29:16 +00:00
{
PlatformFileReadResult file = {0};
2016-06-08 07:29:16 +00:00
i32 status = platform_readFileToBuffer(path, &file);
if (status)
return status;
2016-06-08 07:29:16 +00:00
const GLchar *source = CAST(char *)file.buffer;
2016-06-08 07:29:16 +00:00
GLuint result = glCreateShader(shadertype);
glShaderSource(result, 1, &source, NULL);
glCompileShader(result);
GLint success;
GLchar infoLog[512];
glGetShaderiv(result, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(result, 512, NULL, infoLog);
printf("glCompileShader() failed: %s\n", infoLog);
}
platform_closeFileReadResult(&file);
2016-06-08 07:29:16 +00:00
return result;
}
const i32 asset_loadShaderFiles(const char *const vertexPath,
const char *const fragmentPath,
const enum ShaderList type)
2016-06-08 07:29:16 +00:00
{
2016-06-17 14:40:40 +00:00
GLuint vertexShader = createShaderFromPath(vertexPath, GL_VERTEX_SHADER);
GLuint fragmentShader =
createShaderFromPath(fragmentPath, GL_FRAGMENT_SHADER);
2016-06-08 07:29:16 +00:00
Shader shader;
2016-06-17 14:40:40 +00:00
i32 result = shader_loadProgram(&shader, vertexShader, fragmentShader);
2016-06-08 07:29:16 +00:00
if (result)
return result;
assetManager.shaders[type] = shader;
2016-06-08 07:29:16 +00:00
return 0;
}