add error texture
This commit is contained in:
parent
6a894ac4b2
commit
f685b69bd9
@ -69,7 +69,7 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
|||||||
|
|
||||||
Gfx.setClearColor(self.bg);
|
Gfx.setClearColor(self.bg);
|
||||||
Gfx.drawTexture(self.tex, self.player_pos, .init(100, 100), .rgb(200, 0, 0));
|
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(.{
|
if (ImGUI.beginWindow(.{
|
||||||
.name = "Hello",
|
.name = "Hello",
|
||||||
|
|||||||
@ -10,6 +10,7 @@ a: f32,
|
|||||||
|
|
||||||
pub const black = rgb(0, 0, 0);
|
pub const black = rgb(0, 0, 0);
|
||||||
pub const white = rgb(255, 255, 255);
|
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 {
|
pub fn rgba(r: u8, g: u8, b: u8, a: f32) Color {
|
||||||
assert(0 <= a and a <= 1);
|
assert(0 <= a and a <= 1);
|
||||||
|
|||||||
@ -24,6 +24,7 @@ const State = struct {
|
|||||||
bindings: sg.Bindings,
|
bindings: sg.Bindings,
|
||||||
|
|
||||||
default_texture: TextureId,
|
default_texture: TextureId,
|
||||||
|
error_texture: TextureId,
|
||||||
default_sampler: sg.Sampler,
|
default_sampler: sg.Sampler,
|
||||||
|
|
||||||
quads_buffer: [2048]Quad,
|
quads_buffer: [2048]Quad,
|
||||||
@ -129,7 +130,8 @@ pub fn init(gpa: std.mem.Allocator, logger: sg.Logger) !void {
|
|||||||
.clear_color = .black,
|
.clear_color = .black,
|
||||||
.textures_buffer = undefined,
|
.textures_buffer = undefined,
|
||||||
.textures_slotmap = .init(try .initCapacity(gpa, max_textures)),
|
.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();
|
self.default_texture = try initTexture();
|
||||||
try setTexture(self.default_texture, &pixels, width, height);
|
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 {
|
pub fn deinit() void {
|
||||||
@ -306,7 +336,8 @@ pub fn deinitTexture(id: TextureId) void {
|
|||||||
const texture = self.textures_buffer[id.index];
|
const texture = self.textures_buffer[id.index];
|
||||||
const bound_view = &self.bindings.views[shd.VIEW_tex];
|
const bound_view = &self.bindings.views[shd.VIEW_tex];
|
||||||
if (texture.view.id == bound_view.id) {
|
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);
|
sg.destroyView(texture.view);
|
||||||
@ -366,24 +397,27 @@ const DrawOptions = struct {
|
|||||||
|
|
||||||
pub fn draw(opts: DrawOptions) void {
|
pub fn draw(opts: DrawOptions) void {
|
||||||
const self = &g_state;
|
const self = &g_state;
|
||||||
if (!self.textures_slotmap.exists(opts.texture)) {
|
|
||||||
// TODO: Draw purple-black texture
|
var quad = opts.quad;
|
||||||
return;
|
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];
|
const bound_view = &self.bindings.views[shd.VIEW_tex];
|
||||||
if (bound_view.id != texture.view.id) {
|
if (bound_view.id != view.id) {
|
||||||
flush();
|
flush();
|
||||||
bound_view.* = texture.view;
|
bound_view.* = view;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self.quads.items.len == self.quads.capacity) {
|
if (self.quads.items.len == self.quads.capacity) {
|
||||||
flush();
|
flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
self.quads.appendAssumeCapacity(opts.quad);
|
self.quads.appendAssumeCapacity(quad);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Texture = struct {
|
const Texture = struct {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user