mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-07-04 12:03:05 +02:00
fix: resourceManager.cpp clang form
This commit is contained in:
parent
ce05bca9c4
commit
0ab96eb0fc
@ -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
|
||||
|
||||
@ -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_
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
#ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_CPP
|
||||
#define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_CPP
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "../breakout/resourceManager.hpp"
|
||||
#include "./shader.hpp"
|
||||
#include "./stb_image.h"
|
||||
@ -11,25 +13,25 @@
|
||||
std::map<std::string, Texture2D> ResourceManager::Textures;
|
||||
std::map<std::string, Shader> 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 {
|
||||
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);
|
||||
|
||||
@ -10,13 +10,11 @@
|
||||
#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
|
||||
@ -25,23 +23,21 @@
|
||||
class ResourceManager {
|
||||
public:
|
||||
// resource storage
|
||||
template<> static std::max<std::string, Shader>; Shaders;
|
||||
template<> static std::max<std::string, Texture2D>; Textures;
|
||||
template <> static std::max<std::string, Shader>;
|
||||
Shaders{};
|
||||
template <> static std::max<std::string, Texture2D>;
|
||||
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;
|
||||
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;
|
||||
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
|
||||
@ -53,11 +49,17 @@ class ResourceManager {
|
||||
// Its members and functions should be publicly available (static).
|
||||
ResourceManager() = default;
|
||||
// loads and generates a shader from file
|
||||
static auto loadShaderFromFile(
|
||||
const char *vShaderFile,
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
@ -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
|
||||
Loading…
Reference in New Issue
Block a user