engineer-thesis-WUT/breakout/resourceManager.cpp

117 lines
3.5 KiB
C++
Raw Normal View History

// Copyright [2023] Krzysztof Rudnicki
2023-04-02 17:59:15 +02:00
#ifndef BREAKOUT_RESOURCE_MANAGER_HPP_
#define BREAKOUT_RESOURCE_MANAGER_HPP_
2023-04-02 17:59:15 +02:00
#include <iostream>
#include <sstream>
#include <fstream>
#include "../breakout/resourceManager.hpp"
#include "./shader.hpp"
#include "./texture.hpp"
2023-04-02 18:26:43 +02:00
#include "./stb_image.h"
2023-04-02 17:59:15 +02:00
// Instantiate static variables
std::map<std::string, Texture2D> ResourceManager::Textures;
std::map<std::string, Shader> ResourceManager::Shaders;
Shader ResourceManager::LoadShader(
const char *vShaderFile,
const char *fShaderFile,
const char *gShaderFile,
std::string name) {
2023-04-02 17:59:15 +02:00
Shaders[name] = loadShaderFromFile(vShaderFile, fShaderFile, gShaderFile);
return Shaders[name];
}
Shader ResourceManager::GetShader(std::string name) {
2023-04-02 17:59:15 +02:00
return Shaders[name];
}
Texture2D ResourceManager::LoadTexture(
const char *file,
bool alpha,
std::string name) {
2023-04-02 17:59:15 +02:00
Textures[name] = loadTextureFromFile(file, alpha);
return Textures[name];
}
Texture2D ResourceManager::GetTexture(std::string name) {
2023-04-02 17:59:15 +02:00
return Textures[name];
}
void ResourceManager::Clear() {
// (properly) delete all shaders
2023-04-02 17:59:15 +02:00
for (auto iter : Shaders)
glDeleteProgram(iter.second.ID);
// (properly) delete all textures
for (auto iter : Textures)
glDeleteTextures(1, &iter.second.ID);
}
Shader ResourceManager::loadShaderFromFile(
const char *vShaderFile,
const char *fShaderFile,
const char *gShaderFile) {
2023-04-02 17:59:15 +02:00
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::string geometryCode;
try {
2023-04-02 17:59:15 +02:00
// open files
std::ifstream vertexShaderFile(vShaderFile);
std::ifstream fragmentShaderFile(fShaderFile);
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream << vertexShaderFile.rdbuf();
fShaderStream << fragmentShaderFile.rdbuf();
// close file handlers
vertexShaderFile.close();
fragmentShaderFile.close();
// convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
// if geometry shader path is present, also load a geometry shader
if (gShaderFile != nullptr) {
2023-04-02 17:59:15 +02:00
std::ifstream geometryShaderFile(gShaderFile);
std::stringstream gShaderStream;
gShaderStream << geometryShaderFile.rdbuf();
geometryShaderFile.close();
geometryCode = gShaderStream.str();
}
}
catch (std::exception const&) {
2023-04-02 17:59:15 +02:00
std::cout << "ERROR::SHADER: Failed to read shader files" << std::endl;
}
const char *vShaderCode = vertexCode.c_str();
const char *fShaderCode = fragmentCode.c_str();
const char *gShaderCode = geometryCode.c_str();
// 2. now create shader object from source code
Shader shader;
shader.Compile(vShaderCode,
fShaderCode, gShaderFile != nullptr ? gShaderCode : nullptr);
2023-04-02 17:59:15 +02:00
return shader;
}
Texture2D ResourceManager::loadTextureFromFile(const char *file, bool alpha) {
2023-04-02 17:59:15 +02:00
// create texture object
Texture2D texture;
if (alpha) {
2023-04-02 17:59:15 +02:00
texture.Internal_Format = GL_RGBA;
texture.Image_Format = GL_RGBA;
}
// load image
int width, height, nrChannels;
unsigned char* data = stbi_load(file, &width, &height, &nrChannels, 0);
// now generate texture
texture.Generate(width, height, data);
// and finally free image data
stbi_image_free(data);
return texture;
}
#endif