add Quad struct utilities
This commit is contained in:
parent
963041cd4c
commit
6a894ac4b2
176
src/graphics.zig
176
src/graphics.zig
@ -252,16 +252,6 @@ pub fn endFrame() void {
|
|||||||
sg.commit();
|
sg.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn drawQuad(quad: Quad) void {
|
|
||||||
const self = &g_state;
|
|
||||||
|
|
||||||
if (self.quads.items.len == self.quads.capacity) {
|
|
||||||
flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
self.quads.appendAssumeCapacity(quad);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn drawRectangle(pos: Vec2, size: Vec2, color: Color) void {
|
pub fn drawRectangle(pos: Vec2, size: Vec2, color: Color) void {
|
||||||
const self = &g_state;
|
const self = &g_state;
|
||||||
|
|
||||||
@ -269,6 +259,8 @@ pub fn drawRectangle(pos: Vec2, size: Vec2, color: Color) void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn drawLine(from: Vec2, to: Vec2, color: Vec4, width: f32) void {
|
pub fn drawLine(from: Vec2, to: Vec2, color: Vec4, width: f32) void {
|
||||||
|
const self = &g_state;
|
||||||
|
|
||||||
const step = to.sub(from).normalized().multiplyScalar(width/2);
|
const step = to.sub(from).normalized().multiplyScalar(width/2);
|
||||||
|
|
||||||
const top_left = from.add(step.rotateLeft90());
|
const top_left = from.add(step.rotateLeft90());
|
||||||
@ -276,27 +268,17 @@ pub fn drawLine(from: Vec2, to: Vec2, color: Vec4, width: f32) void {
|
|||||||
const top_right = to.add(step.rotateLeft90());
|
const top_right = to.add(step.rotateLeft90());
|
||||||
const bottom_right = to.add(step.rotateRight90());
|
const bottom_right = to.add(step.rotateRight90());
|
||||||
|
|
||||||
drawQuad(Quad{
|
var quad: Quad = undefined;
|
||||||
Vertex{
|
quad.setColor(color);
|
||||||
.position = top_right,
|
quad.setUVRect(.init(0, 0), .init(1, 1));
|
||||||
.color = color,
|
quad.vertices[0] = top_right;
|
||||||
.texcoord = .init(0, 0)
|
quad.vertices[1] = top_left;
|
||||||
},
|
quad.vertices[2] = bottom_right;
|
||||||
Vertex{
|
quad.vertices[3] = bottom_left;
|
||||||
.position = top_left,
|
|
||||||
.color = color,
|
draw(.{
|
||||||
.texcoord = .init(1, 0)
|
.texture = self.default_texture,
|
||||||
},
|
.quad = quad
|
||||||
Vertex{
|
|
||||||
.position = bottom_right,
|
|
||||||
.color = color,
|
|
||||||
.texcoord = .init(0, 1)
|
|
||||||
},
|
|
||||||
Vertex{
|
|
||||||
.position = bottom_left,
|
|
||||||
.color = color,
|
|
||||||
.texcoord = .init(1, 1)
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -366,13 +348,30 @@ pub fn setTexture(id: TextureId, rgba8_pixels: [*]const u8, width: u32, height:
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn drawTexture(id: TextureId, pos: Vec2, size: Vec2, color: Color) void {
|
pub fn drawTexture(id: TextureId, pos: Vec2, size: Vec2, color: Color) void {
|
||||||
|
var quad: Quad = undefined;
|
||||||
|
quad.setColor(color);
|
||||||
|
quad.setRect(pos, size);
|
||||||
|
quad.setUVRect(.init(0, 0), .init(1, 1));
|
||||||
|
|
||||||
|
draw(.{
|
||||||
|
.texture = id,
|
||||||
|
.quad = quad
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const DrawOptions = struct {
|
||||||
|
texture: TextureId,
|
||||||
|
quad: Quad
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn draw(opts: DrawOptions) void {
|
||||||
const self = &g_state;
|
const self = &g_state;
|
||||||
if (!self.textures_slotmap.exists(id)) {
|
if (!self.textures_slotmap.exists(opts.texture)) {
|
||||||
// TODO: Draw purple-black texture
|
// TODO: Draw purple-black texture
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const texture = self.textures_buffer[id.index];
|
const texture = self.textures_buffer[opts.texture.index];
|
||||||
|
|
||||||
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 != texture.view.id) {
|
||||||
@ -380,34 +379,11 @@ pub fn drawTexture(id: TextureId, pos: Vec2, size: Vec2, color: Color) void {
|
|||||||
bound_view.* = texture.view;
|
bound_view.* = texture.view;
|
||||||
}
|
}
|
||||||
|
|
||||||
const color_vec4 = Vec4{
|
if (self.quads.items.len == self.quads.capacity) {
|
||||||
.x = color.r,
|
flush();
|
||||||
.y = color.g,
|
}
|
||||||
.z = color.b,
|
|
||||||
.w = color.a,
|
self.quads.appendAssumeCapacity(opts.quad);
|
||||||
};
|
|
||||||
drawQuad(Quad{
|
|
||||||
Vertex{
|
|
||||||
.position = pos,
|
|
||||||
.color = color_vec4,
|
|
||||||
.texcoord = .init(0, 0)
|
|
||||||
},
|
|
||||||
Vertex{
|
|
||||||
.position = pos.add(.{ .x = size.x, .y = 0 }),
|
|
||||||
.color = color_vec4,
|
|
||||||
.texcoord = .init(1, 0)
|
|
||||||
},
|
|
||||||
Vertex{
|
|
||||||
.position = pos.add(.{ .x = 0, .y = size.y }),
|
|
||||||
.color = color_vec4,
|
|
||||||
.texcoord = .init(0, 1)
|
|
||||||
},
|
|
||||||
Vertex{
|
|
||||||
.position = pos.add(size),
|
|
||||||
.color = color_vec4,
|
|
||||||
.texcoord = .init(1, 1)
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const Texture = struct {
|
const Texture = struct {
|
||||||
@ -415,53 +391,43 @@ const Texture = struct {
|
|||||||
view: sg.View,
|
view: sg.View,
|
||||||
};
|
};
|
||||||
|
|
||||||
// const SlotMap = SlotMapType(u16, u8);
|
|
||||||
//
|
|
||||||
// const TextureId = u64;
|
|
||||||
//
|
|
||||||
// pub const Interface = struct {
|
|
||||||
// clear_color: Color,
|
|
||||||
//
|
|
||||||
// quads: std.ArrayList(Quad),
|
|
||||||
// quads_overflow: bool,
|
|
||||||
//
|
|
||||||
// commands: std.ArrayList(Command),
|
|
||||||
// commands_overflow: bool,
|
|
||||||
//
|
|
||||||
// // blob: std.ArrayList(u8),
|
|
||||||
// // blob_overflow: bool,
|
|
||||||
//
|
|
||||||
// fn pushCommand(self: *Interface, cmd: Command) void {
|
|
||||||
// self.commands.appendBounded(cmd) catch {
|
|
||||||
// if (!self.commands_overflow) {
|
|
||||||
// log.err("max commands reached, limit: {}", .{self.commands.capacity});
|
|
||||||
// }
|
|
||||||
// self.commands_overflow = true;
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// pub fn createTexture(self: *Interface) void {
|
|
||||||
// _ = self; // autofix
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// pub fn destroyTexture(self: *Interface) void {
|
|
||||||
// _ = self; // autofix
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// pub fn setTextureData(self: *Interface, rgba8_pixels: [*]u8, width: u32, height: u32) void {
|
|
||||||
// _ = rgba8_pixels; // autofix
|
|
||||||
// _ = width; // autofix
|
|
||||||
// _ = height; // autofix
|
|
||||||
// _ = self; // autofix
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
pub const Vertex = extern struct {
|
pub const Vertex = extern struct {
|
||||||
position: Vec2,
|
position: Vec2,
|
||||||
texcoord: Vec2,
|
texcoord: Vec2,
|
||||||
color: Vec4,
|
color: Vec4,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Quad = [4]Vertex;
|
pub const Quad = extern struct {
|
||||||
|
vertices: [4]Vertex,
|
||||||
|
|
||||||
|
pub fn init(vertices: [4]Vertex) Quad {
|
||||||
|
return Quad{
|
||||||
|
.vertices = vertices
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn setColor(self: *Quad, color: Color) void {
|
||||||
|
for (&self.vertices) |*vertex| {
|
||||||
|
vertex.color = Vec4{
|
||||||
|
.x = color.r,
|
||||||
|
.y = color.g,
|
||||||
|
.z = color.b,
|
||||||
|
.w = color.a,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn setUVRect(self: *Quad, pos: Vec2, size: Vec2) void {
|
||||||
|
self.vertices[0].texcoord = pos;
|
||||||
|
self.vertices[1].texcoord = pos.add(.init(size.x, 0));
|
||||||
|
self.vertices[2].texcoord = pos.add(.init(0, size.y));
|
||||||
|
self.vertices[3].texcoord = pos.add(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn setRect(self: *Quad, pos: Vec2, size: Vec2) void {
|
||||||
|
self.vertices[0].position = pos;
|
||||||
|
self.vertices[1].position = pos.add(.init(size.x, 0));
|
||||||
|
self.vertices[2].position = pos.add(.init(0, size.y));
|
||||||
|
self.vertices[3].position = pos.add(size);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user