This commit is contained in:
Mitchell Hashimoto
2022-04-04 11:11:24 -07:00
parent e0b2aefad0
commit 1a40544220
7 changed files with 2257 additions and 87 deletions

View File

@ -3,7 +3,6 @@ const Builder = std.build.Builder;
const LibExeObjStep = std.build.LibExeObjStep; const LibExeObjStep = std.build.LibExeObjStep;
const glfw = @import("vendor/mach/glfw/build.zig"); const glfw = @import("vendor/mach/glfw/build.zig");
const ft = @import("src/freetype/build.zig"); const ft = @import("src/freetype/build.zig");
const cglm = @import("src/cglm.zig");
pub fn build(b: *std.build.Builder) !void { pub fn build(b: *std.build.Builder) !void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
@ -13,13 +12,13 @@ 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("src/");
exe.addCSourceFile("src/gb_math.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, .{});
exe.linkSystemLibrary("epoxy"); exe.linkSystemLibrary("epoxy");
try cglm.build(b, exe, target, mode, .{});
const ftlib = try ft.create(b, target, mode, .{}); const ftlib = try ft.create(b, target, mode, .{});
ftlib.link(exe); ftlib.link(exe);
// to link to system: // to link to system:

View File

@ -32,12 +32,6 @@ pub fn init(alloc: std.mem.Allocator) !App {
// renderer at some point. // renderer at some point.
try glfw.makeContextCurrent(window); try glfw.makeContextCurrent(window);
try glfw.swapInterval(1); 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);
// Blending for text // Blending for text
gl.c.glEnable(gl.c.GL_CULL_FACE); gl.c.glEnable(gl.c.GL_CULL_FACE);
@ -48,6 +42,13 @@ pub fn init(alloc: std.mem.Allocator) !App {
var texter = try TextRenderer.init(alloc); var texter = try TextRenderer.init(alloc);
errdefer texter.deinit(); errdefer texter.deinit();
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);
return App{ return App{
.window = window, .window = window,
.text = texter, .text = texter,

View File

@ -3,6 +3,7 @@ const TextRenderer = @This();
const std = @import("std"); const std = @import("std");
const ftc = @import("freetype/c.zig"); const ftc = @import("freetype/c.zig");
const gl = @import("opengl.zig"); const gl = @import("opengl.zig");
const gb = @import("gb_math.zig");
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
ft: ftc.FT_Library, ft: ftc.FT_Library,
@ -113,6 +114,10 @@ pub fn init(alloc: std.mem.Allocator) !TextRenderer {
@embedFile("../shaders/text.f.glsl"), @embedFile("../shaders/text.f.glsl"),
); );
// Our project
var proj: gb.gbMat4 = undefined;
gb.gb_mat4_ortho2d(&proj, 0, 640, 0, 480);
return TextRenderer{ return TextRenderer{
.alloc = alloc, .alloc = alloc,
.ft = ft, .ft = ft,

View File

@ -1,78 +0,0 @@
const std = @import("std");
/// Compile-time options for the library. These mostly correspond to
/// options exposed by the native build system used by the library.
pub const BuildOptions = struct {};
// Build and link this library.
pub fn build(
b: *std.build.Builder,
step: *std.build.LibExeObjStep,
target: std.zig.CrossTarget,
mode: std.builtin.Mode,
opts: BuildOptions,
) !void {
_ = opts;
const ret = b.addStaticLibrary("cglm", null);
ret.setTarget(target);
ret.setBuildMode(mode);
var flags = std.ArrayList([]const u8).init(b.allocator);
defer flags.deinit();
try flags.appendSlice(&.{
"-DCGLM_STATIC",
});
// C files
ret.addCSourceFiles(srcs, flags.items);
ret.addIncludeDir(include_dir);
ret.linkLibC();
step.addIncludeDir(include_dir);
step.linkLibrary(ret);
}
fn root() []const u8 {
return (std.fs.path.dirname(@src().file) orelse unreachable) ++ "/../vendor/cglm/";
}
/// Directories with our includes.
const include_dir = root() ++ "include";
const srcs = &.{
root() ++ "src/euler.c",
root() ++ "src/affine.c",
root() ++ "src/io.c",
root() ++ "src/quat.c",
root() ++ "src/cam.c",
root() ++ "src/vec2.c",
root() ++ "src/vec3.c",
root() ++ "src/vec4.c",
root() ++ "src/mat2.c",
root() ++ "src/mat3.c",
root() ++ "src/mat4.c",
root() ++ "src/plane.c",
root() ++ "src/frustum.c",
root() ++ "src/box.c",
root() ++ "src/project.c",
root() ++ "src/sphere.c",
root() ++ "src/ease.c",
root() ++ "src/curve.c",
root() ++ "src/bezier.c",
root() ++ "src/ray.c",
root() ++ "src/affine2d.c",
root() ++ "src/clipspace/persp_lh_zo.c",
root() ++ "src/clipspace/persp_rh_zo.c",
root() ++ "src/clipspace/persp_lh_no.c",
root() ++ "src/clipspace/persp_rh_no.c",
root() ++ "src/clipspace/ortho_lh_zo.c",
root() ++ "src/clipspace/ortho_rh_zo.c",
root() ++ "src/clipspace/ortho_lh_no.c",
root() ++ "src/clipspace/ortho_rh_no.c",
root() ++ "src/clipspace/view_lh_zo.c",
root() ++ "src/clipspace/view_rh_zo.c",
root() ++ "src/clipspace/view_lh_no.c",
root() ++ "src/clipspace/view_rh_no.c",
};

6
src/gb_math.c Normal file
View File

@ -0,0 +1,6 @@
// This file defines the implementation of gb_math and is compiled using clang.
// gb_math.zig then imports gb_math.h without the implementation define set
// so that it can just get the function prototypes.
#define GB_MATH_IMPLEMENTATION
#include "gb_math.h"

2234
src/gb_math.h Normal file

File diff suppressed because it is too large Load Diff

3
src/gb_math.zig Normal file
View File

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