feat: make all code compliant with cpplint

This commit is contained in:
Krzysztof Rudnicki 2023-07-02 14:44:29 +02:00
parent 40cee498e3
commit 72951f8a8c
8 changed files with 234 additions and 175 deletions

View File

@ -1,38 +1,27 @@
// Copyright [2023] Krzysztof Rudnicki // Copyright [2023] Krzysztof Rudnicki
#ifndef GAME_CPP #ifndef GAME_CPP
#define GAME_CPP #define GAME_CPP
#include "./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() {
{
} }
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 // GAME_CPP #endif
// GAME_CPP

View File

@ -2,9 +2,10 @@
#ifndef BREAKOUT_GAME_HPP_ #ifndef BREAKOUT_GAME_HPP_
#define BREAKOUT_GAME_HPP_ #define BREAKOUT_GAME_HPP_
#include "../dependencies/include/glad/glad.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "../dependencies/include/glad/glad.h"
// Represents the current state of the game // Represents the current state of the game
enum GameState { enum GameState {
GAME_ACTIVE, GAME_ACTIVE,

View File

@ -2,13 +2,15 @@
#ifndef BREAKOUT_RESOURCE_MANAGER_HPP_ #ifndef BREAKOUT_RESOURCE_MANAGER_HPP_
#define BREAKOUT_RESOURCE_MANAGER_HPP_ #define BREAKOUT_RESOURCE_MANAGER_HPP_
#include "resourceManager.hpp"
#include "./shader.hpp"
#include "./texture.hpp"
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#include "../breakout/resourceManager.hpp"
#include "./shader.hpp"
#include "./texture.hpp"
#include "./stb_image.h" #include "./stb_image.h"
// Instantiate static variables // Instantiate static variables
@ -16,30 +18,32 @@ std::map<std::string, Texture2D> ResourceManager::Textures;
std::map<std::string, Shader> ResourceManager::Shaders; std::map<std::string, Shader> ResourceManager::Shaders;
Shader ResourceManager::LoadShader(const char *vShaderFile, const char *fShaderFile, const char *gShaderFile, std::string name) Shader ResourceManager::LoadShader(
{ const char *vShaderFile,
const char *fShaderFile,
const char *gShaderFile,
std::string name) {
Shaders[name] = loadShaderFromFile(vShaderFile, fShaderFile, gShaderFile); Shaders[name] = loadShaderFromFile(vShaderFile, fShaderFile, gShaderFile);
return Shaders[name]; return Shaders[name];
} }
Shader ResourceManager::GetShader(std::string name) Shader ResourceManager::GetShader(std::string name) {
{
return Shaders[name]; return Shaders[name];
} }
Texture2D ResourceManager::LoadTexture(const char *file, bool alpha, std::string name) Texture2D ResourceManager::LoadTexture(
{ const char *file,
bool alpha,
std::string name) {
Textures[name] = loadTextureFromFile(file, alpha); Textures[name] = loadTextureFromFile(file, alpha);
return Textures[name]; return Textures[name];
} }
Texture2D ResourceManager::GetTexture(std::string name) Texture2D ResourceManager::GetTexture(std::string name) {
{
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 (auto iter : Shaders)
glDeleteProgram(iter.second.ID); glDeleteProgram(iter.second.ID);
@ -48,14 +52,15 @@ void ResourceManager::Clear()
glDeleteTextures(1, &iter.second.ID); glDeleteTextures(1, &iter.second.ID);
} }
Shader ResourceManager::loadShaderFromFile(const char *vShaderFile, const char *fShaderFile, const char *gShaderFile) Shader ResourceManager::loadShaderFromFile(
{ const char *vShaderFile,
const char *fShaderFile,
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);
@ -70,8 +75,7 @@ Shader ResourceManager::loadShaderFromFile(const char *vShaderFile, const char *
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();
@ -79,8 +83,7 @@ Shader ResourceManager::loadShaderFromFile(const char *vShaderFile, const char *
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();
@ -88,16 +91,15 @@ Shader ResourceManager::loadShaderFromFile(const char *vShaderFile, const char *
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, fShaderCode, gShaderFile != nullptr ? gShaderCode : nullptr); shader.Compile(vShaderCode,
fShaderCode, gShaderFile != nullptr ? gShaderCode : nullptr);
return shader; return shader;
} }
Texture2D ResourceManager::loadTextureFromFile(const char *file, bool alpha) Texture2D ResourceManager::loadTextureFromFile(const char *file, 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;
} }

View File

@ -7,15 +7,16 @@
** 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.
******************************************************************/ ******************************************************************/
#define BREAKOUT_RESOURCE_MANAGER_HPP_ #ifndef BREAKOUT_RESOURCEMANAGER_HPP_
#define BREAKOUT_RESOURCEMANAGER_HPP_
#include <map> #include <map>
#include <string> #include <string>
#include "../dependencies/include/glad/glad.h" #include "../dependencies/include/glad/glad.h"
#include "texture.hpp" #include "../breakout/texture.hpp"
#include "shader.hpp" #include "../breakout/shader.hpp"
// A static singleton ResourceManager class that hosts several // A static singleton ResourceManager class that hosts several
@ -23,27 +24,43 @@
// 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
static std::map<std::string, Shader> Shaders; static std::map<std::string, Shader> Shaders;
static std::map<std::string, Texture2D> Textures; static std::map<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 // loads (and generates) a shader program from file loading vertex,
static Shader LoadShader(const char *vShaderFile, const char *fShaderFile, const char *gShaderFile, std::string name); // fragment (and geometry) shader's source code.
// If gShaderFile is not nullptr, it also loads a geometry shader
static Shader LoadShader(
const char *vShaderFile,
const char *fShaderFile,
const char *gShaderFile,
std::string name);
// retrieves a stored sader // retrieves a stored sader
static Shader GetShader(std::string name); static Shader GetShader(std::string name);
// loads (and generates) a texture from file // loads (and generates) a texture from file
static Texture2D LoadTexture(const char *file, bool alpha, std::string name); static Texture2D LoadTexture(
const char *file,
bool alpha,
std::string name);
// retrieves a stored texture // retrieves a stored texture
static Texture2D GetTexture(std::string name); static Texture2D GetTexture(std::string name);
// properly de-allocates all loaded resources // properly de-allocates all loaded resources
static void Clear(); 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). private:
// private constructor,
// that is we do not want any actual resource manager objects.
// Its members and functions should be publicly available (static).
ResourceManager() { } ResourceManager() { }
// loads and generates a shader from file // loads and generates a shader from file
static Shader loadShaderFromFile(const char *vShaderFile, const char *fShaderFile, const char *gShaderFile = nullptr); static Shader loadShaderFromFile(
const char *vShaderFile,
const char *fShaderFile,
const char *gShaderFile = nullptr);
// loads a single texture from file // loads a single texture from file
static Texture2D loadTextureFromFile(const char *file, bool alpha); static Texture2D loadTextureFromFile(const char *file, bool alpha);
}; };
#endif // BREAKOUT_RESOURCEMANAGER_HPP_

