Initial PyTexture class

no testing done.
should enable rectangular (non-square) textures

"sprite" method; let's just overwrite sprites with texture coords
Hoping to replace awful code like:
`self->data->sprite.sprite.setTextureRect(self->data->sprite.itex->spriteCoordinates(val));`

with something like:
`self->data->sprite = self->data->texture->sprite(val);`
This commit is contained in:
John McCardle 2024-03-16 11:31:39 -04:00
parent cdaf309272
commit 47d0e34a17
2 changed files with 34 additions and 0 deletions

20
src/PyTexture.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "PyTexture.h"
PyTexture::PyTexture(std::string filename, int sprite_w, int sprite_h)
: sprite_width(sprite_w), sprite_height(sprite_h)
{
// TODO - get image resolution and get sheet width and height
sheet_width = 0;
sheet_height = 0;
source = filename;
}
sf::Sprite PyTexture::sprite(int index, float x = 0.0, float y = 0.0, float s = 1.0)
{
int tx = index % sprite_width, ty = index / sprite_height;
auto ir = sf::IntRect(tx * sprite_width, ty * sprite_height, sprite_width, sprite_height);
auto sprite = sf::Sprite(texture, ir);
sprite.setPosition(x, y);
sprite.setScale(s, s);
return sprite;
}

14
src/PyTexture.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include "Common.h"
#include "Python.h"
class PyTexture
{
private:
sf::Texture texture;
std::string source;
int sprite_width, sprite_height, sheet_width, sheet_height;
public:
PyTexture(std::string filename, int sprite_w, int sprite_h);
sf::Sprite sprite(int index, float x = 0.0, float y = 0.0, float s = 1.0);
};