diff --git a/.gitignore b/.gitignore index d0e8666..2e6ab17 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ storage.dat archive dungeon_rush dungeonrush +dungeonrush-zig c-dungeonrush *.o *.gch diff --git a/zsrc/game.zig b/zsrc/game.zig index a4a20c6..c05eb33 100644 --- a/zsrc/game.zig +++ b/zsrc/game.zig @@ -1411,13 +1411,9 @@ fn makeSnakeCross(snake: *pl.Snake) bool { // So it goes from: {character-type}_run_anim -> {character-type}_hit_anim // NOTE: Only some characters have the _hit_anim var deathPtr: usize = @intFromPtr(sprite.ani.origin); - std.log.debug("Death dbg: {s}, b4 ptr: {*}", .{ std.mem.sliceTo(&sprite.ani.origin.dbgName, 0), sprite.ani.origin }); if (isPlayer(snake)) deathPtr += (@sizeOf(tps.Texture) * 1); - const possiblyNewTexture = @as(*tps.Texture, @ptrFromInt(deathPtr)); - std.log.debug("Death dbg: {s}, aft ptr: {*}", .{ std.mem.sliceTo(&possiblyNewTexture.dbgName, 0), possiblyNewTexture }); - dropItem(sprite); _ = ren.createAndPushAnimation( diff --git a/zsrc/main.zig b/zsrc/main.zig index a2f2e68..a78719d 100644 --- a/zsrc/main.zig +++ b/zsrc/main.zig @@ -29,6 +29,14 @@ const ui = @import("ui.zig"); const alloc = @import("alloc.zig"); pub fn main() !void { + // First grab the path to the exe. + var buff: [512]u8 = undefined; + const exeDir = try std.fs.selfExeDirPath(&buff); + + try realMain(exeDir); +} + +fn realMain(exePath: []const u8) !void { std.log.info(res.nameOfTheGame, .{}); prng.prngSrand(@as(c_uint, @bitCast(@as(c_int, @truncate(c.time(null)))))); @@ -50,7 +58,7 @@ pub fn main() !void { if (!res.init()) { std.log.err("Failed to init SDL and/or the game.", .{}); } else { - if (!try res.loadMedia()) { + if (!try res.loadMedia(exePath)) { std.log.err("Failed to load media.", .{}); } else { try ui.mainUi(); diff --git a/zsrc/res.zig b/zsrc/res.zig index 80a87ff..119463d 100644 --- a/zsrc/res.zig +++ b/zsrc/res.zig @@ -336,7 +336,11 @@ const soundfxList = &[_][]const u8{ "bowhit.wav", }; -// Globals +/// Globals +/// This var gets populated in main of the root path of the executable. +/// This way, if the .exe is double-clicked, it will still find the res/ +/// folder because on MacOS, scripts or commands execute from the home folder. +var rootPath: []const u8 = undefined; pub var textures: [TEXTURES_SIZE]tps.Texture = undefined; pub var texturesCount: usize = 0; pub var bgms: [AUDIO_BGM_SIZE]?*c.Mix_Music = undefined; @@ -442,7 +446,7 @@ fn loadTextset() bool { return success; } -fn loadTileset(path: [*]const u8, origin: ?*c.SDL_Texture) bool { +fn loadTileset(path: [:0]const u8, origin: ?*c.SDL_Texture) bool { if (origin == null) { @panic("origin should never be null!"); } @@ -450,6 +454,11 @@ fn loadTileset(path: [*]const u8, origin: ?*c.SDL_Texture) bool { const file = c.fopen(path, "r"); defer _ = c.fclose(file); + if (file == null) { + std.log.err("Couldn't find file at path: {s}", .{path}); + return false; + } + var x: c_int = undefined; var y: c_int = undefined; var w: c_int = undefined; @@ -459,6 +468,7 @@ fn loadTileset(path: [*]const u8, origin: ?*c.SDL_Texture) bool { var resName: [256]u8 = undefined; // Convention of tileset: name, x, y, w, h, f (num of frames) + var count: usize = 0; while (c.fscanf(file, "%s %d %d %d %d %d", &resName, &x, &y, &w, &h, &f) == 6) { const p = &textures[texturesCount]; texturesCount += 1; @@ -472,11 +482,8 @@ fn loadTileset(path: [*]const u8, origin: ?*c.SDL_Texture) bool { p.crops[i].w = w; } - p.dbgName = resName; - - std.log.debug("Texture Res: {d}). {s} ptr:{*}, x:{d}, y:{d}, w:{d}, h:{d}, f:{d}", .{ + std.log.debug("Texture Res: {d}). ptr:{*}, x:{d}, y:{d}, w:{d}, h:{d}, f:{d}", .{ texturesCount - 1, - std.mem.sliceTo(&p.dbgName, 0), p, x, y, @@ -484,6 +491,7 @@ fn loadTileset(path: [*]const u8, origin: ?*c.SDL_Texture) bool { h, f, }); + count += 1; } return true; } @@ -523,18 +531,21 @@ pub fn loadAudio() !bool { return true; } -pub fn loadMedia() !bool { +pub fn loadMedia(exePath: []const u8) !bool { + rootPath = exePath; + // load effects initCommonEffects(); + var buf: [PATH_LEN + 4]u8 = undefined; + // Load tileset for (tilesetPath, 0..) |path, idx| { - var buf: [PATH_LEN + 4]u8 = undefined; - const img = try std.fmt.bufPrintZ(&buf, "{s}.png", .{path}); + const imgPath = try std.fmt.bufPrintZ(&buf, "{s}/{s}.png", .{ rootPath, path }); + originTextures[idx] = loadSDLTexture(std.mem.sliceTo(imgPath, 0)); - originTextures[idx] = loadSDLTexture(std.mem.sliceTo(img, 0)); - const pptr = path.ptr; - _ = loadTileset(pptr, originTextures[idx]); + const defPath = try std.fmt.bufPrintZ(&buf, "{s}/{s}", .{ rootPath, path }); + _ = loadTileset(defPath, originTextures[idx]); if (originTextures[idx] == null) { return false; } diff --git a/zsrc/types.zig b/zsrc/types.zig index b5a9f31..4bac653 100644 --- a/zsrc/types.zig +++ b/zsrc/types.zig @@ -82,7 +82,6 @@ pub const Texture = struct { width: c_int, height: c_int, frames: c_int, - dbgName: [256]u8 = undefined, crops: []c.SDL_Rect, };