mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
stb
This commit is contained in:
@ -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, .{});
|
||||
|
||||
|
33
src/main.zig
33
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;
|
||||
|
1
src/stb.zig
Normal file
1
src/stb.zig
Normal file
@ -0,0 +1 @@
|
||||
pub const c = @import("stb/c.zig");
|
3
src/stb/c.zig
Normal file
3
src/stb/c.zig
Normal file
@ -0,0 +1,3 @@
|
||||
pub usingnamespace @cImport({
|
||||
@cInclude("stb_image.h");
|
||||
});
|
2
src/stb/stb.c
Normal file
2
src/stb/stb.c
Normal file
@ -0,0 +1,2 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
Reference in New Issue
Block a user