From f685b69bd9ef3dcfde3f94d975768967a7fbd149 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Fri, 10 Jul 2026 02:35:18 +0300 Subject: [PATCH] add error texture --- src/app.zig | 2 +- src/color.zig | 1 + src/graphics.zig | 52 +++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/app.zig b/src/app.zig index 3d637ae..0ff8d22 100644 --- a/src/app.zig +++ b/src/app.zig @@ -69,7 +69,7 @@ pub fn frame(self: *App, plt: Platform.Frame) !void { Gfx.setClearColor(self.bg); Gfx.drawTexture(self.tex, self.player_pos, .init(100, 100), .rgb(200, 0, 0)); - Gfx.drawRectangle(self.player_pos.add(.init(0, 200)), .init(100, 100), .rgb(200, 200, 0)); + Gfx.drawRectangle(self.player_pos.add(.init(0, 200)), .init(100, 100), .rgb(255, 255, 255)); if (ImGUI.beginWindow(.{ .name = "Hello", diff --git a/src/color.zig b/src/color.zig index 2e5d2a8..007f842 100644 --- a/src/color.zig +++ b/src/color.zig @@ -10,6 +10,7 @@ a: f32, pub const black = rgb(0, 0, 0); pub const white = rgb(255, 255, 255); +pub const purple = rgb(255, 0, 255); pub fn rgba(r: u8, g: u8, b: u8, a: f32) Color { assert(0 <= a and a <= 1); diff --git a/src/graphics.zig b/src/graphics.zig index f5c4797..a8dca2e 100644 --- a/src/graphics.zig +++ b/src/graphics.zig @@ -24,6 +24,7 @@ const State = struct { bindings: sg.Bindings, default_texture: TextureId, + error_texture: TextureId, default_sampler: sg.Sampler, quads_buffer: [2048]Quad, @@ -129,7 +130,8 @@ pub fn init(gpa: std.mem.Allocator, logger: sg.Logger) !void { .clear_color = .black, .textures_buffer = undefined, .textures_slotmap = .init(try .initCapacity(gpa, max_textures)), - .default_texture = undefined + .default_texture = undefined, + .error_texture = undefined }; { @@ -142,6 +144,34 @@ pub fn init(gpa: std.mem.Allocator, logger: sg.Logger) !void { self.default_texture = try initTexture(); try setTexture(self.default_texture, &pixels, width, height); } + + { + const width = 8; + const height = 8; + + var pixels: [width * height * 4]u8 = undefined; + for (0..height) |y| { + for (0..width) |x| { + const pixel_index = y * width + x; + + var color: Color = undefined; + if ((pixel_index+y) % 2 == 0) { + color = .purple; + } else { + color = .black; + } + + const pixel = pixels[(4*pixel_index)..]; + pixel[0] = @intFromFloat(color.r*255); + pixel[1] = @intFromFloat(color.g*255); + pixel[2] = @intFromFloat(color.b*255); + pixel[3] = @intFromFloat(color.a*255); + } + } + + self.error_texture = try initTexture(); + try setTexture(self.error_texture, &pixels, width, height); + } } pub fn deinit() void { @@ -306,7 +336,8 @@ pub fn deinitTexture(id: TextureId) void { const texture = self.textures_buffer[id.index]; const bound_view = &self.bindings.views[shd.VIEW_tex]; if (texture.view.id == bound_view.id) { - bound_view.* = self.default_view; + assert(self.textures_slotmap.exists(self.default_texture)); + bound_view.* = self.textures_buffer[self.default_texture.index].view; } sg.destroyView(texture.view); @@ -366,24 +397,27 @@ const DrawOptions = struct { pub fn draw(opts: DrawOptions) void { const self = &g_state; - if (!self.textures_slotmap.exists(opts.texture)) { - // TODO: Draw purple-black texture - return; + + var quad = opts.quad; + var texture = opts.texture; + if (!self.textures_slotmap.exists(texture)) { + texture = self.error_texture; + quad.setColor(.white); } - const texture = self.textures_buffer[opts.texture.index]; + const view = self.textures_buffer[texture.index].view; const bound_view = &self.bindings.views[shd.VIEW_tex]; - if (bound_view.id != texture.view.id) { + if (bound_view.id != view.id) { flush(); - bound_view.* = texture.view; + bound_view.* = view; } if (self.quads.items.len == self.quads.capacity) { flush(); } - self.quads.appendAssumeCapacity(opts.quad); + self.quads.appendAssumeCapacity(quad); } const Texture = struct {