From 087343599c81025a3ffcbe50f4128f9ced8c0324 Mon Sep 17 00:00:00 2001 From: MichaelFisher1997 Date: Mon, 6 Jan 2025 18:32:22 +0000 Subject: [PATCH] Added a simple shader hardcoded in a string, and a function for creating and compiling the shader --- src/sdl.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/sdl.hpp | 53 +++++++++++++++++++++++------------------- 2 files changed, 96 insertions(+), 23 deletions(-) diff --git a/src/sdl.cpp b/src/sdl.cpp index fb1e3d8..c86bac6 100644 --- a/src/sdl.cpp +++ b/src/sdl.cpp @@ -1,6 +1,10 @@ #include // Include GLEW before ? #include "sdl.hpp" +#include #include +//#include +#include +#include SdlWindow::SdlWindow(const char* title, int width, int height) : m_window(nullptr), @@ -69,6 +73,27 @@ SdlWindow::SdlWindow(const char* title, int width, int height) glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0); + std::string vertexShader = + "#version 330 core\n" + "\n" + "layout(location = 0) in vec4 position;\n" + "\n" + "void main()\n" + "{\n" + " gl_Position = position;\n" + "}\n"; + + std::string fragmentShader = + "#version 330 core\n" + "\n" + "layout(location = 0) out vec4 color;\n" + "\n" + "void main()\n" + "{\n" + " color = vec4(1.0, 0.0, 0.0, 1.0);\n" + "}\n"; + unsigned int shader = createShader(vertexShader, fragmentShader); + glUseProgram(shader); } @@ -187,3 +212,44 @@ void SdlWindow::setFullscreen(bool fullscreen) { } } +unsigned int SdlWindow::compileShader(unsigned int type, const std::string& source) { + unsigned int id = glCreateShader(type); + const char* src = source.c_str(); // <--- this string needs to exist when compiling/running + glShaderSource(id, 1, &src, nullptr); + glCompileShader(id); + //TODO: error handling + int result; + glGetShaderiv(id, GL_COMPILE_STATUS, &result); + if (result == GL_FALSE) { + int length; + glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length); + char* message = (char*)alloca(length * sizeof(char)); //do i need to deallocate this?? + glGetShaderInfoLog(id, length, &length, message); + std::cout << "Failed to compile" << (type == GL_VERTEX_SHADER ? "vertex":"fragment") << "shader" << std::endl; // print out what type of shader it is + std::cout << message << std::endl; + glDeleteShader(id); + return 0; + + } + // + return id; + + +} + +unsigned int SdlWindow::createShader(const std::string& vertexShader, const std::string& fragmentShader) { + unsigned int program = glCreateProgram(); + unsigned int vs = compileShader(GL_VERTEX_SHADER, vertexShader); + unsigned int fs = compileShader(GL_FRAGMENT_SHADER, fragmentShader); + + glAttachShader(program, vs); + glAttachShader(program, fs); + glLinkProgram(program); + glValidateProgram(program); + + glDeleteShader(vs); + glDeleteShader(fs); + + return program; + +} diff --git a/src/sdl.hpp b/src/sdl.hpp index aa1fff1..068305d 100644 --- a/src/sdl.hpp +++ b/src/sdl.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include // Include the OpenGL headers (these may vary by system) #if defined(__APPLE__) #include // On macOS, is often used @@ -8,40 +9,46 @@ #include // or if using GLAD #endif -#include +//#include // Forward declaration of classes and structs if needed // class SomethingElse; class SdlWindow { public: - // Constructor - SdlWindow(const char* title, int width, int height); + // Constructor + SdlWindow(const char* title, int width, int height); - // Destructor - ~SdlWindow(); + // Destructor + ~SdlWindow(); - // Run the main loop - void run(); + // Run the main loop + void run(); + unsigned int compileShader(unsigned int type, const std::string& source); + unsigned int createShader(const std::string& vetexShader, const std::string& fragmentShader); - void setFullscreen(bool fullscreen); + void setFullscreen(bool fullscreen); private: - // Private data members - SDL_Window* m_window; - SDL_Renderer* m_renderer; - bool m_isRunning; - bool m_isFullscreen; - int m_width; - int m_height; - int m_windowedWidth; // stored width before fullscreen - int m_windowedHeight; // stored height before fullscreen - SDL_GLContext m_glContext; - + // Private data members + SDL_Window* m_window; + SDL_Renderer* m_renderer; + bool m_isRunning; + bool m_isFullscreen; + int m_width; + int m_height; + int m_windowedWidth; // stored width before fullscreen + int m_windowedHeight; // stored height before fullscreen + SDL_GLContext m_glContext; + // temp shader stuff + std::string vetexShader; + std::string fragmentShader; + unsigned int shader; + - // Private methods - void processEvents(); - void update(); - void render(); + // Private methods + void processEvents(); + void update(); + void render(); };