feat: run clang-tidy fix on repo

This commit is contained in:
Krzysztof Rudnicki 2023-07-06 17:35:25 +02:00
parent 140fbbc4a1
commit a88c7a2cab
12 changed files with 276 additions and 325 deletions

View File

@ -1,38 +1,12 @@
--- ---
Checks: '*' Checks: '*,-llvmlibc-implementation-in-namespace'
WarningsAsErrors: '' WarningsAsErrors: ''
HeaderFilterRegex: '' HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false AnalyzeTemporaryDtors: false
FormatStyle: none FormatStyle: none
User: kuchy User: kuchy
CheckOptions: CheckOptions:
- key: cert-dcl16-c.NewSuffixes
value: 'L;LL;LU;LLU'
- key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField
value: '0'
- key: cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors
value: '1'
- key: cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
value: '1'
- key: google-readability-braces-around-statements.ShortStatementLines
value: '1'
- key: google-readability-function-size.LineThreshold - key: google-readability-function-size.LineThreshold
value: '16' value: '16'
- key: google-readability-namespace-comments.ShortNamespaceLines
value: '10'
- key: google-readability-namespace-comments.SpacesBeforeComments
value: '2'
- key: modernize-loop-convert.MaxCopySize
value: '16'
- key: modernize-loop-convert.MinConfidence
value: reasonable
- key: modernize-loop-convert.NamingStyle
value: CamelCase
- key: modernize-pass-by-value.IncludeStyle
value: llvm
- key: modernize-replace-auto-ptr.IncludeStyle
value: llvm
- key: modernize-use-nullptr.NullMacros
value: 'NULL'
... ...

View File

@ -17,6 +17,7 @@
"ms-vscode.cpptools-themes", "ms-vscode.cpptools-themes",
"twxs.cmake", "twxs.cmake",
"ms-vscode.cmake-tools", "ms-vscode.cmake-tools",
"mine.cpplint" "mine.cpplint",
"ms-vscode.cpptools"
] ]
} }

View File

