diff --git a/breakout/game.cpp b/breakout/game.cpp index 4bd0f44..e677d9e 100644 --- a/breakout/game.cpp +++ b/breakout/game.cpp @@ -1,38 +1,27 @@ // Copyright [2023] Krzysztof Rudnicki #ifndef GAME_CPP #define GAME_CPP -#include "./game.hpp" - -Game::Game(unsigned int width, unsigned int height) - : State(GAME_ACTIVE), Keys(), Width(width), Height(height) -{ +#include "../breakout/game.hpp" +Game::Game(unsigned int width, unsigned int 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 \ No newline at end of file +#endif +// GAME_CPP diff --git a/breakout/game.hpp b/breakout/game.hpp index 54698f5..d1f3b08 100644 --- a/breakout/game.hpp +++ b/breakout/game.hpp @@ -2,32 +2,33 @@ #ifndef BREAKOUT_GAME_HPP_ #define BREAKOUT_GAME_HPP_ -#include "../dependencies/include/glad/glad.h" #include +#include "../dependencies/include/glad/glad.h" + // Represents the current state of the game enum GameState { GAME_ACTIVE, GAME_MENU, GAME_WIN -}; +}; class Game { - public: - // game state - GameState State; - bool Keys[1024]; - unsigned int Width, Height; - // constructor/destructor - Game(unsigned int width, unsigned int height); - ~Game(); - // initialize game state (load all shaders/textures/levels) - void Init(); - // game loop - void ProcessInput(float dt); - void Update(float dt); - void Render(); + public: + // game state + GameState State; + bool Keys[1024]; + unsigned int Width, Height; + // constructor/destructor + Game(unsigned int width, unsigned int height); + ~Game(); + // initialize game state (load all shaders/textures/levels) + void Init(); + // game loop + void ProcessInput(float dt); + void Update(float dt); + void Render(); }; -#endif // BREAKOUT_GAME_HPP_ \ No newline at end of file +#endif // BREAKOUT_GAME_HPP_ diff --git a/breakout/resourceManager.cpp b/breakout/resourceManager.cpp index 0dcef7b..fde20f0 100644 --- a/breakout/resourceManager.cpp +++ b/breakout/resourceManager.cpp @@ -2,13 +2,15 @@ #ifndef BREAKOUT_RESOURCE_MANAGER_HPP_ #define BREAKOUT_RESOURCE_MANAGER_HPP_ -#include "resourceManager.hpp" -#include "./shader.hpp" -#include "./texture.hpp" + #include #include #include + +#include "../breakout/resourceManager.hpp" +#include "./shader.hpp" +#include "./texture.hpp" #include "./stb_image.h" // Instantiate static variables @@ -16,31 +18,33 @@ std::map ResourceManager::Textures; std::map 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); return Shaders[name]; } -Shader ResourceManager::GetShader(std::string name) -{ +Shader ResourceManager::GetShader(std::string 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); return Textures[name]; } -Texture2D ResourceManager::GetTexture(std::string name) -{ +Texture2D ResourceManager::GetTexture(std::string name) { return Textures[name]; } -void ResourceManager::Clear() -{ - // (properly) delete all shaders +void ResourceManager::Clear() { + // (properly) delete all shaders for (auto iter : Shaders) glDeleteProgram(iter.second.ID); // (properly) delete all textures @@ -48,14 +52,15 @@ void ResourceManager::Clear() 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 std::string vertexCode; std::string fragmentCode; std::string geometryCode; - try - { + try { // open files std::ifstream vertexShaderFile(vShaderFile); std::ifstream fragmentShaderFile(fShaderFile); @@ -70,8 +75,7 @@ Shader ResourceManager::loadShaderFromFile(const char *vShaderFile, const char * vertexCode = vShaderStream.str(); fragmentCode = fShaderStream.str(); // if geometry shader path is present, also load a geometry shader - if (gShaderFile != nullptr) - { + if (gShaderFile != nullptr) { std::ifstream geometryShaderFile(gShaderFile); std::stringstream gShaderStream; gShaderStream << geometryShaderFile.rdbuf(); @@ -79,8 +83,7 @@ Shader ResourceManager::loadShaderFromFile(const char *vShaderFile, const char * geometryCode = gShaderStream.str(); } } - catch (std::exception const&) - { + catch (std::exception const&) { std::cout << "ERROR::SHADER: Failed to read shader files" << std::endl; } const char *vShaderCode = vertexCode.c_str(); @@ -88,16 +91,15 @@ Shader ResourceManager::loadShaderFromFile(const char *vShaderFile, const char * const char *gShaderCode = geometryCode.c_str(); // 2. now create shader object from source code Shader shader; - shader.Compile(vShaderCode, fShaderCode, gShaderFile != nullptr ? gShaderCode : nullptr); + shader.Compile(vShaderCode, + fShaderCode, gShaderFile != nullptr ? gShaderCode : nullptr); return shader; } -Texture2D ResourceManager::loadTextureFromFile(const char *file, bool alpha) -{ +Texture2D ResourceManager::loadTextureFromFile(const char *file, bool alpha) { // create texture object Texture2D texture; - if (alpha) - { + if (alpha) { texture.Internal_Format = GL_RGBA; texture.Image_Format = GL_RGBA; } @@ -111,4 +113,4 @@ Texture2D ResourceManager::loadTextureFromFile(const char *file, bool alpha) return texture; } -#endif \ No newline at end of file +#endif diff --git a/breakout/resourceManager.hpp b/breakout/resourceManager.hpp index f3189c7..20f89f2 100644 --- a/breakout/resourceManager.hpp +++ b/breakout/resourceManager.hpp @@ -7,43 +7,60 @@ ** Creative Commons, either version 4 of the License, or (at your ** option) any later version. ******************************************************************/ -#define BREAKOUT_RESOURCE_MANAGER_HPP_ +#ifndef BREAKOUT_RESOURCEMANAGER_HPP_ +#define BREAKOUT_RESOURCEMANAGER_HPP_ #include #include #include "../dependencies/include/glad/glad.h" -#include "texture.hpp" -#include "shader.hpp" +#include "../breakout/texture.hpp" +#include "../breakout/shader.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 +// handles. All functions and resources are static and no // public constructor is defined. -class ResourceManager -{ -public: - // resource storage - static std::map Shaders; - static std::map 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 Shader LoadShader(const char *vShaderFile, const char *fShaderFile, const char *gShaderFile, std::string name); - // retrieves a stored sader - static Shader GetShader(std::string name); - // loads (and generates) a texture from file - static Texture2D LoadTexture(const char *file, bool alpha, std::string name); - // retrieves a stored texture - static Texture2D GetTexture(std::string name); - // 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() { } - // loads and generates a shader from file - static Shader loadShaderFromFile(const char *vShaderFile, const char *fShaderFile, const char *gShaderFile = nullptr); - // loads a single texture from file - static Texture2D loadTextureFromFile(const char *file, bool alpha); +class ResourceManager { + public: + // resource storage + static std::map Shaders; + static std::map 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 Shader LoadShader( + const char *vShaderFile, + const char *fShaderFile, + const char *gShaderFile, + std::string name); + // retrieves a stored sader + static Shader GetShader(std::string name); + // loads (and generates) a texture from file + static Texture2D LoadTexture( + const char *file, + bool alpha, + std::string name); + // retrieves a stored texture + static Texture2D GetTexture(std::string name); + // 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() { } + // loads and generates a shader from file + static Shader loadShaderFromFile( + const char *vShaderFile, + const char *fShaderFile, + const char *gShaderFile = nullptr); + // loads a single texture from file + static Texture2D loadTextureFromFile(const char *file, bool alpha); }; + +#endif // BREAKOUT_RESOURCEMANAGER_HPP_ diff --git a/breakout/shader.cpp b/breakout/shader.cpp index 09d1ddd..2f48801 100644 --- a/breakout/shader.cpp +++ b/breakout/shader.cpp @@ -1,7 +1,7 @@ // Copyright [2023] Krzysztof Rudnicki #ifndef BREAKOUT_SHADER_CPP #define BREAKOUT_SHADER_CPP -#include "./shader.hpp" +#include "../breakout/shader.hpp" #include @@ -11,8 +11,9 @@ Shader &Shader::Use() 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; // vertex Shader sVertex = glCreateShader(GL_VERTEX_SHADER); @@ -25,8 +26,7 @@ void Shader::Compile(const char* vertexSource, const char* fragmentSource, const glCompileShader(sFragment); checkCompileErrors(sFragment, "FRAGMENT"); // if geometry shader source code is given, also compile geometry shader - if (geometrySource != nullptr) - { + if (geometrySource != nullptr) { gShader = glCreateShader(GL_GEOMETRY_SHADER); glShaderSource(gShader, 1, &geometrySource, NULL); glCompileShader(gShader); @@ -40,95 +40,110 @@ void Shader::Compile(const char* vertexSource, const char* fragmentSource, const glAttachShader(this->ID, gShader); glLinkProgram(this->ID); 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(sFragment); if (geometrySource != nullptr) glDeleteShader(gShader); } -void Shader::SetFloat(const char *name, float value, bool useShader) -{ +void Shader::SetFloat(const char *name, float value, bool useShader) { if (useShader) this->Use(); 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) this->Use(); 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) this->Use(); 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) this->Use(); 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) this->Use(); 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) 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) this->Use(); 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) 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) 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; char infoLog[1024]; - if (type != "PROGRAM") - { + if (type != "PROGRAM") { glGetShaderiv(object, GL_COMPILE_STATUS, &success); - if (!success) - { + if (!success) { glGetShaderInfoLog(object, 1024, NULL, infoLog); - std::cout << "| ERROR::SHADER: Compile-time error: Type: " << type << "\n" - << infoLog << "\n -- --------------------------------------------------- -- " - << std::endl; + std::cout << "| ERROR::SHADER: Compile-time error: Type: " + << type << "\n" + << infoLog + << "\n -- --------------------------------------------------- -- " + << std::endl; } - } - else - { + } else { glGetProgramiv(object, GL_LINK_STATUS, &success); - if (!success) - { + if (!success) { glGetProgramInfoLog(object, 1024, NULL, infoLog); - std::cout << "| ERROR::Shader: Link-time error: Type: " << type << "\n" - << infoLog << "\n -- --------------------------------------------------- -- " - << std::endl; + std::cout << "| ERROR::Shader: Link-time error: Type: " + << type << "\n" + << infoLog + << "\n -- --------------------------------------------------- -- " + << std::endl; } } } -#endif // BREAKOUT_SHADER_CPP \ No newline at end of file +#endif // BREAKOUT_SHADER_CPP diff --git a/breakout/shader.hpp b/breakout/shader.hpp index b4b29c7..d34b67c 100644 --- a/breakout/shader.hpp +++ b/breakout/shader.hpp @@ -10,32 +10,50 @@ // 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. -class Shader -{ -public: +class Shader { + public: // state - unsigned int ID; + unsigned int ID; // constructor Shader() { } // sets the current shader as active Shader &Use(); // 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 - void SetFloat (const char *name, float value, bool useShader = false); - void SetInteger (const char *name, int value, bool useShader = false); - void SetVector2f (const char *name, 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: + void SetFloat( + const char *name, + float value, + bool useShader = false); + void SetInteger(const char *name, + int value, + bool useShader = false); + void SetVector2f(const char *name, + 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: // 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); }; -#endif // BREAKOUT_SHADER_HPP_ \ No newline at end of file +#endif // BREAKOUT_SHADER_HPP_ diff --git a/breakout/texture.cpp b/breakout/texture.cpp index befc1aa..6b286e3 100644 --- a/breakout/texture.cpp +++ b/breakout/texture.cpp @@ -5,22 +5,32 @@ #include -#include "texture.hpp" +#include "../breakout/texture.hpp" 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); } -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->Height = height; // create Texture 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 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->Wrap_S); 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); } -void Texture2D::Bind() const -{ +void Texture2D::Bind() const { glBindTexture(GL_TEXTURE_2D, this->ID); } -#endif // BREAKOUT_TEXTURE_CPP_ \ No newline at end of file +#endif // BREAKOUT_TEXTURE_CPP diff --git a/breakout/texture.hpp b/breakout/texture.hpp index 3803a45..423bb6a 100644 --- a/breakout/texture.hpp +++ b/breakout/texture.hpp @@ -8,19 +8,27 @@ // It also hosts utility functions for easy management. class Texture2D { -public: - // holds the ID of the texture object, used for all texture operations to reference to this particular texture + public: + // holds the ID of the texture object, + // used for all texture operations to reference to this particular texture unsigned int ID; // 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 - unsigned int Internal_Format; // format of texture object - unsigned int Image_Format; // format of loaded image + unsigned int Internal_Format; + // format of texture object + unsigned int Image_Format; + // format of loaded image // texture configuration - unsigned int Wrap_S; // wrapping mode on S axis - unsigned int Wrap_T; // 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 + unsigned int Wrap_S; + // wrapping mode on S axis + unsigned int Wrap_T; + // 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) Texture2D(); // generates texture from image data @@ -29,4 +37,4 @@ public: void Bind() const; }; -#endif // BREAKOUT_TEXTURE_HPP_ \ No newline at end of file +#endif // BREAKOUT_TEXTURE_HPP_