Fixed sprite indexing error in PyTexture; needs non-square sprite tests, but feeling confident!

This commit is contained in:
John McCardle 2024-03-17 16:23:52 -04:00
parent afd4ff1925
commit 20f80c4114
1 changed files with 4 additions and 4 deletions

View File

@ -6,9 +6,9 @@ PyTexture::PyTexture(std::string filename, int sprite_w, int sprite_h)
texture = sf::Texture();
texture.loadFromFile(source);
auto size = texture.getSize();
sheet_width = size.x;
sheet_height = size.y;
if (sheet_width % sprite_width != 0 || sheet_height % sprite_height != 0)
sheet_width = (size.x / sprite_width);
sheet_height = (size.y / sprite_height);
if (size.x % sprite_width != 0 || size.y % sprite_height != 0)
{
std::cout << "Warning: Texture `" << source << "` is not an even number of sprite widths or heights across." << std::endl
<< "Sprite size given was " << sprite_w << "x" << sprite_h << "px but the file has a resolution of " << sheet_width << "x" << sheet_height << "px." << std::endl;
@ -17,7 +17,7 @@ PyTexture::PyTexture(std::string filename, int sprite_w, int sprite_h)
sf::Sprite PyTexture::sprite(int index, sf::Vector2f pos, sf::Vector2f s)
{
int tx = index % sprite_width, ty = index / sprite_height;
int tx = index % sheet_width, ty = index / sheet_width;
auto ir = sf::IntRect(tx * sprite_width, ty * sprite_height, sprite_width, sprite_height);
auto sprite = sf::Sprite(texture, ir);
sprite.setPosition(pos);