summaryrefslogtreecommitdiffstats
path: root/glslview.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2016-02-03 12:53:39 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2016-02-03 12:53:39 +0100
commit22e600bde83f04fd4f140bcf2c7f7b90d5920e55 (patch)
treef3dcadce077b879fc47aab3115ea1e6936979b29 /glslview.c
parent17d48c425cd6b16a015f7f88988bdbd1ec8a3233 (diff)
downloadglslview-22e600bde83f04fd4f140bcf2c7f7b90d5920e55.tar
glslview-22e600bde83f04fd4f140bcf2c7f7b90d5920e55.zip
Restructure glslview/glslwrite code
Diffstat (limited to 'glslview.c')
-rw-r--r--glslview.c220
1 files changed, 19 insertions, 201 deletions
diff --git a/glslview.c b/glslview.c
index 4061282..c9a9839 100644
--- a/glslview.c
+++ b/glslview.c
@@ -23,12 +23,13 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "common.h"
+
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
-#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
@@ -38,17 +39,6 @@
# include <sys/types.h>
#endif
-#ifdef GLSLWRITE
-# include <png.h>
-#endif
-
-#include <GL/glew.h>
-
-#include <SDL2/SDL.h>
-#include <SDL2/SDL_opengl.h>
-
-
-#ifndef GLSLWRITE
static float speed = 1.0;
static unsigned previous_ticks = 0;
@@ -61,108 +51,13 @@ static GLint param_k_loc;
static GLint param_l_loc;
static GLint param_m_loc;
+
#ifdef USE_INOTIFY
+
static int inotify_fd = -1;
static int inotify_watch = -1;
-#endif
-
-#endif
-
-
-static char *filename;
-static float current_time = 0;
-static GLint time_loc;
-static GLint res_loc;
-
-
-static char * readfile(const char *name) {
- FILE *f = fopen(name, "r");
- if (!f)
- return NULL;
-
- size_t size = 1024;
- char *buffer = malloc(size+1);
- size_t count = 0, r;
-
- do {
- if (count == size) {
- size *= 2;
- buffer = realloc(buffer, size+1);
- }
-
- r = fread(buffer+count, 1, size-count, f);
- count += r;
- } while (r);
-
- fclose(f);
-
- buffer[count] = 0;
-
- return buffer;
-}
-
-static void printShaderInfoLog(GLuint obj) {
- GLint length = 0;
-
- glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &length);
-
- if(length > 0) {
- char log[length];
-
- glGetShaderInfoLog(obj, sizeof(log), NULL, log);
- fprintf(stderr, "%s\n", log);
- }
-}
-
-static void printProgramInfoLog(GLuint obj) {
- GLint length = 0;
-
- glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &length);
-
- if(length > 0) {
- char log[length];
-
- glGetProgramInfoLog(obj, sizeof(log), NULL, log);
- fprintf(stderr, "%s\n", log);
- }
-}
-
-static void load(void) {
- char *shader = readfile(filename);
- if (!shader) {
- fprintf(stderr, "Error: unable to read '%s'\n", filename);
- return;
- }
-
- GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(frag, 1, (const GLchar **)&shader, NULL);
- glCompileShader(frag);
- printShaderInfoLog(frag);
-
- GLuint program = glCreateProgram();
- glAttachShader(program, frag);
-
- glLinkProgram(program);
- printProgramInfoLog(program);
-
- glBindFragDataLocation(program, 0, "fragColor");
- time_loc = glGetUniformLocation(program, "time");
- res_loc = glGetUniformLocation(program, "res");
-
-#ifndef GLSLWRITE
- param_k_loc = glGetUniformLocation(program, "param_k");
- param_l_loc = glGetUniformLocation(program, "param_l");
- param_m_loc = glGetUniformLocation(program, "param_m");
-#endif
- glUseProgram(program);
- free(shader);
- glDeleteShader(frag);
- glDeleteProgram(program);
-}
-
-#if defined(USE_INOTIFY) && !defined(GLSLWRITE)
static void reset_watch(void) {
if (inotify_watch >= 0) {
if (inotify_rm_watch(inotify_fd, inotify_watch)) {
@@ -177,10 +72,8 @@ static void reset_watch(void) {
return;
}
}
-#endif
-static void init(void) {
-#if defined(USE_INOTIFY) && !defined(GLSLWRITE)
+static void init_watch(void) {
inotify_fd = inotify_init();
if (inotify_fd < 0) {
fprintf(stderr, "unable to initialize inotify\n");
@@ -190,77 +83,30 @@ static void init(void) {
fcntl(inotify_fd, F_SETFL, fcntl(inotify_fd, F_GETFL)|O_NONBLOCK);
reset_watch();
-#endif
-
- glewInit();
- glDisable(GL_DEPTH_TEST);
- glColor4f(0, 0, 0, 0);
-
- load();
}
static void check_reload(void) {
-#if defined(USE_INOTIFY) && !defined(GLSLWRITE)
char buf[sizeof(struct inotify_event) + NAME_MAX + 1];
if (read(inotify_fd, buf, sizeof(buf)) > 0)
load();
-#endif
}
-#ifdef GLSLWRITE
-static void savePNG(int width, int height, const char *output_dir) {
- uint8_t pixels[width*height*4];
- glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, pixels);
-
- char filename[strlen(output_dir) + 20];
- snprintf(filename, sizeof(filename), "%s/%010u.png", output_dir, (unsigned)current_time);
-
- FILE *f = fopen(filename, "wb");
- if (!f) {
- fprintf(stderr, "unable to open PNG file\n");
- exit(1);
- }
-
- png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (!png_ptr) {
- fprintf(stderr, "unable to open PNG file\n");
- exit(1);
- }
-
- png_set_swap_alpha(png_ptr);
-
- png_infop info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr) {
- fprintf(stderr, "unable to create PNG info struct\n");
- exit(1);
- }
-
- if (setjmp(png_jmpbuf(png_ptr))) {
- fprintf(stderr, "unable to write PNG file\n");
- exit(1);
- }
-
- png_init_io(png_ptr, f);
+#else
- png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
- PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+static void reset_watch(void) {}
+static void init_watch(void) {}
+static void check_reload(void) {}
- uint8_t *row_pointers[height];
- for (size_t i = 0; i < height; i++)
- row_pointers[i] = &pixels[4*(height-1-i)*width];
+#endif
- png_set_rows(png_ptr, info_ptr, row_pointers);
- png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
- png_destroy_write_struct(&png_ptr, &info_ptr);
- fclose(f);
+void extra_load(GLuint program) {
+ param_k_loc = glGetUniformLocation(program, "param_k");
+ param_l_loc = glGetUniformLocation(program, "param_l");
+ param_m_loc = glGetUniformLocation(program, "param_m");
}
-#endif
-
-static void render(int width, int height) {
- glViewport(0, 0, (GLsizei)width, (GLsizei)height);
-#ifndef GLSLWRITE
+static void update(void) {
unsigned ticks = SDL_GetTicks();
current_time += speed * (ticks - previous_ticks);
previous_ticks = ticks;
@@ -268,27 +114,13 @@ static void render(int width, int height) {
glUniform1i(param_k_loc, param_k);
glUniform1i(param_l_loc, param_l);
glUniform1i(param_m_loc, param_m);
-#endif
-
- glUniform1f(time_loc, current_time);
- glUniform2f(res_loc, width, height);
-
- glBegin(GL_QUADS);
- glVertex2f(-1, -1);
- glVertex2f(1, -1);
- glVertex2f(1, 1);
- glVertex2f(-1, 1);
- glEnd();
}
-#ifndef GLSLWRITE
static void handle_input(const char *input) {
for (; *input; input++) {
switch (*input) {
case 'l':
-#ifdef USE_INOTIFY
reset_watch();
-#endif
load();
break;
@@ -326,20 +158,12 @@ static void handle_input(const char *input) {
}
}
}
-#endif
int main(int argc, char *argv[]) {
-#ifdef GLSLWRITE
- if (argc != 3) {
- fprintf(stderr, "Usage: glslwrite <shader> <output directory>\n");
- exit(1);
- }
-#else
if (argc != 2) {
fprintf(stderr, "Usage: glslview <shader>\n");
exit(1);
}
-#endif
SDL_Init(SDL_INIT_VIDEO);
@@ -352,7 +176,10 @@ int main(int argc, char *argv[]) {
SDL_GLContext ctx = SDL_GL_CreateContext(window);
filename = argv[1];
+
+ init_watch();
init();
+ load();
SDL_StartTextInput();
@@ -363,7 +190,6 @@ int main(int argc, char *argv[]) {
switch (e.type) {
case SDL_KEYDOWN:
switch (e.key.keysym.sym) {
-#ifndef GLSLWRITE
case SDLK_PAGEDOWN:
param_k++;
break;
@@ -388,14 +214,11 @@ int main(int argc, char *argv[]) {
param_m--;
break;
-#endif
-
case SDLK_ESCAPE:
goto quit;
}
break;
-#ifndef GLSLWRITE
case SDL_TEXTINPUT:
handle_input(e.text.text);
break;
@@ -403,7 +226,6 @@ int main(int argc, char *argv[]) {
case SDL_MOUSEWHEEL:
speed /= pow(1.1, e.wheel.y);
break;
-#endif
case SDL_QUIT:
goto quit;
@@ -415,13 +237,9 @@ int main(int argc, char *argv[]) {
int width, height;
SDL_GetWindowSize(window, &width, &height);
+ update();
render(width, height);
SDL_GL_SwapWindow(window);
-
-#ifdef GLSLWRITE
- savePNG(width, height, argv[2]);
- current_time += 20;
-#endif
}
quit: