diff --git a/build.zig b/build.zig index 1aeac6adf..28314a2b5 100644 --- a/build.zig +++ b/build.zig @@ -11,6 +11,8 @@ pub fn build(b: *std.build.Builder) void { exe.setTarget(target); exe.setBuildMode(mode); exe.install(); + exe.addIncludeDir("vendor/stb"); + exe.addCSourceFile("src/stb/stb.c", &.{}); exe.addPackagePath("glfw", "vendor/mach/glfw/src/main.zig"); glfw.link(b, exe, .{}); diff --git a/src/main.zig b/src/main.zig index f71cd6ba2..e6ce3328c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,12 +1,27 @@ const std = @import("std"); const glfw = @import("glfw"); const gl = @import("opengl.zig"); -const c = gl.c; +const stb = @import("stb.zig"); pub fn main() !void { try glfw.init(.{}); defer glfw.terminate(); + // 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; + stb.c.stbi_image_free(data); + // Create our window const window = try glfw.Window.create(640, 480, "ghostty", null, null, .{ .context_version_major = 3, @@ -22,7 +37,7 @@ pub fn main() !void { window.setSizeCallback((struct { fn callback(_: glfw.Window, width: i32, height: i32) void { std.log.info("set viewport {} {}", .{ width, height }); - c.glViewport(0, 0, width, height); + gl.c.glViewport(0, 0, width, height); } }).callback); @@ -55,9 +70,9 @@ pub fn main() !void { const vbo = try gl.Buffer.create(); defer vbo.destroy(); try vao.bind(); - var binding = try vbo.bind(c.GL_ARRAY_BUFFER); - try binding.setData(&vertices, c.GL_STATIC_DRAW); - try binding.vertexAttribPointer(0, 3, c.GL_FLOAT, false, 3 * @sizeOf(f32), null); + 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(); @@ -66,14 +81,14 @@ pub fn main() !void { // Wait for the user to close the window. while (!window.shouldClose()) { // Setup basic OpenGL settings - c.glClearColor(0.2, 0.3, 0.3, 1.0); - c.glClear(c.GL_COLOR_BUFFER_BIT); + gl.c.glClearColor(0.2, 0.3, 0.3, 1.0); + gl.c.glClear(gl.c.GL_COLOR_BUFFER_BIT); try program.use(); try program.setUniform("vertexColor", @Vector(4, f32){ 0.0, 1.0, 0.0, 1.0 }); try vao.bind(); - c.glDrawArrays(c.GL_TRIANGLES, 0, 3); + gl.c.glDrawArrays(gl.c.GL_TRIANGLES, 0, 3); // const pos = try window.getCursorPos(); // std.log.info("CURSOR: {}", .{pos}); @@ -83,6 +98,8 @@ pub fn main() !void { } } +const texsrc = @embedFile("tex.png"); + const vs_source = \\#version 330 core \\layout (location = 0) in vec3 aPos; diff --git a/src/stb.zig b/src/stb.zig new file mode 100644 index 000000000..ad4459344 --- /dev/null +++ b/src/stb.zig @@ -0,0 +1 @@ +pub const c = @import("stb/c.zig"); diff --git a/src/stb/c.zig b/src/stb/c.zig new file mode 100644 index 000000000..26c88ca4e --- /dev/null +++ b/src/stb/c.zig @@ -0,0 +1,3 @@ +pub usingnamespace @cImport({ + @cInclude("stb_image.h"); +}); diff --git a/src/stb/stb.c b/src/stb/stb.c new file mode 100644 index 000000000..917728838 --- /dev/null +++ b/src/stb/stb.c @@ -0,0 +1,2 @@ +#define STB_IMAGE_IMPLEMENTATION +#include