chore: add new folder

This commit is contained in:
Krzysztof Rudnicki 2023-04-02 17:59:15 +02:00
parent 56cd687c30
commit f097369bab
19 changed files with 9744 additions and 26 deletions

View File

@ -1,22 +1,20 @@
{
"configurations": [
{
"defines":[
"GLFW_INCLUDE_NONE"
],
"name": "linux-gcc-x64",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/dependencies/include",
"${workspaceFolder}/dependencies/include/glad/**"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [],
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
"configurations": [
{
"defines": [
"GLFW_INCLUDE_NONE"
],
"name": "linux-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [],
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}

View File

@ -1,8 +1,6 @@
{
"recommendations": [
"aaron-bond.better-comments",
"ms-vscode.cpptools",
"ms-vscode.cpptools-themes",
"wmaurer.change-case",
"xaver.clang-format",
"streetsidesoftware.code-spell-checker",
@ -15,6 +13,7 @@
"davidanson.vscode-markdownlint",
"pkief.material-icon-theme",
"streetsidesoftware.code-spell-checker-polish",
"slevesque.shader"
"slevesque.shader",
"ms-vscode.cpptools-themes"
]
}

47
.vscode/launch.json vendored
View File

@ -5,7 +5,33 @@
"defines": [
"GLFW_INCLUDE_NONE"
],
"name": "C/C++ Runner: Debug Session",
"name": "C/C++ Runner: BREAKOUT",
"type": "cppdbg",
"request": "launch",
"args": [
""
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/Engine/breakout",
"environment": [],
"program": "${workspaceFolder}/Engine/breakout/build/Debug/outDebug",
"internalConsoleOptions": "openOnSessionStart",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"externalConsole": false,
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"defines": [
"GLFW_INCLUDE_NONE"
],
"name": "C/C++ Runner: learn opgl",
"type": "cppdbg",
"request": "launch",
"args": [
@ -26,6 +52,25 @@
"ignoreFailures": true
}
]
},
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "/home/kuchy/inzynierka/engineer-thesis-WUT/breakout",
"program": "/home/kuchy/inzynierka/engineer-thesis-WUT/breakout/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

View File

@ -106,5 +106,7 @@
"RENDERLOOP",
"VERTICE",
"vshader"
]
],
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.useMsvc": false
}

38
.vscode/tasks.json vendored
View File

@ -2,7 +2,7 @@
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-20 build active file",
"label": "C/C++: g++-20 build learnogl",
"command": "/usr/bin/g++",
"args": [
"-g",
@ -35,6 +35,42 @@
"isDefault": true
},
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: g++-20 build breakout",
"command": "/usr/bin/g++",
"args": [
"-g",
"-std=c++2a",
"-I${workspaceFolder}/dependencies/include",
"-L${workspaceFolder}/dependencies/library",
"-Wall",
"${workspaceFolder}/Engine/breakout/*.hpp",
"${workspaceFolder}/Engine/breakout/*.cpp",
"${workspaceFolder}/Engine/breakout/glad.c",
"-o",
"${workspaceFolder}/Engine/breakout/breakout",
"-lglut",
"-lglfw",
"-lGLU",
"-lGL",
"-lm",
"-ldl",
"-pipe",
"-O0"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"

119
breakout/breakout.cpp Normal file
View File

@ -0,0 +1,119 @@
#ifndef BREAKOUT_CPP
#define BREAKOUT_CPP
#include "../dependencies/include/glad/glad.h"
#include <GLFW/glfw3.h>
#include "game.hpp"
#include "resourceManager.hpp"
#include <iostream>
// GLFW function declarations
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
// The Width of the screen
const unsigned int SCREEN_WIDTH = 800;
// The height of the screen
const unsigned int SCREEN_HEIGHT = 600;
Game Breakout(SCREEN_WIDTH, SCREEN_HEIGHT);
int main(int argc, char *argv[])
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
glfwWindowHint(GLFW_RESIZABLE, false);
GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Breakout", nullptr, nullptr);
glfwMakeContextCurrent(window);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glfwSetKeyCallback(window, key_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// OpenGL configuration
// --------------------
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// initialize game
// ---------------
Breakout.Init();
// deltaTime variables
// -------------------
float deltaTime = 0.0f;
float lastFrame = 0.0f;
while (!glfwWindowShouldClose(window))
{
// calculate delta time
// --------------------
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glfwPollEvents();
// manage user input
// -----------------
Breakout.ProcessInput(deltaTime);
// update game state
// -----------------
Breakout.Update(deltaTime);
// render
// ------
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
Breakout.Render();
glfwSwapBuffers(window);
}
// delete all resources as loaded using the resource manager
// ---------------------------------------------------------
ResourceManager::Clear();
glfwTerminate();
return 0;
}
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, 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 // BREAKOUT_CPP

Binary file not shown.

BIN
breakout/build/Debug/game.o Normal file

Binary file not shown.

37
breakout/game.cpp Normal file
View File

@ -0,0 +1,37 @@
#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)
{
}
Game::~Game()
{
}
void Game::Init()
{
}
void Game::Update(float dt)
{
}
void Game::ProcessInput(float dt)
{
}
void Game::Render()
{
}
#endif // GAME_CPP

32
breakout/game.hpp Normal file
View File

@ -0,0 +1,32 @@
#ifndef BREAKOUT_GAME_HPP_
#define BREAKOUT_GAME_HPP_
#include "../dependencies/include/glad/glad.h"
#include <GLFW/glfw3.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();
};
#endif // BREAKOUT_GAME_HPP_

1140
breakout/glad.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,112 @@
#ifndef BREAKOUT_RESOURCE_MANAGER_HPP_
#define BREAKOUT_RESOURCE_MANAGER_HPP_
#include "resourceManager.hpp"
#include <iostream>
#include <sstream>
#include <fstream>
#include "../dependencies/include/stb_image.h"
// Instantiate static variables
std::map<std::string, Texture2D> ResourceManager::Textures;
std::map<std::string, Shader> ResourceManager::Shaders;
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)
{
return Shaders[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)
{
return Textures[name];
}
void ResourceManager::Clear()
{
// (properly) delete all shaders
for (auto iter : Shaders)
glDeleteProgram(iter.second.ID);
// (properly) delete all textures
for (auto iter : Textures)
glDeleteTextures(1, &iter.second.ID);
}
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
{
// open files
std::ifstream vertexShaderFile(vShaderFile);
std::ifstream fragmentShaderFile(fShaderFile);
std::stringstream vShaderStream, 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 e)
{
std::cout << "ERROR::SHADER: Failed to read shader files" << std::endl;
}
const char *vShaderCode = vertexCode.c_str();
const char *fShaderCode = fragmentCode.c_str();
const char *gShaderCode = geometryCode.c_str();
// 2. now create shader object from source code
Shader shader;
shader.Compile(vShaderCode, fShaderCode, gShaderFile != nullptr ? gShaderCode : nullptr);
return shader;
}
Texture2D ResourceManager::loadTextureFromFile(const char *file, bool alpha)
{
// create texture object
Texture2D texture;
if (alpha)
{
texture.Internal_Format = GL_RGBA;
texture.Image_Format = GL_RGBA;
}
// load image
int width, height, nrChannels;
unsigned char* data = stbi_load(file, &width, &height, &nrChannels, 0);
// now generate texture
texture.Generate(width, height, data);
// and finally free image data
stbi_image_free(data);
return texture;
}
#endif

View File

@ -0,0 +1,53 @@
/*******************************************************************
** This code is part of Breakout.
**
** Breakout is free software: you can redistribute it and/or modify
** it under the terms of the CC BY 4.0 license as published by
** Creative Commons, either version 4 of the License, or (at your
** option) any later version.
******************************************************************/
#ifndef BREAKOUT_RESOURCE_MANAGER_HPP_
#define BREAKOUT_RESOURCE_MANAGER_HPP_
#include <map>
#include <string>
#include "../dependencies/include/glad/glad.h"
#include "texture.hpp"
#include "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
// public constructor is defined.
class ResourceManager
{
public:
// resource storage
static std::map<std::string, Shader> Shaders;
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
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_RESOURCE_MANAGER_HPP_

133
breakout/shader.cpp Normal file
View File

@ -0,0 +1,133 @@
#ifndef BREAKOUT_SHADER_CPP
#define BREAKOUT_SHADER_CPP
#include "./shader.hpp"
#include <iostream>
Shader &Shader::Use()
{
glUseProgram(this->ID);
return *this;
}
void Shader::Compile(const char* vertexSource, const char* fragmentSource, const char* geometrySource)
{
unsigned int sVertex, sFragment, gShader;
// vertex Shader
sVertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(sVertex, 1, &vertexSource, NULL);
glCompileShader(sVertex);
checkCompileErrors(sVertex, "VERTEX");
// fragment Shader
sFragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(sFragment, 1, &fragmentSource, NULL);
glCompileShader(sFragment);
checkCompileErrors(sFragment, "FRAGMENT");
// if geometry shader source code is given, also compile geometry shader
if (geometrySource != nullptr)
{
gShader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(gShader, 1, &geometrySource, NULL);
glCompileShader(gShader);
checkCompileErrors(gShader, "GEOMETRY");
}
// shader program
this->ID = glCreateProgram();
glAttachShader(this->ID, sVertex);
glAttachShader(this->ID, sFragment);
if (geometrySource != nullptr)
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
glDeleteShader(sVertex);
glDeleteShader(sFragment);
if (geometrySource != nullptr)
glDeleteShader(gShader);
}
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)
{
if (useShader)
this->Use();
glUniform1i(glGetUniformLocation(this->ID, name), value);
}
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)
{
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)
{
if (useShader)
this->Use();
glUniform3f(glGetUniformLocation(this->ID, name), x, y, z);
}
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);
}
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)
{
if (useShader)
this->Use();
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)
{
if (useShader)
this->Use();
glUniformMatrix4fv(glGetUniformLocation(this->ID, name), 1, false, glm::value_ptr(matrix));
}
void Shader::checkCompileErrors(unsigned int object, std::string type)
{
int success;
char infoLog[1024];
if (type != "PROGRAM")
{
glGetShaderiv(object, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(object, 1024, NULL, infoLog);
std::cout << "| ERROR::SHADER: Compile-time error: Type: " << type << "\n"
<< infoLog << "\n -- --------------------------------------------------- -- "
<< std::endl;
}
}
else
{
glGetProgramiv(object, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(object, 1024, NULL, infoLog);
std::cout << "| ERROR::Shader: Link-time error: Type: " << type << "\n"
<< infoLog << "\n -- --------------------------------------------------- -- "
<< std::endl;
}
}
}
#endif // BREAKOUT_SHADER_CPP

42
breakout/shader.hpp Normal file
View File

@ -0,0 +1,42 @@
#ifndef BREAKOUT_SHADER_HPP_
#define BREAKOUT_SHADER_HPP_
#include <string>
#include "../dependencies/include/glad/glad.h"
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
// General purpose shader object. Compiles from file, generates
// compile/link-time error messages and hosts several utility
// functions for easy management.
class Shader
{
public:
// state
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
// 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:
// checks if compilation or linking failed and if so, print the error logs
void checkCompileErrors(unsigned int object, std::string type);
};
#endif // BREAKOUT_SHADER_HPP_

37
breakout/texture.cpp Normal file
View File

@ -0,0 +1,37 @@
#ifndef BREAKOUT_TEXTURE_CPP_
#define BREAKOUT_TEXTURE_CPP_
#include <iostream>
#include "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)
{
glGenTextures(1, &this->ID);
}
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);
// 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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, this->Filter_Min);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, this->Filter_Max);
// unbind texture
glBindTexture(GL_TEXTURE_2D, 0);
}
void Texture2D::Bind() const
{
glBindTexture(GL_TEXTURE_2D, this->ID);
}
#endif // BREAKOUT_TEXTURE_CPP_

31
breakout/texture.hpp Normal file
View File

@ -0,0 +1,31 @@
#ifndef BREAKOUT_TEXTURE_HPP_
#define BREAKOUT_TEXTURE_HPP_
#include "../dependencies/include/glad/glad.h"
// Texture2D is able to store and configure a texture in OpenGL.
// 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
unsigned int ID;
// texture image dimensions
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
// 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
// constructor (sets default texture modes)
Texture2D();
// generates texture from image data
void Generate(unsigned int width, unsigned int height, unsigned char* data);
// binds the texture as the current active GL_TEXTURE_2D texture object
void Bind() const;
};
#endif // BREAKOUT_TEXTURE_HPP_

7897
dependencies/include/stb_image.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,5 @@
#ifndef STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#endif