-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5fa6ae
commit e138226
Showing
17 changed files
with
258 additions
and
12 deletions.
There are no files selected for viewing
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const std = @import("std"); | ||
const allocator = std.heap.c_allocator; | ||
const Image = @import("zigimg").Image; | ||
|
||
const ConvertFile = struct { | ||
path: []const u8, | ||
bits: u4, | ||
transparency: bool, | ||
}; | ||
|
||
pub fn main() !void { | ||
var args = std.process.args(); | ||
_ = args.next(); | ||
var in_files = std.ArrayList(ConvertFile).init(allocator); | ||
var out_path: []const u8 = undefined; | ||
while (args.next()) |arg| { | ||
if (std.mem.eql(u8, arg, "-i")) { | ||
const path = args.next() orelse return error.MissingArg; | ||
const bits = args.next() orelse return error.MissingArg; | ||
const transparency = args.next() orelse return error.MissingArg; | ||
try in_files.append(.{ .path = path, .bits = @intCast(bits[0] - '0'), .transparency = transparency[0] == 't' }); | ||
} else if (std.mem.eql(u8, arg, "-o")) { | ||
out_path = args.next() orelse return error.MissingArg; | ||
} | ||
} | ||
std.debug.print("{s}\n", .{out_path}); | ||
|
||
const out_file = try std.fs.cwd().createFile(out_path, .{}); | ||
defer out_file.close(); | ||
|
||
const writer = out_file.writer(); | ||
try writer.writeAll("const PackedIntSlice = @import(\"std\").packed_int_array.PackedIntSlice;\n"); | ||
try writer.writeAll("const DisplayColor = @import(\"wasm4\").DisplayColor;\n\n"); | ||
|
||
for (in_files.items) |in_file| { | ||
try convert(in_file, writer); | ||
} | ||
} | ||
|
||
fn convert(args: ConvertFile, writer: std.fs.File.Writer) !void { | ||
const N = 8 / args.bits; | ||
|
||
var image = try Image.fromFilePath(allocator, args.path); | ||
defer image.deinit(); | ||
|
||
var colors = std.ArrayList(Color).init(allocator); | ||
defer colors.deinit(); | ||
if (args.transparency) try colors.append(.{ .r = 31, .g = 0, .b = 31 }); | ||
var indices = try std.ArrayList(usize).initCapacity(allocator, image.width * image.height); | ||
defer indices.deinit(); | ||
var it = image.iterator(); | ||
while (it.next()) |pixel| { | ||
const color = Color{ | ||
.r = @intFromFloat(31.0 * pixel.r), | ||
.g = @intFromFloat(63.0 * pixel.g), | ||
.b = @intFromFloat(31.0 * pixel.b), | ||
}; | ||
const index = try getIndex(&colors, color); | ||
indices.appendAssumeCapacity(index); | ||
} | ||
std.debug.print("{} colors: {any}\n", .{ colors.items.len, colors.items }); | ||
var packed_data = try allocator.alloc(u8, indices.items.len / N); | ||
defer allocator.free(packed_data); | ||
for (packed_data, 0..) |_, i| { | ||
packed_data[i] = 0; | ||
for (0..N) |n| { | ||
const shift: u3 = @intCast(n * args.bits); | ||
packed_data[i] |= @intCast(indices.items[N * i + n] << shift); | ||
} | ||
} | ||
|
||
{ | ||
const name = std.fs.path.stem(args.path); | ||
try writer.print("pub const {s} = struct {{\n", .{name}); | ||
|
||
try writer.print(" pub const w = {};\n", .{image.width}); | ||
try writer.print(" pub const h = {};\n", .{image.height}); | ||
|
||
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.writeAll(" };\n"); | ||
|
||
try writer.print(" pub const indices = PackedIntSlice(u{}).init(@constCast(data[0..]), data.len * {});\n", .{ args.bits, N }); | ||
try writer.writeAll(" const data = [_]u8{\n"); | ||
for (packed_data, 0..) |index, i| { | ||
if (i % 32 == 0) try writer.writeAll(" "); | ||
try writer.print("{}, ", .{index}); | ||
if ((i + 1) % 32 == 0) try writer.writeAll("\n"); | ||
} | ||
try writer.writeAll(" };\n"); | ||
|
||
try writer.writeAll("};\n\n"); | ||
} | ||
} | ||
|
||
pub const Color = packed struct(u16) { | ||
b: u5, | ||
g: u6, | ||
r: u5, | ||
|
||
fn eql(self: Color, other: Color) bool { | ||
return @as(u16, @bitCast(self)) == @as(u16, @bitCast(other)); | ||
} | ||
}; | ||
|
||
fn getIndex(colors: *std.ArrayList(Color), color: Color) !usize { | ||
for (colors.items, 0..) |c, i| { | ||
if (c.eql(color)) return i; | ||
} | ||
try colors.append(color); | ||
return colors.items.len - 1; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const wasm4 = @import("wasm4"); | ||
const gfx = @import("gfx"); | ||
|
||
fn blitZero() void { | ||
var y: usize = 0; | ||
while (y < 32) : (y += 1) { | ||
var x: usize = 0; | ||
while (x < 24) : (x += 1) { | ||
const index = gfx.zero.indices.get(y * gfx.zero.w + x); | ||
if (index == 0) continue; | ||
wasm4.framebuffer[(y + 96) * wasm4.screen_width + x + 80 - 12] = gfx.zero.colors[index]; | ||
} | ||
} | ||
} | ||
|
||
fn blitGopher() void { | ||
var y: usize = 0; | ||
while (y < 24) : (y += 1) { | ||
var x: usize = 0; | ||
while (x < 24) : (x += 1) { | ||
const index = gfx.gopher.indices.get(y * gfx.gopher.w + x); | ||
if (index == 0) continue; | ||
wasm4.framebuffer[(y + 104) * wasm4.screen_width + x + 92] = gfx.gopher.colors[index]; | ||
} | ||
} | ||
} | ||
|
||
pub fn blitSpriteOpaque(sheet: anytype, dx: u32, dy: u32) void { | ||
var y: usize = 0; | ||
while (y < sheet.h) : (y += 1) { | ||
var x: usize = 0; | ||
while (x < sheet.w) : (x += 1) { | ||
const index = sheet.indices.get(y * sheet.w + x); | ||
wasm4.framebuffer[(y + dy) * wasm4.screen_width + x + dx] = sheet.colors[index]; | ||
} | ||
} | ||
} | ||
|
||
pub fn blitSprite(sheet: anytype, dx: u32, dy: u32) void { | ||
var y: usize = 0; | ||
while (y < sheet.h) : (y += 1) { | ||
var x: usize = 0; | ||
while (x < sheet.w) : (x += 1) { | ||
const index = sheet.indices.get(y * sheet.w + x); | ||
if (index == 0) continue; | ||
wasm4.framebuffer[(y + dy) * wasm4.screen_width + x + dx] = sheet.colors[index]; | ||
} | ||
} | ||
} | ||
|
||
pub export fn update() void { | ||
blitSpriteOpaque(gfx.needleman, 0, 0); | ||
blitSpriteOpaque(gfx.title, 0, 40); | ||
blitSprite(gfx.font, 16, 0); | ||
blitSpriteOpaque(gfx.door, 0, 0); | ||
blitSprite(gfx.healthbar, 0, 0); | ||
blitSprite(gfx.hurt, 92, 104); | ||
blitSprite(gfx.spike, 0, 104); | ||
blitSprite(gfx.teleport, 68 - 24, 96); | ||
blitSprite(gfx.effects, 0, 72); | ||
blitZero(); | ||
blitGopher(); | ||
blitSprite(gfx.shot, 100, 112); | ||
} |
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