View File

@ -1,7 +1,7 @@
// Copyright [2023] Krzysztof Rudnicki // Copyright [2023] Krzysztof Rudnicki
#ifndef BREAKOUT_SHADER_CPP #ifndef BREAKOUT_SHADER_CPP
#define BREAKOUT_SHADER_CPP #define BREAKOUT_SHADER_CPP
#include "./shader.hpp" #include "../breakout/shader.hpp"
#include <iostream> #include <iostream>
@ -11,8 +11,9 @@ Shader &Shader::Use()
return *this; return *this;
} }
void Shader::Compile(const char* vertexSource, const char* fragmentSource, const char* geometrySource) void Shader::Compile(const char* vertexSource,
{ const char* fragmentSource,
const char* geometrySource) {
unsigned int sVertex, sFragment, gShader; unsigned int sVertex, sFragment, gShader;
// vertex Shader // vertex Shader
sVertex = glCreateShader(GL_VERTEX_SHADER); sVertex = glCreateShader(GL_VERTEX_SHADER);
@ -25,8 +26,7 @@ void Shader::Compile(const char* vertexSource, const char* fragmentSource, const
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, NULL);
glCompileShader(gShader); glCompileShader(gShader);
@ -40,92 +40,107 @@ void Shader::Compile(const char* vertexSource, const char* fragmentSource, const
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 program now and no longer necessary // delete the shaders as they're linked into our
// 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, bool useShader) void Shader::SetVector2f(
{ const char *name,
const glm::vec2 &value,
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, bool useShader) void Shader::SetVector3f(
{ const char *name,
float x,
float y,
float z,
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, bool useShader) void Shader::SetVector3f(
{ const char *name,
const glm::vec3 &value,
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, bool useShader) void Shader::SetVector4f(
{ const char *name,
float x, float y, float z, float w, 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, bool useShader) void Shader::SetVector4f(
{ const char *name,
const glm::vec4 &value,
bool useShader) {
if (useShader) if (useShader)
this->Use(); this->Use();
glUniform4f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z, value.w); glUniform4f(
glGetUniformLocation(this->ID, name),
value.x, value.y, value.z, value.w);
} }
void Shader::SetMatrix4(const char *name, const glm::mat4 &matrix, bool useShader) void Shader::SetMatrix4(
{ const char *name,
const glm::mat4 &matrix,
bool useShader) {
if (useShader) if (useShader)
this->Use(); this->Use();
glUniformMatrix4fv(glGetUniformLocation(this->ID, name), 1, false, glm::value_ptr(matrix)); glUniformMatrix4fv(
glGetUniformLocation(this->ID, name),
1, false, glm::value_ptr(matrix));
} }
void Shader::checkCompileErrors(unsigned int object, std::string type) void Shader::checkCompileErrors(unsigned int object, std::string type) {
{
int success; int success;
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) {
{
glGetShaderInfoLog(object, 1024, NULL, infoLog); glGetShaderInfoLog(object, 1024, NULL, infoLog);
std::cout << "| ERROR::SHADER: Compile-time error: Type: " << type << "\n" std::cout << "| ERROR::SHADER: Compile-time error: Type: "
<< infoLog << "\n -- --------------------------------------------------- -- " << type << "\n"
<< infoLog
<< "\n -- --------------------------------------------------- -- "
<< std::endl; << std::endl;
} }
} } else {
else
{
glGetProgramiv(object, GL_LINK_STATUS, &success); glGetProgramiv(object, GL_LINK_STATUS, &success);
if (!success) if (!success) {
{
glGetProgramInfoLog(object, 1024, NULL, infoLog); glGetProgramInfoLog(object, 1024, NULL, infoLog);
std::cout << "| ERROR::Shader: Link-time error: Type: " << type << "\n" std::cout << "| ERROR::Shader: Link-time error: Type: "
<< infoLog << "\n -- --------------------------------------------------- -- " << type << "\n"
<< infoLog
<< "\n -- --------------------------------------------------- -- "
<< std::endl; << std::endl;
} }
} }

View File

@ -12,9 +12,8 @@
// 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
@ -22,18 +21,37 @@ public:
// sets the current shader as active // sets the current shader as active
Shader &Use(); Shader &Use();
// compiles the shader from given source code // compiles the shader from given source code
void Compile(const char *vertexSource, const char *fragmentSource, const char *geometrySource = nullptr); // note: geometry source code is optional void Compile(
const char *vertexSource,
const char *fragmentSource,
const char *geometrySource = nullptr);
// note: geometrysource code is optional
// utility functions // utility functions
void SetFloat (const char *name, float value, bool useShader = false); void SetFloat(
void SetInteger (const char *name, int value, bool useShader = false); const char *name,
void SetVector2f (const char *name, float x, float y, bool useShader = false); float value,
void SetVector2f (const char *name, const glm::vec2 &value, bool useShader = false); bool useShader = false);
void SetVector3f (const char *name, float x, float y, float z, bool useShader = false); void SetInteger(const char *name,
void SetVector3f (const char *name, const glm::vec3 &value, bool useShader = false); int value,
void SetVector4f (const char *name, float x, float y, float z, float w, bool useShader = false); bool useShader = false);
void SetVector4f (const char *name, const glm::vec4 &value, bool useShader = false); void SetVector2f(const char *name,
void SetMatrix4 (const char *name, const glm::mat4 &matrix, bool useShader = false); float x, float y, bool useShader = false);
private: 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:
// 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); void checkCompileErrors(unsigned int object, std::string type);
}; };

View File

@ -5,22 +5,32 @@
#include <iostream> #include <iostream>
#include "texture.hpp" #include "../breakout/texture.hpp"
Texture2D::Texture2D() Texture2D::Texture2D()
: Width(0), Height(0), Internal_Format(GL_RGB), Image_Format(GL_RGB), Wrap_S(GL_REPEAT), Wrap_T(GL_REPEAT), Filter_Min(GL_LINEAR), Filter_Max(GL_LINEAR) : Width(0),
{ Height(0),
Internal_Format(GL_RGB),
Image_Format(GL_RGB),
Wrap_S(GL_REPEAT),
Wrap_T(GL_REPEAT),
Filter_Min(GL_LINEAR),
Filter_Max(GL_LINEAR) {
glGenTextures(1, &this->ID); glGenTextures(1, &this->ID);
} }
void Texture2D::Generate(unsigned int width, unsigned int height, unsigned char* data) void Texture2D::Generate(
{ unsigned int width,
unsigned int height,
unsigned char* data) {
this->Width = width; this->Width = width;
this->Height = height; this->Height = height;
// create Texture // create Texture
glBindTexture(GL_TEXTURE_2D, this->ID); glBindTexture(GL_TEXTURE_2D, this->ID);
glTexImage2D(GL_TEXTURE_2D, 0, this->Internal_Format, width, height, 0, this->Image_Format, GL_UNSIGNED_BYTE, data); glTexImage2D(GL_TEXTURE_2D,
0, this->Internal_Format, width, height,
0, this->Image_Format, GL_UNSIGNED_BYTE, data);
// set Texture wrap and filter modes // set Texture wrap and filter modes
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->Wrap_S); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->Wrap_S);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, this->Wrap_T); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, this->Wrap_T);
@ -30,9 +40,8 @@ void Texture2D::Generate(unsigned int width, unsigned int height, unsigned char*
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }
void Texture2D::Bind() const void Texture2D::Bind() const {
{
glBindTexture(GL_TEXTURE_2D, this->ID); glBindTexture(GL_TEXTURE_2D, this->ID);
} }
#endif // BREAKOUT_TEXTURE_CPP_ #endif // BREAKOUT_TEXTURE_CPP

