2016-06-04 12:42:22 +00:00
|
|
|
#define STBI_FAILURE_USERMSG
|
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
|
|
#include <STB/stb_image.h>
|
|
|
|
|
2016-06-18 09:12:09 +00:00
|
|
|
#include <Dengine/Platform.h>
|
|
|
|
#include <Dengine/AssetManager.h>
|
|
|
|
GLOBAL_VAR AssetManager assetManager;
|
2016-06-04 12:42:22 +00:00
|
|
|
|
2016-06-18 09:12:09 +00:00
|
|
|
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
|
2016-06-18 09:12:09 +00:00
|
|
|
if (type < texlist_count)
|
|
|
|
return &assetManager.textures[type];
|
2016-06-08 07:29:16 +00:00
|
|
|
|
2016-06-17 16:01:43 +00:00
|
|
|
return NULL;
|
2016-06-08 07:29:16 +00:00
|
|
|
}
|
2016-06-04 12:42:22 +00:00
|
|
|
|
2016-06-18 09:12:09 +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 =
|
2016-06-17 16:01:43 +00:00
|
|
|
stbi_load(path, &imgWidth, &imgHeight, &bytesPerPixel, 0);
|
2016-06-08 07:29:16 +00:00
|
|
|
|
|
|
|
if (!image)
|
|
|
|
{
|
2016-06-17 16:01:43 +00:00
|
|
|
printf("stdbi_load() failed: %s\n", stbi_failure_reason());
|
2016-06-08 07:29:16 +00:00
|
|
|
return -1;
|
2016-06-04 12:42:22 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2016-06-18 09:12:09 +00:00
|
|
|
assetManager.textures[type] = tex;
|
2016-06-08 07:29:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-18 09:12:09 +00:00
|
|
|
Shader *asset_getShader(const enum ShaderList type)
|
2016-06-08 07:29:16 +00:00
|
|
|
{
|
2016-06-18 09:12:09 +00:00
|
|
|
if (type < shaderlist_count)
|
|
|
|
return &assetManager.shaders[type];
|
2016-06-08 07:29:16 +00:00
|
|
|
|
2016-06-18 09:12:09 +00:00
|
|
|
return NULL;
|
2016-06-08 07:29:16 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 16:01:43 +00:00
|
|
|
INTERNAL GLuint createShaderFromPath(const char *const path, GLuint shadertype)
|
2016-06-08 07:29:16 +00:00
|
|
|
{
|
2016-06-17 16:01:43 +00:00
|
|
|
PlatformFileReadResult file = {0};
|
2016-06-08 07:29:16 +00:00
|
|
|
|
2016-06-17 16:01:43 +00:00
|
|
|
i32 status = platform_readFileToBuffer(path, &file);
|
|
|
|
if (status)
|
|
|
|
return status;
|
2016-06-08 07:29:16 +00:00
|
|
|
|
2016-06-17 16:01:43 +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);
|
2016-06-17 16:01:43 +00:00
|
|
|
printf("glCompileShader() failed: %s\n", infoLog);
|
2016-06-04 12:42:22 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 16:01:43 +00:00
|
|
|
platform_closeFileReadResult(&file);
|
|
|
|
|
2016-06-08 07:29:16 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-17 16:01:43 +00:00
|
|
|
const i32 asset_loadShaderFiles(const char *const vertexPath,
|
|
|
|
const char *const fragmentPath,
|
2016-06-18 09:12:09 +00:00
|
|
|
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;
|
|
|
|
|
2016-06-18 09:12:09 +00:00
|
|
|
assetManager.shaders[type] = shader;
|
2016-06-08 07:29:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|