dora_ssr/dora/
render_target.rs

1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10	fn rendertarget_type() -> i32;
11	fn rendertarget_get_width(slf: i64) -> i32;
12	fn rendertarget_get_height(slf: i64) -> i32;
13	fn rendertarget_set_camera(slf: i64, val: i64);
14	fn rendertarget_get_camera(slf: i64) -> i64;
15	fn rendertarget_get_texture(slf: i64) -> i64;
16	fn rendertarget_render(slf: i64, target: i64);
17	fn rendertarget_render_clear(slf: i64, color: i32, depth: f32, stencil: i32);
18	fn rendertarget_render_clear_with_target(slf: i64, target: i64, color: i32, depth: f32, stencil: i32);
19	fn rendertarget_save_async(slf: i64, filename: i64, func0: i32, stack0: i64);
20	fn rendertarget_new(width: i32, height: i32) -> i64;
21}
22use crate::dora::IObject;
23/// A RenderTarget is a buffer that allows you to render a Node into a texture.
24pub struct RenderTarget { raw: i64 }
25crate::dora_object!(RenderTarget);
26impl RenderTarget {
27	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
28		(unsafe { rendertarget_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
29			match raw {
30				0 => None,
31				_ => Some(Box::new(RenderTarget { raw: raw }))
32			}
33		})
34	}
35	/// Gets the width of the rendering target.
36	pub fn get_width(&self) -> i32 {
37		return unsafe { rendertarget_get_width(self.raw()) };
38	}
39	/// Gets the height of the rendering target.
40	pub fn get_height(&self) -> i32 {
41		return unsafe { rendertarget_get_height(self.raw()) };
42	}
43	/// Sets the camera used for rendering the scene.
44	pub fn set_camera(&mut self, val: &dyn crate::dora::ICamera) {
45		unsafe { rendertarget_set_camera(self.raw(), val.raw()) };
46	}
47	/// Gets the camera used for rendering the scene.
48	pub fn get_camera(&self) -> Option<crate::dora::Camera> {
49		return unsafe { crate::dora::Camera::from(rendertarget_get_camera(self.raw())) };
50	}
51	/// Gets the texture generated by the rendering target.
52	pub fn get_texture(&self) -> crate::dora::Texture2D {
53		return unsafe { crate::dora::Texture2D::from(rendertarget_get_texture(self.raw())).unwrap() };
54	}
55	/// Renders a node to the target without replacing its previous contents.
56	///
57	/// # Arguments
58	///
59	/// * `target` - The node to be rendered onto the render target.
60	pub fn render(&mut self, target: &dyn crate::dora::INode) {
61		unsafe { rendertarget_render(self.raw(), target.raw()); }
62	}
63	/// Clears the previous color, depth and stencil values on the render target.
64	///
65	/// # Arguments
66	///
67	/// * `color` - The clear color used to clear the render target.
68	/// * `depth` - Optional. The value used to clear the depth buffer of the render target. Default is 1.
69	/// * `stencil` - Optional. The value used to clear the stencil buffer of the render target. Default is 0.
70	pub fn render_clear(&mut self, color: &crate::dora::Color, depth: f32, stencil: i32) {
71		unsafe { rendertarget_render_clear(self.raw(), color.to_argb() as i32, depth, stencil); }
72	}
73	/// Renders a node to the target after clearing the previous color, depth and stencil values on it.
74	///
75	/// # Arguments
76	///
77	/// * `target` - The node to be rendered onto the render target.
78	/// * `color` - The clear color used to clear the render target.
79	/// * `depth` - The value used to clear the depth buffer of the render target. Default can be 1.
80	/// * `stencil` - The value used to clear the stencil buffer of the render target. Default can be 0.
81	pub fn render_clear_with_target(&mut self, target: &dyn crate::dora::INode, color: &crate::dora::Color, depth: f32, stencil: i32) {
82		unsafe { rendertarget_render_clear_with_target(self.raw(), target.raw(), color.to_argb() as i32, depth, stencil); }
83	}
84	/// Saves the contents of the render target to a PNG file asynchronously.
85	///
86	/// # Arguments
87	///
88	/// * `filename` - The name of the file to save the contents to.
89	/// * `handler` - The function to call when the save operation is complete. The function will be passed a boolean value indicating whether the save operation was successful.
90	pub fn save_async(&mut self, filename: &str, mut handler: Box<dyn FnMut(bool)>) {
91		let mut stack0 = crate::dora::CallStack::new();
92		let stack_raw0 = stack0.raw();
93		let func_id0 = crate::dora::push_function(Box::new(move || {
94			handler(stack0.pop_bool().unwrap())
95		}));
96		unsafe { rendertarget_save_async(self.raw(), crate::dora::from_string(filename), func_id0, stack_raw0); }
97	}
98	pub fn new(width: i32, height: i32) -> RenderTarget {
99		unsafe { return RenderTarget { raw: rendertarget_new(width, height) }; }
100	}
101}