master
Louis Pearson 2022-01-15 02:46:15 -07:00
parent 7a9626f839
commit d2c5ddd815
8 changed files with 157 additions and 10 deletions

5
assets/Wired.tsx Normal file
View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.5" tiledversion="1.7.2" name="Wired" tilewidth="16" tileheight="16" tilecount="64" columns="8">
<grid orientation="orthogonal" width="8" height="8"/>
<image source="tiles.png" width="128" height="128"/>
</tileset>

32
assets/maps/test.json Normal file
View File

@ -0,0 +1,32 @@
{ "compressionlevel":-1,
"height":20,
"infinite":false,
"layers":[
{
"data":[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 1, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 50, 19, 19, 18, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 1, 50, 19, 52, 1, 1, 1, 1, 20, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 20, 1, 1, 1, 37, 1, 1, 39, 1, 50, 19, 52, 1, 1, 1, 1, 1, 1, 1, 1, 20, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 20, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 1, 50, 19, 52, 1, 1, 1, 1, 51, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 20, 1, 1, 1, 37, 1, 1, 39, 1, 50, 19, 52, 1, 1, 1, 1, 1, 1, 1, 1, 51, 1, 2, 1, 37, 1, 54, 55, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 33, 19, 19, 19, 66, 54, 1, 1, 1, 1, 1, 1, 1, 50, 19, 52, 1, 1, 1, 1, 1, 1, 1, 1, 20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 51, 1, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
"height":20,
"id":1,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":20,
"x":0,
"y":0
}],
"nextlayerid":2,
"nextobjectid":1,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.7.2",
"tileheight":8,
"tilesets":[
{
"firstgid":1,
"source":"wired.tsx"
}],
"tilewidth":8,
"type":"map",
"version":"1.6",
"width":20
}

31
assets/maps/test.tmx Normal file
View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.5" tiledversion="1.7.2" orientation="orthogonal" renderorder="right-down" width="20" height="20" tilewidth="8" tileheight="8" infinite="0" nextlayerid="2" nextobjectid="1">
<editorsettings>
<export target="test.json" format="json"/>
</editorsettings>
<tileset firstgid="1" source="wired.tsx"/>
<layer id="1" name="Tile Layer 1" width="20" height="20">
<data encoding="csv">
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,1,1,
1,39,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,37,1,
1,39,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,37,1,
1,39,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,37,1,
1,39,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,37,1,
1,39,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,37,1,
1,39,1,1,1,1,1,1,1,1,1,50,19,19,18,1,1,1,37,1,
1,39,1,1,1,1,1,50,19,52,1,1,1,1,20,1,1,1,37,1,
1,39,1,1,1,1,1,1,1,1,1,1,1,1,20,1,1,1,37,1,
1,39,1,50,19,52,1,1,1,1,1,1,1,1,20,1,1,1,37,1,
1,39,1,1,1,1,1,1,1,1,1,1,1,1,20,1,1,1,37,1,
1,39,1,1,1,1,1,50,19,52,1,1,1,1,51,1,1,1,37,1,
1,39,1,1,1,1,1,1,1,1,1,1,1,1,20,1,1,1,37,1,
1,39,1,50,19,52,1,1,1,1,1,1,1,1,51,1,2,1,37,1,
54,55,1,1,1,1,1,1,1,1,1,1,1,1,33,19,19,19,66,54,
1,1,1,1,1,1,1,50,19,52,1,1,1,1,1,1,1,1,20,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,51,1,
22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
</data>
</layer>
</map>

4
assets/maps/wired.tsx Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.5" tiledversion="1.7.2" name="wired" tilewidth="8" tileheight="8" tilecount="256" columns="16">
<image source="../tiles.png" width="128" height="128"/>
</tileset>

1
map.zig Normal file
View File

@ -0,0 +1 @@
pub const map: [400]u8 = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 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, 22, 22, 22, 22, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };

65
map2src.zig Normal file
View File

@ -0,0 +1,65 @@
const std = @import("std");
const Layer = struct {
data: []u64,
height: u64,
id: u64,
name: []const u8,
opacity: u64,
@"type": enum { tilelayer },
visible: bool,
width: u64,
x: i64,
y: i64,
};
const MapType = struct {
compressionlevel: i64 = -1,
height: u64 = 0,
infinite: bool = false,
layers: []Layer,
nextlayerid: u64 = 0,
nextobjectid: u64 = 0,
orientation: enum { orthogonal } = .orthogonal,
renderorder: enum { @"right-down" } = .@"right-down",
tiledversion: []const u8 = "",
tileheight: u64 = 0,
tilesets: []struct { firstgid: u64, source: []const u8 },
tilewidth: u64 = 0,
@"type": enum { map } = .map,
version: []const u8 = "",
width: u64 = 0,
};
const KB = 1024;
var heap: [64 * KB]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&heap);
var alloc = fba.allocator();
pub fn main() anyerror!void {
const cwd = std.fs.cwd();
var output = try cwd.createFile("map.zig", .{});
defer output.close();
var argsIter = std.process.args();
const progName = (try argsIter.next(alloc)) orelse "";
defer alloc.free(progName);
std.log.info("{s}", .{progName});
while (try argsIter.next(alloc)) |arg| {
defer alloc.free(arg);
std.log.info("{s}", .{arg});
var filebuffer: [64 * KB]u8 = undefined;
var filecontents = try cwd.readFile(arg, &filebuffer);
@setEvalBranchQuota(10000);
var tokenstream = std.json.TokenStream.init(filecontents);
const options = std.json.ParseOptions{ .allocator = fba.allocator() };
const map = try std.json.parse(MapType, &tokenstream, options);
defer std.json.parseFree(MapType, map, options);
var outbuffer: [64 * KB]u8 = undefined;
var outcontent = try std.fmt.bufPrint(&outbuffer, "pub const map: [{}]u8 = {any};\n", .{ map.layers[0].data.len, map.layers[0].data });
_ = try output.writeAll(outcontent);
}
}

View File

