Dabbling around this morning; still not building

This commit is contained in:
John McCardle 2024-03-24 21:19:37 -04:00
parent 79090b553f
commit 13672c8fdb
3 changed files with 132 additions and 105 deletions

View File

@ -1,43 +1,61 @@
#include "PyColor.h" #include "PyColor.h"
PyColor::PyColor(sf::Color* target, std::weak_ptr<UIDrawable> parent, int index)
PyColor::PyColor()
{ {
data.index = index;
data.parent = parent;
data.target = target;
}
PyColor::PyColor(sf::Color target)
{
data.index = PyColor::SELF_OWNED;
data.parent = std::weak_ptr<UIDrawable>();
data.target = new sf::Color;
}
PyColor::~PyColor()
{
if (data.index == PyColor::SELF_OWNED)
delete data.target;
} }
PyObject* PyColor::pyObject() PyObject* PyColor::pyObject()
{ {
PyObject* obj = PyType_GenericAlloc(&mcrfpydef::PyColorType, 0); PyObject* obj = PyType_GenericAlloc(&mcrfpydef::PyColorType, 0);
((PyColorObject*)obj.data = data; ((PyColorObject*)obj->data = data;
return obj; return obj;
} }
Py_hash_t PyColor::hash(PyObject* obj) Py_hash_t PyColor::hash(PyObject* obj)
{ {
auto self = (PyTextureObject*)obj; auto self = (PyColorObject*)obj;
Py_hash_t value = 0; Py_hash_t value = 0;
value += obj.data.r; auto ptr = self.data.parent.lock();
value << 8; value += obj.data.g; if (ptr || self->data.index == PyColor::SELF_OWNED)
value << 8; value += obj.data.b; {
value << 8; value += obj.data.a; value += self->data.target->r;
value << 8; value += self->data.target->g;
value << 8; value += self->data.target->b;
value << 8; value += self->data.target->a;
}
if (ptr)
{
value << (sizeof(*UIDrawable) * 8); value << (sizeof(*UIDrawable) * 8);
if (auto ptr = self.data.parent.lock())
value += reinterpret_cast<Py_hash_t>(ptr); value += reinterpret_cast<Py_hash_t>(ptr);
}
return value; return value;
} }
int PyColor::init(PyColorObject* self, PyObject* args, PyObject* kwds) int PyColor::init(PyColorObject* self, PyObject* args, PyObject* kwds)
{ {
static const char* keywords[] = { "r", "g", "b", "a", nullptr }; static const char* keywords[] = { "r", "g", "b", "a", nullptr };
char* filename;
int sprite_width, sprite_height;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "sii", const_cast<char**>(keywords), &filename, &sprite_width, &sprite_height))
return -1;
self->data = std::make_shared<PyTexture>(filename, sprite_width, sprite_height);
return 0; return 0;
} }
PyObject* PyTexture::pynew(PyTypeObject* type, PyObject* args, PyObject* kwds) PyObject* PyColor::pynew(PyTypeObject* type, PyObject* args, PyObject* kwds)
{ {
return (PyObject*)type->tp_alloc(type, 0); return (PyObject*)type->tp_alloc(type, 0);
} }

View File

@ -3,6 +3,7 @@
#include "Python.h" #include "Python.h"
class PyColor; class PyColor;
class UIDrawable; // forward declare for pointer
typedef struct { typedef struct {
sf::Color* target; // color target to set/get sf::Color* target; // color target to set/get
@ -13,13 +14,16 @@ typedef struct {
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
_PyColorData data; _PyColorData data;
} PyTextureObject; } PyColorObject;
class PyColor class PyColor
{ {
private: private:
_PyColorData data; _PyColorData data;
static int SELF_OWNED = -1;
public: public:
PyColor(sf::Color* target, std::weak_ptr<UIDrawable> parent, int index) // linked constructor
PyColor::PyColor(sf::Color target) // simple color container
void set(sf::Color); void set(sf::Color);
sf::Color get(); sf::Color get();
PyObject* pyObject(); PyObject* pyObject();
@ -27,11 +31,14 @@ public:
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 PyObject* get_member(PyObject*, void*);
static int set_member(PyObject*, PyObject*, void*);
static PyGetSetDef getsetters[] = { static PyGetSetDef getsetters[] = {
{"r", (getter)PyColor_get_member, (setter)PyColor_set_member, "Red component", (void*)0}, {"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}, {"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}, {"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}, {"a", (getter)PyColor::get_member, (setter)PyColor::set_member, "Alpha component", (void*)3},
{NULL} {NULL}
}; };
}; };
@ -78,6 +85,6 @@ namespace mcrfpydef {
.tp_doc = PyDoc_STR("SFML Color Object"), .tp_doc = PyDoc_STR("SFML Color Object"),
.tp_getset = PyColor::getsetters, .tp_getset = PyColor::getsetters,
.tp_init = (initproc)PyColor::init, .tp_init = (initproc)PyColor::init,
.tp_new = PyTColor::pynew, .tp_new = PyColor::pynew,
}; };
} }

168
src/UI.h
View File

