cleaning up for merge

This commit is contained in:
John McCardle 2024-03-21 22:22:35 -04:00
commit b114ec3085
5 changed files with 40 additions and 153 deletions

View file

@ -1,5 +1,4 @@
#include "PyTexture.h"
using namespace mcrfpydef;
PyTexture::PyTexture(std::string filename, int sprite_w, int sprite_h)
@ -17,17 +16,6 @@ PyTexture::PyTexture(std::string filename, int sprite_w, int sprite_h)
}
}
/* // bit misguided here: holding self might prevent the shared_ptr from ever being released.
PyTexture::~PyTexture()
{
if (self != NULL)
{
(PyTextureObject*)self->data.reset();
Py_DECREF(self);
}
}
*/
sf::Sprite PyTexture::sprite(int index, sf::Vector2f pos, sf::Vector2f s)
{
int tx = index % sheet_width, ty = index / sheet_width;
@ -40,30 +28,21 @@ sf::Sprite PyTexture::sprite(int index, sf::Vector2f pos, sf::Vector2f s)
PyObject* PyTexture::pyObject()
{
//PyTextureObject* self = (PyTextureObject*)pynew(&mcrfpydef::PyTextureType, NULL, NULL);
std::cout << "tp_alloc (GenericAlloc)" << std::endl;
//PyObject* obj = ((&PyTextureType)->tp_alloc(&PyTextureType, 0));
//PyObject* obj = pynew(&PyTextureType);
PyObject* obj = PyType_GenericAlloc(&PyTextureType, 0);
std::cout << "alloc worked" << std::endl;
//Py_INCREF(self);
PyObject* obj = PyType_GenericAlloc(&mcrfpydef::PyTextureType, 0);
try {
std::cout << "assign data to self" << std::endl;
((PyTextureObject*)obj)->data = shared_from_this();
}
catch (std::bad_weak_ptr& e)
{
std::cout << "Bad weak ptr: shared_from_this() failed" << std::endl;
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
std::cout << "returning PyObject" << std::endl;
return obj;
}
Py_hash_t PyTexture::hash(PyObject* obj)
{
auto self = (PyTextureObject*)obj;
//return static_cast<Py_hash_t>(reinterpret_cast<long>(self->data));
return reinterpret_cast<Py_hash_t>(self->data.get());
}