Graphics buffering changes, WIP

This commit is contained in:
Dusk 2022-08-02 22:19:31 +02:00
parent c9fe252019
commit 5c7ea5ea37
4 changed files with 77 additions and 28 deletions

View File

@ -53,7 +53,7 @@ timer_right_arr: Timer,
timer_right_das: Timer, timer_right_das: Timer,
timer_gravity: Timer, timer_gravity: Timer,
pub fn init(renderer: Renderer.Renderer) Self { pub fn init(renderer: *Renderer.Renderer) Self {
var ret = Self{ var ret = Self{
.grid = Grid.init(), .grid = Grid.init(),

View File

@ -10,12 +10,12 @@ const color = @import("../color.zig");
const Self = @This(); const Self = @This();
renderer: Renderer, renderer: *Renderer,
grid_cell_size: i32, grid_cell_size: i32,
grid_pos_x: i32, grid_pos_x: i32,
grid_pos_y: i32, grid_pos_y: i32,
pub fn init(renderer: Renderer) Self { pub fn init(renderer: *Renderer) Self {
var wsize = renderer.getOutputSize(); var wsize = renderer.getOutputSize();
const grid_cell_size = @divFloor(@minimum(wsize.width, wsize.height), 32); const grid_cell_size = @divFloor(@minimum(wsize.width, wsize.height), 32);

View File

@ -12,6 +12,14 @@ color_loc: u32,
buffer: gl.Buffer, buffer: gl.Buffer,
color: [4]f32, 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 { pub fn init() !Self {
try sdl.init(.{ .video = true, .audio = true, .events = true }); try sdl.init(.{ .video = true, .audio = true, .events = true });
@ -79,6 +87,10 @@ pub fn init() !Self {
const buf = gl.Buffer.gen(); const buf = gl.Buffer.gen();
buf.bind(.array_buffer); 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); 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 // 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 } }; 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); sdl.gl.swapWindow(self.window);
gl.clear(.{ .color = true }); gl.clear(.{ .color = true });
} }
pub fn deinit(self: Self) void { pub fn deinit(self: *Self) void {
gl.disableVertexAttribArray(0);
sdl.quit(); sdl.quit();
self.window.destroy(); self.window.destroy();
sdl.gl.deleteContext(self.context); 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 { 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 }; //const indices = [_]u8{ 0, 1, 1, 2, 2, 3, 3, 0 };
self.renderRectangle(x, y, w, h, .lines, &indices); //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 { pub fn fillRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void {
const indices = [_]u8{ 1, 3, 0, 1, 3, 2 }; //const indices = [_]u8{ 1, 3, 0, 1, 3, 2 };
self.renderRectangle(x, y, w, h, .triangles, &indices); 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 { fn renderRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void {
gl.uniform4fv(self.color_loc, &.{self.color}); self.test_counter += 1;
//gl.uniform4fv(self.color_loc, &.{self.color});
var xf = @intToFloat(f32, x); var xf = @intToFloat(f32, x);
var yf = @intToFloat(f32, y); var yf = @intToFloat(f32, y);
var wf = @intToFloat(f32, w); var wf = @intToFloat(f32, w);
var hf = @intToFloat(f32, h); var hf = @intToFloat(f32, h);
gl.vertexAttribPointer(0, 2, .float, false, 0, 0); const i = self.vbo_index;
self.colors[i + 0] = self.color;
const vertex_buffer = [_]f32{ self.colors[i + 1] = self.color;
xf , yf , // up-left self.colors[i + 2] = self.color;
xf + wf, yf , // up-right self.colors[i + 3] = self.color;
xf + wf, yf + hf, // down-right self.colors[i + 4] = self.color;
xf , yf + hf, // down-left self.colors[i + 5] = self.color;
}; self.colors[i + 6] = self.color;
self.colors[i + 7] = self.color;
self.buffer.data(f32, &vertex_buffer, .static_draw); self.colors[i + 8] = self.color;
self.colors[i + 9] = self.color;
gl.enableVertexAttribArray(0); self.colors[i + 10] = self.color;
gl.drawElements(primitive, indices.len, .u8, @ptrToInt(indices.ptr)); self.colors[i + 11] = self.color;
gl.disableVertexAttribArray(0); 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 }; pub const OutputSize = struct { width: c_int, height: c_int };

View File

@ -12,7 +12,7 @@ pub fn main() !void {
defer renderer.deinit(); defer renderer.deinit();
var main_menu = MainMenu.init(renderer); var main_menu = MainMenu.init(renderer);
var game = Game.init(renderer); var game = Game.init(&renderer);
var current_state: State = main_menu.state; var current_state: State = main_menu.state;