1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use crate::{
engine::Engine,
graphics::{ RendererTextureHandle },
resources::{ ResourceStorage, Resource, ResourceLoadType, Material },
ecs::{ DeferredUpdateManagerPointer },
config::*,
};
use pill_core::{ PillSlotMapKey, PillTypeMapKey, PillStyle, get_type_name };
use std::collections::HashSet;
use std::path::{ Path, PathBuf };
use anyhow::{ Result, Context, Error };
pill_core::define_new_pill_slotmap_key! {
pub struct TextureHandle;
}
#[derive(Clone, Copy, Debug)]
pub enum TextureType {
Color,
Normal,
}
#[readonly::make]
pub struct Texture {
#[readonly]
pub name: String,
#[readonly]
pub load_type: ResourceLoadType,
#[readonly]
pub texture_type: TextureType,
pub(crate) renderer_resource_handle: Option<RendererTextureHandle>,
}
impl Texture {
pub fn new(name: &str, texture_type: TextureType, resource_load_type: ResourceLoadType) -> Self {
Self {
name: name.to_string(),
load_type: resource_load_type,
texture_type,
renderer_resource_handle: None,
}
}
}
impl PillTypeMapKey for Texture {
type Storage = ResourceStorage<Texture>;
}
impl Resource for Texture {
type Handle = TextureHandle;
fn get_name(&self) -> String {
self.name.clone()
}
fn initialize(&mut self, engine: &mut Engine) -> Result<()> {
let error_message = format!("Initializing {} {} failed", "Resource".gobj_style(), get_type_name::<Self>().sobj_style());
let image_data = match &self.load_type {
ResourceLoadType::Path(path) => {
pill_core::validate_asset_path(path, &["png", "jpg", "gif", "tif"])?;
image::open(path)?
},
ResourceLoadType::Bytes(bytes) => {
image::load_from_memory(bytes)?
},
};
let renderer_resource_handle = engine.renderer.create_texture(&self.name, &image_data, self.texture_type).context(error_message.clone())?;
self.renderer_resource_handle = Some(renderer_resource_handle);
Ok(())
}
fn destroy<H: PillSlotMapKey>(&mut self, engine: &mut Engine, self_handle: H) -> Result<()> {
if let Some(v) = self.renderer_resource_handle {
engine.renderer.destroy_texture(v).unwrap();
}
let resource_storage = engine.resource_manager.get_resource_storage_mut::<Material>().expect("Critical: Resource not registered");
let materials = &mut resource_storage.data;
for material_slot in materials.iter_mut() {
let material = material_slot.1.as_mut().expect("Critical: Resource is None");
let mut material_updated = false;
for texture_slot in material.get_textures().data.iter_mut() {
if let Some(texture_handle) = texture_slot.1.texture_handle {
if texture_handle.data() == self_handle.data() {
texture_slot.1.texture_handle = None;
texture_slot.1.renderer_texture_handle = None;
material_updated = true;
}
}
}
if material_updated {
engine.renderer.update_material_textures(material.renderer_resource_handle.unwrap(), &material.textures).unwrap();
}
}
Ok(())
}
}