Add command line options

This commit is contained in:
Matthias Schiffer 2016-02-06 11:39:37 +01:00
parent 1ec5502d46
commit ee22df9211
2 changed files with 102 additions and 16 deletions

View file

@ -46,6 +46,12 @@
#endif #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 bool paused = false;
static float speed = 1.0; static float speed = 1.0;
static unsigned previous_ticks = 0; static unsigned previous_ticks = 0;
@ -124,11 +130,13 @@ static void handle_input(const char *input) {
break; break;
case 'r': case 'r':
current_time = 0; current_time = init_time;
break; break;
case 'R': case 'R':
param0 = param1 = param2 = 0; param0 = init_param0;
param1 = init_param1;
param2 = init_param2;
print_params(); print_params();
break; break;
@ -142,9 +150,9 @@ static void handle_input(const char *input) {
case '=': case '=':
if (speed < 0) if (speed < 0)
set_speed(-1.0); set_speed(-init_speed);
else else
set_speed(1.0); set_speed(init_speed);
break; break;
case '<': case '<':
@ -163,12 +171,48 @@ static void handle_input(const char *input) {
} }
} }
int main(int argc, char *argv[]) { static void usage(void) {
if (argc != 2) { fprintf(stderr, "Usage: glslview [-t <time>] [-s <speed>] [-0 <param0>] [-1 <param1>] [-2 <param2>] <shader>\n");
fprintf(stderr, "Usage: glslview <shader>\n");
exit(1); exit(1);
} }
int main(int argc, char *argv[]) {
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_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); 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_Window *window = SDL_CreateWindow("glslview", 0, 0, 800, 800, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
SDL_GLContext ctx = SDL_GL_CreateContext(window); SDL_GLContext ctx = SDL_GL_CreateContext(window);
filename = argv[1]; filename = argv[optind];
init_watch(); init_watch();
init(); init();

View file

@ -27,6 +27,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <png.h> #include <png.h>
@ -36,12 +37,17 @@
#include <SDL2/SDL_opengl.h> #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) { static void savePNG(int width, int height, const char *output_dir) {
uint8_t pixels[width*height*4]; uint8_t pixels[width*height*4];
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, pixels); glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, pixels);
char filename[strlen(output_dir) + 20]; 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"); FILE *f = fopen(filename, "wb");
if (!f) { if (!f) {
@ -84,12 +90,47 @@ static void savePNG(int width, int height, const char *output_dir) {
fclose(f); fclose(f);
} }
int main(int argc, char *argv[]) { static void usage(void) {
if (argc != 3) { fprintf(stderr, "Usage: glslwrite [-s <step>] [-f <frames>] [-0 <param0>] [-1 <param1>] [-2 <param2>] <shader>\n");
fprintf(stderr, "Usage: glslwrite <shader> <output directory>\n");
exit(1); exit(1);
} }
int main(int argc, char *argv[]) {
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_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); 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_Window *window = SDL_CreateWindow("glslwrite", 0, 0, 800, 800, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
SDL_GLContext ctx = SDL_GL_CreateContext(window); SDL_GLContext ctx = SDL_GL_CreateContext(window);
filename = argv[1]; filename = argv[optind];
init(); init();
load(); load();
while (true) { while (!max_frame || frame < max_frame) {
SDL_Event e; SDL_Event e;
while (SDL_PollEvent(&e)) { while (SDL_PollEvent(&e)) {
@ -128,8 +169,9 @@ int main(int argc, char *argv[]) {
render(width, height); render(width, height);
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
savePNG(width, height, argv[2]); savePNG(width, height, argv[optind+1]);
current_time += 20; current_time += step;
frame++;
} }
quit: quit: