[Refactor] More spring cleaning for the renderer.

This commit is contained in:
Ritchie Cunningham 2025-09-27 23:22:51 +01:00
parent e8ca1630c6
commit 5cbcfeb54e

View File

@ -1,41 +1,41 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <string>
#include <GL/glew.h>
#include "shader.h"
/* Read whole file. */
char* read_file(const char* path) {
FILE* file = fopen(path, "rb");
if(!file) {
std::string read_file(const char* path) {
std::ifstream file(path);
if(!file.is_open()) {
printf("Failed to open file: %s\n", path);
return NULL;
return "";
}
fseek(file, 0, SEEK_END);
long len = ftell(file);
fseek(file, 0, SEEK_SET);
char* buffer = (char*)malloc(len+1);
fread(buffer, 1, len, file);
fclose(file);
buffer[len] = '\0';
return buffer;
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
Shader::Shader(const char* vert_path, const char* frag_path) {
char* vert_source = read_file(vert_path);
char* frag_source = read_file(frag_path);
if(!vert_source || !frag_source) {
printf("Failed to read shader files.\n");
std::string vert_string = read_file(vert_path);
std::string frag_string = read_file(frag_path);
if(vert_string.empty() || frag_string.empty()) {
return;
}
const char* vert_source = vert_string.c_str();
const char* frag_source = frag_string.c_str();
unsigned int vert, frag;
vert = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vert, 1, &vert_source, NULL);
glCompileShader(vert);
_check_compile_errors(vert, "VERTEX");
frag = glCreateShader(GL_FRAGMENT_SHADER);
frag = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(frag, 1, &frag_source, NULL);
glCompileShader(frag);
_check_compile_errors(frag, "FRAGMENT");
@ -48,8 +48,6 @@ Shader::Shader(const char* vert_path, const char* frag_path) {
glDeleteShader(vert);
glDeleteShader(frag);
free(vert_source);
free(frag_source);
}
void Shader::use(void) {