add error texture

This commit is contained in:
Rokas Puzonas 2026-07-10 02:35:18 +03:00
parent 6a894ac4b2
commit f685b69bd9
3 changed files with 45 additions and 10 deletions

View File

@ -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",

View File

@ -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);

View File

@ -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 {