From 5c7ea5ea37e2a9b5c06f83de041f375824ca3676 Mon Sep 17 00:00:00 2001 From: Strangedusk Date: Tue, 2 Aug 2022 22:19:31 +0200 Subject: [PATCH] Graphics buffering changes, WIP --- src/Game.zig | 2 +- src/Game/Renderer.zig | 4 +- src/Renderer.zig | 97 ++++++++++++++++++++++++++++++++----------- src/main.zig | 2 +- 4 files changed, 77 insertions(+), 28 deletions(-) diff --git a/src/Game.zig b/src/Game.zig index 070c06c..0aff27a 100644 --- a/src/Game.zig +++ b/src/Game.zig @@ -53,7 +53,7 @@ timer_right_arr: Timer, timer_right_das: Timer, timer_gravity: Timer, -pub fn init(renderer: Renderer.Renderer) Self { +pub fn init(renderer: *Renderer.Renderer) Self { var ret = Self{ .grid = Grid.init(), diff --git a/src/Game/Renderer.zig b/src/Game/Renderer.zig index 5f5395e..ac60e35 100644 --- a/src/Game/Renderer.zig +++ b/src/Game/Renderer.zig @@ -10,12 +10,12 @@ const color = @import("../color.zig"); const Self = @This(); -renderer: Renderer, +renderer: *Renderer, grid_cell_size: i32, grid_pos_x: i32, grid_pos_y: i32, -pub fn init(renderer: Renderer) Self { +pub fn init(renderer: *Renderer) Self { var wsize = renderer.getOutputSize(); const grid_cell_size = @divFloor(@minimum(wsize.width, wsize.height), 32); diff --git a/src/Renderer.zig b/src/Renderer.zig index d61551d..5bb89f3 100644 --- a/src/Renderer.zig +++ b/src/Renderer.zig @@ -12,6 +12,14 @@ color_loc: u32, buffer: gl.Buffer, color: [4]f32, +test_counter: usize = 0, + +vbo: [max_objects]f32 = .{0.0} ** max_objects, +colors: [max_objects][4]f32 = .{[4]f32{ 0.0, 0.0, 0.0, 1.0 }} ** max_objects, +vbo_index: usize = 0, + +const max_objects: usize = 16384; + pub fn init() !Self { try sdl.init(.{ .video = true, .audio = true, .events = true }); @@ -79,6 +87,10 @@ pub fn init() !Self { const buf = gl.Buffer.gen(); buf.bind(.array_buffer); + gl.vertexAttribPointer(0, 2, .float, false, 0, 0); + + gl.enableVertexAttribArray(0); + gl.clearColor(0.91, 0.97, 1.00, 1.00); // TODO: See if there's a more optimal solution to this, maybe activating only when necessary @@ -88,12 +100,30 @@ pub fn init() !Self { return Self{ .window = window, .context = ctx, .color_loc = color_loc, .buffer = buf, .color = .{ 0, 0, 0, 0 } }; } -pub fn render(self: Self) void { +pub fn render(self: *Self) void { + std.debug.print("Numer of rendered elements: {}\n", .{self.test_counter}); + self.test_counter = 0; + + self.buffer.data(f32, &self.vbo, .static_draw); + + { + var i: usize = 0; + while (i < self.vbo_index) { + gl.uniform4fv(self.color_loc, &.{self.colors[i]}); + std.debug.print("Color: {any}\n", .{self.colors[i]}); + gl.drawArrays(.triangles, i, 12); + i += 12; + } + } + + self.vbo_index = 0; + sdl.gl.swapWindow(self.window); gl.clear(.{ .color = true }); } -pub fn deinit(self: Self) void { +pub fn deinit(self: *Self) void { + gl.disableVertexAttribArray(0); sdl.quit(); self.window.destroy(); sdl.gl.deleteContext(self.context); @@ -108,38 +138,57 @@ 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 { - const indices = [_]u8{ 0, 1, 1, 2, 2, 3, 3, 0 }; - self.renderRectangle(x, y, w, h, .lines, &indices); +pub fn drawRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { + //const indices = [_]u8{ 0, 1, 1, 2, 2, 3, 3, 0 }; + //self.renderRectangle(x, y, w, h, .lines, &indices); + _ = x; + _ = y; + _ = self; + _ = w; + _ = h; } -pub fn fillRectangle(self: Self, x: i32, y: i32, w: i32, h: i32) void { - const indices = [_]u8{ 1, 3, 0, 1, 3, 2 }; - self.renderRectangle(x, y, w, h, .triangles, &indices); +pub fn fillRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { + //const indices = [_]u8{ 1, 3, 0, 1, 3, 2 }; + self.renderRectangle(x, y, w, h); } -fn renderRectangle(self: Self, x: i32, y: i32, w: i32, h: i32, primitive: gl.PrimitiveType, indices: []const u8) void { - gl.uniform4fv(self.color_loc, &.{self.color}); +fn renderRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { + self.test_counter += 1; + + //gl.uniform4fv(self.color_loc, &.{self.color}); var xf = @intToFloat(f32, x); var yf = @intToFloat(f32, y); var wf = @intToFloat(f32, w); var hf = @intToFloat(f32, h); - gl.vertexAttribPointer(0, 2, .float, false, 0, 0); - - const vertex_buffer = [_]f32{ - xf , yf , // up-left - xf + wf, yf , // up-right - xf + wf, yf + hf, // down-right - xf , yf + hf, // down-left - }; - - self.buffer.data(f32, &vertex_buffer, .static_draw); - - gl.enableVertexAttribArray(0); - gl.drawElements(primitive, indices.len, .u8, @ptrToInt(indices.ptr)); - gl.disableVertexAttribArray(0); + const i = self.vbo_index; + self.colors[i + 0] = self.color; + self.colors[i + 1] = self.color; + self.colors[i + 2] = self.color; + self.colors[i + 3] = self.color; + self.colors[i + 4] = self.color; + self.colors[i + 5] = self.color; + self.colors[i + 6] = self.color; + self.colors[i + 7] = self.color; + self.colors[i + 8] = self.color; + self.colors[i + 9] = self.color; + self.colors[i + 10] = self.color; + self.colors[i + 11] = self.color; + self.vbo[i + 0] = xf; + self.vbo[i + 1] = yf; // up-left + self.vbo[i + 2] = xf + wf; + self.vbo[i + 3] = yf; // up-right + self.vbo[i + 4] = xf; + self.vbo[i + 5] = yf + hf; // down-left + self.vbo[i + 6] = xf + wf; + self.vbo[i + 7] = yf + hf; // down-right + self.vbo[i + 8] = xf + wf; + self.vbo[i + 9] = yf; // up-right + self.vbo[i + 10] = xf; + self.vbo[i + 11] = yf + hf; // down-left + self.vbo_index += 12; } pub const OutputSize = struct { width: c_int, height: c_int }; diff --git a/src/main.zig b/src/main.zig index 746cc9b..542c5a1 100644 --- a/src/main.zig +++ b/src/main.zig @@ -12,7 +12,7 @@ pub fn main() !void { defer renderer.deinit(); var main_menu = MainMenu.init(renderer); - var game = Game.init(renderer); + var game = Game.init(&renderer); var current_state: State = main_menu.state;