const std = @import("std"); const sdl = @import("sdl2"); const zlm = @import("zlm"); const gl = @import("zgl"); const m = zlm.SpecializeOn(f32); const Self = @This(); window: sdl.Window, context: sdl.gl.Context, color_loc: u32, buffer: gl.Buffer, color: [4]f32, 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 }); try sdl.gl.setAttribute(.{ .multisamplebuffers = true }); try sdl.gl.setAttribute(.{ .multisamplesamples = 4 }); const window = try sdl.createWindow( "USG", .{ .centered = {} }, .{ .centered = {} }, 1280, 720, .{ .context = .opengl, .vis = .shown } ); const ctx = try sdl.gl.createContext(window); var mvp_loc: u32 = undefined; 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(); mvp_loc = program.uniformLocation("mvp").?; color_loc = program.uniformLocation("color").?; } program.use(); var wsize = window.getSize(); var xunit = @intToFloat(f32, wsize.width); var yunit = @intToFloat(f32, wsize.height); const ortho = m.Mat4.createOrthogonal(0.0, xunit, yunit, 0.0, 0.0, 100.0); gl.uniformMatrix4fv(mvp_loc, false, &.{ ortho.fields }); var vertex_array = gl.VertexArray.gen(); vertex_array.bind(); const buf = gl.Buffer.gen(); buf.bind(.array_buffer); gl.clearColor(0.91, 0.97, 1.00, 1.00); gl.enable(.blend); gl.blendFunc(.src_alpha, .one_minus_src_alpha); return Self{ .window = window, .context = ctx, .color_loc = color_loc, .buffer = buf, .color = .{0,0,0,0} }; } pub fn render(self: Self) void { sdl.gl.swapWindow(self.window); gl.clear(.{ .color = true }); } pub fn deinit(self: Self) void { sdl.quit(); self.window.destroy(); sdl.gl.deleteContext(self.context); } pub fn setColor(self: *Self, r: u8, g: i32, b: i32, a: i32) void { self.*.color = .{ @intToFloat(f32, r)/255.0, @intToFloat(f32, g)/255.0, @intToFloat(f32, b)/255.0, @intToFloat(f32, a)/255.0, }; } pub fn drawRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { 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 { 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 + 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.drawElements(.triangles, 6, .u8, @ptrToInt(&indices)); gl.disableVertexAttribArray(0); } pub const OutputSize = struct { width: c_int, height: c_int }; pub fn getOutputSize(self: Self) OutputSize { var wsize = self.window.getSize(); return OutputSize{ .width = wsize.width, .height = wsize.height, }; }