diff options
| author | s-ol <s+removethis@s-ol.nu> | 2023-10-29 22:45:14 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2023-10-29 22:45:14 +0000 |
| commit | 814dfea28471daa3f29f7a66f2d85f611e12bc78 (patch) | |
| tree | 06ec6242614effa3158746234c19bced175e3950 | |
| parent | update for zig 0.11.0 (diff) | |
| download | glsl-view-814dfea28471daa3f29f7a66f2d85f611e12bc78.tar.gz glsl-view-814dfea28471daa3f29f7a66f2d85f611e12bc78.zip | |
load shaders relative to config file / project_root
| -rw-r--r-- | src/config.zig | 5 | ||||
| -rw-r--r-- | src/main.zig | 20 |
2 files changed, 15 insertions, 10 deletions
diff --git a/src/config.zig b/src/config.zig index 166fdf4..27d0b44 100644 --- a/src/config.zig +++ b/src/config.zig @@ -50,6 +50,7 @@ pub const Config = struct { outputs: []const OutputConfig, fragment: []const u8, parameters: []const ParameterConfig, + project_root: fs.Dir, osc: OSCConfig, pub fn parse(allocator: *const std.mem.Allocator, filename: []const u8) !Config { @@ -62,12 +63,14 @@ pub const Config = struct { const len: usize = try file.read(buffer[0..]); c.yaml_parser_set_input_string(&parser, buffer[0..], len); + const dirname = fs.path.dirname(filename) orelse ""; var config: Config = .{ .width = 1920, .height = 1080, .outputs = ([_]OutputConfig{defaultOutput})[0..], .fragment = "", .parameters = ([0]ParameterConfig{})[0..], + .project_root = try fs.cwd().openDir(dirname, .{}), .osc = .{ .URL = "osc.udp://:9000" }, }; @@ -97,6 +100,8 @@ pub const Config = struct { config.outputs = try parseOutputs(&parser, allocator); } else if (std.mem.eql(u8, key, "parameters")) { config.parameters = try parseParameters(&parser, allocator); + } else if (std.mem.eql(u8, key, "project_root")) { + config.project_root = try fs.cwd().openDir(try parseString(&parser, allocator), .{}); } else if (std.mem.eql(u8, key, "osc")) { config.osc = try parseOSC(&parser, allocator); } else { diff --git a/src/main.zig b/src/main.zig index 328e73a..1c76c61 100644 --- a/src/main.zig +++ b/src/main.zig @@ -103,9 +103,9 @@ pub fn main() !void { var fbo = try gl.FramebufferObject.create(config.width, config.height); defer fbo.destroy(); - const shader_file = try fs.cwd().openFile(config.fragment, .{}); + const shader_file = try config.project_root.openFile(config.fragment, .{}); var last_stat = try shader_file.stat(); - try reloadShader(&main_program, config.fragment, last_stat.size); + try reloadShader(&config, &main_program, config.fragment, last_stat.size); var cache = gl.UniformCache.init(std.heap.c_allocator, &main_program); defer cache.deinit(); @@ -122,7 +122,7 @@ pub fn main() !void { const stat = try shader_file.stat(); if (stat.mtime > last_stat.mtime) { - try reloadShader(&main_program, config.fragment, stat.size); + try reloadShader(&config, &main_program, config.fragment, stat.size); try cache.refresh(); last_stat = stat; } @@ -153,17 +153,17 @@ pub fn main() !void { } // given a file and the directory it is in, resolve references -fn loadFile(writer: anytype, dir: []const u8, filename: []const u8) !void { +fn loadFile(config: *const cfg.Config, writer: anytype, dir: []const u8, filename: []const u8) !void { const file_dir = try fs.path.resolve(c_allocator, &[_][]const u8{ dir, - std.fs.path.dirname(filename) orelse "", + fs.path.dirname(filename) orelse "", }); defer c_allocator.free(file_dir); - const file_path = try std.fs.path.resolve(c_allocator, &[_][]const u8{ dir, filename }); + const file_path = try fs.path.resolve(c_allocator, &[_][]const u8{ dir, filename }); defer c_allocator.free(file_path); - var file = try fs.cwd().openFile(file_path, .{}); + var file = try config.project_root.openFile(file_path, .{}); defer file.close(); var reader = file.reader(); @@ -186,7 +186,7 @@ fn loadFile(writer: anytype, dir: []const u8, filename: []const u8) !void { } include_path = include_path[1 .. include_path.len - 1]; - loadFile(writer, file_dir, include_path) catch unreachable; + loadFile(config, writer, file_dir, include_path) catch unreachable; continue; } else { @@ -200,11 +200,11 @@ fn loadFile(writer: anytype, dir: []const u8, filename: []const u8) !void { } } -fn reloadShader(current: *gl.ShaderProgram, frag_filename: []const u8, size: u64) !void { +fn reloadShader(config: *const cfg.Config, current: *gl.ShaderProgram, frag_filename: []const u8, size: u64) !void { var buffer = try std.ArrayList(u8).initCapacity(c_allocator, @as(usize, @intCast(size))); errdefer buffer.deinit(); - try loadFile(buffer.writer(), "", frag_filename); + try loadFile(config, buffer.writer(), "", frag_filename); const frag_source = try buffer.toOwnedSlice(); defer c_allocator.free(frag_source); |
