summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--glslview.c60
-rw-r--r--glslwrite.c58
2 files changed, 102 insertions, 16 deletions
diff --git a/glslview.c b/glslview.c
index 4ff4dd3..b0b5d1b 100644
--- a/glslview.c
+++ b/glslview.c
@@ -46,6 +46,12 @@
#endif
+static int init_param0 = 0;
+static int init_param1 = 0;
+static int init_param2 = 0;
+static float init_time = 0;
+static float init_speed = 1.0;
+
static bool paused = false;
static float speed = 1.0;
static unsigned previous_ticks = 0;
@@ -124,11 +130,13 @@ static void handle_input(const char *input) {
break;
case 'r':
- current_time = 0;
+ current_time = init_time;
break;
case 'R':
- param0 = param1 = param2 = 0;
+ param0 = init_param0;
+ param1 = init_param1;
+ param2 = init_param2;
print_params();
break;
@@ -142,9 +150,9 @@ static void handle_input(const char *input) {
case '=':
if (speed < 0)
- set_speed(-1.0);
+ set_speed(-init_speed);
else
- set_speed(1.0);
+ set_speed(init_speed);
break;
case '<':
@@ -163,12 +171,48 @@ static void handle_input(const char *input) {
}
}
+static void usage(void) {
+ fprintf(stderr, "Usage: glslview [-t <time>] [-s <speed>] [-0 <param0>] [-1 <param1>] [-2 <param2>] <shader>\n");
+ exit(1);
+}
+
int main(int argc, char *argv[]) {
- if (argc != 2) {
- fprintf(stderr, "Usage: glslview <shader>\n");
- exit(1);
+ while (true) {
+ int c = getopt(argc, argv, "t:s:0:1:2:");
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 't':
+ current_time = init_time = atof(optarg);
+ break;
+
+ case 's':
+ speed = atof(optarg);
+ init_speed = fabsf(speed);
+ break;
+
+ case '0':
+ param0 = init_param0 = atoi(optarg);
+ break;
+
+ case '1':
+ param1 = init_param1 = atoi(optarg);
+ break;
+
+ case '2':
+ param2 = init_param2 = atoi(optarg);
+ break;
+
+ default:
+ fprintf(stderr, "Invalid option '%c'\n", optopt);
+ usage();
+ }
}
+ if (argc - optind != 1)
+ usage();
+
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
@@ -179,7 +223,7 @@ int main(int argc, char *argv[]) {
SDL_Window *window = SDL_CreateWindow("glslview", 0, 0, 800, 800, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
SDL_GLContext ctx = SDL_GL_CreateContext(window);
- filename = argv[1];
+ filename = argv[optind];
init_watch();
init();
diff --git a/glslwrite.c b/glslwrite.c
index 2913ae8..6673146 100644
--- a/glslwrite.c
+++ b/glslwrite.c
@@ -27,6 +27,7 @@
#include <stdbool.h>
#include <stdio.h>
+#include <unistd.h>
#include <png.h>
@@ -36,12 +37,17 @@
#include <SDL2/SDL_opengl.h>
+static float step = 20;
+static unsigned frame = 0;
+static unsigned max_frame = 0;
+
+
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);
+ snprintf(filename, sizeof(filename), "%s/%010u.png", output_dir, frame);
FILE *f = fopen(filename, "wb");
if (!f) {
@@ -84,12 +90,47 @@ static void savePNG(int width, int height, const char *output_dir) {
fclose(f);
}
+static void usage(void) {
+ fprintf(stderr, "Usage: glslwrite [-s <step>] [-f <frames>] [-0 <param0>] [-1 <param1>] [-2 <param2>] <shader>\n");
+ exit(1);
+}
+
int main(int argc, char *argv[]) {
- if (argc != 3) {
- fprintf(stderr, "Usage: glslwrite <shader> <output directory>\n");
- exit(1);
+ while (true) {
+ int c = getopt(argc, argv, "s:f:0:1:2:");
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 's':
+ step = atof(optarg);
+ break;
+
+ case 'f':
+ max_frame = atoi(optarg);
+ break;
+
+ case '0':
+ param0 = atoi(optarg);
+ break;
+
+ case '1':
+ param1 = atoi(optarg);
+ break;
+
+ case '2':
+ param2 = atoi(optarg);
+ break;
+
+ default:
+ fprintf(stderr, "Invalid option '%c'\n", optopt);
+ usage();
+ }
}
+ if (argc - optind != 2)
+ usage();
+
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
@@ -100,11 +141,11 @@ int main(int argc, char *argv[]) {
SDL_Window *window = SDL_CreateWindow("glslwrite", 0, 0, 800, 800, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
SDL_GLContext ctx = SDL_GL_CreateContext(window);
- filename = argv[1];
+ filename = argv[optind];
init();
load();
- while (true) {
+ while (!max_frame || frame < max_frame) {
SDL_Event e;
while (SDL_PollEvent(&e)) {
@@ -128,8 +169,9 @@ int main(int argc, char *argv[]) {
render(width, height);
SDL_GL_SwapWindow(window);
- savePNG(width, height, argv[2]);
- current_time += 20;
+ savePNG(width, height, argv[optind+1]);
+ current_time += step;
+ frame++;
}
quit: