working on err handling
This commit is contained in:
parent
76a94edde9
commit
a2ca2b6827
@ -21,6 +21,8 @@ puffin_http = "0.6"
|
|||||||
dirty-fsm = "^0.2.2"
|
dirty-fsm = "^0.2.2"
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
sentry = "0.23"
|
sentry = "0.23"
|
||||||
|
image = "0.23"
|
||||||
|
tempfile = "3.2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
puffin_viewer = "0.6"
|
puffin_viewer = "0.6"
|
||||||
|
@ -1,3 +1,47 @@
|
|||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use raylib::{texture::Texture2D, RaylibHandle, RaylibThread};
|
||||||
|
use tempfile::{tempfile, NamedTempFile};
|
||||||
|
|
||||||
|
/// Contains all game assets.
|
||||||
|
///
|
||||||
|
/// This uses macro magic to automatically embed the contents of `game/assets/` into the executable
|
||||||
|
/// file so we only have to distribute a single file, instead of a game and its assets separately
|
||||||
#[derive(rust_embed::RustEmbed)]
|
#[derive(rust_embed::RustEmbed)]
|
||||||
#[folder = "assets"]
|
#[folder = "assets"]
|
||||||
pub struct StaticGameData;
|
pub struct StaticGameData;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum ResourceLoadError {
|
||||||
|
#[error(transparent)]
|
||||||
|
Io(#[from] std::io::Error),
|
||||||
|
#[error("Could not load embedded asset: {0}")]
|
||||||
|
AssetNotFound(String),
|
||||||
|
#[error("Generic error: {0}")]
|
||||||
|
Generic(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_texture_from_internal_data(
|
||||||
|
raylib_handle: &RaylibHandle,
|
||||||
|
thread: &RaylibThread,
|
||||||
|
path: &str,
|
||||||
|
) -> Result<Texture2D, ResourceLoadError> {
|
||||||
|
// Create a temp file path to work with
|
||||||
|
let tmp_path = NamedTempFile::new()?.into_temp_path();
|
||||||
|
|
||||||
|
// Unpack the raw image data to a real file on the local filesystem so raylib will read it correctly
|
||||||
|
std::fs::write(
|
||||||
|
tmp_path,
|
||||||
|
&StaticGameData::get(path)
|
||||||
|
.ok_or(ResourceLoadError::AssetNotFound(path.to_string()))?
|
||||||
|
.data,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// Call through via FFI to re-load the file
|
||||||
|
let texture = raylib_handle.load_texture(thread, tmp_path.to_str().unwrap()).map_err(|e| ResourceLoadError::Generic(e));
|
||||||
|
|
||||||
|
// Close the file
|
||||||
|
tmp_path.close()?;
|
||||||
|
|
||||||
|
Ok(texture)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user