Last active
October 3, 2024 14:37
-
-
Save peterhellberg/e294f28dad648131b811b2da39eea26c to your computer and use it in GitHub Desktop.
Variant of the Olive.c example flag_jp.c π―π΅ in Zig β‘, this time generating the Swedish flag πΈπͺ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
zig-out | |
.zig-cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
pub fn build(b: *std.Build) void { | |
const target = b.standardTargetOptions(.{}); | |
const optimize = b.standardOptimizeOption(.{}); | |
const exe = b.addExecutable(.{ | |
.name = "olive-flag-se", | |
.root_source_file = b.path("main.zig"), | |
.target = target, | |
.optimize = optimize, | |
}); | |
const olive = b.dependency("olive", .{}); | |
exe.addCSourceFile(.{ .file = olive.path("olive.c") }); | |
exe.addIncludePath(olive.path(".")); | |
const stb = b.dependency("stb", .{}); | |
exe.addCSourceFile(.{ .file = b.path("stb_image_write.c") }); | |
exe.addIncludePath(stb.path(".")); | |
exe.linkLibC(); | |
b.installArtifact(exe); | |
const run_cmd = b.addRunArtifact(exe); | |
run_cmd.step.dependOn(b.getInstallStep()); | |
const run_step = b.step("run", "Run the app"); | |
run_step.dependOn(&run_cmd.step); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.{ | |
.name = "olive-flag-se", | |
.version = "0.0.0", | |
.dependencies = .{ | |
.olive = .{ | |
.url = "https://github.com/tsoding/olive.c/archive/refs/heads/master.zip", | |
.hash = "12208a1706b00a5fcdd12775f5b3457ebdd3daea3d49d298894b962058b6b393e2f8", | |
}, | |
.stb = .{ | |
.url = "https://github.com/nothings/stb/archive/refs/heads/master.zip", | |
.hash = "1220aefdc5ff6261afb86675b54f987d9e86c575049b2050ee8f23d49c954ff4970a", | |
}, | |
}, | |
.paths = .{ | |
"build.zig", | |
"build.zig.zon", | |
"src", | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const olive = @import("olive.zig"); | |
const stbi = @import("stbi.zig"); | |
pub fn main() u8 { | |
const WIDTH = 1600; | |
const HEIGHT = 1000; | |
// https://www.norden.org/en/information/swedish-flag | |
const BLUE = 0xFF9B5500; | |
const YELLOW = 0xFF00CEFF; | |
return swedishFlag(WIDTH, HEIGHT, BLUE, YELLOW, "flag_se.png"); | |
} | |
pub fn swedishFlag(comptime w: usize, comptime h: usize, blue: u32, yellow: u32, file_path: [*c]const u8) u8 { | |
var pixels: [w * h]u32 = std.mem.zeroes([w * h]u32); | |
const oc: olive.Canvas = olive.canvas(pixels[0..], w, h, w); | |
const SIZE = h / 10; | |
olive.fill(oc, blue); | |
olive.rect(oc, 5 * SIZE, 0, 2 * SIZE, h, yellow); | |
olive.rect(oc, 0, 4 * SIZE, w, 2 * SIZE, yellow); | |
const stride_bytes = @sizeOf(u32) * w; | |
if (stbi.write_png(file_path, w, h, 4, pixels[0..], stride_bytes) == 0) { | |
_ = stbi.c.fprintf(stbi.c.stderr, "ERROR: could not write %s\n", file_path); | |
return 1; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const c = @cImport({ | |
@cDefine("OLIVEC_IMPLEMENTATION", ""); | |
@cInclude("olive.c"); | |
}); | |
pub const Canvas = c.Olivec_Canvas; | |
pub const canvas = c.olivec_canvas; | |
pub const fill = c.olivec_fill; | |
pub const rect = c.olivec_rect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define STB_IMAGE_WRITE_IMPLEMENTATION | |
#include "stb_image_write.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub const c = @cImport({ | |
@cDefine("STB_IMAGE_WRITE_IMPLEMENTATION", ""); | |
@cInclude("stb_image_write.h"); | |
}); | |
pub const write_png = c.stbi_write_png; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by this example: https://github.com/tsoding/olive.c?tab=readme-ov-file#quick-example-flag-of-japan