Files
ghostty/pkg/opengl/glad.zig
Mitchell Hashimoto eccd07f009 pkg: replace @cImport with addTranslateC in pkg/
@cImport is going to disappear in Zig 0.17. Its deprecated in Zig 0.16.
Let's remove it now.

Replace @cImport with addTranslateC across pkg/ packages. Each
package now has a c_import.h header that is translated at build
time via addTranslateC and exposed as a "cimport" module import.

Converted packages:
- dcimgui
- fontconfig
- freetype
- glslang
- harfbuzz
- macos
- oniguruma
- opengl
- sentry
- spirv-cross
- wuffs

Omitted:
- gtk4-layer-shell - This has a bit more complexity with how it
  interacts with GTK headers, so I need to consider this a bit more.
- src/ - It'll be cleaner to do this separately.
2026-04-16 21:35:51 -07:00

46 lines
1.5 KiB
Zig

const std = @import("std");
const c = @import("c");
pub const Context = c.GladGLContext;
/// This is the current context. Set this var manually prior to calling
/// any of this package's functions. I know its nasty to have a global but
/// this makes it match OpenGL API styles where it also operates on a
/// threadlocal global.
pub threadlocal var context: Context = undefined;
/// 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 GlProc = *const fn () callconv(.c) void;
const GlfwFn = *const fn ([*:0]const u8) callconv(.c) ?GlProc;
const res = switch (@TypeOf(getProcAddress)) {
// glfw
GlfwFn => c.gladLoadGLContext(&context, @ptrCast(getProcAddress)),
// null proc address means that we are just loading the globally
// pointed gl functions
@TypeOf(null) => c.gladLoaderLoadGLContext(&context),
// try as-is. If this introduces a compiler error, then add a new case.
else => c.gladLoadGLContext(&context, @ptrCast(getProcAddress)),
};
if (res == 0) return error.GLInitFailed;
return res;
}
pub fn unload() void {
c.gladLoaderUnloadGLContext(&context);
context = undefined;
}
pub fn versionMajor(res: c_uint) c_uint {
return c.GLAD_VERSION_MAJOR(res);
}
pub fn versionMinor(res: c_uint) c_uint {
return c.GLAD_VERSION_MINOR(res);
}