@@ -662,7 +662,7 @@ impl BitOr<Rect> for Rect {
662662 }
663663}
664664
665- /// Immutable point type, consisting of x and y.
665+ /// Immutable point type, consisting of integer x and y.
666666#[ derive( Copy , Clone ) ]
667667pub struct Point {
668668 raw : sys:: SDL_Point ,
@@ -897,6 +897,209 @@ impl std::iter::Sum for Point {
897897 }
898898}
899899
900+ /// Immutable point type, consisting of floating point x and y.
901+ #[ derive( Copy , Clone ) ]
902+ pub struct FPoint {
903+ raw : sys:: SDL_FPoint ,
904+ }
905+
906+ impl :: std:: fmt:: Debug for FPoint {
907+ fn fmt ( & self , fmt : & mut :: std:: fmt:: Formatter ) -> Result < ( ) , :: std:: fmt:: Error > {
908+ return write ! ( fmt, "FPoint {{ x: {}, y: {} }}" , self . raw. x, self . raw. y) ;
909+ }
910+ }
911+
912+ impl PartialEq for FPoint {
913+ fn eq ( & self , other : & FPoint ) -> bool {
914+ self . raw . x == other. raw . x && self . raw . y == other. raw . y
915+ }
916+ }
917+
918+ impl Deref for FPoint {
919+ type Target = sys:: SDL_FPoint ;
920+
921+ /// # Example
922+ ///
923+ /// ```rust
924+ /// use sdl2::rect::FPoint;
925+ /// let point = FPoint::new(2.0, 3.0);
926+ /// assert_eq!(2.0, point.x);
927+ /// ```
928+ fn deref ( & self ) -> & sys:: SDL_FPoint {
929+ & self . raw
930+ }
931+ }
932+
933+ impl DerefMut for FPoint {
934+ /// # Example
935+ ///
936+ /// ```rust
937+ /// use sdl2::rect::FPoint;
938+ /// let mut point = FPoint::new(2.0, 3.0);
939+ /// point.x = 4.0;
940+ /// assert_eq!(4.0, point.x);
941+ /// ```
942+ fn deref_mut ( & mut self ) -> & mut sys:: SDL_FPoint {
943+ & mut self . raw
944+ }
945+ }
946+
947+ impl AsRef < sys:: SDL_FPoint > for FPoint {
948+ fn as_ref ( & self ) -> & sys:: SDL_FPoint {
949+ & self . raw
950+ }
951+ }
952+
953+ impl AsMut < sys:: SDL_FPoint > for FPoint {
954+ fn as_mut ( & mut self ) -> & mut sys:: SDL_FPoint {
955+ & mut self . raw
956+ }
957+ }
958+
959+ impl From < sys:: SDL_FPoint > for FPoint {
960+ fn from ( prim : sys:: SDL_FPoint ) -> FPoint {
961+ FPoint { raw : prim }
962+ }
963+ }
964+
965+ impl From < ( f32 , f32 ) > for FPoint {
966+ fn from ( ( x, y) : ( f32 , f32 ) ) -> FPoint {
967+ FPoint :: new ( x, y)
968+ }
969+ }
970+
971+ impl Into < sys:: SDL_FPoint > for FPoint {
972+ fn into ( self ) -> sys:: SDL_FPoint {
973+ self . raw
974+ }
975+ }
976+
977+ impl Into < ( f32 , f32 ) > for FPoint {
978+ fn into ( self ) -> ( f32 , f32 ) {
979+ ( self . x ( ) , self . y ( ) )
980+ }
981+ }
982+
983+ impl FPoint {
984+ /// Creates a new FPoint from the given coordinates.
985+ pub fn new ( x : f32 , y : f32 ) -> FPoint {
986+ FPoint {
987+ raw : sys:: SDL_FPoint { x, y } ,
988+ }
989+ }
990+
991+ pub fn from_ll ( raw : sys:: SDL_FPoint ) -> FPoint {
992+ FPoint :: new ( raw. x , raw. y )
993+ }
994+
995+ #[ doc( alias = "SDL_FPoint" ) ]
996+ pub fn raw_slice ( slice : & [ FPoint ] ) -> * const sys:: SDL_FPoint {
997+ slice. as_ptr ( ) as * const sys:: SDL_FPoint
998+ }
999+ // this can prevent introducing UB until
1000+ // https://github.com/rust-lang/rust-clippy/issues/5953 is fixed
1001+ #[ allow( clippy:: trivially_copy_pass_by_ref) ]
1002+ pub fn raw ( & self ) -> * const sys:: SDL_FPoint {
1003+ & self . raw
1004+ }
1005+
1006+ /// Returns a new FPoint by shifting this point's coordinates by the given
1007+ /// x and y values.
1008+ pub fn offset ( self , x : f32 , y : f32 ) -> FPoint {
1009+ FPoint :: new ( self . raw . x + x, self . raw . y + y)
1010+ }
1011+
1012+ /// Returns a new point by multiplying this point's coordinates by the
1013+ /// given scale factor.
1014+ pub fn scale ( self , f : f32 ) -> FPoint {
1015+ FPoint :: new ( self . raw . x * f, self . raw . y * f)
1016+ }
1017+
1018+ /// Returns the x-coordinate of this point.
1019+ pub fn x ( self ) -> f32 {
1020+ self . raw . x
1021+ }
1022+
1023+ /// Returns the y-coordinate of this point.
1024+ pub fn y ( self ) -> f32 {
1025+ self . raw . y
1026+ }
1027+ }
1028+
1029+ impl Add for FPoint {
1030+ type Output = FPoint ;
1031+
1032+ fn add ( self , rhs : FPoint ) -> FPoint {
1033+ self . offset ( rhs. x ( ) , rhs. y ( ) )
1034+ }
1035+ }
1036+
1037+ impl AddAssign for FPoint {
1038+ fn add_assign ( & mut self , rhs : FPoint ) {
1039+ self . raw . x = self . x ( ) + rhs. x ( ) ;
1040+ self . raw . y = self . y ( ) + rhs. y ( ) ;
1041+ }
1042+ }
1043+
1044+ impl Neg for FPoint {
1045+ type Output = FPoint ;
1046+
1047+ fn neg ( self ) -> FPoint {
1048+ FPoint :: new ( -self . x ( ) , -self . y ( ) )
1049+ }
1050+ }
1051+
1052+ impl Sub for FPoint {
1053+ type Output = FPoint ;
1054+
1055+ fn sub ( self , rhs : FPoint ) -> FPoint {
1056+ self . offset ( -rhs. x ( ) , -rhs. y ( ) )
1057+ }
1058+ }
1059+
1060+ impl SubAssign for FPoint {
1061+ fn sub_assign ( & mut self , rhs : FPoint ) {
1062+ self . raw . x = self . x ( ) - rhs. x ( ) ;
1063+ self . raw . y = self . y ( ) - rhs. y ( ) ;
1064+ }
1065+ }
1066+
1067+ impl Mul < f32 > for FPoint {
1068+ type Output = FPoint ;
1069+
1070+ fn mul ( self , rhs : f32 ) -> FPoint {
1071+ self . scale ( rhs)
1072+ }
1073+ }
1074+
1075+ impl MulAssign < f32 > for FPoint {
1076+ fn mul_assign ( & mut self , rhs : f32 ) {
1077+ self . raw . x = self . x ( ) * rhs;
1078+ self . raw . y = self . y ( ) * rhs;
1079+ }
1080+ }
1081+
1082+ impl Div < f32 > for FPoint {
1083+ type Output = FPoint ;
1084+
1085+ fn div ( self , rhs : f32 ) -> FPoint {
1086+ FPoint :: new ( self . x ( ) / rhs, self . y ( ) / rhs)
1087+ }
1088+ }
1089+
1090+ impl DivAssign < f32 > for FPoint {
1091+ fn div_assign ( & mut self , rhs : f32 ) {
1092+ self . raw . x /= rhs;
1093+ self . raw . y /= rhs;
1094+ }
1095+ }
1096+
1097+ impl std:: iter:: Sum for FPoint {
1098+ fn sum < I : Iterator < Item = Self > > ( iter : I ) -> Self {
1099+ iter. fold ( FPoint :: new ( 0.0 , 0.0 ) , FPoint :: add)
1100+ }
1101+ }
1102+
9001103#[ cfg( test) ]
9011104mod test {
9021105 use super :: { max_int_value, min_int_value, Point , Rect } ;
0 commit comments