scratch/2021-03-16/zig8/src/display.zig

55 lines
1.2 KiB
Zig

const DISPLAY_WIDTH: usize = 64;
const DISPLAY_HEIGHT: usize = 32;
const std = @import("std");
pub const Point = struct {
x: u16,
y: u16,
};
pub const Display = struct {
buf: [DISPLAY_HEIGHT * DISPLAY_WIDTH]u8,
pub fn new() Display {
return .{
.buf = [_]u8{0x00} ** (DISPLAY_HEIGHT * DISPLAY_WIDTH)
};
}
};
pub fn clear(disp: *Display) void {
disp.buf = u8[DISPLAY_WIDTH * DISPLAY_HEIGHT]{0x00};
}
pub fn draw_sprite(disp: *Display, pos: *const Point, data: *const []u8) bool {
var set_vf: bool = false;
for (data.*) |byte, y_offset| {
var offset_count: u8 = 0;
while (offset_count < 8) : (offset_count += 1) {
const x_bit_offset = @intCast(u3, offset_count);
const x = @intCast(u8, pos.x + (7 - x_bit_offset));
const y = @intCast(u8, pos.y + y_offset);
const temp = (byte >> x_bit_offset) & 0x01;
const bit = @intCast(u1, temp);
const i = DISPLAY_WIDTH * y + x;
if (i >= disp.buf.len) break;
if (bit == 0x1 and disp.buf[i] == 0x01) {
set_vf = true;
}
disp.buf[i] ^= bit;
}
}
return set_vf;
}