From 928743085b7f0f88b3e1b0e59e2d7ebec7816bd2 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 1 Oct 2012 17:36:22 +0200 Subject: [PATCH] Move slurping of files into its own function. --- main.cpp | 14 +++----------- util.cpp | 15 +++++++++++++++ util.h | 4 ++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index 3b2bcad..8c28c8f 100644 --- a/main.cpp +++ b/main.cpp @@ -40,19 +40,11 @@ float gain_r = 1.0f, gain_g = 1.0f, gain_b = 1.0f; GLhandleARB read_shader(const char* filename, GLenum type) { - static char buf[131072]; - FILE *fp = fopen(filename, "r"); - if (fp == NULL) { - perror(filename); - exit(1); - } - - int len = fread(buf, 1, sizeof(buf), fp); - fclose(fp); + std::string shader_src = read_file(filename); GLhandleARB obj = glCreateShaderObjectARB(type); - const GLchar* source[] = { buf }; - const GLint length[] = { len }; + const GLchar* source[] = { shader_src.data() }; + const GLint length[] = { shader_src.size() }; glShaderSource(obj, 1, source, length); glCompileShader(obj); diff --git a/util.cpp b/util.cpp index 592e4d5..b427625 100644 --- a/util.cpp +++ b/util.cpp @@ -42,3 +42,18 @@ void hsv2rgb(float h, float s, float v, float *r, float *g, float *b) *g += m; *b += m; } + +std::string read_file(const std::string &filename) +{ + static char buf[131072]; + FILE *fp = fopen(filename.c_str(), "r"); + if (fp == NULL) { + perror(filename.c_str()); + exit(1); + } + + int len = fread(buf, 1, sizeof(buf), fp); + fclose(fp); + + return std::string(buf, len); +} diff --git a/util.h b/util.h index 3abd8d1..3957b86 100644 --- a/util.h +++ b/util.h @@ -4,9 +4,13 @@ #include #include +#include + // assumes h in [0, 2pi> or [-pi, pi> void hsv2rgb(float h, float s, float v, float *r, float *g, float *b); +std::string read_file(const std::string &filename); + #ifdef NDEBUG #define check_error() #else -- 2.39.2