fix: resourceManager.cpp clang form

This commit is contained in:
Krzysztof Rudnicki 2023-07-11 19:20:19 +02:00
parent ce05bca9c4
commit 0ab96eb0fc
5 changed files with 117 additions and 84 deletions

View File

@ -1,5 +1,5 @@
--- ---
Checks: '*,-altera-unroll-loops, -cppcoreguidelines-pro-type-reinterpret-cast,-llvmlibc-implementation-in-namespace,-llvmlibc-callee-namespace, -llvmlibc-restrict-system-libc-headers, -modernize-use-trailing-return-type' Checks: '*,-llvm-header-guard, -altera-unroll-loops, -cppcoreguidelines-pro-type-reinterpret-cast,-llvmlibc-implementation-in-namespace,-llvmlibc-callee-namespace, -llvmlibc-restrict-system-libc-headers, -modernize-use-trailing-return-type'
WarningsAsErrors: '' WarningsAsErrors: ''
HeaderFilterRegex: '' HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false AnalyzeTemporaryDtors: false

View File

@ -5,6 +5,7 @@
namespace constants { namespace constants {
constexpr int MAX_KEYS_TRACKED { 1024 }; constexpr int MAX_KEYS_TRACKED { 1024 };
constexpr int GAME_WINDOW_SIZE_ALLIGNMENT { 8 }; constexpr int GAME_WINDOW_SIZE_ALLIGNMENT { 8 };
constexpr int DEFAULT_INVALID_NUMBER { -1 };
} }
#endif // BREAKOUT_CONSTANTS_HPP_ #endif // BREAKOUT_CONSTANTS_HPP_

View File

@ -2,6 +2,8 @@
#ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_CPP #ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_CPP
#define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_CPP #define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_CPP
#include <iostream>
#include "../breakout/resourceManager.hpp" #include "../breakout/resourceManager.hpp"
#include "./shader.hpp" #include "./shader.hpp"
#include "./stb_image.h" #include "./stb_image.h"
@ -11,25 +13,25 @@
std::map<std::string, Texture2D> ResourceManager::Textures; std::map<std::string, Texture2D> ResourceManager::Textures;
std::map<std::string, Shader> ResourceManager::Shaders; std::map<std::string, Shader> ResourceManager::Shaders;
auto ResourceManager::LoadShader(const char *vShaderFile, Shader ResourceManager::LoadShader(const char *vShaderFile,
const char *fShaderFile, const char *fShaderFile,
const char *gShaderFile, const char *gShaderFile,
const std::string &name) -> Shader { const std::string &name) {
Shaders[name] = loadShaderFromFile(vShaderFile, fShaderFile, gShaderFile); Shaders[name] = loadShaderFromFile(vShaderFile, fShaderFile, gShaderFile);
return Shaders[name]; return Shaders[name];
} }
auto ResourceManager::GetShader(const std::string &name) -> Shader { Shader ResourceManager::GetShader(const std::string &name) {
return Shaders[name]; return Shaders[name];
} }
auto ResourceManager::LoadTexture(const char *file, bool alpha, Texture2D ResourceManager::LoadTexture(const char *file, bool alpha,
const std::string &name) -> Texture2D { const std::string &name) {
Textures[name] = loadTextureFromFile(file, alpha); Textures[name] = loadTextureFromFile(file, alpha);
return Textures[name]; return Textures[name];
} }
auto ResourceManager::GetTexture(const std::string &name) -> Texture2D { Texture2D ResourceManager::GetTexture(const std::string &name) {
return Textures[name]; return Textures[name];
} }
@ -44,39 +46,9 @@ void ResourceManager::Clear() {
} }
} }
auto ResourceManager::loadShaderFromFile(const char *vShaderFile, static Shader ResourceManager::createShaderObject(
const char *fShaderFile, const std::string& vertexCode, const std::string& fragmentCode,
const char *gShaderFile) -> Shader { const char *gShaderFile, const std::string& geometryCode) {
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::string geometryCode;
try {
// open files
std::ifstream vertexShaderFile(vShaderFile);
std::ifstream fragmentShaderFile(fShaderFile);
std::stringstream vShaderStream;
std::stringstream 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) {
std::ifstream geometryShaderFile(gShaderFile);
std::stringstream gShaderStream;
gShaderStream << geometryShaderFile.rdbuf();
geometryShaderFile.close();
geometryCode = gShaderStream.str();
}
} catch (std::exception const &) {
std::cout << "ERROR::SHADER: Failed to read shader files" << std::endl;
}
const char *vShaderCode = vertexCode.c_str(); const char *vShaderCode = vertexCode.c_str();
const char *fShaderCode = fragmentCode.c_str(); const char *fShaderCode = fragmentCode.c_str();
const char *gShaderCode = geometryCode.c_str(); const char *gShaderCode = geometryCode.c_str();
@ -87,18 +59,76 @@ auto ResourceManager::loadShaderFromFile(const char *vShaderFile,
return shader; return shader;
} }
auto ResourceManager::loadTextureFromFile(const char *file, bool alpha) static std::string ResourceManager::loadFragmentCode(const char *fShaderFile) {
-> Texture2D { std::string fragmentCode;
// open files
std::ifstream fragmentShaderFile(fShaderFile);
std::stringstream fShaderStream;
// read file's buffer contents into streams
fShaderStream << fragmentShaderFile.rdbuf();
// close file handlers
fragmentShaderFile.close();
// convert stream into string
fragmentCode = fShaderStream.str();
return fragmentCode;
}
static std::string ResourceManager::loadVertexCode(const char *vShaderFile) {
std::string vertexCode;
std::ifstream vertexShaderFile(vShaderFile);
std::stringstream vShaderStream;
vShaderStream << vertexShaderFile.rdbuf();
vertexShaderFile.close();
vertexCode = vShaderStream.str();
return vertexCode
}
static std::string ResourceManager::loadGeometryCode(const char *gShaderFile) {
std::string geometryCode;
std::ifstream geometryShaderFile(gShaderFile);
std::stringstream gShaderStream;
gShaderStream << geometryShaderFile.rdbuf();
geometryShaderFile.close();
geometryCode = gShaderStream.str();
return geometryCode;
}
static Shader ResourceManager::loadShaderFromFile(const char *vShaderFile,
const char *fShaderFile,
const char *gShaderFile) {
std::string vertexCode;
std::string fragmentCode;
std::string geometryCode;
try {
vertexCode = loadVertexCode(vShaderFile);
fragmentCode = loadFragmentCode(fShaderFile);
// if geometry shader path is present, also load a geometry shader
if (gShaderFile != nullptr) {
geometryCode = loadGeometryCode(gShaderFile);
}
} catch (std::exception const &) {
std::cout << "ERROR::SHADER: Failed to read shader files" << std::endl;
}
return createShaderObject(
vertexCode, fragmentCode, gShaderFile, geometryCode);
}
static Texture2D ResourceManager::createTextureObject(bool alpha) {
// create texture object // create texture object
Texture2D texture; Texture2D texture;
if (alpha) { if (alpha) {
texture.Internal_Format = GL_RGBA; texture.Internal_Format = GL_RGBA;
texture.Image_Format = GL_RGBA; texture.Image_Format = GL_RGBA;
} }
return texture;
}
Texture2D ResourceManager::loadTextureFromFile(const char *file, bool alpha) {
Texture2D texture = createTextureObject(alpha);
// load image // load image
int width; int width = -1;
int height; int height = -1;
int nrChannels; int nrChannels = -1;
unsigned char *data = stbi_load(file, &width, &height, &nrChannels, 0); unsigned char *data = stbi_load(file, &width, &height, &nrChannels, 0);
// now generate texture // now generate texture
texture.Generate(width, height, data); texture.Generate(width, height, data);

View File

@ -10,54 +10,56 @@
#ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_HPP #ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_HPP
#define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_HPP #define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_HPP
#include "../dependencies/include/glad/glad.h" #include "../dependencies/include/glad/glad.h"
#include "../breakout/shader.hpp" #include "../breakout/shader.hpp"
#include "../breakout/texture.hpp" #include "../breakout/texture.hpp"
// A static singleton ResourceManager class that hosts several // A static singleton ResourceManager class that hosts several
// functions to load Textures and Shaders. Each loaded texture // functions to load Textures and Shaders. Each loaded texture
// and/or shader is also stored for future reference by string // and/or shader is also stored for future reference by string
// handles. All functions and resources are static and no // handles. All functions and resources are static and no
// public constructor is defined. // public constructor is defined.
class ResourceManager { class ResourceManager {
public: public:
// resource storage // resource storage
template<> static std::max<std::string, Shader>; Shaders; template <> static std::max<std::string, Shader>;
template<> static std::max<std::string, Texture2D>; Textures; Shaders{};
template <> static std::max<std::string, Texture2D>;
Textures{};
// loads (and generates) a shader program from file loading vertex, // loads (and generates) a shader program from file loading vertex,
// fragment (and geometry) shader's source code. // fragment (and geometry) shader's source code.
// If gShaderFile is not nullptr, it also loads a geometry shader // If gShaderFile is not nullptr, it also loads a geometry shader
static auto LoadShader( static auto LoadShader(const char *vShaderFile, const char *fShaderFile,
const char *vShaderFile, const char *gShaderFile, const std::string &name)
const char *fShaderFile, -> Shader;
const char *gShaderFile,
const std::string& name) -> Shader;
// retrieves a stored sader // retrieves a stored sader
static auto GetShader(const std::string& name) -> Shader; static auto GetShader(const std::string &name) -> Shader;
// loads (and generates) a texture from file // loads (and generates) a texture from file
static auto LoadTexture( static auto LoadTexture(const char *file, bool alpha, const std::string &name)
const char *file, -> Texture2D;
bool alpha,
const std::string& name) -> Texture2D;
// retrieves a stored texture // retrieves a stored texture
static auto GetTexture(const std::string& name) -> Texture2D; static auto GetTexture(const std::string &name) -> Texture2D;
// properly de-allocates all loaded resources // properly de-allocates all loaded resources
static void Clear(); static void Clear();
private: private:
// private constructor, // private constructor,
// that is we do not want any actual resource manager objects. // that is we do not want any actual resource manager objects.
// Its members and functions should be publicly available (static). // Its members and functions should be publicly available (static).
ResourceManager() = default; ResourceManager() = default;
// loads and generates a shader from file // loads and generates a shader from file
static auto loadShaderFromFile( static Shader ResourceManager::createShaderObject(
const char *vShaderFile, std::string vertexCode, std::string fragmentCode,
const char *gShaderFile, std::string geometryCode);
static std::string ResourceManager::loadFragmentCode(const char *fShaderFile);
static std::string ResourceManager::loadVertexCode(const char *vShaderFile);
static std::string ResourceManager::loadGeometryCode(const char *gShaderFile);
static auto loadShaderFromFile(const char *vShaderFile,
const char *fShaderFile, const char *fShaderFile,
const char *gShaderFile) -> Shader; const char *gShaderFile) -> Shader;
// loads a single texture from file // loads a single texture from file
static Texture2D ResourceManager::createTextureObject();
static auto loadTextureFromFile(const char *file, bool alpha) -> Texture2D; static auto loadTextureFromFile(const char *file, bool alpha) -> Texture2D;
}; };

View File

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/sh
clang-tidy --config-file=./.clang-tidy -p ./build/compile_commands.json ./breakout/breakout.cpp clang-tidy --config-file=./.clang-tidy -p ./build/compile_commands.json ./breakout/resourceManager.cpp
cpplint ./breakout/breakout.cpp cpplint ./breakout/breakout.cpp