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 /src/main.zig | |
| 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
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 20 |
1 files changed, 10 insertions, 10 deletions
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); |
