From 5bbdd75d70c5108e3e01701cd70424bb24cbb158 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 3 Apr 2022 20:39:32 -0700 Subject: [PATCH] clean up the main App --- shaders/shape.f.glsl | 7 ++ shaders/shape.v.glsl | 7 ++ src/App.zig | 107 +++++++++++++++++++++++++++++ src/main.zig | 157 ++----------------------------------------- 4 files changed, 127 insertions(+), 151 deletions(-) create mode 100644 shaders/shape.f.glsl create mode 100644 shaders/shape.v.glsl create mode 100644 src/App.zig diff --git a/shaders/shape.f.glsl b/shaders/shape.f.glsl new file mode 100644 index 000000000..0c56a32fe --- /dev/null +++ b/shaders/shape.f.glsl @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); +} diff --git a/shaders/shape.v.glsl b/shaders/shape.v.glsl new file mode 100644 index 000000000..c74ea10c9 --- /dev/null +++ b/shaders/shape.v.glsl @@ -0,0 +1,7 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +void main() +{ + gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); +} diff --git a/src/App.zig b/src/App.zig new file mode 100644 index 000000000..152e67094 --- /dev/null +++ b/src/App.zig @@ -0,0 +1,107 @@ +//! App is the primary GUI application for ghostty. This builds the window, +//! sets up the renderer, etc. The primary run loop is started by calling +//! the "run" function. +const App = @This(); + +const std = @import("std"); +const gl = @import("opengl.zig"); +const glfw = @import("glfw"); + +const log = std.log; + +window: glfw.Window, + +glprog: gl.Program, +vao: gl.VertexArray, + +/// Initialize the main app instance. This creates the main window, sets +/// up the renderer state, compiles the shaders, etc. This is the primary +/// "startup" logic. +pub fn init() !App { + // Create our window + const window = try glfw.Window.create(640, 480, "ghostty", null, null, .{ + .context_version_major = 3, + .context_version_minor = 3, + .opengl_profile = .opengl_core_profile, + .opengl_forward_compat = true, + }); + errdefer window.destroy(); + + // Setup OpenGL + // NOTE(mitchellh): we probably want to extract this to a dedicated + // renderer at some point. + try glfw.makeContextCurrent(window); + try glfw.swapInterval(1); + window.setSizeCallback((struct { + fn callback(_: glfw.Window, width: i32, height: i32) void { + log.info("set viewport {} {}", .{ width, height }); + try gl.viewport(0, 0, width, height); + } + }).callback); + + // Compile our shaders + const vs = try gl.Shader.create(gl.c.GL_VERTEX_SHADER); + try vs.setSourceAndCompile(vs_source); + errdefer vs.destroy(); + + const fs = try gl.Shader.create(gl.c.GL_FRAGMENT_SHADER); + try fs.setSourceAndCompile(fs_source); + errdefer fs.destroy(); + + // Link our shader program + const program = try gl.Program.create(); + errdefer program.destroy(); + try program.attachShader(vs); + try program.attachShader(fs); + try program.link(); + vs.destroy(); + fs.destroy(); + + // Create our bufer or vertices + const vertices = [_]f32{ + -0.5, -0.5, 0.0, // left + 0.5, -0.5, 0.0, // right + 0.0, 0.5, 0.0, // top + }; + const vao = try gl.VertexArray.create(); + //defer vao.destroy(); + const vbo = try gl.Buffer.create(); + //defer vbo.destroy(); + try vao.bind(); + var binding = try vbo.bind(gl.c.GL_ARRAY_BUFFER); + try binding.setData(&vertices, gl.c.GL_STATIC_DRAW); + try binding.vertexAttribPointer(0, 3, gl.c.GL_FLOAT, false, 3 * @sizeOf(f32), null); + try binding.enableVertexAttribArray(0); + binding.unbind(); + try gl.VertexArray.unbind(); + + return App{ + .window = window, + .glprog = program, + + .vao = vao, + }; +} + +pub fn deinit(self: *App) void { + self.window.destroy(); + self.* = undefined; +} + +pub fn run(self: App) !void { + while (!self.window.shouldClose()) { + // Setup basic OpenGL settings + gl.clearColor(0.2, 0.3, 0.3, 1.0); + gl.clear(gl.c.GL_COLOR_BUFFER_BIT); + + try self.glprog.use(); + try self.vao.bind(); + try gl.drawArrays(gl.c.GL_TRIANGLES, 0, 3); + + try self.window.swapBuffers(); + try glfw.waitEvents(); + } +} + +const vs_source = @embedFile("../shaders/shape.v.glsl"); +const fs_source = @embedFile("../shaders/shape.f.glsl"); diff --git a/src/main.zig b/src/main.zig index b5d17e50e..81ab80c47 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4,160 +4,15 @@ const gl = @import("opengl.zig"); const stb = @import("stb.zig"); const fonts = @import("fonts.zig"); +const App = @import("App.zig"); + pub fn main() !void { // List our fonts - try fonts.list(); - try glfw.init(.{}); defer glfw.terminate(); - // Create our window - const window = try glfw.Window.create(640, 480, "ghostty", null, null, .{ - .context_version_major = 3, - .context_version_minor = 3, - .opengl_profile = .opengl_core_profile, - .opengl_forward_compat = true, - }); - defer window.destroy(); - - // Setup OpenGL - try glfw.makeContextCurrent(window); - try glfw.swapInterval(1); - window.setSizeCallback((struct { - fn callback(_: glfw.Window, width: i32, height: i32) void { - std.log.info("set viewport {} {}", .{ width, height }); - try gl.viewport(0, 0, width, height); - } - }).callback); - - // Load our image - var imgwidth: c_int = 0; - var imgheight: c_int = 0; - var imgchannels: c_int = 0; - const data = stb.c.stbi_load_from_memory( - texsrc, - texsrc.len, - &imgwidth, - &imgheight, - &imgchannels, - 0, - ); - if (data == null) return error.TexFail; - defer stb.c.stbi_image_free(data); - - // Setup a texture - const tex = try gl.Texture.create(); - defer tex.destroy(); - var texbind = try tex.bind(gl.c.GL_TEXTURE_2D); - defer texbind.unbind(); - try texbind.parameter(gl.c.GL_TEXTURE_WRAP_S, gl.c.GL_REPEAT); - try texbind.parameter(gl.c.GL_TEXTURE_WRAP_T, gl.c.GL_REPEAT); - try texbind.parameter(gl.c.GL_TEXTURE_MIN_FILTER, gl.c.GL_LINEAR); - try texbind.parameter(gl.c.GL_TEXTURE_MAG_FILTER, gl.c.GL_LINEAR); - try texbind.image2D( - 0, - gl.c.GL_RGB, - imgwidth, - imgheight, - 0, - gl.c.GL_RGB, - gl.c.GL_UNSIGNED_BYTE, - data, - ); - texbind.generateMipmap(); - - // Create our vertex shader - const vs = try gl.Shader.create(gl.c.GL_VERTEX_SHADER); - try vs.setSourceAndCompile(vs_source); - defer vs.destroy(); - - const fs = try gl.Shader.create(gl.c.GL_FRAGMENT_SHADER); - try fs.setSourceAndCompile(fs_source); - defer fs.destroy(); - - // Shader program - const program = try gl.Program.create(); - defer program.destroy(); - try program.attachShader(vs); - try program.attachShader(fs); - try program.link(); - vs.destroy(); - fs.destroy(); - - // Create our bufer or vertices - const vertices = [_]f32{ - -0.8, -0.8, 0.0, 0.0, 0.0, // left - 0.8, -0.8, 0.0, 1.0, 0.0, // right - 0.0, 0.8, 0.0, 0.5, 1.0, // top - }; - const vao = try gl.VertexArray.create(); - defer vao.destroy(); - const vbo = try gl.Buffer.create(); - defer vbo.destroy(); - try vao.bind(); - var binding = try vbo.bind(gl.c.GL_ARRAY_BUFFER); - try binding.setData(&vertices, gl.c.GL_STATIC_DRAW); - try binding.vertexAttribPointer(0, 3, gl.c.GL_FLOAT, false, 5 * @sizeOf(f32), null); - try binding.enableVertexAttribArray(0); - try binding.vertexAttribPointer( - 1, - 2, - gl.c.GL_FLOAT, - false, - 5 * @sizeOf(f32), - @intToPtr(*const anyopaque, 3 * @sizeOf(f32)), - ); - try binding.enableVertexAttribArray(1); - - binding.unbind(); - try gl.VertexArray.unbind(); - - // Wait for the user to close the window. - while (!window.shouldClose()) { - // Setup basic OpenGL settings - gl.clearColor(0.2, 0.3, 0.3, 1.0); - gl.clear(gl.c.GL_COLOR_BUFFER_BIT); - - try program.use(); - - _ = try tex.bind(gl.c.GL_TEXTURE_2D); - try vao.bind(); - try gl.drawArrays(gl.c.GL_TRIANGLES, 0, 3); - - // const pos = try window.getCursorPos(); - // std.log.info("CURSOR: {}", .{pos}); - - try window.swapBuffers(); - try glfw.waitEvents(); - } + // Run our app + var app = try App.init(); + defer app.deinit(); + try app.run(); } - -const texsrc = @embedFile("tex.png"); - -const vs_source = - \\#version 330 core - \\layout (location = 0) in vec3 aPos; - \\layout (location = 1) in vec2 aTexCoord; - \\ - \\out vec2 TexCoord; - \\ - \\void main() - \\{ - \\ gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); - \\ TexCoord = aTexCoord; - \\} -; - -const fs_source = - \\#version 330 core - \\out vec4 FragColor; - \\ - \\in vec2 TexCoord; - \\ - \\uniform sampler2D ourTexture; - \\ - \\void main() - \\{ - \\ FragColor = texture(ourTexture, TexCoord); - \\} -;