@ -48,8 +48,12 @@ const Anim = struct {
// Components // Components
const Pos = Vec2f; const Pos = Vec2f;
const Control = struct { controller: enum { player }, state: enum { stand, walk, jump, fall } }; const Control = struct {
const Sprite = usize; controller: enum { player },
state: enum { stand, walk, jump, fall },
facing: enum { left, right } = .right,
};
const Sprite = struct { index: usize, flags: w4.BlitFlags };
const StaticAnim = Anim; const StaticAnim = Anim;
const ControlAnim = struct { anims: []AnimData, state: Anim }; const ControlAnim = struct { anims: []AnimData, state: Anim };
const Component = struct { const Component = struct {
@ -89,7 +93,7 @@ export fn start() void {
_ = world.create(.{ _ = world.create(.{
.pos = .{ 76, 76 }, .pos = .{ 76, 76 },
.control = .{ .controller = .player, .state = .stand }, .control = .{ .controller = .player, .state = .stand },
.sprite = 0, .sprite = .{ .index = 0, .flags = .{ .bpp = .b1 } },
.controlAnim = ControlAnim{ .controlAnim = ControlAnim{
.anims = playerAnim, .anims = playerAnim,
.state = Anim{ .anim = &.{} }, .state = Anim{ .anim = &.{} },
@ -116,19 +120,20 @@ export fn update() void {
fn drawProcess(_: f32, pos: *Pos, sprite: *Sprite) void { fn drawProcess(_: f32, pos: *Pos, sprite: *Sprite) void {
w4.DRAW_COLORS.* = 0x0030; w4.DRAW_COLORS.* = 0x0030;
const tx = (sprite.* * 8) % 128; const ipos = w4.Vec2{ @floatToInt(i32, pos.*[0]), @floatToInt(i32, pos.*[1]) };
const ty = (sprite.* * 8) / 128; const t = w4.Vec2{ @intCast(i32, (sprite.index * 8) % 128), @intCast(i32, (sprite.index * 8) / 128) };
w4.externs.blitSub(&assets.sprites, @floatToInt(i32, pos.*[0]), @floatToInt(i32, pos.*[1]), 8, 8, tx, ty, 128, assets.sprites_flags); w4.blitSub(&assets.sprites, ipos, .{ 8, 8 }, t, 128, sprite.flags);
} }
fn staticAnimProcess(_: f32, sprite: *Sprite, anim: *StaticAnim) void { fn staticAnimProcess(_: f32, sprite: *Sprite, anim: *StaticAnim) void {
anim.update(sprite); anim.update(&sprite.index);
} }
fn controlAnimProcess(_: f32, sprite: *Sprite, anim: *ControlAnim, control: *Control) void { fn controlAnimProcess(_: f32, sprite: *Sprite, anim: *ControlAnim, control: *Control) void {
const a: usize = if (control.state == .stand) 0 else 1; const a: usize = if (control.state == .stand) 0 else 1;
sprite.flags.flip_x = (control.facing == .left);
anim.state.play(anim.anims[a]); anim.state.play(anim.anims[a]);
anim.state.update(sprite); anim.state.update(&sprite.index);
} }
fn controlProcess(_: f32, pos: *Pos, control: *Control) void { fn controlProcess(_: f32, pos: *Pos, control: *Control) void {
@ -140,6 +145,10 @@ fn controlProcess(_: f32, pos: *Pos, control: *Control) void {
if (delta[0] != 0 or delta[1] != 0) { if (delta[0] != 0 or delta[1] != 0) {
control.state = .walk; control.state = .walk;
pos.* += delta; pos.* += delta;
if (pos.*[0] < 0) pos.*[0] = 0;
if (pos.*[0] > 152) pos.*[0] = 152;
if (delta[0] > 0) control.facing = .right;
if (delta[0] < 0) control.facing = .left;
} else { } else {
control.state = .stand; control.state = .stand;
} }

View File

@ -200,13 +200,13 @@ pub const externs = struct {
/// Copies pixels to the framebuffer. /// Copies pixels to the framebuffer.
pub fn blit(sprite: []const u8, pos: Vec2, size: Vec2, flags: BlitFlags) void { pub fn blit(sprite: []const u8, pos: Vec2, size: Vec2, flags: BlitFlags) void {
if (sprite.len * 8 != size[x] * size[y]) unreachable; if (sprite.len * 8 < size[x] * size[y]) unreachable;
externs.blit(sprite.ptr, pos[x], pos[y], size[x], size[y], @bitCast(u32, flags)); externs.blit(sprite.ptr, pos[x], pos[y], size[x], size[y], @bitCast(u32, flags));
} }
/// Copies a subregion within a larger sprite atlas to the framebuffer. /// Copies a subregion within a larger sprite atlas to the framebuffer.
pub fn blitSub(sprite: []const u8, pos: Vec2, size: Vec2, src: Vec2, strie: i32, flags: BlitFlags) void { pub fn blitSub(sprite: []const u8, pos: Vec2, size: Vec2, src: Vec2, strie: i32, flags: BlitFlags) void {
if (sprite.len * 8 >= size[x] * size[y]) trace("Sprite not large enough {}", .{sprite.len}); if (sprite.len * 8 < size[x] * size[y]) unreachable;
externs.blitSub(sprite.ptr, pos[x], pos[y], size[x], size[y], @intCast(u32, src[x]), @intCast(u32, src[y]), strie, @bitCast(u32, flags)); externs.blitSub(sprite.ptr, pos[x], pos[y], size[x], size[y], @intCast(u32, src[x]), @intCast(u32, src[y]), strie, @bitCast(u32, flags));
} }