Squashed basically all the compile bugs in UISprite, but UIEntity and UIGrid use textures as well, so they need to be fixed too before the project will build again

This commit is contained in:
John McCardle 2024-03-16 14:52:35 -04:00
parent 47d0e34a17
commit bfd33102d1
4 changed files with 111 additions and 44 deletions

View File

@ -3,18 +3,24 @@
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;
texture = sf::Texture(source);
auto size = texture.getSize();
sheet_width = size.x;
sheet_height = size.y;
source = filename;
if (sheet_width % sprite_width != 0 || sheet_height % 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;
}
}
sf::Sprite PyTexture::sprite(int index, float x = 0.0, float y = 0.0, float s = 1.0)
sf::Sprite PyTexture::sprite(int index, sf::Vector2f pos = sf::Vector2f(0, 0), sf::Vector2f s = sf::Vector2f(1.0, 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);
sprite.setPosition(pos);
sprite.setScale(s);
return sprite;
}

View File

@ -10,5 +10,5 @@ private:
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);
sf::Sprite sprite(int index, sf::Vector2f pos = sf::Vector2f(0, 0), sf::Vector2f s = sf::Vector2f(1.0, 1.0));
};

View File

@ -168,7 +168,8 @@ void UICaption::render(sf::Vector2f offset)
}
UISprite::UISprite() {}
/*
// * tearing down the old IndexTexture way of life
UISprite::UISprite(IndexTexture* _itex, int _sprite_index, float x = 0.0, float y = 0.0, float s = 1.0)
: itex(_itex), sprite_index(_sprite_index)
{
@ -186,6 +187,13 @@ UISprite::UISprite(IndexTexture* _itex, int _sprite_index, sf::Vector2f pos, flo
sprite.setPosition(pos);
sprite.setScale(sf::Vector2f(s, s));
}
*/
UISprite::UISprite(std::shared_ptr<PyTexture> _ptex, int _sprite_index, sf::Vector2f _pos, float _scale)
: ptex(_ptex), sprite_index(_sprite_index)
{
sprite = ptex(sprite_index, _pos, sf::Vector2f(_scale, _scale));
}
//void UISprite::update()
//{
@ -212,19 +220,55 @@ void UISprite::render(sf::Vector2f offset, sf::RenderTexture& target)
sprite.move(-offset);
}
/*
void UISprite::setPosition(float x, float y)
{
setPosition(sf::Vector2f(x, y));
}
*/
void UISprite::setPosition(sf::Vector2f pos)
{
sprite.setPosition(pos);
}
void UISprite::setScale(float s)
void UISprite::setScale(sf::Vector2f s)
{
sprite.setScale(sf::Vector2f(s, s));
sprite.setScale(s);
}
void UISprite::setTexture(std::shared_ptr<PyTexture> _ptex, int _sprite_index=-1)
{
ptex = _ptex;
if (_sprite_index != -1) // if you are changing textures, there's a good chance you need a new index too
sprite_index = _sprite_index;
sprite = ptex->sprite(sprite_index, sprite.getPosition(), sprite.getScale());
}
void UISprite::setSpriteIndex(int _sprite_index)
{
sprite_index = _sprite_index;
sprite = ptex->sprite(sprite_index, sprite.getPosition(), sprite.getScale());
}
sf::Vector2f UISprite::getScale()
{
return sprite.getScale();
}
sf::Vector2f UISprite::getPosition()
{
return sprite.getPosition();
}
std::shared_ptr<PyTexture> UISprite::getTexture()
{
return ptex;
}
int UISprite::getSpriteIndex()
{
return sprite_index;
}
PyObjectsEnum UICaption::derived_type()

View File

