clang format added

This commit is contained in:
MichaelFisher1997 2025-01-14 23:18:11 +00:00
parent 6b0cc46045
commit 47f8068cdd
8 changed files with 293 additions and 340 deletions

View File

@ -1,60 +1,65 @@
#include "IndexBuffer.h" #include "IndexBuffer.h"
#include "Renderer.h" #include "Renderer.h"
IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int count) IndexBuffer::IndexBuffer(const unsigned int *data, unsigned int count)
: m_Count(count) : m_Count(count) {
{
ASSERT(sizeof(unsigned int) == sizeof(GLuint)); ASSERT(sizeof(unsigned int) == sizeof(GLuint));
// //
//std::cout << "m_RendererID: " << &m_RendererID << " " << m_RendererID << std::endl; // std::cout << "m_RendererID: " << &m_RendererID << " " << m_RendererID <<
// std::endl;
GLCall(glGenBuffers(1, &m_RendererID)); GLCall(glGenBuffers(1, &m_RendererID));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer' GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,
GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), data, GL_STATIC_DRAW)); // assigne buffer size, static as we use many times, but does not change m_RendererID)); // select buffer called 'buffer'
GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int),
data,
GL_STATIC_DRAW)); // assigne buffer size, static as we use
// many times, but does not change
} }
// Destructor // Destructor
IndexBuffer::~IndexBuffer() { IndexBuffer::~IndexBuffer() {
if (m_RendererID != 0) { // Only delete if valid if (m_RendererID != 0) { // Only delete if valid
GLCall(glDeleteBuffers(1, &m_RendererID)); GLCall(glDeleteBuffers(1, &m_RendererID));
std::cout << "IndexBuffer destroyed with RendererID: " << m_RendererID << std::endl; std::cout << "IndexBuffer destroyed with RendererID: " << m_RendererID
} << std::endl;
}
} }
// Move Constructor // Move Constructor
IndexBuffer::IndexBuffer(IndexBuffer&& other) noexcept IndexBuffer::IndexBuffer(IndexBuffer &&other) noexcept
: m_RendererID(other.m_RendererID), m_Count(other.m_Count) { : m_RendererID(other.m_RendererID), m_Count(other.m_Count) {
other.m_RendererID = 0; // Invalidate the moved-from object other.m_RendererID = 0; // Invalidate the moved-from object
other.m_Count = 0; other.m_Count = 0;
std::cout << "IndexBuffer moved (constructor)" << std::endl; std::cout << "IndexBuffer moved (constructor)" << std::endl;
} }
// Move Assignment Operator // Move Assignment Operator
IndexBuffer& IndexBuffer::operator=(IndexBuffer&& other) noexcept { IndexBuffer &IndexBuffer::operator=(IndexBuffer &&other) noexcept {
if (this != &other) { if (this != &other) {
// Free existing resources // Free existing resources
if (m_RendererID != 0) { if (m_RendererID != 0) {
GLCall(glDeleteBuffers(1, &m_RendererID)); GLCall(glDeleteBuffers(1, &m_RendererID));
}
// Transfer ownership
m_RendererID = other.m_RendererID;
m_Count = other.m_Count;
// Invalidate the moved-from object
other.m_RendererID = 0;
other.m_Count = 0;
std::cout << "IndexBuffer moved (assignment)" << std::endl;
} }
return *this;
// Transfer ownership
m_RendererID = other.m_RendererID;
m_Count = other.m_Count;
// Invalidate the moved-from object
other.m_RendererID = 0;
other.m_Count = 0;
std::cout << "IndexBuffer moved (assignment)" << std::endl;
}
return *this;
} }
void IndexBuffer::Bind() const void IndexBuffer::Bind() const {
{ GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer' m_RendererID)); // select buffer called 'buffer'
} }
void IndexBuffer::Unbind() const void IndexBuffer::Unbind() const {
{ GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); //select buffer called 'buffer' 0)); // select buffer called 'buffer'
} }

View File

@ -3,19 +3,20 @@
#include "Renderer.h" #include "Renderer.h"
void GLClearError() { void GLClearError() {
while (glGetError() != GL_NO_ERROR); while (glGetError() != GL_NO_ERROR)
;
} }
bool GLLogCall() { bool GLLogCall() {
Color::Modifier red(Color::FG_RED); Color::Modifier red(Color::FG_RED);
Color::Modifier def(Color::FG_DEFAULT); Color::Modifier def(Color::FG_DEFAULT);
while (GLenum error = glGetError()) { while (GLenum error = glGetError()) {
std::cout << red << "[OpenGL Error] (" << error << ")" << def << std::endl; //if error, it will return a number, this needs to be converted to hex to then look up that value inn GL docs std::cout
<< red << "[OpenGL Error] (" << error << ")" << def
<< std::endl; // if error, it will return a number, this needs to be
// converted to hex to then look up that value inn GL docs
return false; return false;
} }
return true; return true;
} }

View File

@ -1,157 +1,135 @@
#include "Shader.h" #include "Shader.h"
#include "IndexBuffer.h"
#include "Renderer.h"
#include "VertexArray.h"
#include "VertexBuffer.h"
#include "fstream"
#include <cstdio> #include <cstdio>
#include <iostream> #include <iostream>
#include <string>
#include <sstream> #include <sstream>
#include "VertexBuffer.h" #include <string>
#include "VertexArray.h"
#include "IndexBuffer.h"
#include "fstream"
#include "Renderer.h"
Shader::Shader(const std::string& filepath) Shader::Shader(const std::string &filepath)
: m_FilePath(filepath), m_RendererID(0) : m_FilePath(filepath), m_RendererID(0) {
{ ShaderProgramSource source = ParseShader(filepath);
ShaderProgramSource source = ParseShader(filepath);
std::cout << "VERTEX" << std::endl << source.VertexSource << std::endl; std::cout << "VERTEX" << std::endl << source.VertexSource << std::endl;
std::cout << "FRAGMENT" << std::endl << source.FragmentSource << std::endl; std::cout << "FRAGMENT" << std::endl << source.FragmentSource << std::endl;
m_RendererID = CreateShader(source.VertexSource, source.FragmentSource); m_RendererID = CreateShader(source.VertexSource, source.FragmentSource);
GLCall( glUseProgram(m_RendererID) ); GLCall(glUseProgram(m_RendererID));
} }
Shader::~Shader() Shader::~Shader() { GLCall(glDeleteProgram(m_RendererID)); }
{
GLCall( glDeleteProgram(m_RendererID) ); void Shader::Bind() const { GLCall(glUseProgram(m_RendererID)); }
void Shader::Unbind() const { GLCall(glUseProgram(0)); }
int Shader::GetUniformLocation(const std::string &name) {
if (m_UniformLocationCache.find(name) != m_UniformLocationCache.end())
return m_UniformLocationCache[name];
GLCall(int location = glGetUniformLocation(m_RendererID, name.c_str()));
if (location == -1)
std::cout << "No active uniform variable with name " << name << " found"
<< std::endl;
m_UniformLocationCache[name] = location;
return location;
} }
void Shader::Bind() const void Shader::SetUniform1f(const std::string &name, float value) {
{ GLCall(glUniform1f(GetUniformLocation(name), value));
GLCall( glUseProgram(m_RendererID) );
} }
void Shader::Unbind() const void Shader::SetUniform4f(const std::string &name, float f0, float f1, float f2,
{ float f3) {
GLCall( glUseProgram(0) ); GLCall(glUniform4f(GetUniformLocation(name), f0, f1, f2, f3));
} }
int Shader::GetUniformLocation(const std::string& name) enum ShaderType { NONE = -1, VERTEX = 0, FRAGMENT = 1 };
{
if (m_UniformLocationCache.find(name) != m_UniformLocationCache.end())
return m_UniformLocationCache[name];
GLCall( int location = glGetUniformLocation(m_RendererID, name.c_str()) ); struct ShaderProgramSource Shader::ParseShader(const std::string &filepath) {
if (location == -1)
std::cout << "No active uniform variable with name " << name << " found" << std::endl;
m_UniformLocationCache[name] = location; std::ifstream stream(filepath);
std::string line;
std::stringstream ss[2];
ShaderType type = NONE;
return location; while (getline(stream, line)) {
} if (line.find("#shader") != std::string::npos) {
if (line.find("vertex") != std::string::npos)
void Shader::SetUniform1f(const std::string& name, float value) type = VERTEX;
{ else if (line.find("fragment") != std::string::npos)
GLCall( glUniform1f(GetUniformLocation(name), value) ); type = FRAGMENT;
} } else {
ss[(int)type] << line << '\n';
void Shader::SetUniform4f(const std::string& name, float f0, float f1, float f2, float f3)
{
GLCall( glUniform4f(GetUniformLocation(name), f0, f1, f2, f3) );
}
enum ShaderType
{
NONE = -1, VERTEX = 0, FRAGMENT = 1
};
struct ShaderProgramSource Shader::ParseShader(const std::string& filepath)
{
std::ifstream stream(filepath);
std::string line;
std::stringstream ss[2];
ShaderType type = NONE;
while (getline(stream, line))
{
if (line.find("#shader") != std::string::npos)
{
if (line.find("vertex") != std::string::npos)
type = VERTEX;
else if (line.find("fragment") != std::string::npos)
type = FRAGMENT;
}
else
{
ss[(int)type] << line << '\n';
}
} }
}
struct ShaderProgramSource sps = { ss[0].str(), ss[1].str() }; struct ShaderProgramSource sps = {ss[0].str(), ss[1].str()};
return sps; return sps;
} }
unsigned int Shader::CompileShader(unsigned int type, const std::string& source) unsigned int Shader::CompileShader(unsigned int type,
{ const std::string &source) {
GLCall( unsigned int id = glCreateShader(type) ); GLCall(unsigned int id = glCreateShader(type));
const char* src = source.c_str(); const char *src = source.c_str();
GLCall( glShaderSource(id, 1, &src, nullptr) ); GLCall(glShaderSource(id, 1, &src, nullptr));
GLCall( glCompileShader(id) ); GLCall(glCompileShader(id));
// Error handling // Error handling
int result; int result;
GLCall( glGetShaderiv(id, GL_COMPILE_STATUS, &result) ); GLCall(glGetShaderiv(id, GL_COMPILE_STATUS, &result));
std::cout << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader compile status: " << result << std::endl; std::cout << (type == GL_VERTEX_SHADER ? "vertex" : "fragment")
if ( result == GL_FALSE ) << " shader compile status: " << result << std::endl;
{ if (result == GL_FALSE) {
int length; int length;
GLCall( glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length) ); GLCall(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
char* message = (char*) alloca(length * sizeof(char)); char *message = (char *)alloca(length * sizeof(char));
GLCall( glGetShaderInfoLog(id, length, &length, message) ); GLCall(glGetShaderInfoLog(id, length, &length, message));
std::cout std::cout << "Failed to compile "
<< "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << "shader"
<< (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << std::endl;
<< "shader" std::cout << message << std::endl;
<< std::endl; GLCall(glDeleteShader(id));
std::cout << message << std::endl; return 0;
GLCall( glDeleteShader(id) ); }
return 0;
}
return id; return id;
} }
unsigned int Shader::CreateShader(const std::string& vertexShader, const std::string& fragmentShader) unsigned int Shader::CreateShader(const std::string &vertexShader,
{ const std::string &fragmentShader) {
// create a shader program // create a shader program
unsigned int program = glCreateProgram(); unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader); unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader); unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
GLCall( glAttachShader(program, vs) ); GLCall(glAttachShader(program, vs));
GLCall( glAttachShader(program, fs) ); GLCall(glAttachShader(program, fs));
GLCall( glLinkProgram(program) ); GLCall(glLinkProgram(program));
GLint program_linked; GLint program_linked;
GLCall( glGetProgramiv(program, GL_LINK_STATUS, &program_linked) ); GLCall(glGetProgramiv(program, GL_LINK_STATUS, &program_linked));
std::cout << "Program link status: " << program_linked << std::endl; std::cout << "Program link status: " << program_linked << std::endl;
if (program_linked != GL_TRUE) if (program_linked != GL_TRUE) {
{ GLsizei log_length = 0;
GLsizei log_length = 0; GLchar message[1024];
GLchar message[1024]; GLCall(glGetProgramInfoLog(program, 1024, &log_length, message));
GLCall( glGetProgramInfoLog(program, 1024, &log_length, message) ); std::cout << "Failed to link program" << std::endl;
std::cout << "Failed to link program" << std::endl; std::cout << message << std::endl;
std::cout << message << std::endl; }
}
GLCall( glValidateProgram(program) ); GLCall(glValidateProgram(program));
GLCall( glDeleteShader(vs) ); GLCall(glDeleteShader(vs));
GLCall( glDeleteShader(fs) ); GLCall(glDeleteShader(fs));
return program; return program;
} }

View File

@ -1,41 +1,33 @@
#include "VertexArray.h" #include "VertexArray.h"
#include "Renderer.h" #include "Renderer.h"
VertexArray::VertexArray() VertexArray::VertexArray() {
{
std::cout << "VertexArray::VertexArray()" << std::endl; std::cout << "VertexArray::VertexArray()" << std::endl;
std::cout << "m_RendererID: " << &m_RendererID << std::endl; std::cout << "m_RendererID: " << &m_RendererID << std::endl;
GLCall(glGenVertexArrays(1, &m_RendererID)); GLCall(glGenVertexArrays(1, &m_RendererID));
//glGenVertexArrays(1, &m_RendererID); // glGenVertexArrays(1, &m_RendererID);
std::cout << "m_RendererID: " << &m_RendererID << std::endl; std::cout << "m_RendererID: " << &m_RendererID << std::endl;
if (!m_RendererID) { if (!m_RendererID) {
std::cerr << "Failed to generate VAO" << std::endl; std::cerr << "Failed to generate VAO" << std::endl;
} }
}
VertexArray::~VertexArray()
{
GLCall(glDeleteVertexArrays(1, &m_RendererID));
} }
VertexArray::~VertexArray() { GLCall(glDeleteVertexArrays(1, &m_RendererID)); }
void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) void VertexArray::AddBuffer(const VertexBuffer &vb,
{ const VertexBufferLayout &layout) {
Bind(); Bind();
vb.Bind(); vb.Bind();
const std::vector<VertexBufferElement> elements = layout.GetElements(); const std::vector<VertexBufferElement> elements = layout.GetElements();
unsigned int offset = 0; unsigned int offset = 0;
for (unsigned int i = 0; i < elements.size(); i++) { for (unsigned int i = 0; i < elements.size(); i++) {
const auto& element = elements[i]; const auto &element = elements[i];
GLCall(glEnableVertexAttribArray(i)); GLCall(glEnableVertexAttribArray(i));
GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized,layout.GetStride(), INT2VOIDP(offset)) ); GLCall(glVertexAttribPointer(i, element.count, element.type,
offset += element.count * VertexBufferElement::GetSizeOfType(element.type); element.normalized, layout.GetStride(),
} INT2VOIDP(offset)));
offset += element.count * VertexBufferElement::GetSizeOfType(element.type);
}
} }
void VertexArray::Bind() const { void VertexArray::Bind() const { GLCall(glBindVertexArray(m_RendererID)); }
GLCall(glBindVertexArray(m_RendererID)); void VertexArray::Unbind() const { GLCall(glBindVertexArray(0)); }
}
void VertexArray::Unbind() const {
GLCall(glBindVertexArray(0));
}

View File

@ -1,24 +1,22 @@
#include "VertexBuffer.h" #include "VertexBuffer.h"
#include "Renderer.h" #include "Renderer.h"
VertexBuffer::VertexBuffer(const void* data, unsigned int size) VertexBuffer::VertexBuffer(const void *data, unsigned int size) {
{
GLCall(glGenBuffers(1, &m_RendererID)); GLCall(glGenBuffers(1, &m_RendererID));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer' GLCall(glBindBuffer(GL_ARRAY_BUFFER,
GLCall(glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW)); // assigne buffer size, static as we use many times, but does not change m_RendererID)); // select buffer called 'buffer'
GLCall(glBufferData(GL_ARRAY_BUFFER, size, data,
GL_STATIC_DRAW)); // assigne buffer size, static as we use
// many times, but does not change
} }
VertexBuffer::~VertexBuffer() VertexBuffer::~VertexBuffer() { GLCall(glDeleteBuffers(1, &m_RendererID)); }
{
GLCall(glDeleteBuffers(1, &m_RendererID)); void VertexBuffer::Bind() const {
GLCall(glBindBuffer(GL_ARRAY_BUFFER,
m_RendererID)); // select buffer called 'buffer'
} }
void VertexBuffer::Bind() const void VertexBuffer::Unbind() const {
{ GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0)); // select buffer called 'buffer'
GLCall(glBindBuffer(GL_ARRAY_BUFFER, m_RendererID)); //select buffer called 'buffer'
}
void VertexBuffer::Unbind() const
{
GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0)); //select buffer called 'buffer'
} }

View File

@ -1,14 +1,13 @@
#include "sdl.hpp" #include "sdl.hpp"
int main(int argc, char* argv[]) { int main(int argc, char *argv[]) {
// Create an instance of our SdlWindow class // Create an instance of our SdlWindow class
std::cout << "starting" << std::endl; std::cout << "starting" << std::endl;
SdlWindow window("My SDL2 Window", 800, 600); SdlWindow window("My SDL2 Window", 800, 600);
std::cout << "About to run: " << std::endl; std::cout << "About to run: " << std::endl;
// Run the main loop // Run the main loop
window.run(); window.run();
return 0; return 0;
} }

View File

@ -1,107 +1,91 @@
#include <GL/glew.h> // Include GLEW before <SDL2/SDL.h>?
#include "sdl.hpp" #include "sdl.hpp"
#include "VertexBufferLayout.h" #include "VertexBufferLayout.h"
#include <GL/glew.h> // Include GLEW before <SDL2/SDL.h>?
#include <SDL2/SDL_video.h> #include <SDL2/SDL_video.h>
#include <alloca.h> #include <alloca.h>
#include <cstdio> #include <cstdio>
#include <iostream> #include <iostream>
#include <string>
#include <ostream> #include <ostream>
#include <string> #include <string>
#include "Renderer.h"
#include "VertexBuffer.h"
#include "VertexArray.h"
#include "IndexBuffer.h" #include "IndexBuffer.h"
#include "Renderer.h"
#include "Shader.h" #include "Shader.h"
#include "VertexArray.h"
#include "VertexBuffer.h"
SdlWindow::SdlWindow(const char* title, int width, int height) SdlWindow::SdlWindow(const char *title, int width, int height)
: m_window(nullptr), : m_window(nullptr), m_renderer(nullptr), m_isRunning(false),
m_renderer(nullptr), m_isFullscreen(false), m_width(width), m_height(height),
m_isRunning(false), m_windowedWidth(width), m_windowedHeight(height), r(0.5f),
m_isFullscreen(false), m_glContext(nullptr), increment(0.05f), m_Shader(nullptr), m_Location(-1),
m_width(width), m_IB(nullptr), m_VB(nullptr), m_VA(nullptr) {
m_height(height),
m_windowedWidth(width),
m_windowedHeight(height),
r(0.5f),
m_glContext(nullptr),
increment(0.05f),
m_Shader(nullptr),
m_Location(-1),
m_IB(nullptr),
m_VB(nullptr),
m_VA(nullptr)
{
std::cout << "Step 0: hellow world" << std::endl; std::cout << "Step 0: hellow world" << std::endl;
// 1. Set attributes // 1. Set attributes
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
std::cout << "Step 1: SDL_GL_SetAttribute completed" << std::endl; std::cout << "Step 1: SDL_GL_SetAttribute completed" << std::endl;
// 2. Create the window with OpenGL flag // 2. Create the window with OpenGL flag
m_window = SDL_CreateWindow(title, m_window = SDL_CreateWindow(
SDL_WINDOWPOS_CENTERED, title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height,
SDL_WINDOWPOS_CENTERED, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
width,
height,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN| SDL_WINDOW_RESIZABLE);
if (!m_window) { if (!m_window) {
std::cerr << "Failed to create window: " << SDL_GetError() << std::endl; std::cerr << "Failed to create window: " << SDL_GetError() << std::endl;
return; return;
} }
std::cout << "Step 2: SDL_CreateWindow completed" << std::endl; std::cout << "Step 2: SDL_CreateWindow completed" << std::endl;
m_glContext = SDL_GL_CreateContext(m_window); m_glContext = SDL_GL_CreateContext(m_window);
if (!m_glContext) { if (!m_glContext) {
std::cerr << "Failed to create GL context: " << SDL_GetError() << std::endl; std::cerr << "Failed to create GL context: " << SDL_GetError() << std::endl;
return; return;
} }
std::cout << "Step 3: SDL_GL_CreateContext completed" << std::endl; std::cout << "Step 3: SDL_GL_CreateContext completed" << std::endl;
if (SDL_GL_MakeCurrent(m_window, m_glContext) != 0) { if (SDL_GL_MakeCurrent(m_window, m_glContext) != 0) {
std::cerr << "Failed to make GL context current: " << SDL_GetError() << std::endl; std::cerr << "Failed to make GL context current: " << SDL_GetError()
return; << std::endl;
return;
} }
std::cout << "Step 4: SDL_GL_MakeCurrent completed" << std::endl; std::cout << "Step 4: SDL_GL_MakeCurrent completed" << std::endl;
#ifndef __APPLE__ #ifndef __APPLE__
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
GLenum glewErr = glewInit(); GLenum glewErr = glewInit();
if (glewErr != GLEW_OK) { if (glewErr != GLEW_OK) {
std::cerr << "Failed to init GLEW: " << glewGetErrorString(glewErr) << std::endl; std::cerr << "Failed to init GLEW: " << glewGetErrorString(glewErr)
return; << std::endl;
return;
} }
std::cout << "Step 5: GLEW initialized successfully" << std::endl; std::cout << "Step 5: GLEW initialized successfully" << std::endl;
glGetError(); // Clear GLEW's initial error glGetError(); // Clear GLEW's initial error
#endif #endif
std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl; std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
std::cout << "GLSL Version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl; std::cout << "GLSL Version: " << glGetString(GL_SHADING_LANGUAGE_VERSION)
<< std::endl;
std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl; std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl;
// 5. Set vsync (optional) // 5. Set vsync (optional)
SDL_GL_SetSwapInterval(1); //Vsync SDL_GL_SetSwapInterval(1); // Vsync
// 6. Mark as running // 6. Mark as running
m_isRunning = true; m_isRunning = true;
float positions[] = { // vertex for a triangle float positions[] = {
//x,y // vertex for a triangle
-0.5f, -0.5f, // 0 // x,y
0.5f, -0.5f, // 1 -0.5f, -0.5f, // 0
0.5f, 0.5f, //2 0.5f, -0.5f, // 1
-0.5f, 0.5f // 3 0.5f, 0.5f, // 2
-0.5f, 0.5f // 3
}; };
unsigned int indices[] = { unsigned int indices[] = {0, 1, 2, 2, 3, 0};
0, 1, 2,
2, 3, 0
};
m_VA = new VertexArray(); m_VA = new VertexArray();
m_VB = new VertexBuffer(positions, 4 * 2 * sizeof(float)); m_VB = new VertexBuffer(positions, 4 * 2 * sizeof(float));
m_IB = new IndexBuffer(indices, 6); m_IB = new IndexBuffer(indices, 6);
@ -111,32 +95,29 @@ SdlWindow::SdlWindow(const char* title, int width, int height)
m_VA->AddBuffer(*m_VB, layout); m_VA->AddBuffer(*m_VB, layout);
m_Shader = new Shader("res/shaders/Basic.shader"); m_Shader = new Shader("res/shaders/Basic.shader");
m_Shader->Bind(); m_Shader->Bind();
} }
SdlWindow::~SdlWindow() { SdlWindow::~SdlWindow() {
// If using SDL Renderer, destroy it. But if youre purely using OpenGL, you might remove it. // If using SDL Renderer, destroy it. But if youre purely using OpenGL, you
// might remove it.
if (m_renderer) { if (m_renderer) {
SDL_DestroyRenderer(m_renderer); SDL_DestroyRenderer(m_renderer);
m_renderer = nullptr; m_renderer = nullptr;
} }
// Delete the OpenGL context // Delete the OpenGL context
if (m_glContext) { if (m_glContext) {
SDL_GL_DeleteContext(m_glContext); SDL_GL_DeleteContext(m_glContext);
m_glContext = nullptr; m_glContext = nullptr;
} }
if (m_window) { if (m_window) {
SDL_DestroyWindow(m_window); SDL_DestroyWindow(m_window);
m_window = nullptr; m_window = nullptr;
} }
////TODO ////TODO
//some point check everything is being deleted // some point check everything is being deleted
SDL_Quit(); SDL_Quit();
} }
// //
@ -150,7 +131,7 @@ void SdlWindow::run() {
} }
// //
void SdlWindow::processEvents() { void SdlWindow::processEvents() {
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) { if (event.type == SDL_QUIT) {
m_isRunning = false; m_isRunning = false;
@ -167,22 +148,21 @@ SDL_Event event;
} }
if (event.key.keysym.sym == SDLK_ESCAPE) { if (event.key.keysym.sym == SDLK_ESCAPE) {
std::cout << "Bye!" << std::endl; std::cout << "Bye!" << std::endl;
m_isRunning = false; //exit application m_isRunning = false; // exit application
} }
} } else if (event.type == SDL_WINDOWEVENT) {
else if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_RESIZED) { if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
// SDL gives the new width/height in event.window.data1/data2 // SDL gives the new width/height in event.window.data1/data2
int newWidth = event.window.data1; int newWidth = event.window.data1;
int newHeight = event.window.data2; int newHeight = event.window.data2;
// Update your internal width/height (if you keep track) // Update your internal width/height (if you keep track)
m_width = newWidth; m_width = newWidth;
m_height = newHeight; m_height = newHeight;
// If we are in windowed mode, update the "windowed" size. // If we are in windowed mode, update the "windowed" size.
if (!m_isFullscreen) { if (!m_isFullscreen) {
m_windowedWidth = newWidth; m_windowedWidth = newWidth;
m_windowedHeight = newHeight; m_windowedHeight = newHeight;
} }
// Update the OpenGL viewport // Update the OpenGL viewport
@ -207,7 +187,7 @@ void SdlWindow::update() {
// //
void SdlWindow::render() { void SdlWindow::render() {
// Use GL calls instead of SDLs renderer // Use GL calls instead of SDLs renderer
GLCall(glClearColor(0.0f, 0.0f, 0.0f, 1.0f)); //background GLCall(glClearColor(0.0f, 0.0f, 0.0f, 1.0f)); // background
// //
GLCall(glClear(GL_COLOR_BUFFER_BIT)); GLCall(glClear(GL_COLOR_BUFFER_BIT));
@ -217,32 +197,33 @@ void SdlWindow::render() {
m_VA->Bind(); m_VA->Bind();
m_IB->Bind(); m_IB->Bind();
// TODO: Draw with OpenGL here (shaders, triangles, etc.) // TODO: Draw with OpenGL here (shaders, triangles, etc.)
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr)); //macro assert for debugging GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT,
nullptr)); // macro assert for debugging
// Swap buffers // Swap buffers
SDL_GL_SwapWindow(m_window); SDL_GL_SwapWindow(m_window);
} }
// //
void SdlWindow::setFullscreen(bool fullscreen) { void SdlWindow::setFullscreen(bool fullscreen) {
if (m_window) { if (m_window) {
m_isFullscreen = fullscreen; m_isFullscreen = fullscreen;
if (m_isFullscreen) { if (m_isFullscreen) {
// Going TO fullscreen: // Going TO fullscreen:
// (Optional) store the current size again, in case it changed // (Optional) store the current size again, in case it changed
m_windowedWidth = m_width; m_windowedWidth = m_width;
m_windowedHeight = m_height; m_windowedHeight = m_height;
SDL_SetWindowFullscreen(m_window, SDL_WINDOW_FULLSCREEN_DESKTOP); SDL_SetWindowFullscreen(m_window, SDL_WINDOW_FULLSCREEN_DESKTOP);
} else { } else {
// Returning FROM fullscreen: // Returning FROM fullscreen:
SDL_SetWindowFullscreen(m_window, 0); // return to windowed SDL_SetWindowFullscreen(m_window, 0); // return to windowed
// Now restore the windows old size // Now restore the windows old size
SDL_SetWindowSize(m_window, m_windowedWidth, m_windowedHeight); SDL_SetWindowSize(m_window, m_windowedWidth, m_windowedHeight);
// Update m_width, m_height so they reflect the new (restored) size // Update m_width, m_height so they reflect the new (restored) size
m_width = m_windowedWidth; m_width = m_windowedWidth;
m_height = m_windowedHeight; m_height = m_windowedHeight;
}
} }
}
} }

View File

@ -4,16 +4,16 @@
#include <string> #include <string>
// Include the OpenGL headers (these may vary by system) // Include the OpenGL headers (these may vary by system)
#if defined(__APPLE__) #if defined(__APPLE__)
#include <OpenGL/gl3.h> // On macOS, <OpenGL/gl3.h> is often used #include <OpenGL/gl3.h> // On macOS, <OpenGL/gl3.h> is often used
#else #else
#include <GL/glew.h> // or <glad/glad.h> if using GLAD #include <GL/glew.h> // or <glad/glad.h> if using GLAD
#endif #endif
#include <string>
#include "IndexBuffer.h" #include "IndexBuffer.h"
#include "VertexBufferLayout.h"
#include "VertexArray.h"
#include "Shader.h" #include "Shader.h"
#include "VertexArray.h"
#include "VertexBufferLayout.h"
#include <string>
// Forward declaration of classes and structs if needed // Forward declaration of classes and structs if needed
// class SomethingElse; // class SomethingElse;
@ -21,50 +21,49 @@
class SdlWindow { class SdlWindow {
public: public:
// Constructor // Constructor
SdlWindow(const char* title, int width, int height); SdlWindow(const char *title, int width, int height);
// Destructor // Destructor
~SdlWindow(); ~SdlWindow();
// Run the main loop // Run the main loop
void run(); void run();
// unsigned int compileShader(unsigned int type, const std::string& source); // unsigned int compileShader(unsigned int type, const std::string& source);
// unsigned int createShader(const std::string& vetexShader, const std::string& fragmentShader); // unsigned int createShader(const std::string& vetexShader, const
// // for spliting our shaders // std::string& fragmentShader);
// //struct ShaderProgramSource { // // for spliting our shaders
// // std::string VetexSource, FragmentSource; // //struct ShaderProgramSource {
// struct ShaderProgramSource { // // std::string VetexSource, FragmentSource;
// std::string VertexSource; // struct ShaderProgramSource {
// std::string FragmentSource; // std::string VertexSource;
// }; // std::string FragmentSource;
// ShaderProgramSource parseShader(const std::string& filepath); // };
// ShaderProgramSource parseShader(const std::string& filepath);
void setFullscreen(bool fullscreen); void setFullscreen(bool fullscreen);
private: private:
// Private data members // Private data members
SDL_Window* m_window; SDL_Window *m_window;
SDL_Renderer* m_renderer; SDL_Renderer *m_renderer;
bool m_isRunning; bool m_isRunning;
bool m_isFullscreen; bool m_isFullscreen;
int m_width; int m_width;
int m_height; int m_height;
int m_windowedWidth; // stored width before fullscreen int m_windowedWidth; // stored width before fullscreen
int m_windowedHeight; // stored height before fullscreen int m_windowedHeight; // stored height before fullscreen
float r; float r;
SDL_GLContext m_glContext; SDL_GLContext m_glContext;
float increment; float increment;
// // temp shader stuff // // temp shader stuff
GLint m_Location; GLint m_Location;
VertexArray* m_VA; VertexArray *m_VA;
IndexBuffer* m_IB; IndexBuffer *m_IB;
VertexBuffer* m_VB; VertexBuffer *m_VB;
Shader* m_Shader; Shader *m_Shader;
// Private methods // Private methods
void processEvents(); void processEvents();
void update(); void update();
void render(); void render();
}; };