Hello, so im currently learning rust and i decided to go for simple 2d game engine. The issue im facing is due to how rust std::fs::File works. So my 'engine' is a library that i include in my sandbox app by doing engine = {path = "../engine"}. So on the screenshot below you can see my file structure. I have a function that compiles and returns me a shader program. It takes two arguments as a path to the file (vertex_path: &str, fragment_path: &str). Now in my renderer.rs I want to get that shader so i call this by doing
let shader_program = Shader::new("./src/rendering/shaders/test_vertex.glsl", "./src/rendering/shaders/test_fragment.glsl")?; although i cant get the file path to be correct, so i decided to debug it. It ended up that the path is actually in the app folder for some reason even though i call it in renderer.rs that is in engine folder. Any ideas what should i do in this case?
#std::fs::File issues
25 messages · Page 1 of 1 (latest)
Also to check where the std::fs::File path is currently i decided to call std::fs::File::create() and it created the foo.txt that you can see on the screenshot above
When you open a file, it's relative to the "cwd" (current working directory) that you're running the app from.
On Linux, this is the directory you're in when you execute "cargo run".
oh, ok.
So what should i do then
is there any way to make the path start from the file directory
where it is called?
it's kinda tough because the knowledge of where your source files are, is mostly erased when you compile the final program
you can read files at compile time, relative to the source files, if you use include_str! but that's an entirely different API
I take a look at it
for now i think ill just put the shaders folder into app dir
and then worry about that stuff
thanks for help mate
👍🏿
yw!
Or use a build step to copy the shaders to your build target directory and make your paths relative to the location of your exe. https://doc.rust-lang.org/std/env/fn.current_exe.html
Returns the full filesystem path of the current running executable.
this is actually smart
thanks g
Another alternative is to embed your shaders into your executable. https://doc.rust-lang.org/std/macro.include_bytes.html
Includes a file as a reference to a byte array.
alternatively, if it's fewer steps, https://doc.rust-lang.org/std/macro.include_str.html (I would presume a glsl file would be utf-8 compatible given it's probably entirely ascii chars so this might be simpler)
Includes a UTF-8 encoded file as a string.