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"
PyColor::PyColor()
PyColor::PyColor(sf::Color* target, std::weak_ptr<UIDrawable> parent, int index)
{
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* obj = PyType_GenericAlloc(&mcrfpydef::PyColorType, 0);
((PyColorObject*)obj.data = data;
((PyColorObject*)obj->data = data;
return obj;
}
Py_hash_t PyColor::hash(PyObject* obj)
{
auto self = (PyTextureObject*)obj;
auto self = (PyColorObject*)obj;
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())
auto ptr = self.data.parent.lock();
if (ptr || self->data.index == PyColor::SELF_OWNED)
{
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 += reinterpret_cast<Py_hash_t>(ptr);
}
return value;
}
int PyColor::init(PyColorObject* self, PyObject* args, PyObject* kwds)
{
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;
}
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);
}

View File

@ -3,6 +3,7 @@
#include "Python.h"
class PyColor;
class UIDrawable; // forward declare for pointer
typedef struct {
sf::Color* target; // color target to set/get
@ -13,13 +14,16 @@ typedef struct {
typedef struct {
PyObject_HEAD
_PyColorData data;
} PyTextureObject;
} PyColorObject;
class PyColor
{
private:
_PyColorData data;
static int SELF_OWNED = -1;
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);
sf::Color get();
PyObject* pyObject();
@ -27,11 +31,14 @@ public:
static int init(PyColorObject*, PyObject*, PyObject*);
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[] = {
{"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},
{"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}
};
};
@ -78,6 +85,6 @@ namespace mcrfpydef {
.tp_doc = PyDoc_STR("SFML Color Object"),
.tp_getset = PyColor::getsetters,
.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;
*/
/* // Moved to PyColor.h
typedef struct {
PyObject_HEAD
std::shared_ptr<sf::Color> data;
} PyColorObject;
*/
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)
{
auto member_ptr = reinterpret_cast<long>(closure);
if (member_ptr == 0)
return PyLong_FromLong(self->data->r);
else if (member_ptr == 1)
return PyLong_FromLong(self->data->g);
else if (member_ptr == 2)
return PyLong_FromLong(self->data->b);
else if (member_ptr == 3)
return PyLong_FromLong(self->data->a);
else
{
PyErr_SetString(PyExc_AttributeError, "Invalid attribute");
return nullptr;
}
}
static int PyColor_set_member(PyColorObject* self, PyObject* value, void* closure)
{
if (PyLong_Check(value))
{
long int_val = PyLong_AsLong(value);
if (int_val < 0)
int_val = 0;
else if (int_val > 255)
int_val = 255;
auto member_ptr = reinterpret_cast<long>(closure);
if (member_ptr == 0)
self->data->r = static_cast<sf::Uint8>(int_val);
else if (member_ptr == 1)
self->data->g = static_cast<sf::Uint8>(int_val);
else if (member_ptr == 2)
self->data->b = static_cast<sf::Uint8>(int_val);
else if (member_ptr == 3)
self->data->a = static_cast<sf::Uint8>(int_val);
}
else
{
PyErr_SetString(PyExc_TypeError, "Value must be an integer.");
return -1;
}
return 0;
}
static PyGetSetDef PyColor_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;
}
};
// static PyObject* PyColor_get_member(PyColorObject* self, void* closure)
// {
// auto member_ptr = reinterpret_cast<long>(closure);
// if (member_ptr == 0)
// return PyLong_FromLong(self->data->r);
// else if (member_ptr == 1)
// return PyLong_FromLong(self->data->g);
// else if (member_ptr == 2)
// return PyLong_FromLong(self->data->b);
// else if (member_ptr == 3)
// return PyLong_FromLong(self->data->a);
// else
// {
// PyErr_SetString(PyExc_AttributeError, "Invalid attribute");
// return nullptr;
// }
// }
//
// static int PyColor_set_member(PyColorObject* self, PyObject* value, void* closure)
// {
// if (PyLong_Check(value))
// {
// long int_val = PyLong_AsLong(value);
// if (int_val < 0)
// int_val = 0;
// else if (int_val > 255)
// int_val = 255;
// auto member_ptr = reinterpret_cast<long>(closure);
// if (member_ptr == 0)
// self->data->r = static_cast<sf::Uint8>(int_val);
// else if (member_ptr == 1)
// self->data->g = static_cast<sf::Uint8>(int_val);
// else if (member_ptr == 2)
// self->data->b = static_cast<sf::Uint8>(int_val);
// else if (member_ptr == 3)
// self->data->a = static_cast<sf::Uint8>(int_val);
// }
// else
// {
// PyErr_SetString(PyExc_TypeError, "Value must be an integer.");
// return -1;
// }
// return 0;
// }
//
// static PyGetSetDef PyColor_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;
// }
// };
/*
*