From beb079a85e91aeba08d958be5d77e6ed15df3fe5 Mon Sep 17 00:00:00 2001 From: octaspire Date: Fri, 17 Jun 2022 12:23:51 +0300 Subject: [PATCH] Fix portability issues presented on macOS --- doc/API.org | 6 ++++-- src/package.lisp | 2 ++ src/system.lisp | 32 +++++++++++++++++++++++------- test/system-test.lisp | 45 ++++++++++++++++++++++++------------------- 4 files changed, 56 insertions(+), 29 deletions(-) diff --git a/doc/API.org b/doc/API.org index bff32b4..1f8108e 100644 --- a/doc/API.org +++ b/doc/API.org @@ -101,7 +101,8 @@ Helper function ~CLAMP~ is available. members ~mx~ and ~my~. Use exported symbol ~:VECTOR2F~ instead. So instead of writing ~(:STRUCT SFVECTOR2F)~, please write ~:VECTOR2F~. Additional constructor ~MAKE-VECTOR2F~ and destructor ~RELEASE-VECTOR2F~ - are available. Predicate function ~VECTOR2F-EQUAL-P~ compares one to n + are available. Helper function ~VECTOR2F-SET~ can be used to set the + components to given values. Predicate function ~VECTOR2F-EQUAL-P~ compares one to n ~VECTOR2F~ values and returns true if the given vectors are all equal (when only one time value is given, returns true). Function ~VECTOR2F-LERP~ can be used for linear interpolation between two ~:VECTOR2F~ values. @@ -111,7 +112,8 @@ Helper function ~CLAMP~ is available. members ~mx~, ~my~, and ~mz~. Use exported symbol ~:VECTOR3F~ instead. So instead of writing ~(:STRUCT SFVECTOR3F)~, please write ~:VECTOR3F~. Additional constructor ~MAKE-VECTOR3F~ and destructor ~RELEASE-VECTOR3F~ - are available. Predicate function ~VECTOR3F-EQUAL-P~ compares one to n + are available. Helper function ~VECTOR3F-SET~ can be used to set the + components to given values. Predicate function ~VECTOR3F-EQUAL-P~ compares one to n ~VECTOR3F~ values and returns true if the given vectors are all equal (when only one time value is given, returns true). Function ~VECTOR3F-LERP~ can be used for linear interpolation between two ~:VECTOR3F~ values. diff --git a/src/package.lisp b/src/package.lisp index bb53f14..8705daa 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -32,11 +32,13 @@ :vector2u-lerp :vector2f :mx :my :make-vector2f :release-vector2f + :vector2f-set :vector2f-to-str :vector2f-equal-p :vector2f-lerp :vector3f :mx :my :mz :make-vector3f :release-vector3f + :vector3f-set :vector3f-to-str :vector3f-equal-p :vector3f-lerp diff --git a/src/system.lisp b/src/system.lisp index 6b7a472..ba12a05 100644 --- a/src/system.lisp +++ b/src/system.lisp @@ -100,14 +100,22 @@ Foreign CSFML type is defined in file SFML/System/Vector2.h." "Constructor for allocating and initializing new :VECTOR2F." (let ((v (foreign-alloc :vector2f))) (with-foreign-slots ((mx my) v :vector2f) - (setf mx (float x) - my (float y))) + (setf mx (coerce x 'single-float) + my (coerce y 'single-float))) (return-from make-vector2f v))) (defun release-vector2f (self) "Release memory allocated for :VECTOR2F SELF." (foreign-free self)) +(defun vector2f-set (self &key (x 0 x-supplied-p) (y 0 y-supplied-p)) + "Helper function for setting X and Y components of :VECTOR2F SELF." + (with-foreign-slots ((mx my) self :vector2f) + (when x-supplied-p + (setf mx (coerce x 'single-float))) + (when y-supplied-p + (setf my (coerce y 'single-float))))) + ;; ;; VECTOR3 ;; @@ -133,9 +141,9 @@ Foreign CSFML type is defined in file SFML/System/Vector3.h." "Constructor for allocating and initializing new :VECTOR3F." (let ((v (foreign-alloc :vector3f))) (with-foreign-slots ((mx my mz) v :vector3f) - (setf mx (float x) - my (float y) - mz (float z))) + (setf mx (coerce x 'single-float) + my (coerce y 'single-float) + mz (coerce z 'single-float))) (return-from make-vector3f v))) (defun release-vector3f (self) @@ -144,6 +152,16 @@ Foreign CSFML type is defined in file SFML/System/Vector3.h." ;; VECTOR RELATED HELPER FUNCTIONS +(defun vector3f-set (self &key (x 0 x-supplied-p) (y 0 y-supplied-p) (z 0 z-supplied-p)) + "Helper function for setting X, Y, and Z components of :VECTO32F SELF." + (with-foreign-slots ((mx my mz) self :vector3f) + (when x-supplied-p + (setf mx (coerce x 'single-float))) + (when y-supplied-p + (setf my (coerce y 'single-float))) + (when z-supplied-p + (setf mz (coerce z 'single-float))))) + (defun vector2i-equal-p (v1 &rest vectors) "Predicate for telling if the csfml :VECTOR2I value V1 is equal to all the rest given VECTORS of type :VECTOR2I (if only one value is given, @@ -212,7 +230,7 @@ Real value A is clamped between zero and one (inclusive)." (defun vector2f-to-str (self) "Return string representation of SELF of type :VECTOR2F." - (format nil "(make-vector2f ~A ~A)" + (format nil "(make-vector2f ~f ~f)" (foreign-slot-value self :vector2f 'mx) (foreign-slot-value self :vector2f 'my))) @@ -241,7 +259,7 @@ Real value A is clamped between zero and one (inclusive)." (defun vector3f-to-str (self) "Return string representation of SELF of type :VECTOR3F." - (format nil "(make-vector3f ~A ~A ~A)" + (format nil "(make-vector3f ~f ~f ~f)" (foreign-slot-value self :vector3f 'mx) (foreign-slot-value self :vector3f 'my) (foreign-slot-value self :vector3f 'mz))) diff --git a/test/system-test.lisp b/test/system-test.lisp index e338c13..a00efd6 100644 --- a/test/system-test.lisp +++ b/test/system-test.lisp @@ -128,10 +128,9 @@ (let ((x 123.1) (y 999.2)) (with-foreign-slots ((mx my) v :vector2f) - (setf mx x - my y) - (is (= x mx) #?"X component is $(mx) when $(x) was expected.") - (is (= y my) #?"Y component is $(my) when $(y) was expected."))))) + (vector2f-set v :x x :y y) + (is (= (coerce x 'single-float) mx) #?"X component is $(mx) when $(x) was expected.") + (is (= (coerce y 'single-float) my) #?"Y component is $(my) when $(y) was expected."))))) (fiveam:test test-make-vector2f--release-vector2f @@ -139,8 +138,8 @@ (y 20.2) (v (make-vector2f x y))) (with-foreign-slots ((mx my) v :vector2f) - (is (= x mx) #?"X component is $(mx) when $(x) was expected.") - (is (= y my) #?"Y component is $(my) when $(y) was expected.")) + (is (= (coerce x 'single-float) mx) #?"X component is $(mx) when $(x) was expected.") + (is (= (coerce y 'single-float) my) #?"Y component is $(my) when $(y) was expected.")) (release-vector2f v))) ;; VECTOR3F @@ -152,27 +151,33 @@ (y 512.2) (z 999.3)) (with-foreign-slots ((mx my mz) v :vector3f) - (setf mx x - my y - mz z) - (is (= x mx) #?"X component is $(mx) when $(x) was expected.") - (is (= y my) #?"Y component is $(my) when $(y) was expected.") - (is (= z mz) #?"Z component is $(mz) when $(z) was expected."))))) + (vector3f-set v :x x :y y :z z) + (is (= (coerce x 'single-float) mx) #?"X component is $(mx) when $(x) was expected.") + (is (= (coerce y 'single-float) my) #?"Y component is $(my) when $(y) was expected.") + (is (= (coerce z 'single-float) mz) #?"Z component is $(mz) when $(z) was expected."))))) (fiveam:test - test-make-vector3f--release-vectorff + test-make-vector3f--release-vector3f (let* ((x 10.1) (y 20.2) (z 30.3) (v (make-vector3f x y z))) (with-foreign-slots ((mx my mz) v :vector3f) - (is (= x mx) #?"X component is $(mx) when $(x) was expected.") - (is (= y my) #?"Y component is $(my) when $(y) was expected.") - (is (= z mz) #?"Z component is $(mz) when $(z) was expected.")) + (is (= (coerce x 'single-float) mx) #?"X component is $(mx) when $(x) was expected.") + (is (= (coerce y 'single-float) my) #?"Y component is $(my) when $(y) was expected.") + (is (= (coerce z 'single-float) mz) #?"Z component is $(mz) when $(z) was expected.")) (release-vector3f v))) ;; VECTOR RELATED HELPER FUNCTIONS +(fiveam:test + test-vector3f-to-str-10.1-20.2-30.3 + (let* ((v (make-vector3f 10.1 20.2 30.3)) + (e "(make-vector3f 10.1 20.2 30.3)") + (s (vector3f-to-str v))) + (is (string= e s) #?"Is as string $(s) when $(e) expected") + (release-vector3f v))) + (fiveam:test test-vector2i-equal-p--10-11 (let* ((v1 (make-vector2i 10 11))) @@ -496,7 +501,7 @@ was expected."))))) (fiveam:test test-sfseconds-0-01 - (let* ((seconds 0.01) + (let* ((seconds 0.01f0) (v (sfseconds seconds)) (e 10000)) (is (= e v) @@ -531,21 +536,21 @@ was expected."))))) (fiveam:test test-time-asseconds-10000 (let* ((s (time-asseconds 10000)) - (e 0.01)) + (e 0.01f0)) (is (= e s) #?"Time was $(s) seconds when $(e) was expected."))) (fiveam:test test-time-asseconds-100000 (let* ((s (time-asseconds 100000)) - (e 0.1)) + (e 0.1f0)) (is (= e s) #?"Time was $(s) seconds when $(e) was expected."))) (fiveam:test test-time-asseconds-1000000 (let* ((s (time-asseconds 1000000)) - (e 1.0)) + (e 1.0f0)) (is (= e s) #?"Time was $(s) seconds when $(e) was expected."))) -- 2.45.2