Skip to content

Commit f4b5622

Browse files
authored
feat(zig): add opengl_version option (#3979)
Added `opengl_version` option to `src/build.zig`.
1 parent 7a1cad3 commit f4b5622

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/build.zig

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
8888
try c_source_files.append("rtextures.c");
8989
}
9090

91+
if (options.opengl_version != .auto) {
92+
raylib.defineCMacro(options.opengl_version.toCMacroStr(), null);
93+
}
94+
9195
switch (target.result.os.tag) {
9296
.windows => {
9397
try c_source_files.append("rglfw.c");
@@ -134,7 +138,11 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
134138

135139
raylib.defineCMacro("PLATFORM_DESKTOP", null);
136140
} else {
137-
raylib.linkSystemLibrary("GLESv2");
141+
if (options.opengl_version == .auto) {
142+
raylib.linkSystemLibrary("GLESv2");
143+
raylib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null);
144+
}
145+
138146
raylib.linkSystemLibrary("EGL");
139147
raylib.linkSystemLibrary("drm");
140148
raylib.linkSystemLibrary("gbm");
@@ -145,7 +153,6 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
145153
raylib.addIncludePath(.{ .cwd_relative = "/usr/include/libdrm" });
146154

147155
raylib.defineCMacro("PLATFORM_DRM", null);
148-
raylib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null);
149156
raylib.defineCMacro("EGL_NO_X11", null);
150157
raylib.defineCMacro("DEFAULT_BATCH_BUFFER_ELEMENT", "2048");
151158
}
@@ -183,7 +190,9 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
183190
},
184191
.emscripten => {
185192
raylib.defineCMacro("PLATFORM_WEB", null);
186-
raylib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null);
193+
if (options.opengl_version == .auto) {
194+
raylib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null);
195+
}
187196

188197
if (b.sysroot == null) {
189198
@panic("Pass '--sysroot \"$EMSDK/upstream/emscripten\"'");
@@ -239,11 +248,34 @@ pub const Options = struct {
239248
platform_drm: bool = false,
240249
shared: bool = false,
241250
linux_display_backend: LinuxDisplayBackend = .X11,
251+
opengl_version: OpenglVersion = .auto,
242252

243253
raylib_dependency_name: []const u8 = "raylib",
244254
raygui_dependency_name: []const u8 = "raygui",
245255
};
246256

257+
pub const OpenglVersion = enum {
258+
auto,
259+
gl_1_1,
260+
gl_2_1,
261+
gl_3_3,
262+
gl_4_3,
263+
gles_2,
264+
gles_3,
265+
266+
pub fn toCMacroStr(self: @This()) []const u8 {
267+
switch (self) {
268+
.auto => @panic("OpenglVersion.auto cannot be turned into a C macro string"),
269+
.gl_1_1 => return "GRAPHICS_API_OPENGL_11",
270+
.gl_2_1 => return "GRAPHICS_API_OPENGL_21",
271+
.gl_3_3 => return "GRAPHICS_API_OPENGL_33",
272+
.gl_4_3 => return "GRAPHICS_API_OPENGL_43",
273+
.gles_2 => return "GRAPHICS_API_OPENGL_ES2",
274+
.gles_3 => return "GRAPHICS_API_OPENGL_ES3",
275+
}
276+
}
277+
};
278+
247279
pub const LinuxDisplayBackend = enum {
248280
X11,
249281
Wayland,
@@ -270,6 +302,7 @@ pub fn build(b: *std.Build) !void {
270302
.rshapes = b.option(bool, "rshapes", "Compile with shapes support") orelse defaults.rshapes,
271303
.shared = b.option(bool, "shared", "Compile as shared library") orelse defaults.shared,
272304
.linux_display_backend = b.option(LinuxDisplayBackend, "linux_display_backend", "Linux display backend to use") orelse defaults.linux_display_backend,
305+
.opengl_version = b.option(OpenglVersion, "opengl_version", "OpenGL version to use") orelse defaults.opengl_version,
273306
};
274307

275308
const lib = try compileRaylib(b, target, optimize, options);

0 commit comments

Comments
 (0)