Implement sprite index validation for Issue #33
Added validation to prevent setting sprite indices outside the valid range for a texture. The implementation: - Adds getSpriteCount() method to PyTexture to expose total sprites - Validates sprite_number setter to ensure index is within bounds - Provides clear error messages showing valid range - Works for both Sprite and Entity objects closes #33
This commit is contained in:
parent
1e7f5e9e7e
commit
cb0130b46e
5 changed files with 153 additions and 2 deletions
|
|
@ -19,6 +19,7 @@ public:
|
|||
int sprite_width, sprite_height; // just use them read only, OK?
|
||||
PyTexture(std::string filename, int sprite_w, int sprite_h);
|
||||
sf::Sprite sprite(int index, sf::Vector2f pos = sf::Vector2f(0, 0), sf::Vector2f s = sf::Vector2f(1.0, 1.0));
|
||||
int getSpriteCount() const { return sheet_width * sheet_height; }
|
||||
|
||||
PyObject* pyObject();
|
||||
static PyObject* repr(PyObject*);
|
||||
|
|
|
|||
|
|
@ -151,6 +151,20 @@ int UISprite::set_int_member(PyUISpriteObject* self, PyObject* value, void* clos
|
|||
PyErr_SetString(PyExc_TypeError, "Value must be an integer.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Validate sprite index is within texture bounds
|
||||
auto texture = self->data->getTexture();
|
||||
if (texture) {
|
||||
int sprite_count = texture->getSpriteCount();
|
||||
|
||||
if (val < 0 || val >= sprite_count) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"Sprite index %d out of range. Texture has %d sprites (0-%d)",
|
||||
val, sprite_count, sprite_count - 1);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
self->data->setSpriteIndex(val);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue