const sdl = @import("sdl2"); const gl = @import("zgl"); const Self = @This(); window: sdl.Window, context: sdl.gl.Context, color_loc: u32, buffer: gl.Buffer, pub fn init() !Self { try sdl.init(.{ .video = true, .audio = true, .events = true }); // Set OpenGL version try sdl.gl.setAttribute(.{ .context_major_version = 3 }); try sdl.gl.setAttribute(.{ .context_minor_version = 3 }); const window = try sdl.createWindow( "USG", .{ .centered = {} }, .{ .centered = {} }, 1280, 720, .{ .opengl = true, .shown = true } ); const ctx = try sdl.gl.createContext(window); var color_loc: u32 = undefined; const program = gl.Program.create(); { const vs = gl.Shader.create(.vertex); defer vs.delete(); vs.source(1, &.{@embedFile("shaders/vector.vs")}); vs.compile(); const fs = gl.Shader.create(.fragment); defer fs.delete(); fs.source(1, &.{@embedFile("shaders/fragment.fs")}); fs.compile(); program.attach(vs); defer program.detach(vs); program.attach(fs); defer program.detach(fs); program.link(); color_loc = program.uniformLocation("color").?; } program.use(); var vertex_array = gl.VertexArray.gen(); vertex_array.bind(); const vertex_buffer = [_]f32{ -1.0, -1.0, 0.0, -1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, -1.0, 0.0, 1.0, -1.0, 0.0, }; const buf = gl.Buffer.gen(); buf.bind(.array_buffer); buf.data(f32, &vertex_buffer, .static_draw); gl.clearColor(0.91, 0.97, 1.00, 1.00); return Self{ .window = window, .context = ctx, .color_loc = color_loc, .buffer = buf, }; } pub fn render(self: Self) void { gl.clear(.{ .color = true }); gl.enableVertexAttribArray(0); self.buffer.bind(.array_buffer); gl.vertexAttribPointer(0, 3, .float, false, 0, 0); gl.uniform4f(self.color_loc, 0.45, 0.21, 0.70, 1); gl.drawArrays(.triangles, 0, 3); gl.uniform4f(self.color_loc, 0.2, 0.5, 1.0, 1); gl.drawArrays(.triangles, 3, 3); gl.disableVertexAttribArray(0); sdl.gl.swapWindow(self.window); } pub fn deinit(self: Self) void { sdl.quit(); self.window.destroy(); sdl.gl.deleteContext(self.context); }