Messy, but monumental: PyTexture::pyObject works

this also coincidentally fixes a weird bug I encountered while
(mis?)using tp_alloc: by using PyType_GenericAlloc, I avoid the segfault
that tp_alloc sometimes causes. See the horrible UIDrawable retrieval
macro that I use in UI.h for a workaround that can probably be replaced
with this technique
This commit is contained in:
John McCardle 2024-03-21 21:39:15 -04:00
commit d7228172c4
5 changed files with 110 additions and 51 deletions

View file

@ -8,6 +8,8 @@
#include "PyCallable.h"
#include "PyTexture.h"
using namespace mcrfpydef;
enum PyObjectsEnum : int
{
UIFRAME = 1,
@ -187,6 +189,7 @@ public:
sf::RectangleShape box;
float center_x, center_y, zoom;
//IndexTexture* itex;
std::shared_ptr<PyTexture> getTexture();
sf::Sprite sprite, output;
sf::RenderTexture renderTexture;
std::vector<UIGridPoint> points;
@ -1215,7 +1218,8 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
static PyObject* PyUISprite_get_texture(PyUISpriteObject* self, void* closure)
{
return NULL;
std::cout << "Calling pyObject" << std::endl;
return self->data->getTexture()->pyObject();
}
static int PyUISprite_set_texture(PyUISpriteObject* self, PyObject* value, void* closure)
@ -1791,6 +1795,12 @@ static PyObject* PyUIGrid_get_texture(PyUIGridObject* self, void* closure) {
return self->texture;
}
*/
static PyObject* PyUIGrid_get_texture(PyUIGridObject* self, void* closure) {
//return self->data->getTexture()->pyObject();
PyTextureObject* obj = (PyTextureObject*)((&PyTextureType)->tp_alloc(&PyTextureType, 0));
obj->data = self->data->getTexture();
return (PyObject*)obj;
}
static PyObject* PyUIGrid_at(PyUIGridObject* self, PyObject* o)
{
@ -1842,7 +1852,7 @@ static PyGetSetDef PyUIGrid_getsetters[] = {
{"click", (getter)PyUIDrawable_get_click, (setter)PyUIDrawable_set_click, "Object called with (x, y, button) when clicked", (void*)PyObjectsEnum::UIGRID},
//{"texture", (getter)PyUIGrid_get_texture, NULL, "Texture of the grid", NULL}, //TODO 7DRL-day2-item5
{"texture", (getter)PyUIGrid_get_texture, NULL, "Texture of the grid", NULL}, //TODO 7DRL-day2-item5
{NULL} /* Sentinel */
};