Skip to content

Commit

Permalink
Fix segmentation fault when the the game is double clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
deckarep committed Oct 29, 2024
1 parent 322c778 commit 9267a18
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ storage.dat
archive
dungeon_rush
dungeonrush
dungeonrush-zig
c-dungeonrush
*.o
*.gch
Expand Down
4 changes: 0 additions & 4 deletions zsrc/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 9 additions & 1 deletion zsrc/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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))))));

Expand All @@ -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();
Expand Down
35 changes: 23 additions & 12 deletions zsrc/res.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -442,14 +446,19 @@ 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!");
}

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;
Expand All @@ -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;
Expand All @@ -472,18 +482,16 @@ 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,
w,
h,
f,
});
count += 1;
}
return true;
}
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion zsrc/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down

0 comments on commit 9267a18

Please sign in to comment.