use {
crate::{
SfResult,
cpp::FBox,
graphics::{Image, IntRect, RenderWindow, Texture},
system::Vector2u,
window::Window,
},
std::{
cell::RefCell,
io::{Read, Seek},
rc::Rc,
},
};
#[derive(Debug)]
pub struct RcTexture {
texture: Rc<RefCell<FBox<Texture>>>,
}
impl RcTexture {
#[must_use]
pub fn size(&self) -> Vector2u {
self.texture.borrow().size()
}
#[must_use]
pub fn is_smooth(&self) -> bool {
self.texture.borrow().is_smooth()
}
#[must_use]
pub fn is_repeated(&self) -> bool {
self.texture.borrow().is_repeated()
}
pub fn copy_to_image(&self) -> SfResult<FBox<Image>> {
self.texture.borrow().copy_to_image()
}
#[must_use]
pub fn is_srgb(&self) -> bool {
self.texture.borrow().is_srgb()
}
#[must_use]
pub fn native_handle(&self) -> u32 {
self.texture.borrow().native_handle()
}
pub fn bind(&self) {
self.texture.borrow().bind()
}
pub fn new() -> SfResult<RcTexture> {
Ok(RcTexture {
texture: Rc::new(RefCell::new(Texture::new()?)),
})
}
#[must_use = "Check if texture was created successfully"]
pub fn create(&mut self, width: u32, height: u32) -> SfResult<()> {
self.texture.borrow_mut().create(width, height)
}
#[must_use]
pub fn raw_texture(&self) -> &Texture {
unsafe { &*self.texture.as_ptr() }
}
pub fn load_from_memory(&mut self, mem: &[u8], area: IntRect) -> SfResult<()> {
self.texture.borrow_mut().load_from_memory(mem, area)
}
pub fn load_from_stream<T: Read + Seek>(
&mut self,
stream: &mut T,
area: IntRect,
) -> SfResult<()> {
self.texture.borrow_mut().load_from_stream(stream, area)
}
pub fn load_from_file(&mut self, filename: &str, area: IntRect) -> SfResult<()> {
self.texture.borrow_mut().load_from_file(filename, area)
}
pub fn from_file(filename: &str) -> SfResult<RcTexture> {
Ok(RcTexture {
texture: Rc::new(RefCell::new(Texture::from_file(filename)?)),
})
}
#[must_use]
pub fn from_texture(texture: FBox<Texture>) -> RcTexture {
RcTexture {
texture: Rc::new(RefCell::new(texture)),
}
}
pub fn load_from_image(&mut self, image: &Image, area: IntRect) -> SfResult<()> {
self.texture.borrow_mut().load_from_image(image, area)
}
pub unsafe fn update_from_window(&mut self, window: &Window, x: u32, y: u32) {
unsafe { self.texture.borrow_mut().update_from_window(window, x, y) }
}
pub unsafe fn update_from_render_window(
&mut self,
render_window: &RenderWindow,
x: u32,
y: u32,
) {
unsafe {
self.texture
.borrow_mut()
.update_from_render_window(render_window, x, y)
}
}
pub unsafe fn update_from_image(&mut self, image: &Image, x: u32, y: u32) {
unsafe { self.texture.borrow_mut().update_from_image(image, x, y) }
}
pub unsafe fn update_from_texture(&mut self, texture: &Texture, x: u32, y: u32) {
unsafe { self.texture.borrow_mut().update_from_texture(texture, x, y) }
}
pub fn update_from_pixels(&mut self, pixels: &[u8], width: u32, height: u32, x: u32, y: u32) {
self.texture
.borrow_mut()
.update_from_pixels(pixels, width, height, x, y)
}
pub fn set_smooth(&mut self, smooth: bool) {
self.texture.borrow_mut().set_smooth(smooth)
}
pub fn set_repeated(&mut self, repeated: bool) {
self.texture.borrow_mut().set_repeated(repeated)
}
#[must_use]
pub fn maximum_size() -> u32 {
Texture::maximum_size()
}
pub fn set_srgb(&mut self, srgb: bool) {
self.texture.borrow_mut().set_srgb(srgb)
}
pub fn generate_mipmap(&mut self) -> SfResult<()> {
self.texture.borrow_mut().generate_mipmap()
}
pub fn swap(&mut self, other: &mut Texture) {
self.texture.borrow_mut().swap(other)
}
pub(super) fn downgrade(&self) -> std::rc::Weak<RefCell<FBox<Texture>>> {
Rc::downgrade(&self.texture)
}
}
impl ToOwned for RcTexture {
type Owned = Self;
fn to_owned(&self) -> Self {
RcTexture {
texture: Rc::new(RefCell::new(self.texture.borrow().to_owned())),
}
}
}