Add unit tests and fix bugs for sprites #195
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
There are currently no unit tests for sprites. A comprehensive set of tests have been added which cover most sprite methods, with the exception of those which rely on a window. In the process of creating the tests, I discovered multiple bugs which have been also been rectified.
Position and Velocity Type Changes
_sprite_data
uses thepoint_2d
andvelocity_2d
types forposition
andvelocity
respectively.point_2d
andvelocity_2d
both use thedouble
type.sprite_x
,sprite_y
,sprite_set_x
,sprite_set_y
,sprite_dx
,sprite_dy
,sprite_set_dx
, andsprite_set_dy
use thefloat
type leading to unnecessary casting. Issues arise when the value stored inpoint_2d
orvector_2d
's double is outside the range offloat
. The methods have been altered to accept and returndouble
.Sprite-Point Collision Bug at Corners
sprite_point_collision
callssprite_collision_circle
, which then callsbitmap_cell_circle
which generates acircle
which does not encompass the sprite. Points tested at the edges of the sprite will incorrectly return no collision. This occurs because the function uses the greater of a sprite's half-width or half-height as the radius. Instead, the length of the hypotenuse of a right triangle formed with the sprite's half-width and half-height as the opposite and adjacent should equal the circle's radius.bitmap_bounding_circle
has also been corrected as it contains the same bug.Point Methods Type Changes
point_in_circle
castsdouble
tolong long
which causes inaccuracy due to the rounding errors. The method has been altered to retaincircle
radius'double
precision. In addition,point_in_circle
callspoint_point_distance
which returnsfloat
instead ofdouble
.point_point_distance
has been altered to returndouble
, retaining thedouble
precision.Sprite Speed Methods Type Changes
sprite_speed
andsprite_set_speed
return and acceptfloat
, while calling the methodsvector_magnitude
,vector_multiply
, andunit_vector
which all operate atdouble
precision.sprite_speed
andsprite_set_speed
have been altered to usedouble
precision.Sprite Rotation Periodicity
sprite_set_rotation
incorrectly returns360
when the input value equals360
. Instead, it should convert360
to0
degrees.Additional Unit Tests
As the methods in
sprite.cpp
make calls to methods inimages.cpp
andpoint_geometry.cpp
, it was necessary to add unit tests for those methods. In the case of images, there is an existingunit_test_bitmap.cpp
file which I added to. For geometry tests, I created a newunit_test_geometry.cpp
file. More geometry tests could be added in the future, however at this stage I only focused on those which are relevant to sprites.Type of change
expected)
How Has This Been Tested?
Adding the new tests was achieved by running
cmake -G "Unix Makefiles" .
followed bymake
when in the/splashkit-core/projects/cmake
directory.Testing Checklist
Checklist