250 lines
5.1 KiB
C
250 lines
5.1 KiB
C
/*
|
|
Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
|
|
All rights reserved.
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright notice,
|
|
this list of conditions and the following disclaimer.
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
this list of conditions and the following disclaimer in the documentation
|
|
and/or other materials provided with the distribution.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
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 <stdio.h>
|
|
#include <unistd.h>
|
|
|
|
#ifdef USE_INOTIFY
|
|
# include <sys/inotify.h>
|
|
# include <sys/stat.h>
|
|
# include <sys/types.h>
|
|
#endif
|
|
|
|
|
|
static float speed = 1.0;
|
|
static unsigned previous_ticks = 0;
|
|
|
|
|
|
#ifdef USE_INOTIFY
|
|
|
|
static int inotify_fd = -1;
|
|
static int inotify_watch = -1;
|
|
|
|
|
|
static void reset_watch(void) {
|
|
if (inotify_watch >= 0) {
|
|
if (inotify_rm_watch(inotify_fd, inotify_watch)) {
|
|
fprintf(stderr, "unable to remove watch\n");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
inotify_watch = inotify_add_watch(inotify_fd, filename, IN_CLOSE_WRITE);
|
|
if (inotify_watch < 0) {
|
|
fprintf(stderr, "unable to watch '%s' for changes\n", filename);
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void init_watch(void) {
|
|
inotify_fd = inotify_init();
|
|
if (inotify_fd < 0) {
|
|
fprintf(stderr, "unable to initialize inotify\n");
|
|
exit(1);
|
|
}
|
|
|
|
fcntl(inotify_fd, F_SETFL, fcntl(inotify_fd, F_GETFL)|O_NONBLOCK);
|
|
|
|
reset_watch();
|
|
}
|
|
|
|
static void check_reload(void) {
|
|
char buf[sizeof(struct inotify_event) + NAME_MAX + 1];
|
|
if (read(inotify_fd, buf, sizeof(buf)) > 0)
|
|
load();
|
|
}
|
|
|
|
#else
|
|
|
|
static void reset_watch(void) {}
|
|
static void init_watch(void) {}
|
|
static void check_reload(void) {}
|
|
|
|
#endif
|
|
|
|
|
|
static void update(void) {
|
|
unsigned ticks = SDL_GetTicks();
|
|
current_time += speed * (ticks - previous_ticks);
|
|
previous_ticks = ticks;
|
|
}
|
|
|
|
static void set_speed(float v) {
|
|
speed = v;
|
|
printf("speed: %f\n", v);
|
|
}
|
|
|
|
static void print_params(void) {
|
|
printf("params: %i %i %i\n", (int)param0, (int)param1, (int)param2);
|
|
}
|
|
|
|
static void handle_input(const char *input) {
|
|
for (; *input; input++) {
|
|
switch (*input) {
|
|
case 'l':
|
|
reset_watch();
|
|
load();
|
|
break;
|
|
|
|
case 'r':
|
|
current_time = 0;
|
|
break;
|
|
|
|
case 'R':
|
|
param0 = param1 = param2 = 0;
|
|
print_params();
|
|
break;
|
|
|
|
case '+':
|
|
set_speed(speed * 1.1);
|
|
break;
|
|
|
|
case '-':
|
|
set_speed(speed / 1.1);
|
|
break;
|
|
|
|
case '=':
|
|
if (speed < 0)
|
|
set_speed(-1.0);
|
|
else
|
|
set_speed(1.0);
|
|
break;
|
|
|
|
case '<':
|
|
if (speed > 0)
|
|
set_speed(-speed);
|
|
break;
|
|
|
|
case '>':
|
|
if (speed < 0)
|
|
set_speed(-speed);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc != 2) {
|
|
fprintf(stderr, "Usage: glslview <shader>\n");
|
|
exit(1);
|
|
}
|
|
|
|
SDL_Init(SDL_INIT_VIDEO);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
|
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
|
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
|
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
|
|
|
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];
|
|
|
|
init_watch();
|
|
init();
|
|
load();
|
|
|
|
SDL_StartTextInput();
|
|
|
|
while (true) {
|
|
SDL_Event e;
|
|
|
|
while (SDL_PollEvent(&e)) {
|
|
switch (e.type) {
|
|
case SDL_KEYDOWN:
|
|
switch (e.key.keysym.sym) {
|
|
case SDLK_PAGEDOWN:
|
|
param0++;
|
|
print_params();
|
|
break;
|
|
|
|
case SDLK_PAGEUP:
|
|
param0--;
|
|
print_params();
|
|
break;
|
|
|
|
case SDLK_RIGHT:
|
|
param1++;
|
|
print_params();
|
|
break;
|
|
|
|
case SDLK_LEFT:
|
|
param1--;
|
|
print_params();
|
|
break;
|
|
|
|
case SDLK_DOWN:
|
|
param2++;
|
|
print_params();
|
|
break;
|
|
|
|
case SDLK_UP:
|
|
param2--;
|
|
print_params();
|
|
break;
|
|
|
|
case SDLK_ESCAPE:
|
|
goto quit;
|
|
}
|
|
break;
|
|
|
|
case SDL_TEXTINPUT:
|
|
handle_input(e.text.text);
|
|
break;
|
|
|
|
case SDL_MOUSEWHEEL:
|
|
set_speed(speed / pow(1.1, e.wheel.y));
|
|
break;
|
|
|
|
case SDL_QUIT:
|
|
goto quit;
|
|
}
|
|
}
|
|
|
|
check_reload();
|
|
|
|
int width, height;
|
|
SDL_GetWindowSize(window, &width, &height);
|
|
|
|
update();
|
|
render(width, height);
|
|
SDL_GL_SwapWindow(window);
|
|
}
|
|
|
|
quit:
|
|
SDL_StopTextInput();
|
|
SDL_GL_DeleteContext(ctx);
|
|
SDL_DestroyWindow(window);
|
|
SDL_Quit();
|
|
|
|
return 0;
|
|
}
|