Line drawing optimizations, vertex indexing

This commit is contained in:
Dendy 2022-07-28 16:29:24 +02:00
parent c1cd614040
commit 4da430a02b
3 changed files with 44 additions and 15 deletions

View File

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

View File

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

View File

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