]> git.sesse.net Git - movit/commitdiff
Move slurping of files into its own function.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 15:36:22 +0000 (17:36 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 15:36:22 +0000 (17:36 +0200)
main.cpp
util.cpp
util.h

index 3b2bcad7961dc53c5bd13ed8a8703602c2b5bfd3..8c28c8f6cbde8716efc16fc25c3bdfda25387e20 100644 (file)
--- 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)
 {
 
 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);
 
        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);
 
        glShaderSource(obj, 1, source, length);
        glCompileShader(obj);
 
index 592e4d56b47f842a6bb6493e634992bb9cfe0ec5..b427625219964924db9a88a4f9336523abee7ac8 100644 (file)
--- 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;
 }
        *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 3abd8d124a6e11f6087f0e6ccb62765db238b39b..3957b863c1ec8a635ab720f88a263320d0c54919 100644 (file)
--- a/util.h
+++ b/util.h
@@ -4,9 +4,13 @@
 #include <stdio.h>
 #include <stdlib.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 
+#include <string>
+
 // assumes h in [0, 2pi> or [-pi, pi>
 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b);
 
 // 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
 #ifdef NDEBUG
 #define check_error()
 #else