e2rcore/implement/render/
texture_collection.rs1use std::collections::HashMap;
2use std::string::String;
3
4
5use implement::render::router;
6
7pub struct TextureCollection {
8 _textures: HashMap< u64, ( router::ShaderType, i64 ) >,
9 _id_to_descrip: HashMap< u64, String >,
10 _descrip_to_id: HashMap< String, u64 >,
11 _id_generator: u64,
12}
13
14
15impl Default for TextureCollection {
16 fn default() -> TextureCollection {
17 TextureCollection {
18 _textures: HashMap::new(),
19 _id_to_descrip: HashMap::new(),
20 _descrip_to_id: HashMap::new(),
21 _id_generator: 0u64,
22 }
23 }
24}
25
26impl Drop for TextureCollection {
27 fn drop( & mut self ) {
28 self._textures.clear();
29 }
30}
31
32impl TextureCollection {
33 pub fn add( & mut self, shader_type: router::ShaderType, internal_handle: i64, descrip: String ) -> Result< (u64), & 'static str > {
34 let mut handle = self._id_generator;
35 while self._textures.contains_key( &handle ) {
36 handle = handle + 1;
37 }
38 match self._textures.insert( handle, ( shader_type, internal_handle ) ) {
39 None => (),
40 Some( ( _shader_type, _old_handle ) ) => {
41 return Err( &"adding texture handle failed")
42 }
43 }
44 self._id_generator = handle;
45 self._id_to_descrip.insert( handle, descrip.clone() );
46 self._descrip_to_id.insert( descrip, handle );
47 Ok( handle )
48 }
49 pub fn put( & mut self, id: u64, shader_type: router::ShaderType, internal_handle: i64, descrip: String ) -> Result< (), & 'static str > {
50 match self._textures.insert( id, ( shader_type, internal_handle ) ) {
51 None => (),
52 Some( ( shader_type, old_handle ) ) => {
53 router::delete_texture( old_handle, shader_type )?;
54 trace!( "removed old texture( {} ).", old_handle );
55 }
56 }
57 self._id_to_descrip.insert( id, descrip.clone() );
58 self._descrip_to_id.insert( descrip, id );
59 Ok( () )
60 }
61 pub fn clear( & mut self ) -> Result< (), & 'static str > {
62 for ( &_k, &( ref shader_type, ref handle ) ) in self._textures.iter() {
63 router::delete_texture( *handle, (*shader_type).clone() )?
64 }
65 self._id_to_descrip.clear();
66 self._descrip_to_id.clear();
67 Ok( () )
68 }
69 pub fn remove( & mut self, id: u64 ) -> Result< (), & 'static str > {
70 match self._textures.remove( &id ) {
71 Some( ( shader_type, handle ) ) => {
72 router::delete_texture( handle, shader_type )?;
73 if let Some( descrip ) = self._id_to_descrip.remove( &id ) {
74 self._descrip_to_id.remove( &descrip );
75 }
76 },
77 None => (),
78 }
79 Ok( () )
80 }
81 pub fn get( & mut self, id: u64 ) -> Option< i64 > {
82 match self._textures.get( &id ) {
83 Some( &( ref _shader_type, ref handle ) ) => {
84 return Some( *handle )
85 },
86 None => return None
87 }
88 }
89}