This commit is contained in:
Mitchell Hashimoto
2022-04-01 19:45:05 -07:00
parent a54d9bd83b
commit 02848a0535
5 changed files with 33 additions and 8 deletions

View File

@ -11,6 +11,8 @@ pub fn build(b: *std.build.Builder) void {
exe.setTarget(target); exe.setTarget(target);
exe.setBuildMode(mode); exe.setBuildMode(mode);
exe.install(); exe.install();
exe.addIncludeDir("vendor/stb");
exe.addCSourceFile("src/stb/stb.c", &.{});
exe.addPackagePath("glfw", "vendor/mach/glfw/src/main.zig"); exe.addPackagePath("glfw", "vendor/mach/glfw/src/main.zig");
glfw.link(b, exe, .{}); glfw.link(b, exe, .{});

View File

@ -1,12 +1,27 @@
const std = @import("std"); const std = @import("std");
const glfw = @import("glfw"); const glfw = @import("glfw");
const gl = @import("opengl.zig"); const gl = @import("opengl.zig");
const c = gl.c; const stb = @import("stb.zig");
pub fn main() !void { pub fn main() !void {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); 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 // Create our window
const window = try glfw.Window.create(640, 480, "ghostty", null, null, .{ const window = try glfw.Window.create(640, 480, "ghostty", null, null, .{
.context_version_major = 3, .context_version_major = 3,
@ -22,7 +37,7 @@ pub fn main() !void {
window.setSizeCallback((struct { window.setSizeCallback((struct {
fn callback(_: glfw.Window, width: i32, height: i32) void { fn callback(_: glfw.Window, width: i32, height: i32) void {
std.log.info("set viewport {} {}", .{ width, height }); std.log.info("set viewport {} {}", .{ width, height });
c.glViewport(0, 0, width, height); gl.c.glViewport(0, 0, width, height);
} }
}).callback); }).callback);
@ -55,9 +70,9 @@ pub fn main() !void {
const vbo = try gl.Buffer.create(); const vbo = try gl.Buffer.create();
defer vbo.destroy(); defer vbo.destroy();
try vao.bind(); try vao.bind();
var binding = try vbo.bind(c.GL_ARRAY_BUFFER); var binding = try vbo.bind(gl.c.GL_ARRAY_BUFFER);
try binding.setData(&vertices, c.GL_STATIC_DRAW); try binding.setData(&vertices, gl.c.GL_STATIC_DRAW);
try binding.vertexAttribPointer(0, 3, c.GL_FLOAT, false, 3 * @sizeOf(f32), null); try binding.vertexAttribPointer(0, 3, gl.c.GL_FLOAT, false, 3 * @sizeOf(f32), null);
try binding.enableVertexAttribArray(0); try binding.enableVertexAttribArray(0);
binding.unbind(); binding.unbind();
@ -66,14 +81,14 @@ pub fn main() !void {
// Wait for the user to close the window. // Wait for the user to close the window.
while (!window.shouldClose()) { while (!window.shouldClose()) {
// Setup basic OpenGL settings // Setup basic OpenGL settings
c.glClearColor(0.2, 0.3, 0.3, 1.0); gl.c.glClearColor(0.2, 0.3, 0.3, 1.0);
c.glClear(c.GL_COLOR_BUFFER_BIT); gl.c.glClear(gl.c.GL_COLOR_BUFFER_BIT);
try program.use(); try program.use();
try program.setUniform("vertexColor", @Vector(4, f32){ 0.0, 1.0, 0.0, 1.0 }); try program.setUniform("vertexColor", @Vector(4, f32){ 0.0, 1.0, 0.0, 1.0 });
try vao.bind(); try vao.bind();
c.glDrawArrays(c.GL_TRIANGLES, 0, 3); gl.c.glDrawArrays(gl.c.GL_TRIANGLES, 0, 3);
// const pos = try window.getCursorPos(); // const pos = try window.getCursorPos();
// std.log.info("CURSOR: {}", .{pos}); // std.log.info("CURSOR: {}", .{pos});
@ -83,6 +98,8 @@ pub fn main() !void {
} }
} }
const texsrc = @embedFile("tex.png");
const vs_source = const vs_source =
\\#version 330 core \\#version 330 core
\\layout (location = 0) in vec3 aPos; \\layout (location = 0) in vec3 aPos;

1
src/stb.zig Normal file
View File

@ -0,0 +1 @@
pub const c = @import("stb/c.zig");

3
src/stb/c.zig Normal file
View File

@ -0,0 +1,3 @@
pub usingnamespace @cImport({
@cInclude("stb_image.h");
});

2
src/stb/stb.c Normal file
View File

@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>