mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-10 01:28:41 -06:00
cross-platform assets
This commit is contained in:
parent
df3791af17
commit
8aaf64904b
20
kubi/src/filesystem.rs
Normal file
20
kubi/src/filesystem.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
use std::{fs::File, path::Path, io::{Read, Seek}};
|
||||
use anyhow::Result;
|
||||
|
||||
pub trait ReadOnly: Read + Seek {}
|
||||
impl<T: Read + Seek> ReadOnly for T {}
|
||||
|
||||
#[allow(unreachable_code)]
|
||||
pub fn open_asset(path: &Path) -> Result<Box<dyn ReadOnly>> {
|
||||
#[cfg(target_os = "android")] {
|
||||
use anyhow::Context;
|
||||
use std::ffi::CString;
|
||||
|
||||
let asset_manager = ndk_glue::native_activity().asset_manager();
|
||||
let path_cstr = CString::new(path.to_string_lossy().as_bytes())?;
|
||||
let handle = asset_manager.open(&path_cstr).context("Asset doesn't exist")?;
|
||||
return Ok(Box::new(handle));
|
||||
}
|
||||
let asset_path = Path::new("./assets/").join(path);
|
||||
return Ok(Box::new(File::open(asset_path)?))
|
||||
}
|
|
@ -38,6 +38,7 @@ pub(crate) mod color;
|
|||
pub(crate) mod loading_screen;
|
||||
pub(crate) mod connecting_screen;
|
||||
pub(crate) mod fixed_timestamp;
|
||||
pub(crate) mod filesystem;
|
||||
|
||||
use world::{
|
||||
init_game_world,
|
||||
|
|
|
@ -59,7 +59,7 @@ pub fn load_prefabs(
|
|||
log::info!("Loading textures...");
|
||||
storages.add_unique_non_send_sync(BlockTexturesPrefab(
|
||||
load_texture2darray_prefab::<BlockTexture, _>(
|
||||
"./assets/blocks/".into(),
|
||||
"blocks".into(),
|
||||
&renderer.display,
|
||||
MipmapsOption::AutoGeneratedMipmaps
|
||||
)
|
||||
|
|
|
@ -2,6 +2,7 @@ use strum::IntoEnumIterator;
|
|||
use rayon::prelude::*;
|
||||
use std::{fs::File, path::PathBuf, io::BufReader};
|
||||
use glium::{texture::{SrgbTexture2dArray, RawImage2d, MipmapsOption}, backend::Facade};
|
||||
use crate::filesystem::open_asset;
|
||||
use super::AssetPaths;
|
||||
|
||||
pub fn load_texture2darray_prefab<
|
||||
|
@ -20,11 +21,11 @@ pub fn load_texture2darray_prefab<
|
|||
//Get path to the image and open the file
|
||||
let reader = {
|
||||
let path = directory.join(file_name);
|
||||
BufReader::new(File::open(path).expect("Failed to open texture file"))
|
||||
BufReader::new(open_asset(&path).expect("Failed to open texture file"))
|
||||
};
|
||||
//Parse image data
|
||||
let (image_data, dimensions) = {
|
||||
let image =image::load(
|
||||
let image = image::load(
|
||||
reader,
|
||||
image::ImageFormat::Png
|
||||
).unwrap().to_rgba8();
|
||||
|
|
Loading…
Reference in a new issue