Fix bug where some tiles were not showing up

Turns out I was shifting a u7 thinking it was u8, and this caused the
high bit to be chopped off of some values. I also made the high bit
indicate the type instead of the low bit.
master
Louis Pearson 2022-08-05 15:23:22 -06:00
parent 83bd4d9aec
commit 3c0f4580b5
1 changed files with 6 additions and 6 deletions

View File

@ -26,19 +26,19 @@ pub const TileData = union(enum) {
pub fn toByte(data: TileData) u8 { pub fn toByte(data: TileData) u8 {
switch (data) { switch (data) {
.tile => |int| return 0b0000_0001 | (int << 1), .tile => |int| return 0b1000_0000 | @intCast(u8, int),
.flags => |flags| return (@intCast(u7, @boolToInt(flags.solid)) << 1) | (@intCast(u7, flags.circuit) << 2), .flags => |flags| return (@intCast(u7, @boolToInt(flags.solid))) | (@intCast(u7, flags.circuit) << 1),
} }
} }
pub fn fromByte(byte: u8) TileData { pub fn fromByte(byte: u8) TileData {
const is_tile = (0b0000_0001 & byte) > 0; const is_tile = (0b1000_0000 & byte) > 0;
if (is_tile) { if (is_tile) {
const tile = @intCast(u7, (0b1111_1110 & byte) >> 1); const tile = @intCast(u7, (0b0111_1111 & byte));
return TileData{ .tile = tile }; return TileData{ .tile = tile };
} else { } else {
const is_solid = (0b0000_0010 & byte) > 0; const is_solid = (0b0000_0001 & byte) > 0;
const circuit = @intCast(u4, (0b0011_1100 & byte) >> 2); const circuit = @intCast(u4, (0b0001_1110 & byte) >> 1);
return TileData{ .flags = .{ return TileData{ .flags = .{
.solid = is_solid, .solid = is_solid,
.circuit = circuit, .circuit = circuit,