Skip to content

Commit

Permalink
Drops stb_image for zigimg.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikskuh committed May 24, 2021
1 parent f68ea79 commit b9a3db8
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "vendor/stb"]
path = vendor/stb
url = https://github.com/nothings/stb
[submodule "vendor/zigimg"]
path = vendor/zigimg
url = https://github.com/zigimg/zigimg
12 changes: 10 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ fn initApp(app: *std.build.LibExeObjStep) void {
"-fno-sanitize=undefined",
};

app.addCSourceFile("src/rendering/stb_image.c", &cflags);
app.addCSourceFile("src/rendering/stb_truetype.c", &cflags);
app.addIncludeDir("vendor/stb");
}
Expand All @@ -42,9 +41,17 @@ pub fn build(b: *std.build.Builder) !void {

const root_src = "examples/demo-application.zig";

const zigimg = std.build.Pkg{
.name = "zigimg",
.path = "vendor/zigimg/zigimg.zig",
};

var zero_graphics = std.build.Pkg{
.name = "zero-graphics",
.path = "src/zero-graphics.zig",
.dependencies = &[_]std.build.Pkg{
zigimg,
},
};

if (backend == .android) {
Expand All @@ -63,7 +70,7 @@ pub fn build(b: *std.build.Builder) !void {
);

zero_graphics.dependencies = &[_]std.build.Pkg{
sdk.android_package,
zigimg, sdk.android_package,
};

const make_keystore = sdk.initKeystore(key_store, .{});
Expand Down Expand Up @@ -135,6 +142,7 @@ pub fn build(b: *std.build.Builder) !void {
app.setTarget(std.zig.CrossTarget{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
.abi = .musl,
});
app.override_dest_dir = .{ .Custom = "../www" };
app.single_threaded = true;
Expand Down
78 changes: 58 additions & 20 deletions src/rendering/Renderer2D.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ const gles = @import("../gl_es_2v0.zig");

const c = @cImport({
@cInclude("stb_truetype.h");
@cInclude("stb_image.h");
// @cInclude("spng.h");
});

const zigimg = @import("zigimg");

const Self = @This();

const TextureList = std.TailQueue(Texture);
Expand Down Expand Up @@ -199,29 +201,65 @@ pub fn createTexture(self: *Self, width: u15, height: u15, initial_data: ?[]cons
return &tex_node.data;
}

// fn spngWrapper(error_code: c_int) !void {
// if (error_code == 0)
// return;
// std.log.err("spng failure: {s}", .{std.mem.span(c.spng_strerror(error_code))});
// return LoadTextureError.InvalidImageData;
// }

/// Loads a texture from the given `image_data`. It should contain the file data as it would
/// be on disk, for example encoded as JPEG, PNG, BMP or TGA. Other file formats might be supported,
/// but only those four have official support.
/// be on disk, encoded as PNG. Other file formats might be supported,
/// but only PNG has official support.
pub fn loadTexture(self: *Self, image_data: []const u8) LoadTextureError!*const Texture {
var c_width: c_int = undefined;
var c_height: c_int = undefined;
const image_buffer = c.stbi_load_from_memory(
image_data.ptr,
@intCast(c_int, image_data.len),
&c_width,
&c_height,
null,
4,
) orelse return error.InvalidImageData;
defer c.free(image_buffer);

const width = @intCast(u15, c_width);
const height = @intCast(u15, c_height);
// const ctx = c.spng_ctx_new(0) orelse return error.OutOfMemory;
// defer c.spng_ctx_free(ctx);

// const image_format = c.SPNG_FMT_RGBA8;

// try spngWrapper(c.spng_set_image_limits(ctx, std.math.maxInt(u15), std.math.maxInt(u15)));
// try spngWrapper(c.spng_set_png_buffer(ctx, image_data.ptr, image_data.len));

// var ihdr = std.mem.zeroes(c.spng_ihdr);
// try spngWrapper(c.spng_get_ihdr(ctx, &ihdr));

// var image_size: usize = undefined;
// try spngWrapper(c.spng_decoded_image_size(ctx, image_format, &image_size));

// const buffer = try self.allocator.alloc(u8, image_size);
// defer self.allocator.free(buffer);

// try spngWrapper(c.spng_decode_image(ctx, buffer.ptr, buffer.len, image_format, 0));

// std.log.info("size: {} {}, {}", .{ ihdr.width, ihdr.height, image_size });

// return try self.createTexture(
// @intCast(u15, ihdr.width),
// @intCast(u15, ihdr.height),
// buffer[0 .. 4 * @as(usize, ihdr.width) * @as(usize, ihdr.height)],
// );
var image = zigimg.image.Image.fromMemory(self.allocator, image_data) catch return LoadTextureError.InvalidImageData;
defer image.deinit();

var buffer = try self.allocator.alloc(u8, 4 * image.width * image.height);
defer self.allocator.free(buffer);

var i: usize = 0;
var pixels = image.iterator();
while (pixels.next()) |pix| {
const p8 = pix.toIntegerColor8();
buffer[4 * i + 0] = p8.R;
buffer[4 * i + 1] = p8.G;
buffer[4 * i + 2] = p8.B;
buffer[4 * i + 3] = p8.A;
i += 1;
}
std.debug.assert(i == image.width * image.height);

return try self.createTexture(
width,
height,
image_buffer[0 .. 4 * @as(usize, width) * height],
@intCast(u15, image.width),
@intCast(u15, image.height),
buffer,
);
}

Expand Down
20 changes: 0 additions & 20 deletions src/rendering/stb_image.c

This file was deleted.

2 changes: 1 addition & 1 deletion vendor/ZigAndroidTemplate
1 change: 1 addition & 0 deletions vendor/zigimg
Submodule zigimg added at d06bfe

0 comments on commit b9a3db8

Please sign in to comment.