Skip to content

Commit 1de83ed

Browse files
committed
Fixing emscripten builds
1 parent cac4d15 commit 1de83ed

13 files changed

Lines changed: 13201 additions & 5 deletions

File tree

3rdparty/zstbi/.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.cpp text
2+
*.h text
3+
*.zig text
4+
*.txt text
5+
* text eol=lf

3rdparty/zstbi/.gitignore

Lines changed: 6 additions & 0 deletions
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

3rdparty/zstbi/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2021 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.

3rdparty/zstbi/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# [zstbi](https://github.com/zig-gamedev/zstbi)
2+
3+
Zig bindings and build package for stb_image, stb_image_resize and stb_image_write from [Sean Barrett's stb single-file C libraries](https://github.com/nothings/stb)
4+
5+
## Features
6+
7+
* Supports Zig memory allocators
8+
* Supports decoding most popular formats
9+
* Supports HDR images
10+
* Supports 8-bits and 16-bits per channel
11+
* Supports image resizing
12+
* Supports image writing (.png, .jpg)
13+
14+
## Getting started
15+
16+
Add `zstbi` to your `build.zig.zon` .dependencies with:
17+
18+
```
19+
zig fetch --save git+https://github.com/zig-gamedev/zstbi
20+
```
21+
22+
and in your `build.zig` add:
23+
24+
```zig
25+
pub fn build(b: *std.Build) void {
26+
const exe = b.addExecutable(.{ ... });
27+
28+
const zstbi = b.dependency("zstbi", .{});
29+
exe.root_module.addImport("zstbi", zstbi.module("root"));
30+
}
31+
```
32+
Now in your code you may import and use `zstbi`.
33+
34+
Init the lib. `zstbi.init()` is cheap and you may call it whenever you need to change memory allocator. Must be called from the main thread.
35+
```zig
36+
const zstbi = @import("zstbi");
37+
38+
zstbi.init(allocator);
39+
defer zstbi.deinit();
40+
```
41+
```zig
42+
pub const Image = struct {
43+
data: []u8,
44+
width: u32,
45+
height: u32,
46+
num_components: u32,
47+
bytes_per_component: u32,
48+
bytes_per_row: u32,
49+
is_hdr: bool,
50+
...
51+
```
52+
```zig
53+
pub fn loadFromFile(pathname: [:0]const u8, forced_num_components: u32) !Image
54+
55+
pub fn loadFromMemory(data: []const u8, forced_num_components: u32) !Image
56+
57+
pub fn createEmpty(width: u32, height: u32, num_components: u32, args: struct {
58+
bytes_per_component: u32 = 0,
59+
bytes_per_row: u32 = 0,
60+
}) !Image
61+
62+
pub fn info(pathname: [:0]const u8) struct {
63+
is_supported: bool,
64+
width: u32,
65+
height: u32,
66+
num_components: u32,
67+
}
68+
69+
pub fn resize(image: *const Image, new_width: u32, new_height: u32) Image
70+
71+
pub fn writeToFile(
72+
image: *const Image,
73+
filename: [:0]const u8,
74+
image_format: ImageWriteFormat,
75+
) ImageWriteError!void
76+
77+
pub fn writeToFn(
78+
image: *const Image,
79+
write_fn: *const fn (ctx: ?*anyopaque, data: ?*anyopaque, size: c_int) callconv(.C) void,
80+
context: ?*anyopaque,
81+
image_format: ImageWriteFormat,
82+
) ImageWriteError!void
83+
```
84+
```zig
85+
var image = try zstbi.Image.loadFromFile("data/image.png", forced_num_components);
86+
defer image.deinit();
87+
88+
const new_resized_image = image.resize(1024, 1024);
89+
```
90+
Misc functions:
91+
```zig
92+
pub fn isHdr(filename: [:0]const u8) bool
93+
pub fn is16bit(filename: [:0]const u8) bool
94+
95+
pub fn setFlipVerticallyOnLoad(should_flip: bool) void
96+
```

3rdparty/zstbi/build.zig

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const optimize = b.standardOptimizeOption(.{});
5+
const target = b.standardTargetOptions(.{});
6+
7+
const zstbi = b.addModule("root", .{
8+
.root_source_file = b.path("src/zstbi.zig"),
9+
});
10+
11+
zstbi.addIncludePath(b.path("libs/stbi"));
12+
if (optimize == .Debug) {
13+
// TODO: Workaround for Zig bug.
14+
zstbi.addCSourceFile(.{
15+
.file = b.path("src/zstbi.c"),
16+
.flags = &.{
17+
"-std=c99",
18+
"-fno-sanitize=undefined",
19+
"-g",
20+
"-O0",
21+
},
22+
});
23+
} else {
24+
zstbi.addCSourceFile(.{
25+
.file = b.path("src/zstbi.c"),
26+
.flags = &.{
27+
"-std=c99",
28+
"-fno-sanitize=undefined",
29+
},
30+
});
31+
}
32+
33+
// [DelveFramework] Fixing emscripten build bug
34+
// if (target.result.os.tag != .emscripten) {
35+
// zstbi.addIncludePath(.{
36+
// .cwd_relative = b.pathJoin(&.{ b.sysroot.?, "/include" }),
37+
// });
38+
// } else {
39+
// zstbi.link_libc = true;
40+
// }
41+
zstbi.link_libc = true;
42+
43+
const test_step = b.step("test", "Run zstbi tests");
44+
45+
const tests = b.addTest(.{
46+
.name = "zstbi-tests",
47+
.root_module = b.createModule(.{
48+
.root_source_file = b.path("src/zstbi.zig"),
49+
.target = target,
50+
.optimize = optimize,
51+
}),
52+
});
53+
tests.root_module.addImport("zstbi", zstbi);
54+
b.installArtifact(tests);
55+
56+
test_step.dependOn(&b.addRunArtifact(tests).step);
57+
}

3rdparty/zstbi/build.zig.zon

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.{
2+
.name = .zstbi,
3+
.fingerprint = 0x3c971054ff1a412f,
4+
.version = "0.11.0-dev",
5+
.minimum_zig_version = "0.15.1",
6+
.paths = .{
7+
"build.zig",
8+
"build.zig.zon",
9+
"libs",
10+
"src",
11+
"README.md",
12+
},
13+
}

0 commit comments

Comments
 (0)