@ -1,121 +1,120 @@
// Copyright [2023] Krzysztof Rudnicki // Copyright [2023] Krzysztof Rudnicki
#ifndef BREAKOUT_CPP #ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_BREAKOUT_CPP
#define BREAKOUT_CPP #define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_BREAKOUT_CPP
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <iostream>
#include "../dependencies/include/glad/glad.h" #include "../dependencies/include/glad/glad.h"
#include "./game.hpp" #include "./game.hpp"
#include "./resourceManager.hpp" #include "./resourceManager.hpp"
// GLFW function declarations // GLFW function declarations
void framebuffer_size_callback(GLFWwindow* window, int width, int height); void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void key_callback(GLFWwindow* window, int key, void key_callback(GLFWwindow *window, int key, int scancode, int action,
int scancode, int action, int mode); int mode);
// The Width of the screen // The Width of the screen
const unsigned int SCREEN_WIDTH = 800; constexpr int SCREEN_WIDTH = 800;
// The height of the screen // The height of the screen
const unsigned int SCREEN_HEIGHT = 600; constexpr int SCREEN_HEIGHT = 600;
Game Breakout(SCREEN_WIDTH, SCREEN_HEIGHT); Game &getBreakoutInstance() {
static Game Breakout(SCREEN_WIDTH, SCREEN_HEIGHT);
return Breakout;
}
int main(int argc, char *argv[]) { int main(int /*argc*/, char * /*argv*/[]) {
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__ #ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif #endif
glfwWindowHint(GLFW_RESIZABLE, false); glfwWindowHint(GLFW_RESIZABLE, 0);
GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, GLFWwindow *window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Breakout",
"Breakout", nullptr, nullptr); nullptr, nullptr);
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
// glad: load all OpenGL function pointers // glad: load all OpenGL function pointers
// --------------------------------------- // ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { if (gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress)) ==
std::cout << "Failed to initialize GLAD" << std::endl; 0) {
return -1; std::cout << "Failed to initialize GLAD" << std::endl;
} return -1;
}
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, bugprone-easily-swappable-parameters
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); // --------------------
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// OpenGL configuration // initialize game
// ---------------
Breakout.Init();
// deltaTime variables
// -------------------
float deltaTime = 0.0F;
float lastFrame = 0.0F;
while (glfwWindowShouldClose(window) == 0) {
// calculate delta time
// -------------------- // --------------------
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); float const currentFrame = glfwGetTime();
glEnable(GL_BLEND); deltaTime = currentFrame - lastFrame;
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); lastFrame = currentFrame;
glfwPollEvents();
// initialize game // manage user input
// --------------- // -----------------
Breakout.Init(); Breakout.ProcessInput(deltaTime);
// deltaTime variables // update game state
// ------------------- // -----------------
float deltaTime = 0.0f; Breakout.Update(deltaTime);
float lastFrame = 0.0f;
while (!glfwWindowShouldClose(window)) { // render
// calculate delta time // ------
// -------------------- glClearColor(0.0F, 0.0F, 0.0F, 1.0F);
float currentFrame = glfwGetTime(); glClear(GL_COLOR_BUFFER_BIT);
deltaTime = currentFrame - lastFrame; Breakout.Render();
lastFrame = currentFrame;
glfwPollEvents();
// manage user input glfwSwapBuffers(window);
// ----------------- }
Breakout.ProcessInput(deltaTime);
// update game state // delete all resources as loaded using the resource manager
// ----------------- // ---------------------------------------------------------
Breakout.Update(deltaTime); ResourceManager::Clear();
// render glfwTerminate();
// ------ return 0;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); }
glClear(GL_COLOR_BUFFER_BIT);
Breakout.Render();
glfwSwapBuffers(window); void key_callback(GLFWwindow *window, int key, int /*scancode*/, int action,
int /*mode*/) {
// when a user presses the escape key, we set the
// WindowShouldClose property to true, closing the application
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, 1);
}
if (key >= 0 && key < 1024) {
if (action == GLFW_PRESS) {
Breakout.Keys[key] = true;
} else if (action == GLFW_RELEASE) {
Breakout.Keys[key] = false;
} }
}
// delete all resources as loaded using the resource manager
// ---------------------------------------------------------
ResourceManager::Clear();
glfwTerminate();
return 0;
} }
void key_callback(GLFWwindow* window, int key, void framebuffer_size_callback(GLFWwindow * /*window*/, int width, int height) {
int scancode, int action, int mode) { // make sure the viewport matches the new window dimensions;
// when a user presses the escape key, we set the // note that width and
// WindowShouldClose property to true, closing the application // height will be significantly larger than specified on retina displays.
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glViewport(0, 0, width, height);
glfwSetWindowShouldClose(window, true);
if (key >= 0 && key < 1024) {
if (action == GLFW_PRESS)
Breakout.Keys[key] = true;
else if (action == GLFW_RELEASE)
Breakout.Keys[key] = false;
}
} }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
// make sure the viewport matches the new window dimensions;
// note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
#endif #endif
// BREAKOUT_CPP // BREAKOUT_CPP

View File

@ -1,27 +1,20 @@
// Copyright [2023] Krzysztof Rudnicki // Copyright [2023] Krzysztof Rudnicki
#ifndef GAME_CPP #ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_GAME_CPP
#define GAME_CPP #define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_GAME_CPP
#include "../breakout/game.hpp" #include "../breakout/game.hpp"
Game::Game(unsigned int width, unsigned int height) Game::Game(unsigned int width, unsigned int height)
: State(GAME_ACTIVE), Keys(), Width(width), Height(height) { : State(GAME_ACTIVE), Keys(), Width(width), Height(height) {}
}
Game::~Game() { Game::~Game() = default;
}
void Game::Init() { void Game::Init() {}
}
void Game::Update(float dt) { void Game::Update(float dt) {}
}
void Game::ProcessInput(float dt) { void Game::ProcessInput(float dt) {}
}
void Game::Render() {
}
void Game::Render() {}
#endif #endif
// GAME_CPP // GAME_CPP

View File

@ -1,6 +1,6 @@
// Copyright [2023] Krzysztof Rudnicki // Copyright [2023] Krzysztof Rudnicki
#ifndef BREAKOUT_GAME_HPP_ #ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_GAME_HPP
#define BREAKOUT_GAME_HPP_ #define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_GAME_HPP
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
@ -26,4 +26,4 @@ public:
void Render(); void Render();
}; };
#endif // BREAKOUT_GAME_HPP_ #endif // HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_GAME_HPP