View File

@ -8,19 +8,27 @@
// It also hosts utility functions for easy management. // It also hosts utility functions for easy management.
class Texture2D class Texture2D
{ {
public: public:
// holds the ID of the texture object, used for all texture operations to reference to this particular texture // holds the ID of the texture object,
// 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; // width and height of loaded image in pixels unsigned int Width, Height;
// width and height of loaded image in pixels
// texture Format // texture Format
unsigned int Internal_Format; // format of texture object unsigned int Internal_Format;
unsigned int Image_Format; // format of loaded image // format of texture object
unsigned int Image_Format;
// format of loaded image
// texture configuration // texture configuration
unsigned int Wrap_S; // wrapping mode on S axis unsigned int Wrap_S;
unsigned int Wrap_T; // wrapping mode on T axis // wrapping mode on S axis
unsigned int Filter_Min; // filtering mode if texture pixels < screen pixels unsigned int Wrap_T;
unsigned int Filter_Max; // filtering mode if texture pixels > screen pixels // wrapping mode on T axis
unsigned int Filter_Min;
// filtering mode if texture pixels < screen pixels
unsigned int Filter_Max;
// filtering mode if texture pixels > screen pixels
// constructor (sets default texture modes) // constructor (sets default texture modes)
Texture2D(); Texture2D();
// generates texture from image data // generates texture from image data