From 4da430a02b19d714523b639d91ec21e122f96eaf Mon Sep 17 00:00:00 2001 From: Dendy Date: Thu, 28 Jul 2022 16:29:24 +0200 Subject: [PATCH] Line drawing optimizations, vertex indexing --- src/Renderer.zig | 47 +++++++++++++++++++++++++++++++------------ src/main.zig | 8 ++++++++ src/shaders/vector.vs | 4 ++-- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/Renderer.zig b/src/Renderer.zig index a267429..17f5e82 100644 --- a/src/Renderer.zig +++ b/src/Renderer.zig @@ -104,10 +104,31 @@ pub fn setColor(self: *Self, r: u8, g: i32, b: i32, a: i32) void { } pub fn drawRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { - self.fillRectangle(x, y, 1, h); - self.fillRectangle(x, y, w, 1); - self.fillRectangle(w+x-1, y, 1, h); - self.fillRectangle(x, h+y-1, w, 1); + var xf = @intToFloat(f32, x); + var yf = @intToFloat(f32, y); + var wf = @intToFloat(f32, w); + var hf = @intToFloat(f32, h); + + gl.enableVertexAttribArray(0); + gl.vertexAttribPointer(0, 2, .float, false, 0, 0); + + const vertex_buffer = [_]f32{ + xf, yf , + xf + wf, yf , + xf + wf, yf + hf, + xf, yf + hf, + }; + + const indices = [_]u8{0, 1, 1, 2, 2, 3, 3, 0}; + + self.buffer.data(f32, &vertex_buffer, .static_draw); + + gl.uniform4fv(self.color_loc, &.{ self.color }); + _ = indices; + + gl.drawElements(.lines, 8, .u8, @ptrToInt(&indices)); + + gl.disableVertexAttribArray(0); } pub fn fillRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { @@ -117,22 +138,22 @@ pub fn fillRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { var hf = @intToFloat(f32, h); gl.enableVertexAttribArray(0); - gl.vertexAttribPointer(0, 3, .float, false, 0, 0); + gl.vertexAttribPointer(0, 2, .float, false, 0, 0); const vertex_buffer = [_]f32{ - xf, yf , 0.0, - xf + wf, yf , 0.0, - xf, yf + hf, 0.0, - xf + wf, yf + hf, 0.0, - xf + wf, yf , 0.0, - xf, yf + hf, 0.0, + xf + wf, yf , + xf, yf + hf, + xf, yf , + xf + wf, yf + hf, }; + const indices = [_]u8{0, 1, 2, 0, 1, 3}; + self.buffer.data(f32, &vertex_buffer, .static_draw); gl.uniform4fv(self.color_loc, &.{ self.color }); - gl.drawArrays(.triangles, 0, 3); - gl.drawArrays(.triangles, 3, 6); + + gl.drawElements(.triangles, 6, .u8, @ptrToInt(&indices)); gl.disableVertexAttribArray(0); } diff --git a/src/main.zig b/src/main.zig index b423d35..6b802f2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -30,7 +30,15 @@ pub fn main() !void { .main_menu => main_menu.tick(), .game => game.tick(), }; + const start = SDL.getTicks64(); + renderer.render(); + + const delay = SDL.getTicks64() - start; + std.debug.print("{} ms\n", .{delay}); + if (delay < 16) { + SDL.delay(16 - @intCast(u32, delay)); + } } } diff --git a/src/shaders/vector.vs b/src/shaders/vector.vs index c02f467..e863a75 100644 --- a/src/shaders/vector.vs +++ b/src/shaders/vector.vs @@ -1,8 +1,8 @@ #version 330 core uniform mat4 mvp; -layout(location = 0) in vec3 vertexPosition_modelspace; +layout(location = 0) in vec2 pos; void main() { - gl_Position = mvp * vec4(vertexPosition_modelspace, 1); + gl_Position = mvp * vec4((pos + vec2(0.5, 0.5)), 0, 1); }