rust: Support Windows (and Linux hopefully)

This commit is contained in:
2025-06-16 03:48:57 +10:00
parent ebc826792c
commit 80a209d00c
3 changed files with 39 additions and 13 deletions

View File

@@ -3,7 +3,14 @@ name = "NeHe-SDL_GPU"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
sdl3-sys = { version = "0.5.1", features = [ "link-framework" ] } sdl3-sys = "0.5.2"
[target.'cfg(target_os = "macos")'.dependencies]
sdl3-sys = { version = "0.5.2", features = [ "link-framework" ] }
[target.'cfg(windows)'.dependencies]
sdl3-sys = { version = "0.5.2", features = [ "build-from-source" ] }
sdl3-src = { version = "3.1.10-prerelease-0" }
[lib] [lib]
name = "nehe" name = "nehe"

View File

@@ -18,7 +18,7 @@ pub fn get_target_dir() -> PathBuf
Path::new(&manifest_dir).join("target").join(build_type) Path::new(&manifest_dir).join("target").join(build_type)
} }
pub fn copy_resources<const N: usize>(src_dir: &PathBuf, dst_dir: &PathBuf, resources: &[&str; N]) pub fn copy_resources<S: AsRef<str>>(src_dir: &PathBuf, dst_dir: &PathBuf, resources: Vec<S>)
{ {
if !dst_dir.is_dir() if !dst_dir.is_dir()
{ {
@@ -26,20 +26,39 @@ pub fn copy_resources<const N: usize>(src_dir: &PathBuf, dst_dir: &PathBuf, reso
} }
for resource in resources for resource in resources
{ {
std::fs::copy(&src_dir.join(resource), &dst_dir.join(resource)).unwrap(); std::fs::copy(&src_dir.join(resource.as_ref()), &dst_dir.join(resource.as_ref())).unwrap();
} }
} }
pub fn copy_shaders<const N: usize>(src_dir: &PathBuf, dst_dir: &PathBuf, shaders: &[&str; N])
{
let resources = shaders.into_iter().flat_map(|name|
{
if cfg!(target_os="macos")
{
vec![format!("{name}.metallib")]
}
else
{
vec![format!("{name}.vtx.spv"), format!("{name}.frg.spv")]
}
}).collect();
copy_resources(src_dir, dst_dir, resources);
}
pub fn main() pub fn main()
{ {
#[cfg(target_os="macos")] #[cfg(target_os="macos")]
println!("cargo:rustc-link-arg=-Wl,-rpath,/Library/Frameworks"); println!("cargo:rustc-link-arg=-Wl,-rpath,/Library/Frameworks");
#[cfg(target_os="linux")]
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN");
let src_dir = std::env::current_dir().unwrap().join("data"); let src_dir = std::env::current_dir().unwrap().join("data");
let dst_dir = get_target_dir().join("Data"); let dst_dir = get_target_dir().join("Data");
copy_resources(&src_dir, &dst_dir, copy_resources(&src_dir, &dst_dir,
&[ vec![
"NeHe.bmp", "NeHe.bmp",
"Crate.bmp", "Crate.bmp",
"Glass.bmp", "Glass.bmp",
@@ -47,13 +66,13 @@ pub fn main()
"Mud.bmp", "Mud.bmp",
"World.txt", "World.txt",
]); ]);
copy_resources(&src_dir.join("shaders"), &dst_dir.join("Shaders"), copy_shaders(&src_dir.join("shaders"), &dst_dir.join("Shaders"),
&[ &[
"lesson2.metallib", "lesson2",
"lesson3.metallib", "lesson3",
"lesson6.metallib", "lesson6",
"lesson7.metallib", "lesson7",
"lesson8.metallib", "lesson8",
"lesson9.metallib", "lesson9",
]); ]);
} }

View File

@@ -178,8 +178,8 @@ impl NeHeContext
{ {
info.format = SDL_GPU_SHADERFORMAT_SPIRV; info.format = SDL_GPU_SHADERFORMAT_SPIRV;
Ok(( Ok((
self.load_shader(path.appending_ext("vtx.spv"), &info, SDL_GPU_SHADERSTAGE_VERTEX, "main")?, self.load_shader(path.appending_ext("vtx.spv"), &info, SDL_GPU_SHADERSTAGE_VERTEX, "VertexMain")?,
self.load_shader(path.appending_ext("frg.spv"), &info, SDL_GPU_SHADERSTAGE_FRAGMENT, "main")? self.load_shader(path.appending_ext("frg.spv"), &info, SDL_GPU_SHADERSTAGE_FRAGMENT, "FragmentMain")?
)) ))
} }
else if available_formats & SDL_GPU_SHADERFORMAT_DXIL == SDL_GPU_SHADERFORMAT_DXIL else if available_formats & SDL_GPU_SHADERFORMAT_DXIL == SDL_GPU_SHADERFORMAT_DXIL