Skip to content

Commit 87245f0

Browse files
committed
files from monorepo @ bde42aaeec763c1bd8781d26285b0f4dc65b1a4c
0 parents  commit 87245f0

11 files changed

+4805
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Ignore some special directories
2+
.zig-cache
3+
zig-out
4+
5+
# Ignore some special OS files
6+
*.DS_Store

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Michal Ziulek
4+
Copyright (c) 2024 zig-gamedev contributors
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [zsdl](https://github.com/zig-gamedev/zsdl)
2+
3+
Zig bindings for SDL libs.
4+
5+
## Getting started
6+
7+
Example `build.zig`:
8+
9+
```zig
10+
pub fn build(b: *std.Build) !void {
11+
12+
const exe = b.addExecutable(.{ ... });
13+
exe.linkLibC();
14+
15+
const zsdl = b.dependency("zsdl", .{});
16+
exe.root_module.addImport("zsdl2", zsdl.module("zsdl2"));
17+
18+
@import("zsdl").link_SDL2(exe);
19+
20+
// Optionally use prebuilt libs instead of relying on system installed SDL...
21+
22+
@import("zsdl").prebuilt.addLibraryPathsTo(exe);
23+
24+
if (@import("zsdl").prebuilt.install_SDL2(b, target.result, .bin)) |install_sdl2_step| {
25+
b.getInstallStep().dependOn(install_sdl2_step);
26+
}
27+
}
28+
```
29+
30+
NOTE: If you want to use our prebuilt libraries also add the following to your `build.zig.zon`:
31+
```zig
32+
.@"sdl2-prebuilt-macos" = .{
33+
.url = "https://github.com/zig-gamedev/sdl2-prebuilt-macos/archive/f14773fa3de719b3a399b854c31eb4139d63842f.tar.gz",
34+
.hash = "12205cb2da6fb4a7fcf28b9cd27b60aaf12f4d4a55be0260b1ae36eaf93ca5a99f03",
35+
.lazy = true,
36+
},
37+
.@"sdl2-prebuilt-x86_64-windows-gnu" = .{
38+
.url = "https://github.com/zig-gamedev/sdl2-prebuilt-x86_64-windows-gnu/archive/8143e2a5c28dbace399cbff14c3e8749a1afd418.tar.gz",
39+
.hash = "1220ade6b84d06d73bf83cef22c73ec4abc21a6d50b9f48875f348b7942c80dde11b",
40+
.lazy = true,
41+
},
42+
```
43+
44+
Now in your code you may import and use `zsdl2`:
45+
46+
```zig
47+
const std = @import("std");
48+
const sdl = @import("zsdl2");
49+
50+
pub fn main() !void {
51+
...
52+
try sdl.init(.{ .audio = true, .video = true });
53+
defer sdl.quit();
54+
55+
const window = try sdl.Window.create(
56+
"zig-gamedev-window",
57+
sdl.Window.pos_undefined,
58+
sdl.Window.pos_undefined,
59+
600,
60+
600,
61+
.{ .opengl = true, .allow_highdpi = true },
62+
);
63+
defer window.destroy();
64+
...
65+
}
66+
```

build.zig

+280
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
const builtin = @import("builtin");
2+
const std = @import("std");
3+
const assert = std.debug.assert;
4+
5+
pub fn build(b: *std.Build) void {
6+
const zsdl2_module = b.addModule("zsdl2", .{
7+
.root_source_file = b.path("src/sdl2.zig"),
8+
});
9+
10+
_ = b.addModule("zsdl2_ttf", .{
11+
.root_source_file = b.path("src/sdl2_ttf.zig"),
12+
.imports = &.{
13+
.{ .name = "zsdl2", .module = zsdl2_module },
14+
},
15+
});
16+
17+
_ = b.addModule("zsdl2_image", .{
18+
.root_source_file = b.path("src/sdl2_image.zig"),
19+
.imports = &.{
20+
.{ .name = "zsdl2", .module = zsdl2_module },
21+
},
22+
});
23+
24+
_ = b.addModule("zsdl3", .{
25+
.root_source_file = b.path("src/sdl3.zig"),
26+
});
27+
}
28+
29+
pub fn link_SDL2(compile_step: *std.Build.Step.Compile) void {
30+
switch (compile_step.rootModuleTarget().os.tag) {
31+
.windows => {
32+
compile_step.linkSystemLibrary("SDL2");
33+
compile_step.linkSystemLibrary("SDL2main");
34+
},
35+
.linux => {
36+
compile_step.linkSystemLibrary("SDL2");
37+
compile_step.root_module.addRPathSpecial("$ORIGIN");
38+
},
39+
.macos => {
40+
compile_step.linkFramework("SDL2");
41+
compile_step.root_module.addRPathSpecial("@executable_path");
42+
},
43+
else => {},
44+
}
45+
}
46+
47+
pub fn link_SDL2_ttf(compile_step: *std.Build.Step.Compile) void {
48+
switch (compile_step.rootModuleTarget().os.tag) {
49+
.windows => {
50+
compile_step.linkSystemLibrary("SDL2_ttf");
51+
},
52+
.linux => {
53+
compile_step.linkSystemLibrary("SDL2_ttf");
54+
compile_step.root_module.addRPathSpecial("$ORIGIN");
55+
},
56+
.macos => {
57+
compile_step.linkFramework("SDL2_ttf");
58+
compile_step.root_module.addRPathSpecial("@executable_path");
59+
},
60+
else => {},
61+
}
62+
}
63+
64+
pub fn link_SDL2_image(compile_step: *std.Build.Step.Compile) void {
65+
switch (compile_step.rootModuleTarget().os.tag) {
66+
.windows => {
67+
compile_step.linkSystemLibrary("SDL2_image");
68+
},
69+
.linux => {
70+
compile_step.linkSystemLibrary("SDL2_image");
71+
compile_step.root_module.addRPathSpecial("$ORIGIN");
72+
},
73+
.macos => {
74+
compile_step.linkFramework("SDL2_image");
75+
compile_step.root_module.addRPathSpecial("@executable_path");
76+
},
77+
else => {},
78+
}
79+
}
80+
81+
pub fn link_SDL3(compile_step: *std.Build.Step.Compile) void {
82+
switch (compile_step.rootModuleTarget().os.tag) {
83+
.windows => {
84+
compile_step.linkSystemLibrary("SDL3");
85+
},
86+
.linux => {
87+
compile_step.linkSystemLibrary("SDL3");
88+
compile_step.root_module.addRPathSpecial("$ORIGIN");
89+
},
90+
.macos => {
91+
compile_step.linkFramework("SDL3");
92+
compile_step.root_module.addRPathSpecial("@executable_path");
93+
},
94+
else => {},
95+
}
96+
}
97+
98+
pub fn testVersionCheckSDL2(b: *std.Build, target: std.Build.ResolvedTarget) *std.Build.Step {
99+
const test_sdl2_version_check = b.addTest(.{
100+
.name = "sdl2-version-check",
101+
.root_source_file = b.dependency("zsdl", .{}).path("src/sdl2_version_check.zig"),
102+
.target = target,
103+
.optimize = .ReleaseSafe,
104+
});
105+
106+
link_SDL2(test_sdl2_version_check);
107+
108+
prebuilt.addLibraryPathsTo(test_sdl2_version_check);
109+
110+
const version_check_run = b.addRunArtifact(test_sdl2_version_check);
111+
112+
if (target.result.os.tag == .windows) {
113+
version_check_run.setCwd(.{
114+
.cwd_relative = b.getInstallPath(.bin, ""),
115+
});
116+
}
117+
118+
version_check_run.step.dependOn(&test_sdl2_version_check.step);
119+
120+
if (prebuilt.install_SDL2(b, target.result, .bin)) |install_sdl2_step| {
121+
version_check_run.step.dependOn(install_sdl2_step);
122+
}
123+
124+
return &version_check_run.step;
125+
}
126+
127+
pub const prebuilt = struct {
128+
pub fn addLibraryPathsTo(compile_step: *std.Build.Step.Compile) void {
129+
const b = compile_step.step.owner;
130+
const target = compile_step.rootModuleTarget();
131+
switch (target.os.tag) {
132+
.windows => {
133+
if (target.cpu.arch.isX86()) {
134+
if (b.lazyDependency("sdl2-prebuilt-x86_64-windows-gnu", .{})) |sdl2_prebuilt| {
135+
compile_step.addLibraryPath(sdl2_prebuilt.path("lib"));
136+
}
137+
}
138+
},
139+
.linux => {
140+
if (target.cpu.arch.isX86()) {
141+
if (b.lazyDependency("sdl2-prebuilt-x86_64-linux-gnu", .{})) |sdl2_prebuilt| {
142+
compile_step.addLibraryPath(sdl2_prebuilt.path("lib"));
143+
}
144+
}
145+
},
146+
.macos => {
147+
if (b.lazyDependency("sdl2-prebuilt-macos", .{})) |sdl2_prebuilt| {
148+
compile_step.addFrameworkPath(sdl2_prebuilt.path("Frameworks"));
149+
}
150+
},
151+
else => {},
152+
}
153+
}
154+
155+
pub fn install_SDL2(
156+
b: *std.Build,
157+
target: std.Target,
158+
install_dir: std.Build.InstallDir,
159+
) ?*std.Build.Step {
160+
switch (target.os.tag) {
161+
.windows => {
162+
if (target.cpu.arch.isX86()) {
163+
if (b.lazyDependency("sdl2-prebuilt-x86_64-windows-gnu", .{})) |sdl2_prebuilt| {
164+
return &b.addInstallFileWithDir(
165+
sdl2_prebuilt.path("bin/SDL2.dll"),
166+
install_dir,
167+
"SDL2.dll",
168+
).step;
169+
}
170+
}
171+
},
172+
.linux => {
173+
if (target.cpu.arch.isX86()) {
174+
if (b.lazyDependency("sdl2-prebuilt-x86_64-linux-gnu", .{})) |sdl2_prebuilt| {
175+
return &b.addInstallFileWithDir(
176+
sdl2_prebuilt.path("lib/libSDL2.so"),
177+
install_dir,
178+
"libSDL2.so",
179+
).step;
180+
}
181+
}
182+
},
183+
.macos => {
184+
if (b.lazyDependency("sdl2-prebuilt-macos", .{})) |sdl2_prebuilt| {
185+
return &b.addInstallDirectory(.{
186+
.source_dir = sdl2_prebuilt.path("Frameworks/SDL2.framework"),
187+
.install_dir = install_dir,
188+
.install_subdir = "SDL2.framework",
189+
}).step;
190+
}
191+
},
192+
else => {},
193+
}
194+
return null;
195+
}
196+
197+
pub fn install_SDL2_ttf(
198+
b: *std.Build,
199+
target: std.Target,
200+
install_dir: std.Build.InstallDir,
201+
) ?*std.Build.Step {
202+
switch (target.os.tag) {
203+
.windows => {
204+
if (target.cpu.arch.isX86()) {
205+
if (b.lazyDependency("sdl2-prebuilt-x86_64-windows-gnu", .{})) |sdl2_prebuilt| {
206+
return &b.addInstallFileWithDir(
207+
sdl2_prebuilt.path("bin/SDL2_ttf.dll"),
208+
install_dir,
209+
"SDL2_ttf.dll",
210+
).step;
211+
}
212+
}
213+
},
214+
.linux => {
215+
if (target.cpu.arch.isX86()) {
216+
if (b.lazyDependency("sdl2-prebuilt-x86_64-linux-gnu", .{})) |sdl2_prebuilt| {
217+
return &b.addInstallFileWithDir(
218+
sdl2_prebuilt.path("lib/libSDL2_ttf.so"),
219+
install_dir,
220+
"libSDL2_ttf.so",
221+
).step;
222+
}
223+
}
224+
},
225+
.macos => {
226+
if (b.lazyDependency("sdl2-prebuilt-macos", .{})) |sdl2_prebuilt| {
227+
return &b.addInstallDirectory(.{
228+
.source_dir = sdl2_prebuilt.path("Frameworks/SDL2_ttf.framework"),
229+
.install_dir = install_dir,
230+
.install_subdir = "SDL2_ttf.framework",
231+
}).step;
232+
}
233+
},
234+
else => {},
235+
}
236+
return null;
237+
}
238+
239+
pub fn install_SDL2_image(
240+
b: *std.Build,
241+
target: std.Target,
242+
install_dir: std.Build.InstallDir,
243+
) ?*std.Build.Step {
244+
switch (target.os.tag) {
245+
.windows => {
246+
if (target.cpu.arch.isX86()) {
247+
if (b.lazyDependency("sdl2-prebuilt-x86_64-windows-gnu", .{})) |sdl2_prebuilt| {
248+
return &b.addInstallFileWithDir(
249+
sdl2_prebuilt.path("bin/SDL2_image.dll"),
250+
install_dir,
251+
"SDL2_image.dll",
252+
).step;
253+
}
254+
}
255+
},
256+
.linux => {
257+
if (target.cpu.arch.isX86()) {
258+
if (b.lazyDependency("sdl2-prebuilt-x86_64-linux-gnu", .{})) |sdl2_prebuilt| {
259+
return &b.addInstallFileWithDir(
260+
sdl2_prebuilt.path("lib/libSDL2_image.so"),
261+
install_dir,
262+
"libSDL2_image.so",
263+
).step;
264+
}
265+
}
266+
},
267+
.macos => {
268+
if (b.lazyDependency("sdl2-prebuilt-macos", .{})) |sdl2_prebuilt| {
269+
return &b.addInstallDirectory(.{
270+
.source_dir = sdl2_prebuilt.path("Frameworks/SDL2_image.framework"),
271+
.install_dir = install_dir,
272+
.install_subdir = "SDL2_image.framework",
273+
}).step;
274+
}
275+
},
276+
else => {},
277+
}
278+
return null;
279+
}
280+
};

build.zig.zon

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.{
2+
.name = "zsdl",
3+
.version = "0.4.0-dev",
4+
.paths = .{
5+
"build.zig",
6+
"build.zig.zon",
7+
"src",
8+
"README.md",
9+
"LICENSE",
10+
},
11+
}

0 commit comments

Comments
 (0)