Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created September 11, 2020 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/a7ec0c05cc63df3c9ebd22c1f4df0739 to your computer and use it in GitHub Desktop.
Save peterhellberg/a7ec0c05cc63df3c9ebd22c1f4df0739 to your computer and use it in GitHub Desktop.
zigNeHe Lesson01 patched to compile with current zig master https://github.com/mypalmike/zigNeHe
diff --git a/Lesson01/build.zig b/Lesson01/build.zig
index 7d1fc5b..83e4ee4 100644
--- a/Lesson01/build.zig
+++ b/Lesson01/build.zig
@@ -1,5 +1,5 @@
-const Builder = @import("std").build.Builder;
-const builtin = @import("builtin");
+const std = @import("std");
+const Builder = std.build.Builder;
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
@@ -7,7 +7,7 @@ pub fn build(b: *Builder) void {
exe.setBuildMode(mode);
exe.addIncludeDir("/usr/local/include");
- switch (builtin.os) {
+ switch (std.Target.current.os.tag) {
.macosx => {
exe.addFrameworkDir("/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks");
exe.linkFramework("OpenGL");
diff --git a/Lesson01/src/main.zig b/Lesson01/src/main.zig
index cae2b88..67a5c4c 100644
--- a/Lesson01/src/main.zig
+++ b/Lesson01/src/main.zig
@@ -8,11 +8,11 @@ const height: i32 = 768;
var window: *c.GLFWwindow = undefined;
-extern fn errorCallback(err: c_int, description: [*c]const u8) void {
- panic("Error: {}\n", description);
+fn errorCallback(err: c_int, description: [*c]const u8) callconv(.C) void {
+ panic("Error: {}\n", .{description});
}
-extern fn keyCallback(win: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, mods: c_int) void {
+fn keyCallback(win: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, mods: c_int) callconv(.C) void {
if (action != c.GLFW_PRESS) return;
switch (key) {
@@ -28,39 +28,39 @@ fn perspectiveGL(fovY: f64, aspect: f64, zNear: f64, zFar: f64) void {
}
fn init_gl() void {
- c.glMatrixMode(c.GL_PROJECTION); // Select The Projection Matrix
+ c.glMatrixMode(c.GL_PROJECTION); // Select The Projection Matrix
c.glLoadIdentity();
- var aspect_ratio: f32 = f32(height) / f32(width);
+ var aspect_ratio: f32 = @intToFloat(f32, height) / @intToFloat(f32, width);
perspectiveGL(45.0, (1.0 / aspect_ratio), 0.1, 100.0);
c.glMatrixMode(c.GL_MODELVIEW);
- c.glShadeModel(c.GL_SMOOTH); // Enables Smooth Shading
- c.glClearColor(0.0, 0.0, 0.0, 0.0); // Black Background
- c.glClearDepth(1.0); // Depth Buffer Setup
- c.glEnable(c.GL_DEPTH_TEST); // Enables Depth Testing
- c.glDepthFunc(c.GL_LEQUAL); // The Type Of Depth Test To Do
- c.glHint(c.GL_PERSPECTIVE_CORRECTION_HINT, c.GL_NICEST); // Really Nice Perspective Calculations
+ c.glShadeModel(c.GL_SMOOTH); // Enables Smooth Shading
+ c.glClearColor(0.1, 0.6, 0.2, 1.0); // Background color (green)
+ c.glClearDepth(1.0); // Depth Buffer Setup
+ c.glEnable(c.GL_DEPTH_TEST); // Enables Depth Testing
+ c.glDepthFunc(c.GL_LEQUAL); // The Type Of Depth Test To Do
+ c.glHint(c.GL_PERSPECTIVE_CORRECTION_HINT, c.GL_NICEST); // Really Nice Perspective Calculations
+ c.glClear(c.GL_COLOR_BUFFER_BIT | c.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
}
fn init() bool {
_ = c.glfwSetErrorCallback(errorCallback);
if (c.glfwInit() == c.GL_FALSE) {
- warn("Failed to initialize GLFW\n");
+ warn("Failed to initialize GLFW\n", .{});
return false;
}
- c.glfwWindowHint(c.GLFW_SAMPLES, 4); // 4x antialiasing
+ c.glfwWindowHint(c.GLFW_SAMPLES, 4); // 4x antialiasing
- window = c.glfwCreateWindow(width, height, c"Tutorial", null, null) orelse {
- panic("unable to create window\n");
+ window = c.glfwCreateWindow(width, height, "Lesson01", null, null) orelse {
+ panic("unable to create window\n", .{});
};
_ = c.glfwSetKeyCallback(window, keyCallback);
c.glfwMakeContextCurrent(window);
c.glfwSwapInterval(1);
-
init_gl();
return true;
}
@peterhellberg
Copy link
Author

Screenshot 2020-09-11 at 12 05 34

@peterhellberg
Copy link
Author

peterhellberg commented Sep 11, 2020

build.zig

const std = @import("std");
const Builder = std.build.Builder;

pub fn build(b: *Builder) void {
    const mode = b.standardReleaseOptions();
    const exe = b.addExecutable("Lesson01", "src/main.zig");
    exe.setBuildMode(mode);

    exe.addIncludeDir("/usr/local/include");
    switch (std.Target.current.os.tag) {
        .macosx => {
            exe.addFrameworkDir("/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks");
            exe.linkFramework("OpenGL");
        },
        .freebsd => {
            exe.addIncludeDir("/usr/local/include/GL");
            exe.linkSystemLibrary("gl");
            exe.linkSystemLibrary("glu");
        },
        else => {
            @panic("don't know how to build on your system");
        },
    }
    exe.linkSystemLibrary("glfw");

    exe.install();

    const run_cmd = exe.run();
    run_cmd.step.dependOn(b.getInstallStep());

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}

src/main.zig

const std = @import("std");
const warn = std.debug.warn;
const panic = std.debug.panic;
const c = @import("c.zig");

const width: i32 = 1024;
const height: i32 = 768;

var window: *c.GLFWwindow = undefined;

fn errorCallback(err: c_int, description: [*c]const u8) callconv(.C) void {
    panic("Error: {}\n", .{description});
}

fn keyCallback(win: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, mods: c_int) callconv(.C) void {
    if (action != c.GLFW_PRESS) return;

    switch (key) {
        c.GLFW_KEY_ESCAPE => c.glfwSetWindowShouldClose(win, c.GL_TRUE),
        else => {},
    }
}

fn perspectiveGL(fovY: f64, aspect: f64, zNear: f64, zFar: f64) void {
    const fH = std.math.tan(fovY / 360 * std.math.pi) * zNear;
    const fW = fH * aspect;
    c.glFrustum(-fW, fW, -fH, fH, zNear, zFar);
}

fn init_gl() void {
    c.glMatrixMode(c.GL_PROJECTION); // Select The Projection Matrix
    c.glLoadIdentity();

    var aspect_ratio: f32 = @intToFloat(f32, height) / @intToFloat(f32, width);
    perspectiveGL(45.0, (1.0 / aspect_ratio), 0.1, 100.0);

    c.glMatrixMode(c.GL_MODELVIEW);
    c.glShadeModel(c.GL_SMOOTH); // Enables Smooth Shading
    c.glClearColor(0.1, 0.6, 0.2, 1.0); // Background color (green)
    c.glClearDepth(1.0); // Depth Buffer Setup
    c.glEnable(c.GL_DEPTH_TEST); // Enables Depth Testing
    c.glDepthFunc(c.GL_LEQUAL); // The Type Of Depth Test To Do
    c.glHint(c.GL_PERSPECTIVE_CORRECTION_HINT, c.GL_NICEST); // Really Nice Perspective Calculations
    c.glClear(c.GL_COLOR_BUFFER_BIT | c.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
}

fn init() bool {
    _ = c.glfwSetErrorCallback(errorCallback);

    if (c.glfwInit() == c.GL_FALSE) {
        warn("Failed to initialize GLFW\n", .{});
        return false;
    }

    c.glfwWindowHint(c.GLFW_SAMPLES, 4); // 4x antialiasing

    window = c.glfwCreateWindow(width, height, "Lesson01", null, null) orelse {
        panic("unable to create window\n", .{});
    };

    _ = c.glfwSetKeyCallback(window, keyCallback);
    c.glfwMakeContextCurrent(window);
    c.glfwSwapInterval(1);
    init_gl();
    return true;
}

pub fn main() u8 {
    if (!init()) {
        return 1;
    }

    while (c.glfwWindowShouldClose(window) == c.GL_FALSE) {
        // Draw nothing, see you in tutorial 2 !
        // Swap buffers
        c.glfwSwapBuffers(window);

        c.glfwPollEvents();
    }

    return 0;
}

src/c.zig

pub usingnamespace @cImport({
    @cInclude("GLFW/glfw3.h");
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment