Unsaved changes from last night

This commit is contained in:
John McCardle 2024-03-24 08:36:06 -04:00
parent 2cac6f03c6
commit 79090b553f
2 changed files with 57 additions and 13 deletions

View File

@ -5,29 +5,30 @@ PyColor::PyColor()
{ {
} }
PyObject* PyTexture::pyObject() PyObject* PyColor::pyObject()
{ {
PyObject* obj = PyType_GenericAlloc(&mcrfpydef::PyColorType, 0); PyObject* obj = PyType_GenericAlloc(&mcrfpydef::PyColorType, 0);
try { ((PyColorObject*)obj.data = data;
((PyTextureObject*)obj)->data = shared_from_this();
}
catch (std::bad_weak_ptr& e)
{
std::cout << "Bad weak ptr: shared_from_this() failed in PyTexture::pyObject(); did you create a PyTexture outside of std::make_shared? enjoy your segfault, soon!" << std::endl;
}
// TODO - shared_from_this will raise an exception if the object does not have a shared pointer. Constructor should be made private; write a factory function
return obj; return obj;
} }
Py_hash_t PyTexture::hash(PyObject* obj) Py_hash_t PyColor::hash(PyObject* obj)
{ {
auto self = (PyTextureObject*)obj; auto self = (PyTextureObject*)obj;
return reinterpret_cast<Py_hash_t>(self->data.get()); Py_hash_t value = 0;
value += obj.data.r;
value << 8; value += obj.data.g;
value << 8; value += obj.data.b;
value << 8; value += obj.data.a;
value << (sizeof(*UIDrawable) * 8);
if (auto ptr = self.data.parent.lock())
value += reinterpret_cast<Py_hash_t>(ptr);
return value;
} }
int PyTexture::init(PyTextureObject* self, PyObject* args, PyObject* kwds) int PyColor::init(PyColorObject* self, PyObject* args, PyObject* kwds)
{ {
static const char* keywords[] = { "filename", "sprite_width", "sprite_height", nullptr }; static const char* keywords[] = { "r", "g", "b", "a", nullptr };
char* filename; char* filename;
int sprite_width, sprite_height; int sprite_width, sprite_height;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "sii", const_cast<char**>(keywords), &filename, &sprite_width, &sprite_height)) if (!PyArg_ParseTupleAndKeywords(args, kwds, "sii", const_cast<char**>(keywords), &filename, &sprite_width, &sprite_height))

View File

@ -20,11 +20,53 @@ class PyColor
private: private:
_PyColorData data; _PyColorData data;
public: public:
void set(sf::Color);
sf::Color get();
PyObject* pyObject(); PyObject* pyObject();
static Py_hash_t hash(PyObject*); static Py_hash_t hash(PyObject*);
static int init(PyColorObject*, PyObject*, PyObject*); static int init(PyColorObject*, PyObject*, PyObject*);
static PyObject* pynew(PyTypeObject* type, PyObject* args=NULL, PyObject* kwds=NULL); static PyObject* pynew(PyTypeObject* type, PyObject* args=NULL, PyObject* kwds=NULL);
static PyGetSetDef getsetters[] = {
{"r", (getter)PyColor_get_member, (setter)PyColor_set_member, "Red component", (void*)0},
{"g", (getter)PyColor_get_member, (setter)PyColor_set_member, "Green component", (void*)1},
{"b", (getter)PyColor_get_member, (setter)PyColor_set_member, "Blue component", (void*)2},
{"a", (getter)PyColor_get_member, (setter)PyColor_set_member, "Alpha component", (void*)3},
{NULL}
}; };
};
// static PyTypeObject PyColorType = {
// //PyVarObject_HEAD_INIT(NULL, 0)
// .tp_name = "mcrfpy.Color",
// .tp_basicsize = sizeof(PyColorObject),
// .tp_itemsize = 0,
// .tp_dealloc = (destructor)[](PyObject* self)
// {
// PyColorObject* obj = (PyColorObject*)self;
// obj->data.reset();
// Py_TYPE(self)->tp_free(self);
// },
// //.tp_repr = (reprfunc)PyUIFrame_repr,
// //.tp_hash = NULL,
// //.tp_iter
// //.tp_iternext
// .tp_flags = Py_TPFLAGS_DEFAULT,
// .tp_doc = PyDoc_STR("SFML Color object (RGBA)"),
// //.tp_methods = PyUIFrame_methods,
// //.tp_members = PyColor_members,
// .tp_getset = PyColor_getsetters,
// //.tp_base = NULL,
// //.tp_init = (initproc)PyUIFrame_init,
// .tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject*
// {
// PyColorObject* self = (PyColorObject*)type->tp_alloc(type, 0);
// if (self) self->data = std::make_shared<sf::Color>();
// return (PyObject*)self;
// }
// };
namespace mcrfpydef { namespace mcrfpydef {
static PyTypeObject PyColorType = { static PyTypeObject PyColorType = {
@ -34,6 +76,7 @@ namespace mcrfpydef {
.tp_hash = PyColor::hash, .tp_hash = PyColor::hash,
.tp_flags = Py_TPFLAGS_DEFAULT, .tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("SFML Color Object"), .tp_doc = PyDoc_STR("SFML Color Object"),
.tp_getset = PyColor::getsetters,
.tp_init = (initproc)PyColor::init, .tp_init = (initproc)PyColor::init,
.tp_new = PyTColor::pynew, .tp_new = PyTColor::pynew,
}; };