Skip to content

Commit

Permalink
QoL api.zig changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JerwuQu committed May 3, 2024
1 parent ad7a01d commit e8dcaa9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
6 changes: 3 additions & 3 deletions docs/introduction/hello.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const cart = @import("cart-api");
export fn update() void {
// Set background to a nice gray
@memset(cart.framebuffer, cart.DisplayColor{
.red = 10,
.green = 20,
.blue = 10,
.r = 10,
.g = 20,
.b = 10,
});
}
42 changes: 21 additions & 21 deletions samples/feature_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,27 @@ export fn update() void {
for (0..cart.screen_height) |y| {
for (0..cart.screen_width) |x| {
cart.framebuffer[y * cart.screen_width + x] = .{
.red = @intFromFloat(@as(f32, @floatFromInt(x)) / cart.screen_width * 31),
.green = green_565,
.blue = @intFromFloat(@as(f32, @floatFromInt(y)) / cart.screen_height * 31),
.r = @intFromFloat(@as(f32, @floatFromInt(x)) / cart.screen_width * 31),
.g = green_565,
.b = @intFromFloat(@as(f32, @floatFromInt(y)) / cart.screen_height * 31),
};
}
}

for (cart.neopixels, 0..) |*np, i| {
np.* = .{
.red = @intFromFloat(@as(f32, @floatFromInt(i)) / 5 * 255),
.green = @intFromFloat(@as(f32, @floatFromInt(cart.light_level.*)) / std.math.maxInt(u12) * 255),
.blue = @intFromFloat(@as(f32, @floatFromInt(i)) / 5 * 255),
.r = @intFromFloat(@as(f32, @floatFromInt(i)) / 5 * 255),
.g = @intFromFloat(@as(f32, @floatFromInt(cart.light_level.*)) / std.math.maxInt(u12) * 255),
.b = @intFromFloat(@as(f32, @floatFromInt(i)) / 5 * 255),
};
}

cart.blit(.{
.sprite = &.{
.{ .red = 31, .green = 0, .blue = 0 },
.{ .red = 0, .green = 0, .blue = 31 },
.{ .red = 31, .green = 0, .blue = 0 },
.{ .red = 0, .green = 0, .blue = 31 },
.{ .r = 31, .g = 0, .b = 0 },
.{ .r = 0, .g = 0, .b = 31 },
.{ .r = 31, .g = 0, .b = 0 },
.{ .r = 0, .g = 0, .b = 31 },
},
.x = 40,
.y = 40,
Expand All @@ -101,54 +101,54 @@ export fn update() void {
.y1 = 50,
.x2 = 70,
.y2 = 70,
.color = .{ .red = 0, .green = 63, .blue = 0 },
.color = .{ .r = 0, .g = 63, .b = 0 },
});

cart.hline(.{
.x = 30,
.y = 30,
.len = 20,
.color = .{ .red = 31, .green = 0, .blue = 0 },
.color = .{ .r = 31, .g = 0, .b = 0 },
});

cart.vline(.{
.x = 30,
.y = 30,
.len = 20,
.color = .{ .red = 31, .green = 0, .blue = 0 },
.color = .{ .r = 31, .g = 0, .b = 0 },
});

cart.oval(.{
.x = 80,
.y = 80,
.width = 10,
.height = 10,
.stroke_color = .{ .red = 0, .green = 0, .blue = 31 },
.fill_color = .{ .red = 31, .green = 0, .blue = 31 },
.stroke_color = .{ .r = 0, .g = 0, .b = 31 },
.fill_color = .{ .r = 31, .g = 0, .b = 31 },
});

cart.rect(.{
.x = 100,
.y = 100,
.width = 10,
.height = 10,
.stroke_color = .{ .red = 31, .green = 31, .blue = 31 },
.fill_color = .{ .red = 0, .green = 63, .blue = 31 },
.stroke_color = .{ .r = 31, .g = 31, .b = 31 },
.fill_color = .{ .r = 0, .g = 63, .b = 31 },
});

cart.text(.{
.str = fbs.getWritten(),
.x = 0,
.y = 0,
.text_color = .{ .red = 0, .green = 0, .blue = 0 },
.background_color = .{ .red = 31, .green = 63, .blue = 31 },
.text_color = .{ .r = 0, .g = 0, .b = 0 },
.background_color = .{ .r = 31, .g = 63, .b = 31 },
});

cart.text(.{
.str = "\x80\x81\x82\x83\x84\x85\x86\x87\x88",
.x = 0,
.y = 120,
.text_color = .{ .red = 0, .green = 0, .blue = 0 },
.background_color = .{ .red = 31, .green = 63, .blue = 31 },
.text_color = .{ .r = 0, .g = 0, .b = 0 },
.background_color = .{ .r = 31, .g = 63, .b = 31 },
});
}
2 changes: 1 addition & 1 deletion samples/zeroman/build/convert_gfx.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn convert(args: ConvertFile, writer: std.fs.File.Writer) !void {

try writer.writeAll(" pub const colors = [_]DisplayColor{\n");
for (colors.items) |c| {
try writer.print(" .{{ .red = {}, .green = {}, .blue = {} }},\n", .{ c.r, c.g, c.b });
try writer.print(" .{{ .r = {}, .g = {}, .b = {} }},\n", .{ c.r, c.g, c.b });
}
try writer.writeAll(" };\n");

Expand Down
37 changes: 19 additions & 18 deletions src/cart/api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ pub const screen_height: u32 = 128;
const base = if (builtin.target.isWasm()) 0 else 0x20000000;

/// RGB888, true color
pub const NeopixelColor = packed struct(u24) { blue: u8, green: u8, red: u8 };
pub const NeopixelColor = packed struct(u24) { b: u8, g: u8, r: u8 };

/// RGB565, high color
pub const DisplayColor = packed struct(u16) {
/// 0-31
blue: u5,
b: u5,
/// 0-63
green: u6,
g: u6,
/// 0-31
red: u5,
r: u5,

const Optional = enum(i32) {
none = -1,
Expand All @@ -40,7 +40,7 @@ pub const DisplayColor = packed struct(u16) {
};
};

pub const Controls = packed struct {
pub const Controls = packed struct(u9) {
/// START button
start: bool,
/// SELECT button
Expand Down Expand Up @@ -81,7 +81,7 @@ const platform_specific = if (builtin.target.isWasm())
extern fn line(color: DisplayColor, x1: i32, y1: i32, x2: i32, y2: i32) void;
extern fn oval(stroke_color: DisplayColor.Optional, fill_color: DisplayColor.Optional, x: i32, y: i32, width: u32, height: u32) void;
extern fn rect(stroke_color: DisplayColor.Optional, fill_color: DisplayColor.Optional, x: i32, y: i32, width: u32, height: u32) void;
extern fn text(text_color: DisplayColor, background_color: DisplayColor.Optional, str_ptr: [*]const u8, str_len: usize, x: i32, y: i32) void;
extern fn text(text_color: DisplayColor.Optional, background_color: DisplayColor.Optional, str_ptr: [*]const u8, str_len: usize, x: i32, y: i32) void;
extern fn vline(color: DisplayColor, x: i32, y: i32, len: u32) void;
extern fn hline(color: DisplayColor, x: i32, y: i32, len: u32) void;
extern fn tone(frequency: u32, duration: u32, volume: u32, flags: ToneOptions.Flags) void;
Expand Down Expand Up @@ -115,12 +115,12 @@ pub const BlitOptions = struct {
width: u32,
height: u32,
/// x within the sprite atlas.
src_x: i32 = 0,
src_x: u32 = 0,
/// y within the sprite atlas.
src_y: i32 = 0,
src_y: u32 = 0,
/// Width of the entire sprite atlas.
stride: ?u32 = null,
flags: Flags,
flags: Flags = .{},
};

/// Copies pixels to the framebuffer.
Expand Down Expand Up @@ -187,8 +187,8 @@ pub const OvalOptions = struct {
y: i32,
width: u32,
height: u32,
stroke_color: ?DisplayColor,
fill_color: ?DisplayColor,
stroke_color: ?DisplayColor = null,
fill_color: ?DisplayColor = null,
};

/// Draws an oval (or circle).
Expand Down Expand Up @@ -219,8 +219,8 @@ pub const RectOptions = struct {
y: i32,
width: u32,
height: u32,
stroke_color: ?DisplayColor,
fill_color: ?DisplayColor,
stroke_color: ?DisplayColor = null,
fill_color: ?DisplayColor = null,
};

/// Draws a rectangle.
Expand Down Expand Up @@ -250,15 +250,15 @@ pub const TextOptions = struct {
str: []const u8,
x: i32,
y: i32,
text_color: DisplayColor,
background_color: ?DisplayColor,
text_color: ?DisplayColor = null,
background_color: ?DisplayColor = null,
};

/// Draws text using the built-in system font.
pub inline fn text(options: TextOptions) void {
if (comptime builtin.target.isWasm()) {
platform_specific.text(
options.text_color,
DisplayColor.Optional.from(options.text_color),
DisplayColor.Optional.from(options.background_color),
options.str.ptr,
options.str.len,
Expand Down Expand Up @@ -353,8 +353,9 @@ pub const ToneOptions = struct {
};

channel: Channel,
duty_cycle: DutyCycle,
panning: Panning,
/// `duty_cycle` is only used when `channel` is set to `pulse1` or `pulse2`
duty_cycle: DutyCycle = .@"1/8",
panning: Panning = .stereo,
padding: u26 = undefined,
};

Expand Down

0 comments on commit e8dcaa9

Please sign in to comment.