Skip to content

Commit 4e7205a

Browse files
committed
Move away from C based file-handling in favor of the Zig stdlib
1 parent 279168b commit 4e7205a

File tree

2 files changed

+79
-41
lines changed

2 files changed

+79
-41
lines changed

res/drawable/0x72_DungeonTilesetII_v1_3

+2-1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ lizard_f_hit_anim 256 205 16 19 1
157157
lizard_m_idle_anim 128 237 16 19 4
158158
lizard_m_run_anim 192 237 16 19 4
159159
lizard_m_hit_anim 256 237 16 19 1
160+
# Added by r.c. - to represent the Zig iguana.
160161
ziggy_m_idle_anim 127 417 16 19 4
161162
ziggy_m_run_anim 191 417 16 19 4
162-
ziggy_m_hit_anim 255 414 16 19 1
163+
ziggy_m_hit_anim 255 414 16 19 1

zsrc/res.zig

+77-40
Original file line numberDiff line numberDiff line change
@@ -462,54 +462,91 @@ fn loadTextset() bool {
462462
return success;
463463
}
464464

465-
fn loadTileset(path: [:0]const u8, origin: ?*c.SDL_Texture) bool {
466-
if (origin == null) {
467-
@panic("origin should never be null!");
465+
fn initTilesetLine(line: []const u8, origin: ?*c.SDL_Texture) !void {
466+
// Skip over empty lines.
467+
if (std.mem.trim(u8, line, " ").len == 0) {
468+
return;
468469
}
469470

470-
const file = c.fopen(path, "r");
471-
defer _ = c.fclose(file);
471+
// Skip over comment lines.
472+
if (std.mem.startsWith(u8, line, "#")) {
473+
return;
474+
}
472475

473-
if (file == null) {
474-
std.log.err("Couldn't find file at path: {s}", .{path});
475-
return false;
476+
var seq = std.mem.splitSequence(u8, line, " ");
477+
const a0 = seq.next();
478+
const b0 = seq.next();
479+
const c0 = seq.next();
480+
const d0 = seq.next();
481+
const e0 = seq.next();
482+
const f0 = seq.next();
483+
484+
// Stupid sanity check.
485+
if (a0 == null or b0 == null or c0 == null or d0 == null or e0 == null or f0 == null) {
486+
@panic("Expected a tileset line item to have 6 fields defined!");
476487
}
477488

478-
var x: c_int = undefined;
479-
var y: c_int = undefined;
480-
var w: c_int = undefined;
481-
var h: c_int = undefined;
482-
var f: c_int = undefined;
489+
const name = a0.?;
490+
const x = try std.fmt.parseInt(c_int, b0.?, 10);
491+
const y = try std.fmt.parseInt(c_int, c0.?, 10);
492+
const w = try std.fmt.parseInt(c_int, d0.?, 10);
493+
const h = try std.fmt.parseInt(c_int, e0.?, 10);
494+
// fc means frameCount
495+
const fc = try std.fmt.parseInt(c_int, f0.?, 10);
496+
497+
const p = &textures[texturesCount];
498+
texturesCount += 1;
499+
tps.initTexture(p, origin.?, w, h, fc);
500+
501+
var i: usize = 0;
502+
while (i < fc) : (i += 1) {
503+
p.crops[i].x = x + @as(c_int, @intCast(i)) * w;
504+
p.crops[i].y = y;
505+
p.crops[i].h = h;
506+
p.crops[i].w = w;
507+
}
483508

484-
var resName: [256]u8 = undefined;
509+
// Don't delete this: in debug mode, this line is useful to debug textureIds.
510+
std.log.debug("Texture Res: {d}). name: {s}, ptr:{*}, x:{d}, y:{d}, w:{d}, h:{d}, fc:{d}", .{
511+
texturesCount - 1,
512+
name,
513+
p,
514+
x,
515+
y,
516+
w,
517+
h,
518+
fc,
519+
});
520+
}
485521

486-
// Convention of tileset: name, x, y, w, h, f (num of frames)
487-
var count: usize = 0;
488-
while (c.fscanf(file, "%s %d %d %d %d %d", &resName, &x, &y, &w, &h, &f) == 6) {
489-
const p = &textures[texturesCount];
490-
texturesCount += 1;
491-
tps.initTexture(p, origin.?, w, h, f);
492-
493-
var i: usize = 0;
494-
while (i < f) : (i += 1) {
495-
p.crops[i].x = x + @as(c_int, @intCast(i)) * w;
496-
p.crops[i].y = y;
497-
p.crops[i].h = h;
498-
p.crops[i].w = w;
499-
}
522+
fn loadTileset(path: [:0]const u8, origin: ?*c.SDL_Texture) !bool {
523+
if (origin == null) {
524+
@panic("origin should never be null!");
525+
}
500526

501-
std.log.debug("Texture Res: {d}). name: {s}, ptr:{*}, x:{d}, y:{d}, w:{d}, h:{d}, f:{d}", .{
502-
texturesCount - 1,
503-
std.mem.sliceTo(&resName, 0),
504-
p,
505-
x,
506-
y,
507-
w,
508-
h,
509-
f,
510-
});
511-
count += 1;
527+
const file = try std.fs.cwd().openFile(path, .{});
528+
defer file.close();
529+
530+
var bfReader = std.io.bufferedReader(file.reader());
531+
const reader = bfReader.reader();
532+
533+
var line = std.ArrayList(u8).init(gAllocator);
534+
defer line.deinit();
535+
536+
const writer = line.writer();
537+
while (reader.streamUntilDelimiter(writer, '\n', null)) {
538+
defer line.clearRetainingCapacity();
539+
try initTilesetLine(line.items, origin);
540+
} else |err| switch (err) {
541+
error.EndOfStream => {
542+
// Don't forget the last line too!
543+
defer line.clearRetainingCapacity();
544+
try initTilesetLine(line.items, origin);
545+
std.log.debug("end of stream found!", .{});
546+
},
547+
else => return err,
512548
}
549+
513550
return true;
514551
}
515552

@@ -563,7 +600,7 @@ pub fn loadMedia(exePath: []const u8) !bool {
563600
const imgFile = try std.fmt.bufPrintZ(&buf, "{s}.png", .{imgPath});
564601
originTextures[idx] = loadSDLTexture(std.mem.sliceTo(imgFile, 0));
565602

566-
_ = loadTileset(imgPath, originTextures[idx]);
603+
_ = try loadTileset(imgPath, originTextures[idx]);
567604
if (originTextures[idx] == null) {
568605
return false;
569606
}

0 commit comments

Comments
 (0)