From 4db583c4c8ab65cc606e563492ee9d0f26562f8c Mon Sep 17 00:00:00 2001 From: Seth Long Date: Wed, 11 Nov 2026 15:54:47 -0800 Subject: [PATCH] Added error handling for get_source --- gameobject.cpp | 23 +++++++++++++++++++---- main.cpp | 6 +++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/gameobject.cpp b/gameobject.cpp index 623e9b8..898be14 100644 --- a/gameobject.cpp +++ b/gameobject.cpp @@ -2,9 +2,20 @@ /* Remember to free the result! */ char* get_source(const char *filename){ - FILE *source_file = fopen(filename, "r"); - fseek(source_file, 0, SEEK_END); + FILE *source_file = fopen(filename, "rb"); + if(!source_file){ + perror("fopen"); + return 0; + } + if(fseek(source_file, 0, SEEK_END)){ + perror("fseek"); + return 0; + } long length = ftell(source_file); + if(!length){ + printf("Error: Source file %s has length 0\n", filename); + return 0; + } char *source = (char*)malloc(length + 1); rewind(source_file); fread(source, 1, length, source_file); @@ -68,6 +79,10 @@ unsigned int load_shader(string shader_filename, unsigned int type){ } else { char *source = get_source(shader_filename.c_str()); + if(!source){ + printf("Shader %s failed to load\n", shader_filename.c_str()); + exit(1); + } puts(source); unsigned int vs_reference = glCreateShader(type); glShaderSource(vs_reference, 1, (const char**)&source, 0); @@ -79,7 +94,7 @@ unsigned int load_shader(string shader_filename, unsigned int type){ glGetShaderiv(vs_reference, GL_COMPILE_STATUS, &compile_ok); if(!compile_ok){ printf("Shader %s failed to compile\n", shader_filename.c_str()); - return false; + exit(1); } gl_resources[shader_filename] = vs_reference; return vs_reference; @@ -121,7 +136,7 @@ unsigned int make_program(const char* v_file, const char* tcs_file, const char* glGetProgramInfoLog(program, 2048, NULL, log_buffer); puts(log_buffer); puts("Link Failed"); - return 0; + exit(1); } return program; diff --git a/main.cpp b/main.cpp index 1ea8243..910d916 100644 --- a/main.cpp +++ b/main.cpp @@ -229,10 +229,10 @@ void movement_function() { o->movement(1000 * (start_time - last_start)); last_start = start_time; - double end_time = glfwGetTime(); +// double end_time = glfwGetTime(); movement_count++; - double sleep_seconds = 0.001 - (end_time - start_time); - unsigned long sleep_microseconds = sleep_seconds * 1000000.0; +// double sleep_seconds = 0.001 - (end_time - start_time); +// unsigned long sleep_microseconds = sleep_seconds * 1000000.0; // Windows can sleep for a minimum of about 15ms by this method, it seems // this_thread::sleep_for(chrono::microseconds(sleep_microseconds)); -- 2.47.3