diff --git a/src/App.zig b/src/App.zig index 17b74ce04..0e16ea4ce 100644 --- a/src/App.zig +++ b/src/App.zig @@ -34,16 +34,10 @@ pub fn init(alloc: std.mem.Allocator) !App { try glfw.swapInterval(1); // Load OpenGL bindings - const version = gl.c.gladLoadGL(@ptrCast( - fn ([*c]const u8) callconv(.C) ?fn () callconv(.C) void, - glfw.getProcAddress, - )); - if (version == 0) { - return error.OpenGLInitFailed; - } + const version = try gl.glad.load(glfw.getProcAddress); log.info("loaded OpenGL {}.{}", .{ - gl.c.GLAD_VERSION_MAJOR(@intCast(c_uint, version)), - gl.c.GLAD_VERSION_MINOR(@intCast(c_uint, version)), + gl.glad.versionMajor(version), + gl.glad.versionMinor(version), }); // Blending for text diff --git a/src/opengl.zig b/src/opengl.zig index 00e5b8652..5ea8994e8 100644 --- a/src/opengl.zig +++ b/src/opengl.zig @@ -12,9 +12,11 @@ //! comptime help. I'm deferring this until later but have some fun ideas. pub const c = @import("opengl/c.zig"); +pub const glad = @import("opengl/glad.zig"); +pub usingnamespace @import("opengl/draw.zig"); + pub const Buffer = @import("opengl/Buffer.zig"); pub const Program = @import("opengl/Program.zig"); pub const Shader = @import("opengl/Shader.zig"); pub const Texture = @import("opengl/Texture.zig"); pub const VertexArray = @import("opengl/VertexArray.zig"); -pub usingnamespace @import("opengl/draw.zig"); diff --git a/src/opengl/glad.zig b/src/opengl/glad.zig new file mode 100644 index 000000000..d2396f27f --- /dev/null +++ b/src/opengl/glad.zig @@ -0,0 +1,29 @@ +const c = @import("c.zig"); + +/// Initialize Glad. This is guaranteed to succeed if no errors are returned. +/// The getProcAddress param is an anytype so that we can accept multiple +/// forms of the function depending on what we're interfacing with. +pub fn load(getProcAddress: anytype) !c_int { + const res = switch (@TypeOf(getProcAddress)) { + // glfw + fn ([*:0]const u8) callconv(.C) ?fn () callconv(.C) void => c.gladLoadGL(@ptrCast( + fn ([*c]const u8) callconv(.C) ?fn () callconv(.C) void, + getProcAddress, + )), + + // try as-is. If this introduces a compiler error, then add a new case. + else => c.gladLoadGL(getProcAddress), + }; + if (res == 0) return error.GLInitFailed; + return res; +} + +pub fn versionMajor(res: c_int) c_uint { + // The intcast here is due to translate-c weirdness + return c.GLAD_VERSION_MAJOR(@intCast(c_uint, res)); +} + +pub fn versionMinor(res: c_int) c_uint { + // The intcast here is due to translate-c weirdness + return c.GLAD_VERSION_MINOR(@intCast(c_uint, res)); +}