zig-fmt for low-column-screens
s-ol
2 years ago
391 | 391 | break; |
392 | 392 | }, |
393 | 393 | |
394 | c.yaml_event_type_t.YAML_SEQUENCE_START_EVENT, c.yaml_event_type_t.YAML_MAPPING_START_EVENT => depth += 1, | |
395 | ||
396 | c.yaml_event_type_t.YAML_SEQUENCE_END_EVENT, c.yaml_event_type_t.YAML_MAPPING_END_EVENT => { | |
394 | c.yaml_event_type_t.YAML_SEQUENCE_START_EVENT, | |
395 | c.yaml_event_type_t.YAML_MAPPING_START_EVENT, | |
396 | => depth += 1, | |
397 | ||
398 | c.yaml_event_type_t.YAML_SEQUENCE_END_EVENT, | |
399 | c.yaml_event_type_t.YAML_MAPPING_END_EVENT, | |
400 | => { | |
397 | 401 | depth -= 1; |
398 | 402 | |
399 | 403 | if (depth == 0) |
11 | 11 | } |
12 | 12 | } |
13 | 13 | |
14 | fn set_array(comptime T: type, dest: []T, argc: c_int, argv: [*c][*c]c.lo_arg, types: []const u8) !void { | |
14 | fn set_array( | |
15 | comptime T: type, | |
16 | dest: []T, | |
17 | argc: c_int, | |
18 | argv: [*c][*c]c.lo_arg, | |
19 | types: []const u8, | |
20 | ) !void { | |
15 | 21 | if (argc != dest.len) |
16 | 22 | return error.sizeMismatch; |
17 | 23 |
5 | 5 | |
6 | 6 | pub const is_on = if (builtin.mode == builtin.Mode.ReleaseFast) c.GL_FALSE else c.GL_TRUE; |
7 | 7 | |
8 | fn glDebugMessage(source: c.GLenum, typ: c.GLenum, id: c.GLuint, severity: c.GLenum, length: c.GLsizei, _message: [*c]const u8, user: ?*const c_void) callconv(.C) void { | |
8 | fn glDebugMessage( | |
9 | source: c.GLenum, | |
10 | typ: c.GLenum, | |
11 | id: c.GLuint, | |
12 | severity: c.GLenum, | |
13 | length: c.GLsizei, | |
14 | _message: [*c]const u8, | |
15 | user: ?*const c_void, | |
16 | ) callconv(.C) void { | |
9 | 17 | switch (typ) { |
10 | c.GL_DEBUG_TYPE_ERROR, c.GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR, c.GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR => {}, | |
18 | c.GL_DEBUG_TYPE_ERROR, | |
19 | c.GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR, | |
20 | c.GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR, | |
21 | => {}, | |
11 | 22 | else => return, |
12 | 23 | } |
13 | 24 |
0 | 0 | const std = @import("std"); |
1 | 1 | const c_allocator = @import("std").heap.c_allocator; |
2 | const panic = @import("std").debug.panic; | |
3 | 2 | const c = @import("c.zig"); |
4 | 3 | const cfg = @import("config.zig"); |
5 | 4 | |
297 | 296 | c.glUseProgram(self.program_id); |
298 | 297 | } |
299 | 298 | |
300 | pub fn attribLocation(self: ShaderProgram, name: [*]const u8) c.GLint { | |
301 | const id = c.glGetAttribLocation(self.program_id, name); | |
302 | if (id == -1) { | |
303 | panic("invalid attrib: {}\n", .{name}); | |
304 | } | |
305 | return id; | |
306 | } | |
307 | ||
308 | 299 | pub fn uniformLocation(self: ShaderProgram, name: [*]const u8) !c.GLint { |
309 | 300 | const id = c.glGetUniformLocation(self.program_id, name); |
310 | 301 | if (id == -1) |
398 | 389 | } |
399 | 390 | } |
400 | 391 | |
401 | c.glGetUniformIndices(self.shader.program_id, @intCast(c.GLsizei, count), existing_names_c.ptr, existing_indices.ptr); | |
392 | c.glGetUniformIndices( | |
393 | self.shader.program_id, | |
394 | @intCast(c.GLsizei, count), | |
395 | existing_names_c.ptr, | |
396 | existing_indices.ptr, | |
397 | ); | |
402 | 398 | |
403 | 399 | for (existing_indices) |index, i| { |
404 | 400 | const name = existing_names[i]; |
449 | 445 | |
450 | 446 | const message = try c_allocator.alloc(u8, @intCast(usize, error_size)); |
451 | 447 | c.glGetShaderInfoLog(shader_id, error_size, &error_size, message.ptr); |
452 | std.debug.warn("Error compiling {s} shader:\n{s}\n", .{ name, @ptrCast([*:0]const u8, message.ptr) }); | |
448 | std.debug.warn( | |
449 | "Error compiling {s} shader:\n{s}\n", | |
450 | .{ name, @ptrCast([*:0]const u8, message.ptr) }, | |
451 | ); | |
453 | 452 | return error.CompileError; |
454 | 453 | } |
455 | 454 | |
472 | 471 | |
473 | 472 | c.glGenTextures(1, &self.texture_id); |
474 | 473 | c.glBindTexture(c.GL_TEXTURE_2D, self.texture_id); |
475 | c.glTexImage2D(c.GL_TEXTURE_2D, 0, c.GL_RGBA, width, height, 0, c.GL_RGBA, c.GL_UNSIGNED_BYTE, null); | |
474 | c.glTexImage2D( | |
475 | c.GL_TEXTURE_2D, | |
476 | 0, | |
477 | c.GL_RGBA, | |
478 | width, | |
479 | height, | |
480 | 0, | |
481 | c.GL_RGBA, | |
482 | c.GL_UNSIGNED_BYTE, | |
483 | null, | |
484 | ); | |
476 | 485 | c.glTexParameteri(c.GL_TEXTURE_2D, c.GL_TEXTURE_MIN_FILTER, c.GL_LINEAR); |
477 | 486 | c.glTexParameteri(c.GL_TEXTURE_2D, c.GL_TEXTURE_MAG_FILTER, c.GL_LINEAR); |
478 | 487 | c.glBindTexture(c.GL_TEXTURE_2D, 0); |
479 | 488 | |
480 | c.glFramebufferTexture2D(c.GL_FRAMEBUFFER, c.GL_COLOR_ATTACHMENT0, c.GL_TEXTURE_2D, self.texture_id, 0); | |
489 | c.glFramebufferTexture2D( | |
490 | c.GL_FRAMEBUFFER, | |
491 | c.GL_COLOR_ATTACHMENT0, | |
492 | c.GL_TEXTURE_2D, | |
493 | self.texture_id, | |
494 | 0, | |
495 | ); | |
481 | 496 | |
482 | 497 | if (c.glCheckFramebufferStatus(c.GL_FRAMEBUFFER) != c.GL_FRAMEBUFFER_COMPLETE) |
483 | 498 | return error.FBONotComplete; |
30 | 30 | var monitor_count: c_int = 0; |
31 | 31 | const monitors = c.glfwGetMonitors(&monitor_count); |
32 | 32 | for (monitors[0..@intCast(usize, monitor_count)]) |monitor, i| { |
33 | std.debug.warn("monitor {}: '{s}'\n", .{ i, @ptrCast([*:0]const u8, c.glfwGetMonitorName(monitor)) }); | |
33 | std.debug.warn( | |
34 | "monitor {}: '{s}'\n", | |
35 | .{ i, @ptrCast([*:0]const u8, c.glfwGetMonitorName(monitor)) }, | |
36 | ); | |
34 | 37 | } |
35 | 38 | |
36 | 39 | c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MAJOR, 4); |
13 | 13 | }; |
14 | 14 | } |
15 | 15 | |
16 | pub fn create(allocator: *std.mem.Allocator, config: cfg.OutputConfig, constants: *gl.Constants) *Output { | |
16 | pub fn create( | |
17 | allocator: *std.mem.Allocator, | |
18 | config: cfg.OutputConfig, | |
19 | constants: *gl.Constants, | |
20 | ) *Output { | |
17 | 21 | return switch (config.type) { |
18 | 22 | .window => WindowOutput.create(allocator, config, constants), |
19 | 23 | else => unreachable, |
28 | 32 | |
29 | 33 | resized: bool = false, |
30 | 34 | |
31 | pub fn create(allocator: *std.mem.Allocator, config: cfg.OutputConfig, constants: *gl.Constants) *Output { | |
35 | pub fn create( | |
36 | allocator: *std.mem.Allocator, | |
37 | config: cfg.OutputConfig, | |
38 | constants: *gl.Constants, | |
39 | ) *Output { | |
32 | 40 | const self = allocator.create(WindowOutput) catch unreachable; |
33 | 41 | |
34 | 42 | c.glfwDefaultWindowHints(); |
36 | 44 | self.* = WindowOutput{ |
37 | 45 | .constants = constants, |
38 | 46 | .output = Output.init(update, destroy), |
39 | .window = c.glfwCreateWindow(config.width, config.height, "glsl-view output", null, constants.main_window) orelse { | |
47 | .window = c.glfwCreateWindow( | |
48 | config.width, | |
49 | config.height, | |
50 | "glsl-view output", | |
51 | null, | |
52 | constants.main_window, | |
53 | ) orelse { | |
40 | 54 | std.debug.panic("unable to create output window\n", .{}); |
41 | 55 | }, |
42 | 56 | }; |
76 | 90 | scaled_height = @floatToInt(c_int, @intToFloat(f32, width) / self.constants.aspect); |
77 | 91 | } |
78 | 92 | |
79 | c.glViewport(@divFloor(width - scaled_width, 2), @divFloor(height - scaled_height, 2), scaled_width, scaled_height); | |
93 | c.glViewport( | |
94 | @divFloor(width - scaled_width, 2), | |
95 | @divFloor(height - scaled_height, 2), | |
96 | scaled_width, | |
97 | scaled_height, | |
98 | ); | |
80 | 99 | } |
81 | 100 | |
82 | 101 | c.glBindTexture(c.GL_TEXTURE_2D, texture_id); |
95 | 114 | c.glfwDestroyWindow(self.window); |
96 | 115 | } |
97 | 116 | |
98 | fn keyCallback(win: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, mods: c_int) callconv(.C) void { | |
117 | fn keyCallback( | |
118 | win: ?*c.GLFWwindow, | |
119 | key: c_int, | |
120 | scancode: c_int, | |
121 | action: c_int, | |
122 | mods: c_int, | |
123 | ) callconv(.C) void { | |
99 | 124 | if (action != c.GLFW_PRESS) return; |
100 | const self = @ptrCast(*WindowOutput, @alignCast(@alignOf(WindowOutput), c.glfwGetWindowUserPointer(win).?)); | |
125 | const userptr = c.glfwGetWindowUserPointer(win).?; | |
126 | const self = @ptrCast(*WindowOutput, @alignCast(@alignOf(WindowOutput), userptr)); | |
101 | 127 | |
102 | 128 | switch (key) { |
103 | 129 | // c.GLFW_KEY_F => // toggle fullscreen |
108 | 134 | } |
109 | 135 | |
110 | 136 | fn sizeCallback(win: ?*c.GLFWwindow, width: c_int, height: c_int) callconv(.C) void { |
111 | const self = @ptrCast(*WindowOutput, @alignCast(@alignOf(WindowOutput), c.glfwGetWindowUserPointer(win).?)); | |
137 | const userptr = c.glfwGetWindowUserPointer(win).?; | |
138 | const self = @ptrCast(*WindowOutput, @alignCast(@alignOf(WindowOutput), userptr)); | |
112 | 139 | self.resized = true; |
113 | 140 | } |
114 | 141 | }; |