1
0

Setting up

This commit is contained in:
2022-10-19 14:27:04 +02:00
commit e211fb13c3
14 changed files with 8444 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
build/
.vscode/
lib/

12
Makefile Normal file
View File

@@ -0,0 +1,12 @@
CC = g++
CFLAGS = -std=c++17 -O2 -g
LDFLAGS = -lglfw3 -lGL -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi -lz
OUT_NAME = BreakOut
build: src/*.cpp src/*.h
mkdir -p build/
$(CC) $(CFLAGS) -Ilib/GLAD/include -Ilib/glfw/include -o build/$(OUT_NAME) lib/GLAD/src/glad.c src/*.cpp $(LDFLAGS)
run: build build/$(OUT_NAME)
./build/$(OUT_NAME)

1
README.md Normal file
View File

@@ -0,0 +1 @@
# Breakout

33
src/game.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include "game.h"
Game::~Game()
{
}
Game::Game(unsigned int width, unsigned int height)
{
}
void Game::Init()
{
}
void Game::ProcessInput(float dt)
{
}
void Game::Update(float dt)
{
}
void Game::Render()
{
}

25
src/game.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
// Represent the curreent 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();
};

105
src/main.cpp Normal file
View File

@@ -0,0 +1,105 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "game.h"
#include "resourcemanager.h"
// GLFW function declarations
void framebuffer_size_callbacks(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);
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_callbacks);
// 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 applicationl
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
{
Breakout.Keys[key] = false;
}
}
}
void framebuffer_size_callbacks(GLFWwindow* window, int width, int height)
{
glViewport(0,0,width, height);
}

107
src/resourcemanager.cpp Normal file
View File

@@ -0,0 +1,107 @@
#include "resourcemanager.h"
#include <iostream>
#include <sstream>
#include <fstream>
#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;
}

40
src/resourcemanager.h Normal file
View File

@@ -0,0 +1,40 @@
#pragma once
#include <map>
#include <string>
#include <glad/glad.h>
#include "texture2d.h"
#include "shader.h"
// 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);
};

128
src/shader.cpp Normal file
View File

@@ -0,0 +1,128 @@
#include "shader.h"
#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;
}
}
}

33
src/shader.h Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
#include <string>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
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);
};

3
src/stb_image.cpp Normal file
View File

@@ -0,0 +1,3 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

7897
src/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

30
src/texture2d.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "texture2d.h"
#include <iostream>
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);
}

27
src/texture2d.h Normal file
View File

@@ -0,0 +1,27 @@
#pragma once
#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 particlar 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;
};