Dengine/src/AssetManager.cpp

119 lines
2.6 KiB
C++
Raw Normal View History

#include <Dengine\AssetManager.h>
#define STBI_FAILURE_USERMSG
#define STB_IMAGE_IMPLEMENTATION
#include <STB/stb_image.h>
#include <fstream>
namespace Dengine
{
2016-06-09 05:49:03 +00:00
std::map<std::string, Texture> AssetManager::textures;
std::map<std::string, Shader> AssetManager::shaders;
2016-06-09 05:49:03 +00:00
Texture *AssetManager::getTexture(const std::string name)
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-09 05:49:03 +00:00
if (textures.count(name) == 1)
return &textures[name];
2016-06-08 07:29:16 +00:00
return nullptr;
}
2016-06-08 07:29:16 +00:00
const i32 AssetManager::loadTextureImage(const std::string path,
const std::string name)
{
/* Open the texture image */
i32 imgWidth, imgHeight, bytesPerPixel;
stbi_set_flip_vertically_on_load(TRUE);
u8 *image =
stbi_load(path.c_str(), &imgWidth, &imgHeight, &bytesPerPixel, 0);
if (!image)
{
std::cerr << "stdbi_load() failed: " << stbi_failure_reason()
<< std::endl;
return -1;
}
2016-06-08 07:29:16 +00:00
Texture tex;
tex.generate(imgWidth, imgHeight, image);
stbi_image_free(image);
2016-06-09 05:49:03 +00:00
textures[name] = tex;
2016-06-08 07:29:16 +00:00
return 0;
}
2016-06-09 05:49:03 +00:00
Shader *AssetManager::getShader(const std::string name)
2016-06-08 07:29:16 +00:00
{
2016-06-09 05:49:03 +00:00
if (shaders.count(name) == 1)
return &shaders[name];
2016-06-08 07:29:16 +00:00
return nullptr;
}
INTERNAL std::string stringFromFile(const std::string &filename)
{
std::ifstream file;
file.open(filename.c_str(), std::ios::in | std::ios::binary);
std::string output;
std::string line;
if (!file.is_open())
{
2016-06-08 07:29:16 +00:00
std::runtime_error(std::string("Failed to open file: ") + filename);
}
else
{
2016-06-08 07:29:16 +00:00
while (file.good())
{
std::getline(file, line);
output.append(line + "\n");
}
}
2016-06-08 07:29:16 +00:00
file.close();
return output;
}
2016-06-08 07:29:16 +00:00
INTERNAL GLuint createShaderFromPath(std::string path, GLuint shadertype)
{
std::string shaderSource = stringFromFile(path);
const GLchar *source = shaderSource.c_str();
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);
std::cout << "glCompileShader failed: " << infoLog << std::endl;
}
2016-06-08 07:29:16 +00:00
return result;
}
const i32 AssetManager::loadShaderFiles(const std::string vertexPath,
const std::string fragmentPath,
const std::string name)
{
GLuint vertexShader = createShaderFromPath(vertexPath, GL_VERTEX_SHADER);
GLuint fragmentShader =
createShaderFromPath(fragmentPath, GL_FRAGMENT_SHADER);
Shader shader;
i32 result = shader.loadProgram(vertexShader, fragmentShader);
if (result)
return result;
2016-06-09 05:49:03 +00:00
shaders[name] = shader;
2016-06-08 07:29:16 +00:00
return 0;
}
}