Skip to main content

dora_ssr/dora/
texture_2d.rs

1/* Copyright (c) 2016-2026 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 texture2d_type() -> i32;
11	fn texture2d_get_width(slf: i64) -> i32;
12	fn texture2d_get_height(slf: i64) -> i32;
13	fn texture2d_with_file(filename: i64) -> i64;
14}
15use crate::dora::IObject;
16/// A struct represents a 2D texture.
17pub struct Texture2D { raw: i64 }
18crate::dora_object!(Texture2D);
19impl Texture2D {
20	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
21		(unsafe { texture2d_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
22			match raw {
23				0 => None,
24				_ => Some(Box::new(Texture2D { raw: raw }))
25			}
26		})
27	}
28	/// Gets the width of the texture, in pixels.
29	pub fn get_width(&self) -> i32 {
30		return unsafe { texture2d_get_width(self.raw()) };
31	}
32	/// Gets the height of the texture, in pixels.
33	pub fn get_height(&self) -> i32 {
34		return unsafe { texture2d_get_height(self.raw()) };
35	}
36	/// Creates a texture object from the given file.
37	///
38	/// # Arguments
39	///
40	/// * `filename` - The file name of the texture.
41	///
42	/// # Returns
43	///
44	/// * `Texture2D` - The texture object.
45	pub fn with_file(filename: &str) -> Option<Texture2D> {
46		unsafe { return Texture2D::from(texture2d_with_file(crate::dora::from_string(filename))); }
47	}
48}