View File

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

View File

@ -7,16 +7,14 @@
** Creative Commons, either version 4 of the License, or (at your ** Creative Commons, either version 4 of the License, or (at your
** option) any later version. ** option) any later version.
******************************************************************/ ******************************************************************/
#ifndef BREAKOUT_RESOURCEMANAGER_HPP_ #ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_HPP
#define BREAKOUT_RESOURCEMANAGER_HPP_ #define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_HPP
#include <map>
#include <string>
#include "../dependencies/include/glad/glad.h" #include "../dependencies/include/glad/glad.h"
#include "../breakout/texture.hpp"
#include "../breakout/shader.hpp" #include "../breakout/shader.hpp"
#include "../breakout/texture.hpp"
// A static singleton ResourceManager class that hosts several // A static singleton ResourceManager class that hosts several
@ -27,25 +25,25 @@
class ResourceManager { class ResourceManager {
public: public:
// resource storage // resource storage
static std::map<std::string, Shader> Shaders; static std::max<std::string, Shader> Shaders;
static std::map<std::string, Texture2D> Textures; 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 Shader LoadShader( static auto LoadShader(
const char *vShaderFile, const char *vShaderFile,
const char *fShaderFile, const char *fShaderFile,
const char *gShaderFile, const char *gShaderFile,
std::string name); const std::string& name) -> Shader;
// retrieves a stored sader // retrieves a stored sader
static Shader GetShader(std::string name); static auto GetShader(const std::string& name) -> Shader;
// loads (and generates) a texture from file // loads (and generates) a texture from file
static Texture2D LoadTexture( static auto LoadTexture(
const char *file, const char *file,
bool alpha, bool alpha,
std::string name); const std::string& name) -> Texture2D;
// retrieves a stored texture // retrieves a stored texture
static Texture2D GetTexture(std::string name); 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();
@ -53,14 +51,14 @@ class ResourceManager {
// 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() { } ResourceManager() = default;
// loads and generates a shader from file // loads and generates a shader from file
static Shader loadShaderFromFile( static auto loadShaderFromFile(
const char *vShaderFile, const char *vShaderFile,
const char *fShaderFile, const char *fShaderFile,
const char *gShaderFile = nullptr); const char *gShaderFile) -> Shader;
// loads a single texture from file // loads a single texture from file
static Texture2D loadTextureFromFile(const char *file, bool alpha); static auto loadTextureFromFile(const char *file, bool alpha) -> Texture2D;
}; };
#endif // BREAKOUT_RESOURCEMANAGER_HPP_ #endif // HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_RESOURCEMANAGER_HPP

View File

@ -1,32 +1,32 @@
// Copyright [2023] Krzysztof Rudnicki // Copyright [2023] Krzysztof Rudnicki
#ifndef BREAKOUT_SHADER_CPP #ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_SHADER_CPP
#define BREAKOUT_SHADER_CPP #define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_SHADER_CPP
#include "../breakout/shader.hpp" #include "../breakout/shader.hpp"
#include <iostream> auto Shader::Use() -> Shader & {
Shader &Shader::Use() {
glUseProgram(this->ID); glUseProgram(this->ID);
return *this; return *this;
} }
void Shader::Compile(const char *vertexSource, const char *fragmentSource, void Shader::Compile(const char *vertexSource, const char *fragmentSource,
const char *geometrySource) { const char *geometrySource) {
unsigned int sVertex, sFragment, gShader; unsigned int sVertex;
unsigned int sFragment;
unsigned int gShader;
// vertex Shader // vertex Shader
sVertex = glCreateShader(GL_VERTEX_SHADER); sVertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(sVertex, 1, &vertexSource, NULL); glShaderSource(sVertex, 1, &vertexSource, nullptr);
glCompileShader(sVertex); glCompileShader(sVertex);
checkCompileErrors(sVertex, "VERTEX"); checkCompileErrors(sVertex, "VERTEX");
// fragment Shader // fragment Shader
sFragment = glCreateShader(GL_FRAGMENT_SHADER); sFragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(sFragment, 1, &fragmentSource, NULL); glShaderSource(sFragment, 1, &fragmentSource, nullptr);
glCompileShader(sFragment); glCompileShader(sFragment);
checkCompileErrors(sFragment, "FRAGMENT"); checkCompileErrors(sFragment, "FRAGMENT");
// if geometry shader source code is given, also compile geometry shader // if geometry shader source code is given, also compile geometry shader
if (geometrySource != nullptr) { if (geometrySource != nullptr) {
gShader = glCreateShader(GL_GEOMETRY_SHADER); gShader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(gShader, 1, &geometrySource, NULL); glShaderSource(gShader, 1, &geometrySource, nullptr);
glCompileShader(gShader); glCompileShader(gShader);
checkCompileErrors(gShader, "GEOMETRY"); checkCompileErrors(gShader, "GEOMETRY");
} }
@ -34,79 +34,90 @@ void Shader::Compile(const char *vertexSource, const char *fragmentSource,
this->ID = glCreateProgram(); this->ID = glCreateProgram();
glAttachShader(this->ID, sVertex); glAttachShader(this->ID, sVertex);
glAttachShader(this->ID, sFragment); glAttachShader(this->ID, sFragment);
if (geometrySource != nullptr) if (geometrySource != nullptr) {
glAttachShader(this->ID, gShader); glAttachShader(this->ID, gShader);
}
glLinkProgram(this->ID); glLinkProgram(this->ID);
checkCompileErrors(this->ID, "PROGRAM"); checkCompileErrors(this->ID, "PROGRAM");
// delete the shaders as they're linked into our // delete the shaders as they're linked into our
// program now and no longer necessary // program now and no longer necessary
glDeleteShader(sVertex); glDeleteShader(sVertex);
glDeleteShader(sFragment); glDeleteShader(sFragment);
if (geometrySource != nullptr) if (geometrySource != nullptr) {
glDeleteShader(gShader); glDeleteShader(gShader);
}
} }
void Shader::SetFloat(const char *name, float value, bool useShader) { void Shader::SetFloat(const char *name, float value, bool useShader) {
if (useShader) if (useShader) {
this->Use(); this->Use();
}
glUniform1f(glGetUniformLocation(this->ID, name), value); glUniform1f(glGetUniformLocation(this->ID, name), value);
} }
void Shader::SetInteger(const char *name, int value, bool useShader) { void Shader::SetInteger(const char *name, int value, bool useShader) {
if (useShader) if (useShader) {
this->Use(); this->Use();
}
glUniform1i(glGetUniformLocation(this->ID, name), value); glUniform1i(glGetUniformLocation(this->ID, name), value);
} }
void Shader::SetVector2f(const char *name, float x, float y, bool useShader) { void Shader::SetVector2f(const char *name, float x, float y, bool useShader) {
if (useShader) if (useShader) {
this->Use(); this->Use();
}
glUniform2f(glGetUniformLocation(this->ID, name), x, y); glUniform2f(glGetUniformLocation(this->ID, name), x, y);
} }
void Shader::SetVector2f(const char *name, const glm::vec2 &value, void Shader::SetVector2f(const char *name, const glm::vec2 &value,
bool useShader) { bool useShader) {
if (useShader) if (useShader) {
this->Use(); this->Use();
}
glUniform2f(glGetUniformLocation(this->ID, name), value.x, value.y); glUniform2f(glGetUniformLocation(this->ID, name), value.x, value.y);
} }
void Shader::SetVector3f(const char *name, float x, float y, float z, void Shader::SetVector3f(const char *name, float x, float y, float z,
bool useShader) { bool useShader) {
if (useShader) if (useShader) {
this->Use(); this->Use();
}
glUniform3f(glGetUniformLocation(this->ID, name), x, y, z); glUniform3f(glGetUniformLocation(this->ID, name), x, y, z);
} }
void Shader::SetVector3f(const char *name, const glm::vec3 &value, void Shader::SetVector3f(const char *name, const glm::vec3 &value,
bool useShader) { bool useShader) {
if (useShader) if (useShader) {
this->Use(); this->Use();
}
glUniform3f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z); glUniform3f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z);
} }
void Shader::SetVector4f(const char *name, float x, float y, float z, float w, void Shader::SetVector4f(const char *name, float x, float y, float z, float w,
bool useShader) { bool useShader) {
if (useShader) if (useShader) {
this->Use(); this->Use();
}
glUniform4f(glGetUniformLocation(this->ID, name), x, y, z, w); glUniform4f(glGetUniformLocation(this->ID, name), x, y, z, w);
} }
void Shader::SetVector4f(const char *name, const glm::vec4 &value, void Shader::SetVector4f(const char *name, const glm::vec4 &value,
bool useShader) { bool useShader) {
if (useShader) if (useShader) {
this->Use(); this->Use();
}
glUniform4f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z, glUniform4f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z,
value.w); value.w);
} }
void Shader::SetMatrix4(const char *name, const glm::mat4 &matrix, void Shader::SetMatrix4(const char *name, const glm::mat4 &matrix,
bool useShader) { bool useShader) {
if (useShader) if (useShader) {
this->Use(); this->Use();
glUniformMatrix4fv(glGetUniformLocation(this->ID, name), 1, false, }
glUniformMatrix4fv(glGetUniformLocation(this->ID, name), 1, 0u,
glm::value_ptr(matrix)); glm::value_ptr(matrix));
} }
void Shader::checkCompileErrors(unsigned int object, std::string type) { void Shader::checkCompileErrors(unsigned int object, const std::string &type) {
int success; int success = 0;
char infoLog[1024]; char infoLog[1024];
if (type != "PROGRAM") { if (type != "PROGRAM") {
glGetShaderiv(object, GL_COMPILE_STATUS, &success); glGetShaderiv(object, GL_COMPILE_STATUS, &success);
if (!success) { if (success == 0) {
glGetShaderInfoLog(object, 1024, NULL, infoLog); glGetShaderInfoLog(object, 1024, nullptr, infoLog);
std::cout std::cout
<< "| ERROR::SHADER: Compile-time error: Type: " << type << "\n" << "| ERROR::SHADER: Compile-time error: Type: " << type << "\n"
<< infoLog << infoLog
@ -115,8 +126,8 @@ void Shader::checkCompileErrors(unsigned int object, std::string type) {
} }
} else { } else {
glGetProgramiv(object, GL_LINK_STATUS, &success); glGetProgramiv(object, GL_LINK_STATUS, &success);
if (!success) { if (success == 0) {
glGetProgramInfoLog(object, 1024, NULL, infoLog); glGetProgramInfoLog(object, 1024, nullptr, infoLog);
std::cout std::cout
<< "| ERROR::Shader: Link-time error: Type: " << type << "\n" << "| ERROR::Shader: Link-time error: Type: " << type << "\n"
<< infoLog << infoLog
@ -126,4 +137,4 @@ void Shader::checkCompileErrors(unsigned int object, std::string type) {
} }
} }
#endif // BREAKOUT_SHADER_CPP #endif

View File

@ -1,59 +1,41 @@
// Copyright [2023] Krzysztof Rudnicki // Copyright [2023] Krzysztof Rudnicki
#ifndef BREAKOUT_SHADER_HPP_ #ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_SHADER_HPP
#define BREAKOUT_SHADER_HPP_ #define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_SHADER_HPP
#include <string>
#include "../dependencies/include/glad/glad.h" #include "../dependencies/include/glad/glad.h"
#include "../dependencies/include/glm/glm/glm.hpp" #include "../dependencies/include/glm/glm/glm.hpp"
#include "../dependencies/include/glm/glm/gtc/type_ptr.hpp" #include "../dependencies/include/glm/glm/gtc/type_ptr.hpp"
// General purpose shader object. Compiles from file, generates // General purpose shader object. Compiles from file, generates
// compile/link-time error messages and hosts several utility // compile/link-time error messages and hosts several utility
// functions for easy management. // functions for easy management.
class Shader { class Shader {
public: public:
// state // state
unsigned int ID; unsigned int ID{};
// constructor // constructor
Shader() { } Shader() = default;
// sets the current shader as active // sets the current shader as active
Shader &Use(); auto Use() -> Shader &;
// compiles the shader from given source code // compiles the shader from given source code
void Compile( void Compile(const char *vertexSource, const char *fragmentSource,
const char *vertexSource, const char *geometrySource);
const char *fragmentSource, // note: geometrysource code is optional
const char *geometrySource = nullptr); // utility functions
// note: geometrysource code is optional void SetFloat(const char *name, float value, bool useShader);
// utility functions void SetInteger(const char *name, int value, bool useShader);
void SetFloat( void SetVector2f(const char *name, float x, float y, bool useShader);
const char *name, void SetVector2f(const char *name, const glm::vec2 &value, bool useShader);
float value, void SetVector3f(const char *name, float x, float y, float z, bool useShader);
bool useShader = false); void SetVector3f(const char *name, const glm::vec3 &value, bool useShader);
void SetInteger(const char *name, void SetVector4f(const char *name, float x, float y, float z, float w,
int value, bool useShader);
bool useShader = false); void SetVector4f(const char *name, const glm::vec4 &value, bool useShader);
void SetVector2f(const char *name, void SetMatrix4(const char *name, const glm::mat4 &matrix, bool useShader);
float x, float y, bool useShader = false);
void SetVector2f(
const char *name, const glm::vec2 &value, bool useShader = false);
void SetVector3f(
const char *name, float x, float y, float z, bool useShader = false);
void SetVector3f(
const char *name, const glm::vec3 &value, bool useShader = false);
void SetVector4f(
const char *name,
float x, float y, float z, float w,
bool useShader = false);
void SetVector4f(
const char *name, const glm::vec4 &value, bool useShader = false);
void SetMatrix4(
const char *name, const glm::mat4 &matrix, bool useShader = false);
private: private:
// checks if compilation or linking failed and if so, print the error logs // checks if compilation or linking failed and if so, print the error logs
void checkCompileErrors(unsigned int object, std::string type); static void checkCompileErrors(unsigned int object, const std::string &type);
}; };
#endif // BREAKOUT_SHADER_HPP_ #endif // HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_SHADER_HPP

View File

@ -1,8 +1,7 @@
// Copyright [2023] Krzysztof Rudnicki // Copyright [2023] Krzysztof Rudnicki
#ifndef BREAKOUT_TEXTURE_CPP_ #ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_TEXTURE_CPP
#define BREAKOUT_TEXTURE_CPP_ #define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_TEXTURE_CPP
#include <iostream>
#include "../breakout/texture.hpp" #include "../breakout/texture.hpp"
@ -44,4 +43,4 @@ void Texture2D::Bind() const {
glBindTexture(GL_TEXTURE_2D, this->ID); glBindTexture(GL_TEXTURE_2D, this->ID);
} }
#endif // BREAKOUT_TEXTURE_CPP #endif // HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_TEXTURE_CPP

View File

@ -1,6 +1,6 @@
// Copyright [2023] Krzysztof Rudnicki // Copyright [2023] Krzysztof Rudnicki
#ifndef BREAKOUT_TEXTURE_HPP_ #ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_TEXTURE_HPP
#define BREAKOUT_TEXTURE_HPP_ #define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_TEXTURE_HPP
#include "../dependencies/include/glad/glad.h" #include "../dependencies/include/glad/glad.h"
@ -10,7 +10,7 @@ class Texture2D {
public: public:
// holds the ID of the texture object, // holds the ID of the texture object,
// used for all texture operations to reference to this particular texture // used for all texture operations to reference to this particular texture
unsigned int ID; unsigned int ID{};
// texture image dimensions // texture image dimensions
unsigned int Width, Height; unsigned int Width, Height;
// width and height of loaded image in pixels // width and height of loaded image in pixels
@ -36,4 +36,4 @@ public:
void Bind() const; void Bind() const;
}; };
#endif // BREAKOUT_TEXTURE_HPP_ #endif // HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_TEXTURE_HPP

View File

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/sh
clang-tidy --config-file=./.clang-tidy --fix-errors --fix-notes -p ./build/compile_commands.json ./breakout/breakout.cpp ./breakout/game.cpp ./breakout/game.hpp ./breakout/resourceManager.cpp ./breakout/resourceManager.hpp ./breakout/shader.cpp ./breakout/shader.hpp ./breakout/texture.cpp ./breakout/texture.hpp clang-tidy --config-file=./.clang-tidy --fix-errors --fix-notes -p ./build/compile_commands.json ./breakout/breakout.cpp
cpplint ./breakout/breakout.cpp ./breakout/game.cpp ./breakout/game.hpp ./breakout/resourceManager.cpp ./breakout/resourceManager.hpp ./breakout/shader.cpp ./breakout/shader.hpp ./breakout/texture.cpp ./breakout/texture.hpp cpplint ./breakout/breakout.cpp