@ -6,6 +6,7 @@
#include "Resources.h"
#include <list>
#include "PyCallable.h"
#include "PyTexture.h"
enum PyObjectsEnum : int
{
@ -97,23 +98,36 @@ public:
class UISprite: public UIDrawable
{
private:
int sprite_index;
sf::Sprite sprite;
protected:
std::shared_ptr<PyTexture> ptex;
public:
UISprite();
UISprite(IndexTexture*, int, float, float, float);
UISprite(IndexTexture*, int, sf::Vector2f, float);
//UISprite(IndexTexture*, int, float, float, float);
//UISprite(IndexTexture*, int, sf::Vector2f, float);
UISprite(std::shared_ptr<PyTexture>, int, sf::Vector2f, float);
void update();
void render(sf::Vector2f) override final;
virtual UIDrawable* click_at(sf::Vector2f point) override final;
// 7DRL hack - TODO apply RenderTexture concept to all UIDrawables (via `sf::RenderTarget`)
void render(sf::Vector2f, sf::RenderTexture&);
int /*texture_index,*/ sprite_index;
IndexTexture* itex;
//float x, y, scale;
sf::Sprite sprite;
void setPosition(float, float);
//IndexTexture* itex;
//sf::Vector2f pos;
//float scale;
//void setPosition(float, float);
void setPosition(sf::Vector2f);
void setScale(float);
sf::Vector2f getPosition();
void setScale(sf::Vector2f);
sf::Vector2f getScale();
void setSpriteIndex(int);
int getSpriteIndex();
void setTexture(std::shared_ptr<PyTexture> _ptex, int _sprite_index=-1);
std::shared_ptr<PyTexture> getTexture();
PyObjectsEnum derived_type() override final; // { return PyObjectsEnum::UISprite; };
};
@ -155,6 +169,8 @@ public:
class UIGrid: public UIDrawable
{
private:
std::shared_ptr<PyTexture> ptex;
public:
UIGrid();
UIGrid(int, int, IndexTexture*, float, float, float, float);
@ -170,7 +186,7 @@ public:
//int grid_size; // grid sizes are implied by IndexTexture now
sf::RectangleShape box;
float center_x, center_y, zoom;
IndexTexture* itex;
//IndexTexture* itex;
sf::Sprite sprite, output;
sf::RenderTexture renderTexture;
std::vector<UIGridPoint> points;
@ -1071,7 +1087,7 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
typedef struct {
PyObject_HEAD
std::shared_ptr<IndexTexture> data;
std::shared_ptr<PyTexture> data;
} PyTextureObject;
static int PyTexture_init(PyTextureObject* self, PyObject* args, PyObject* kwds)
@ -1085,9 +1101,9 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
{
return -1;
}
sf::Texture t = sf::Texture();
t.loadFromFile((std::string)filename);
self->data = std::make_shared<IndexTexture>(t, grid_size, grid_width, grid_height);
//sf::Texture t = sf::Texture();
//t.loadFromFile((std::string)filename);
self->data = std::make_shared<PyTexture>(filename, grid_size, grid_size);
return 0;
}
@ -1122,11 +1138,11 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
{
auto member_ptr = reinterpret_cast<long>(closure);
if (member_ptr == 0)
return PyFloat_FromDouble(self->data->sprite.getPosition().x);
return PyFloat_FromDouble(self->data->getPosition().x);
else if (member_ptr == 1)
return PyFloat_FromDouble(self->data->sprite.getPosition().y);
return PyFloat_FromDouble(self->data->getPosition().y);
else if (member_ptr == 2)
return PyFloat_FromDouble(self->data->sprite.getScale().x); // scale X and Y are identical, presently
return PyFloat_FromDouble(self->data->getScale().x); // scale X and Y are identical, presently
else
{
PyErr_SetString(PyExc_AttributeError, "Invalid attribute");
@ -1153,11 +1169,11 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
return -1;
}
if (member_ptr == 0) //x
self->data->sprite.setPosition(val, self->data->sprite.getPosition().y);
self->data->setPosition(sf::Vector2f(val, self->data->getPosition().y));
else if (member_ptr == 1) //y
self->data->sprite.setPosition(self->data->sprite.getPosition().x, val);
self->data->setPosition(sf::Vector2f(self->data->getPosition().x, val));
else if (member_ptr == 2) // scale
self->data->sprite.setScale(sf::Vector2f(val, val));
self->data->setScale(sf::Vector2f(val, val));
return 0;
}
@ -1171,7 +1187,7 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
return nullptr;
}
return PyLong_FromDouble(self->data->sprite_index);
return PyLong_FromDouble(self->data->getSpriteIndex());
}
@ -1188,8 +1204,9 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
PyErr_SetString(PyExc_TypeError, "Value must be an integer.");
return -1;
}
self->data->sprite_index = val;
self->data->sprite.setTextureRect(self->data->itex->spriteCoordinates(val));
//self->data->sprite_index = val;
//self->data->sprite.setTextureRect(self->data->itex->spriteCoordinates(val));
self->data->setSpriteIndex(val);
return 0;
}
@ -1218,10 +1235,10 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
std::ostringstream ss;
if (!self->data) ss << "<Sprite (invalid internal object)>";
else {
auto sprite = self->data->sprite;
ss << "<Sprite (x=" << sprite.getPosition().x << ", y=" << sprite.getPosition().y << ", " <<
"scale=" << sprite.getScale().x << ", " <<
"sprite_number=" << self->data->sprite_index << ")>";
//auto sprite = self->data->sprite;
ss << "<Sprite (x=" << self->data->getPosition().x << ", y=" << self->data->getPosition().y << ", " <<
"scale=" << self->data->getScale().x << ", " <<
"sprite_number=" << self->data->getSpriteIndex() << ")>";
}
std::string repr_str = ss.str();
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");
@ -1258,7 +1275,7 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
}
auto pytexture = (PyTextureObject*)texture;
self->data = std::make_shared<UISprite>(pytexture->data.get(), sprite_index, sf::Vector2f(x, y), scale);
self->data->sprite.setPosition(sf::Vector2f(x, y));
self->data->setPosition(sf::Vector2f(x, y));
return 0;
}
@ -1548,7 +1565,7 @@ static PyObject* PyUIEntity_get_gridstate(PyUIEntityObject* self, void* closure)
}
static PyObject* PyUIEntity_get_spritenumber(PyUIEntityObject* self, void* closure) {
return PyLong_FromDouble(self->data->sprite.sprite_index);
return PyLong_FromDouble(self->data->sprite.getSpriteIndex());
}
static int PyUIEntity_set_spritenumber(PyUIEntityObject* self, PyObject* value, void* closure) {
@ -1560,8 +1577,8 @@ static int PyUIEntity_set_spritenumber(PyUIEntityObject* self, PyObject* value,
PyErr_SetString(PyExc_TypeError, "Value must be an integer.");
return -1;
}
self->data->sprite.sprite_index = val;
self->data->sprite.sprite.setTextureRect(self->data->sprite.itex->spriteCoordinates(val)); // TODO - I don't like ".sprite.sprite" in this stack of UIEntity.UISprite.sf::Sprite
//self->data->sprite.sprite_index = val;
self->data->sprite.setSpriteIndex(val); // todone - I don't like ".sprite.sprite" in this stack of UIEntity.UISprite.sf::Sprite
return 0;
}
@ -1648,11 +1665,11 @@ static int PyUIGrid_init(PyUIGridObject* self, PyObject* args, PyObject* kwds) {
}
PyTextureObject* pyTexture = reinterpret_cast<PyTextureObject*>(textureObj);
// TODO (7DRL day 2, item 4.) use shared_ptr / PyTextureObject on UIGrid
IndexTexture* texture = pyTexture->data.get();
//IndexTexture* texture = pyTexture->data.get();
// Initialize UIGrid
//self->data = new UIGrid(grid_x, grid_y, texture, sf::Vector2f(box_x, box_y), sf::Vector2f(box_w, box_h));
self->data = std::make_shared<UIGrid>(grid_x, grid_y, texture, box_x, box_y, box_w, box_h);
self->data = std::make_shared<UIGrid>(grid_x, grid_y, pyTexture->data, box_x, box_y, box_w, box_h);
return 0; // Success
}
@ -1913,7 +1930,7 @@ static int PyUIEntity_init(PyUIEntityObject* self, PyObject* args, PyObject* kwd
self->data = std::make_shared<UIEntity>(*((PyUIGridObject*)grid)->data);
// TODO - PyTextureObjects and IndexTextures are a little bit of a mess with shared/unshared pointers
self->data->sprite = UISprite(pytexture->data.get(), sprite_index, sf::Vector2f(0,0), 1.0);
self->data->sprite = UISprite(pytexture->data, sprite_index, sf::Vector2f(0,0), 1.0);
self->data->position = sf::Vector2f(x, y);
if (grid != NULL) {
PyUIGridObject* pygrid = (PyUIGridObject*)grid;