@ -46,10 +46,12 @@ typedef struct {
} PyColorObject; } PyColorObject;
*/ */
/* // Moved to PyColor.h
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
std::shared_ptr<sf::Color> data; std::shared_ptr<sf::Color> data;
} PyColorObject; } PyColorObject;
*/
class UIFrame: public UIDrawable class UIFrame: public UIDrawable
{ {
@ -444,89 +446,89 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
*/ */
static PyObject* PyColor_get_member(PyColorObject* self, void* closure) // static PyObject* PyColor_get_member(PyColorObject* self, void* closure)
{ // {
auto member_ptr = reinterpret_cast<long>(closure); // auto member_ptr = reinterpret_cast<long>(closure);
if (member_ptr == 0) // if (member_ptr == 0)
return PyLong_FromLong(self->data->r); // return PyLong_FromLong(self->data->r);
else if (member_ptr == 1) // else if (member_ptr == 1)
return PyLong_FromLong(self->data->g); // return PyLong_FromLong(self->data->g);
else if (member_ptr == 2) // else if (member_ptr == 2)
return PyLong_FromLong(self->data->b); // return PyLong_FromLong(self->data->b);
else if (member_ptr == 3) // else if (member_ptr == 3)
return PyLong_FromLong(self->data->a); // return PyLong_FromLong(self->data->a);
else // else
{ // {
PyErr_SetString(PyExc_AttributeError, "Invalid attribute"); // PyErr_SetString(PyExc_AttributeError, "Invalid attribute");
return nullptr; // return nullptr;
} // }
} // }
//
static int PyColor_set_member(PyColorObject* self, PyObject* value, void* closure) // static int PyColor_set_member(PyColorObject* self, PyObject* value, void* closure)
{ // {
if (PyLong_Check(value)) // if (PyLong_Check(value))
{ // {
long int_val = PyLong_AsLong(value); // long int_val = PyLong_AsLong(value);
if (int_val < 0) // if (int_val < 0)
int_val = 0; // int_val = 0;
else if (int_val > 255) // else if (int_val > 255)
int_val = 255; // int_val = 255;
auto member_ptr = reinterpret_cast<long>(closure); // auto member_ptr = reinterpret_cast<long>(closure);
if (member_ptr == 0) // if (member_ptr == 0)
self->data->r = static_cast<sf::Uint8>(int_val); // self->data->r = static_cast<sf::Uint8>(int_val);
else if (member_ptr == 1) // else if (member_ptr == 1)
self->data->g = static_cast<sf::Uint8>(int_val); // self->data->g = static_cast<sf::Uint8>(int_val);
else if (member_ptr == 2) // else if (member_ptr == 2)
self->data->b = static_cast<sf::Uint8>(int_val); // self->data->b = static_cast<sf::Uint8>(int_val);
else if (member_ptr == 3) // else if (member_ptr == 3)
self->data->a = static_cast<sf::Uint8>(int_val); // self->data->a = static_cast<sf::Uint8>(int_val);
} // }
else // else
{ // {
PyErr_SetString(PyExc_TypeError, "Value must be an integer."); // PyErr_SetString(PyExc_TypeError, "Value must be an integer.");
return -1; // return -1;
} // }
return 0; // return 0;
} // }
//
static PyGetSetDef PyColor_getsetters[] = { // static PyGetSetDef PyColor_getsetters[] = {
{"r", (getter)PyColor_get_member, (setter)PyColor_set_member, "Red component", (void*)0}, // {"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}, // {"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}, // {"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}, // {"a", (getter)PyColor_get_member, (setter)PyColor_set_member, "Alpha component", (void*)3},
{NULL} // {NULL}
}; // };
//
//
static PyTypeObject PyColorType = { // static PyTypeObject PyColorType = {
//PyVarObject_HEAD_INIT(NULL, 0) // //PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "mcrfpy.Color", // .tp_name = "mcrfpy.Color",
.tp_basicsize = sizeof(PyColorObject), // .tp_basicsize = sizeof(PyColorObject),
.tp_itemsize = 0, // .tp_itemsize = 0,
.tp_dealloc = (destructor)[](PyObject* self) // .tp_dealloc = (destructor)[](PyObject* self)
{ // {
PyColorObject* obj = (PyColorObject*)self; // PyColorObject* obj = (PyColorObject*)self;
obj->data.reset(); // obj->data.reset();
Py_TYPE(self)->tp_free(self); // Py_TYPE(self)->tp_free(self);
}, // },
//.tp_repr = (reprfunc)PyUIFrame_repr, // //.tp_repr = (reprfunc)PyUIFrame_repr,
//.tp_hash = NULL, // //.tp_hash = NULL,
//.tp_iter // //.tp_iter
//.tp_iternext // //.tp_iternext
.tp_flags = Py_TPFLAGS_DEFAULT, // .tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("SFML Color object (RGBA)"), // .tp_doc = PyDoc_STR("SFML Color object (RGBA)"),
//.tp_methods = PyUIFrame_methods, // //.tp_methods = PyUIFrame_methods,
//.tp_members = PyColor_members, // //.tp_members = PyColor_members,
.tp_getset = PyColor_getsetters, // .tp_getset = PyColor_getsetters,
//.tp_base = NULL, // //.tp_base = NULL,
//.tp_init = (initproc)PyUIFrame_init, // //.tp_init = (initproc)PyUIFrame_init,
.tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject* // .tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject*
{ // {
PyColorObject* self = (PyColorObject*)type->tp_alloc(type, 0); // PyColorObject* self = (PyColorObject*)type->tp_alloc(type, 0);
if (self) self->data = std::make_shared<sf::Color>(); // if (self) self->data = std::make_shared<sf::Color>();
return (PyObject*)self; // return (PyObject*)self;
} // }
}; // };
/* /*
* *