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