mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-14 07:46:12 +03:00
clean up the main App
This commit is contained in:
7
shaders/shape.f.glsl
Normal file
7
shaders/shape.f.glsl
Normal file
@ -0,0 +1,7 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
||||
}
|
7
shaders/shape.v.glsl
Normal file
7
shaders/shape.v.glsl
Normal file
@ -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);
|
||||
}
|
107
src/App.zig
Normal file
107
src/App.zig
Normal file
@ -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");
|
157
src/main.zig
157
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);
|
||||
// Run our app
|
||||
var app = try App.init();
|
||||
defer app.deinit();
|
||||
try app.run();
|
||||
}
|
||||
}).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();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
\\}
|
||||
;
|
||||
|
Reference in New Issue
Block a user