Skip to content

Commit a74e8ae

Browse files
Merge pull request jeremyletang#302 from dogunbound/rc_from_texture
Implemented a convenience method that converts Texture to RcTexture
2 parents e639fd8 + f7f123b commit a74e8ae

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

examples/rc-resources.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use sfml::{
22
graphics::{
3-
Color, RcFont, RcSprite, RcText, RcTexture, RenderTarget, RenderWindow, Transformable,
3+
Color, RcFont, RcSprite, RcText, RcTexture, RenderTarget, RenderWindow, Texture,
4+
Transformable,
45
},
56
system::Vector2f,
67
window::{Event, Style},
@@ -97,19 +98,26 @@ impl FloatingResource {
9798
}
9899
}
99100

101+
fn test_getting_rc_texture_from_texture() -> RcTexture {
102+
RcTexture::from_texture(Texture::from_file(example_res!("frank.jpeg")).unwrap())
103+
}
104+
100105
fn main() {
101106
let mut window =
102107
RenderWindow::new((800, 600), "SFML window", Style::CLOSE, &Default::default());
103108
window.set_framerate_limit(60);
104109

105110
// Create a new texture.
106111
let texture = RcTexture::from_file(example_res!("logo.png")).unwrap();
112+
let texture2 = test_getting_rc_texture_from_texture();
107113

108114
// Create a new font.
109115
let font = RcFont::from_file(example_res!("sansation.ttf")).unwrap();
110116

111117
// Load many resources with no lifetime contingencies
112118
let mut floating_resources = Vec::from([
119+
FloatingResource::with_texture(&texture2, true, true, 1.1f32),
120+
FloatingResource::with_texture(&texture2, true, true, 1.2f32),
113121
FloatingResource::with_texture(&texture, true, true, 1f32),
114122
FloatingResource::with_texture(&texture, true, false, 1.5f32),
115123
FloatingResource::with_texture(&texture, false, true, 2f32),

src/graphics/rc_texture.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ impl RcTexture {
172172
})
173173
}
174174

175+
/// Convenience method to easily move a [`Texture`] into a `RcTexture`
176+
#[must_use]
177+
pub fn from_texture(texture: SfBox<Texture>) -> RcTexture {
178+
RcTexture {
179+
texture: Rc::new(RefCell::new(texture)),
180+
}
181+
}
182+
175183
/// Load texture from an image
176184
///
177185
/// # Arguments

0 commit comments

Comments
 (0)