-1

I was trying to load texture from file using loadTexture() from zig-raylib, but I constantly get error log saying:

WARNING: FILEIO: [pin.png] Failed to open file

This is my minimal example where I recreated the error:

const std = @import("std");
const rl = @import("raylib");

pub fn main() !void {
    const width = 1920;
    const height = 1080;
    rl.initWindow(width, height, "Platformer");
    defer rl.closeWindow();
    rl.setTargetFPS(60);

    const playerTex = try rl.loadTexture("pin.png");
    defer rl.unloadTexture(playerTex);

    while(!rl.windowShouldClose()){
        rl.beginDrawing();
        defer rl.endDrawing();
        rl.clearBackground(.black);

        rl.drawTexture(playerTex, width/2, height/2, .white);
    }
}

I also added this line to my build.zig so my executable directory, includes the image file.

b.getInstallStep().dependOn(&b.addInstallFile(.{.src_path = .{ .owner = b, .sub_path = "src/pin.png" } }, "bin/pin.png").step);
3
  • 1
    How and where do you run this? The way you specify the path (without any directory whatsoever), the file should be in your working directory, the src and bin dir content's shouldn't matter. Commented Jan 26 at 13:06
  • I have pin.png in my src directory. "bin/pin.png" is there so my image and exe are in the same directory zig-out/bin. Commented Jan 26 at 13:42
  • 1
    So it's simply in the wrong directory again, pin.png does not need to be in either src nor bin it needs to be in the working directory. Commented Jan 26 at 14:00

1 Answer 1

1

Okey so, I'm more stupid than I thought I was. This problem accured, becouse when I build my exe, I use zig-out/bin/my_exe command to run it and when i execute this command im in my projects root directory that is parent of zig-out and src.

C:.
│   build.zig
│   build.zig.zon
│
├───src
│       main.zig
│       pin.png
│
└───zig-out
    └───bin
            pin.png
            my_exe.exe
            my_exe.pdb

Error accures becouse my executable looks for pin.png with relative path in current directory, which is C:. when I run command zig-out/bin/my_exe, but image is in src so my program can't find image and throws error.

solution

All I need to do is run my exe in zig-out/bin or move pin.png to src directory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.