Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests and fix bugs for sprites #195

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from

Conversation

matt439
Copy link
Contributor

@matt439 matt439 commented Dec 13, 2024

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 the point_2d and velocity_2d types for position and velocity respectively. point_2d and velocity_2d both use the double type. sprite_x, sprite_y, sprite_set_x, sprite_set_y, sprite_dx, sprite_dy, sprite_set_dx, and sprite_set_dy use the float type leading to unnecessary casting. Issues arise when the value stored in point_2d or vector_2d's double is outside the range of float. The methods have been altered to accept and return double.

Sprite-Point Collision Bug at Corners

sprite_point_collision calls sprite_collision_circle, which then calls bitmap_cell_circle which generates a circle 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 casts double to long long which causes inaccuracy due to the rounding errors. The method has been altered to retain circle radius' double precision. In addition, point_in_circle calls point_point_distance which returns float instead of double. point_point_distance has been altered to return double, retaining the double precision.

Sprite Speed Methods Type Changes

sprite_speed and sprite_set_speed return and accept float, while calling the methods vector_magnitude, vector_multiply, and unit_vector which all operate at double precision. sprite_speed and sprite_set_speed have been altered to use double precision.

Sprite Rotation Periodicity

sprite_set_rotation incorrectly returns 360 when the input value equals 360. Instead, it should convert 360 to 0 degrees.

Additional Unit Tests

As the methods in sprite.cpp make calls to methods in images.cpp and point_geometry.cpp, it was necessary to add unit tests for those methods. In the case of images, there is an existing unit_test_bitmap.cpp file which I added to. For geometry tests, I created a new unit_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

  • Breaking change (fix or feature that would cause existing functionality to not work as
    expected)

How Has This Been Tested?

Adding the new tests was achieved by running cmake -G "Unix Makefiles" . followed by make when in the /splashkit-core/projects/cmake directory.

Testing Checklist

  • Tested with skunit_tests

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant