graphics

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 48091fcb094dd2bb396f4d6c8c70c8c73e90946d
parent 79f0365ab180e5e7ce61efa44e5b8d9d06da5fcd
Author: Brian Swetland <swetland@frotz.net>
Date:   Sun, 10 Feb 2013 22:32:45 -0800

test: reload shaders if modified

Diffstat:
Mcommon/io.cc | 10++++++++++
Mcommon/util.h | 1+
Mtest/test.cc | 25+++++++++++++++++++++++--
3 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/common/io.cc b/common/io.cc @@ -17,6 +17,7 @@ #include <stdlib.h> #include <stdarg.h> #include <string.h> +#include <sys/stat.h> #include "util.h" @@ -54,6 +55,15 @@ void init_io(void) { } #endif +int file_get_mtime(const char *fn) { + struct stat s; + char buf[1024]; + snprintf(buf, 1024, "%s%s", load_file_base_path, fn); + if (stat(buf, &s)) + return -1; + return s.st_mtime; +} + void die(const char *fmt, ...) { printx("ERROR: %s\n", fmt); exit(-1); diff --git a/common/util.h b/common/util.h @@ -20,6 +20,7 @@ void *load_png_rgba(const char *fn, unsigned *width, unsigned *height, int texture); void *load_png_gray(const char *fn, unsigned *width, unsigned *height, int texture); void *load_file(const char *fn, unsigned *sz); +int file_get_mtime(const char *fn); int save_png_rgba(const char *fn, void *data, unsigned width, unsigned height); int save_png_gray(const char *fn, void *data, unsigned width, unsigned height); diff --git a/test/test.cc b/test/test.cc @@ -48,8 +48,14 @@ private: mat4 proj; struct model *m; + + int ps_mtime; + int vs_mtime; }; +static const char *psfn = "TestPS." SL; +static const char *vsfn = "TestVS." SL; + TestApp::TestApp() : App(), r(0.0) { } @@ -57,11 +63,14 @@ void TestApp::release(void) { } int TestApp::init(void) { - if (loadShader(&ps, "TestPS."SL)) + if (loadShader(&ps, psfn)) return -1; - if (loadShader(&vs, "TestVS."SL, obj_layout, sizeof(obj_layout) / sizeof(obj_layout[0]))) + if (loadShader(&vs, vsfn, obj_layout, sizeof(obj_layout) / sizeof(obj_layout[0]))) return -1; + ps_mtime = file_get_mtime(psfn); + vs_mtime = file_get_mtime(vsfn); + if (!(m = load_wavefront_obj("unitcubeoid.obj"))) return error("cannot load model"); printx("Object Loaded. %d vertices, %d indices.\n", m->vcount, m->icount); @@ -88,6 +97,18 @@ int TestApp::init(void) { void TestApp::render(void) { unsigned stride, offset; + int t; + + t = file_get_mtime(psfn); + if (t != ps_mtime) { + loadShader(&ps, psfn); + ps_mtime = t; + } + t = file_get_mtime(vsfn); + if (t != vs_mtime) { + loadShader(&vs, vsfn, obj_layout, sizeof(obj_layout) / sizeof(obj_layout[0])); + vs_mtime = t; + } useConfig(&cfg); struct {