From 0ab96eb0fc448f074c12e8db15b24d45f2f34fc0 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Tue, 11 Jul 2023 19:20:19 +0200 Subject: [PATCH] fix: resourceManager.cpp clang form --- .clang-tidy | 2 +- breakout/constants.hpp | 1 + breakout/resourceManager.cpp | 120 ++++++++++++++++++++++------------- breakout/resourceManager.hpp | 76 +++++++++++----------- clang_script.sh | 2 +- 5 files changed, 117 insertions(+), 84 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 08e52d4..0fb734d 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -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: '' HeaderFilterRegex: '' AnalyzeTemporaryDtors: false diff --git a/breakout/constants.hpp b/breakout/constants.hpp index d7c5de0..0bcbede 100644 --- a/breakout/constants.hpp +++ b/breakout/constants.hpp @@ -5,6 +5,7 @@ namespace constants { constexpr int MAX_KEYS_TRACKED { 1024 }; constexpr int GAME_WINDOW_SIZE_ALLIGNMENT { 8 }; + constexpr int DEFAULT_INVALID_NUMBER { -1 }; } #endif // BREAKOUT_CONSTANTS_HPP_ diff --git a/breakout/resourceManager.cpp b/breakout/resourceManager.cpp index c0bdc5d..0659cf1 100644 --- a/breakout/resourceManager.cpp +++ b/breakout/resourceManager.cpp @@ -2,6 +2,8 @@ #ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_CPP #define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_CPP +#include + #include "../breakout/resourceManager.hpp" #include "./shader.hpp" #include "./stb_image.h" @@ -11,25 +13,25 @@ std::map ResourceManager::Textures; std::map ResourceManager::Shaders; -auto ResourceManager::LoadShader(const char *vShaderFile, +Shader ResourceManager::LoadShader(const char *vShaderFile, const char *fShaderFile, const char *gShaderFile, - const std::string &name) -> Shader { + const std::string &name) { Shaders[name] = loadShaderFromFile(vShaderFile, fShaderFile, gShaderFile); return Shaders[name]; } -auto ResourceManager::GetShader(const std::string &name) -> Shader { +Shader ResourceManager::GetShader(const std::string &name) { return Shaders[name]; } -auto ResourceManager::LoadTexture(const char *file, bool alpha, - const std::string &name) -> Texture2D { +Texture2D ResourceManager::LoadTexture(const char *file, bool alpha, + const std::string &name) { Textures[name] = loadTextureFromFile(file, alpha); return Textures[name]; } -auto ResourceManager::GetTexture(const std::string &name) -> Texture2D { +Texture2D ResourceManager::GetTexture(const std::string &name) { return Textures[name]; } @@ -44,39 +46,9 @@ void ResourceManager::Clear() { } } -auto ResourceManager::loadShaderFromFile(const char *vShaderFile, - const char *fShaderFile, - const char *gShaderFile) -> Shader { - // 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; - } +static Shader ResourceManager::createShaderObject( + const std::string& vertexCode, const std::string& fragmentCode, + const char *gShaderFile, const std::string& geometryCode) { const char *vShaderCode = vertexCode.c_str(); const char *fShaderCode = fragmentCode.c_str(); const char *gShaderCode = geometryCode.c_str(); @@ -87,18 +59,76 @@ auto ResourceManager::loadShaderFromFile(const char *vShaderFile, return shader; } -auto ResourceManager::loadTextureFromFile(const char *file, bool alpha) - -> Texture2D { - // create texture object +static std::string ResourceManager::loadFragmentCode(const char *fShaderFile) { + 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 Texture2D texture; if (alpha) { texture.Internal_Format = GL_RGBA; texture.Image_Format = GL_RGBA; } + return texture; +} + +Texture2D ResourceManager::loadTextureFromFile(const char *file, bool alpha) { + Texture2D texture = createTextureObject(alpha); // load image - int width; - int height; - int nrChannels; + int width = -1; + int height = -1; + int nrChannels = -1; unsigned char *data = stbi_load(file, &width, &height, &nrChannels, 0); // now generate texture texture.Generate(width, height, data); diff --git a/breakout/resourceManager.hpp b/breakout/resourceManager.hpp index f65ea69..8d56132 100644 --- a/breakout/resourceManager.hpp +++ b/breakout/resourceManager.hpp @@ -10,55 +10,57 @@ #ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_HPP #define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_HPP - #include "../dependencies/include/glad/glad.h" #include "../breakout/shader.hpp" #include "../breakout/texture.hpp" - // A static singleton ResourceManager class that hosts several // functions to load Textures and Shaders. Each loaded texture // and/or shader is also stored for future reference by string // handles. All functions and resources are static and no // public constructor is defined. class ResourceManager { - public: - // resource storage - template<> static std::max; Shaders; - template<> static std::max; Textures; - // loads (and generates) a shader program from file loading vertex, - // fragment (and geometry) shader's source code. - // If gShaderFile is not nullptr, it also loads a geometry shader - static auto LoadShader( - const char *vShaderFile, - const char *fShaderFile, - const char *gShaderFile, - const std::string& name) -> Shader; - // retrieves a stored sader - static auto GetShader(const std::string& name) -> Shader; - // loads (and generates) a texture from file - static auto LoadTexture( - const char *file, - bool alpha, - const std::string& name) -> Texture2D; - // retrieves a stored texture - static auto GetTexture(const std::string& name) -> Texture2D; - // properly de-allocates all loaded resources - static void Clear(); +public: + // resource storage + template <> static std::max; + Shaders{}; + template <> static std::max; + Textures{}; + // loads (and generates) a shader program from file loading vertex, + // fragment (and geometry) shader's source code. + // If gShaderFile is not nullptr, it also loads a geometry shader + static auto LoadShader(const char *vShaderFile, const char *fShaderFile, + const char *gShaderFile, const std::string &name) + -> Shader; + // retrieves a stored sader + static auto GetShader(const std::string &name) -> Shader; + // loads (and generates) a texture from file + static auto LoadTexture(const char *file, bool alpha, const std::string &name) + -> Texture2D; + // retrieves a stored texture + static auto GetTexture(const std::string &name) -> Texture2D; + // properly de-allocates all loaded resources + static void Clear(); - private: - // private constructor, - // that is we do not want any actual resource manager objects. - // Its members and functions should be publicly available (static). - ResourceManager() = default; - // loads and generates a shader from file - static auto loadShaderFromFile( - const char *vShaderFile, - const char *fShaderFile, - const char *gShaderFile) -> Shader; - // loads a single texture from file - static auto loadTextureFromFile(const char *file, bool alpha) -> Texture2D; +private: + // private constructor, + // that is we do not want any actual resource manager objects. + // Its members and functions should be publicly available (static). + ResourceManager() = default; + // loads and generates a shader from file + static Shader ResourceManager::createShaderObject( + 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 *gShaderFile) -> Shader; + // loads a single texture from file + static Texture2D ResourceManager::createTextureObject(); + static auto loadTextureFromFile(const char *file, bool alpha) -> Texture2D; }; #endif // HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_HPP diff --git a/clang_script.sh b/clang_script.sh index eb146fd..e505109 100755 --- a/clang_script.sh +++ b/clang_script.sh @@ -1,4 +1,4 @@ #!/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 \ No newline at end of file