/* Copyright (c) 2016, Matthias Schiffer 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 #include #include #include #include #include #ifdef USE_INOTIFY # include # include # include #endif static float speed = 1.0; static unsigned previous_ticks = 0; static GLint param_k = 0; static GLint param_l = 0; static GLint param_m = 0; 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; 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 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"); } static void update(void) { unsigned ticks = SDL_GetTicks(); current_time += speed * (ticks - previous_ticks); previous_ticks = ticks; glUniform1i(param_k_loc, param_k); glUniform1i(param_l_loc, param_l); glUniform1i(param_m_loc, param_m); } 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': param_k = param_l = param_m = 0; break; case '+': speed *= 1.1; break; case '-': speed /= 1.1; break; case '=': if (speed < 0) speed = -1.0; else speed = 1.0; break; case '<': if (speed > 0) speed = -speed; break; case '>': if (speed < 0) speed = -speed; } } } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: glslview \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: param_k++; break; case SDLK_PAGEUP: param_k--; break; case SDLK_RIGHT: param_l++; break; case SDLK_LEFT: param_l--; break; case SDLK_DOWN: param_m++; break; case SDLK_UP: param_m--; break; case SDLK_ESCAPE: goto quit; } break; case SDL_TEXTINPUT: handle_input(e.text.text); break; case SDL_MOUSEWHEEL: 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; }