opengl: glad helpers

This commit is contained in:
Mitchell Hashimoto
2022-04-04 15:10:22 -07:00
parent 8797c41833
commit 530fecee4a
3 changed files with 35 additions and 10 deletions

View File

@ -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

View File

@ -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");

29
src/opengl/glad.zig Normal file
View File

@ -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));
}