Add support for changing the speed

This commit is contained in:
Matthias Schiffer 2016-02-02 09:00:05 +01:00
parent 709e22459b
commit f81f0074c0
2 changed files with 59 additions and 17 deletions

View file

@ -3,9 +3,10 @@ all : glslview glslwrite
USE_INOTIFY = -DUSE_INOTIFY
CFLAGS = -O3 -Wall $(USE_INOTIFY)
LIBS = -lSDL2 -lGL -lGLEW -lm
glslview : glslview.c
$(CC) $(CFLAGS) -o $@ $^ -lSDL2 -lGL -lGLEW
$(CC) $(CFLAGS) -o $@ $^ $(LIBS)
glslwrite : glslview.c
$(CC) $(CFLAGS) -o $@ $^ -DGLSLWRITE -lSDL2 -lpng -lGL -lGLEW
$(CC) $(CFLAGS) -DGLSLWRITE -o $@ $^ $(LIBS) -lpng

View file

@ -1,5 +1,6 @@
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@ -27,7 +28,9 @@ static unsigned frame_time = 0;
#else
static unsigned time_orig = 0;
static float speed = 1.0;
static float current_time = 0;
static unsigned previous_ticks = 0;
#ifdef USE_INOTIFY
static int inotify_fd = -1;
@ -222,10 +225,19 @@ static void render(int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
#ifdef GLSLWRITE
glUniform1f(time_loc, frame_time);
#else
glUniform1f(time_loc, SDL_GetTicks() - time_orig);
unsigned ticks = SDL_GetTicks();
current_time += speed * (ticks - previous_ticks);
previous_ticks = ticks;
glUniform1f(time_loc, current_time);
#endif
glUniform2f(res_loc, width, height);
glBegin(GL_QUADS);
@ -240,6 +252,35 @@ static void render(int width, int height) {
#endif
}
#ifndef GLSLWRITE
static void handle_input(const char *input) {
for (; *input; input++) {
switch (*input) {
case 'l':
#ifdef USE_INOTIFY
reset_watch();
#endif
load();
break;
case 'r':
current_time = 0;
break;
case '+':
speed *= 1.1;
break;
case '-':
speed /= 1.1;
break;
case '=':
speed = 1.0;
}
}
}
#endif
int main(int argc, char *argv[]) {
#ifdef GLSLWRITE
@ -267,6 +308,8 @@ int main(int argc, char *argv[]) {
filename = argv[1];
init();
SDL_StartTextInput();
while (true) {
SDL_Event e;
@ -276,22 +319,19 @@ int main(int argc, char *argv[]) {
switch (e.key.keysym.sym) {
case SDLK_ESCAPE:
goto quit;
#ifndef GLSLWRITE
case SDLK_l:
#ifdef USE_INOTIFY
reset_watch();
#endif
load();
break;
case SDLK_r:
time_orig = SDL_GetTicks();
break;
#endif
}
break;
#ifndef GLSLWRITE
case SDL_TEXTINPUT:
handle_input(e.text.text);
break;
case SDL_MOUSEWHEEL:
speed /= pow(1.1, e.wheel.y);
break;
#endif
case SDL_QUIT:
goto quit;
}
@ -311,6 +351,7 @@ int main(int argc, char *argv[]) {
}
quit:
SDL_StopTextInput();
SDL_GL_DeleteContext(ctx);
SDL_DestroyWindow(window);
SDL_Quit();