11//! Editor-environment specific DMI (texture) handling.
22
3+ use std:: collections:: hash_map:: HashMap ;
34use std:: io;
45use std:: path:: { Path , PathBuf } ;
5- use std:: collections:: hash_map:: HashMap ;
66use std:: sync:: { Arc , RwLock } ;
77
8- use lodepng:: { self , RGBA , Decoder , ColorType } ;
8+ use lodepng:: { self , ColorType , Decoder , RGBA } ;
9+ use sdl3:: pixels:: { PixelFormat , PixelFormatEnum } ;
10+ use sdl3:: render:: Texture ;
11+ use sdl3:: surface:: Surface ;
912
10- use gfx:: { self , Factory as FactoryTrait } ;
11- use crate :: { Factory , Texture } ;
13+ use crate :: support:: TextureCreator ;
1214
1315type Rect = ( u32 , u32 , u32 , u32 ) ;
1416
15- pub use dm :: dmi:: * ;
17+ pub use dreammaker :: dmi:: * ;
1618
1719// ----------------------------------------------------------------------------
1820// Icon file and metadata handling
@@ -23,8 +25,8 @@ pub struct IconCache {
2325}
2426
2527#[ derive( Default ) ]
26- pub struct TextureCache {
27- textures : Vec < Option < Texture > > ,
28+ pub struct TextureCache < ' r > {
29+ textures : Vec < Option < Texture < ' r > > > ,
2830}
2931
3032#[ derive( Default ) ]
@@ -61,12 +63,12 @@ impl IconCache {
6163 lock. icons . push ( arc) ;
6264 lock. paths . insert ( relative_file_path. to_owned ( ) , Some ( i) ) ;
6365 Some ( i)
64- }
66+ } ,
6567 None => {
6668 let mut lock = self . lock . write ( ) . expect ( "IconCache poisoned" ) ;
6769 lock. paths . insert ( relative_file_path. to_owned ( ) , None ) ;
6870 None
69- }
71+ } ,
7072 } ,
7173 }
7274 }
@@ -84,13 +86,17 @@ impl IconCache {
8486 }*/
8587}
8688
87- impl TextureCache {
88- pub fn retrieve ( & mut self , factory : & mut Factory , icons : & IconCache , id : usize ) -> & Texture {
89- use crate :: Fulfill ;
89+ impl < ' r > TextureCache < ' r > {
90+ pub fn retrieve (
91+ & mut self ,
92+ factory : & ' r TextureCreator ,
93+ icons : & IconCache ,
94+ id : usize ,
95+ ) -> & Texture {
9096 if id >= self . textures . len ( ) {
91- self . textures . resize ( id + 1 , None ) ;
97+ self . textures . resize_with ( id + 1 , Default :: default ) ;
9298 }
93- self . textures [ id] . fulfill ( || load_texture ( factory, & icons. get_icon ( id) . bitmap ) )
99+ self . textures [ id] . get_or_insert_with ( || load_texture ( factory, & icons. get_icon ( id) . bitmap ) )
94100 }
95101
96102 pub fn clear ( & mut self ) {
@@ -104,7 +110,7 @@ fn load(path: &Path) -> Option<IconFile> {
104110 Err ( err) => {
105111 eprintln ! ( "error loading icon: {}\n {}" , path. display( ) , err) ;
106112 None
107- }
113+ } ,
108114 }
109115}
110116
@@ -130,21 +136,27 @@ impl IconFile {
130136 }
131137
132138 pub fn uv_of ( & self , icon_state : & str , dir : Dir ) -> Option < [ f32 ; 4 ] > {
133- self . rect_of ( icon_state, dir) . map ( |( x1, y1, w, h) | [
134- x1 as f32 / self . width as f32 ,
135- y1 as f32 / self . height as f32 ,
136- ( x1 + w) as f32 / self . width as f32 ,
137- ( y1 + h) as f32 / self . height as f32 ,
138- ] )
139+ self . rect_of ( icon_state, dir) . map ( |( x1, y1, w, h) | {
140+ [
141+ x1 as f32 / self . width as f32 ,
142+ y1 as f32 / self . height as f32 ,
143+ ( x1 + w) as f32 / self . width as f32 ,
144+ ( y1 + h) as f32 / self . height as f32 ,
145+ ]
146+ } )
139147 }
140148
141149 #[ inline]
142150 pub fn rect_of ( & self , icon_state : & str , dir : Dir ) -> Option < Rect > {
143- self . metadata . rect_of ( self . width , icon_state, dir, 0 )
151+ self . metadata
152+ . rect_of ( self . width , & StateIndex :: from ( icon_state) , dir, 0 )
144153 }
145154}
146155
147- pub fn texture_from_bytes ( factory : & mut Factory , bytes : & [ u8 ] ) -> io:: Result < Texture > {
156+ pub fn texture_from_bytes < ' r > (
157+ factory : & ' r TextureCreator ,
158+ bytes : & [ u8 ] ,
159+ ) -> io:: Result < Texture < ' r > > {
148160 let mut decoder = Decoder :: new ( ) ;
149161 decoder. info_raw_mut ( ) . colortype = ColorType :: RGBA ;
150162 decoder. info_raw_mut ( ) . set_bitdepth ( 8 ) ;
@@ -157,22 +169,30 @@ pub fn texture_from_bytes(factory: &mut Factory, bytes: &[u8]) -> io::Result<Tex
157169 Ok ( load_texture ( factory, & bitmap) )
158170}
159171
160- pub fn load_texture ( factory : & mut Factory , bitmap : & lodepng:: Bitmap < RGBA > ) -> Texture {
172+ pub fn load_texture < ' r > (
173+ factory : & ' r TextureCreator ,
174+ bitmap : & lodepng:: Bitmap < RGBA > ,
175+ ) -> Texture < ' r > {
161176 let width = bitmap. width ;
162177 let height = bitmap. height ;
163- let mut new_buffer = Vec :: with_capacity ( 4 * width * height) ;
164- for pixel in & bitmap. buffer {
165- new_buffer. push ( pixel. r ) ;
166- new_buffer. push ( pixel. g ) ;
167- new_buffer. push ( pixel. b ) ;
168- new_buffer. push ( pixel. a ) ;
169- }
170178
171- let kind = gfx:: texture:: Kind :: D2 ( width as u16 , height as u16 , gfx:: texture:: AaMode :: Single ) ;
172- let ( _, view) = factory. create_texture_immutable_u8 :: < crate :: ColorFormat > (
173- kind,
174- gfx:: texture:: Mipmap :: Provided ,
175- & [ & new_buffer[ ..] ]
176- ) . expect ( "create_texture_immutable_u8" ) ;
177- view
179+ let mut surface = Surface :: new (
180+ width as u32 ,
181+ height as u32 ,
182+ PixelFormat :: from ( PixelFormatEnum :: RGBA8888 ) ,
183+ )
184+ . expect ( "Surface::new" ) ;
185+ surface. with_lock_mut ( |dest| {
186+ let mut dest = dest. iter_mut ( ) ;
187+ for pixel in & bitmap. buffer {
188+ * dest. next ( ) . unwrap ( ) = pixel. r ;
189+ * dest. next ( ) . unwrap ( ) = pixel. g ;
190+ * dest. next ( ) . unwrap ( ) = pixel. b ;
191+ * dest. next ( ) . unwrap ( ) = pixel. a ;
192+ }
193+ } ) ;
194+
195+ factory
196+ . create_texture_from_surface ( surface)
197+ . expect ( "create_texture_from_surface" )
178198}
0 commit comments