cleaning up for merge

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

View File

@ -53,12 +53,21 @@ PyObject* PyInit_mcrfpy()
return NULL;
}
// This code runs, but Python segfaults when accessing the UIFrame type.
//std::cout << "Adding UIFrame object to module\n";
using namespace mcrfpydef;
PyTypeObject* pytypes[] = {&PyColorType, &PyFontType, &PyUICaptionType, &PyTextureType, &PyUISpriteType, &PyUIFrameType, &PyUICollectionType,
&PyUICollectionIterType, &PyUIGridPointType, &PyUIGridPointStateType, &PyUIEntityType, &PyUIEntityCollectionType,
&PyUIEntityCollectionIterType, &PyUIGridType, nullptr};
PyTypeObject* pytypes[] = {
/*SFML exposed types*/
&PyColorType, &PyFontType, &PyTextureType,
/*UI widgets*/
&PyUICaptionType, &PyUISpriteType, &PyUIFrameType, &PyUIEntityType, &PyUIGridType,
/*game map & perspective data*/
&PyUIGridPointType, &PyUIGridPointStateType,
/*collections & iterators*/
&PyUICollectionType, &PyUICollectionIterType,
&PyUIEntityCollectionType, &PyUIEntityCollectionIterType,
nullptr};
int i = 0;
auto t = pytypes[i];
while (t != nullptr)
@ -67,26 +76,6 @@ PyObject* PyInit_mcrfpy()
PyModule_AddType(m, t);
t = pytypes[i++];
}
/*
PyModule_AddType(m, &mcrfpydef::PyColorType);
PyModule_AddType(m, &mcrfpydef::PyFontType);
PyModule_AddType(m, &mcrfpydef::PyUICaptionType);
PyModule_AddType(m, &mcrfpydef::PyTextureType);
PyModule_AddType(m, &mcrfpydef::PyUISpriteType);
PyModule_AddType(m, &mcrfpydef::PyUIFrameType);
PyModule_AddType(m, &mcrfpydef::PyUICollectionType);
PyModule_AddType(m, &mcrfpydef::PyUICollectionIterType);
PyModule_AddType(m, &mcrfpydef::PyUIGridPointType);
PyModule_AddType(m, &mcrfpydef::PyUIGridPointStateType);
PyModule_AddType(m, &mcrfpydef::PyUIEntityType);
PyModule_AddType(m, &mcrfpydef::PyUIEntityCollectionIterType);
PyModule_AddType(m, &mcrfpydef::PyUIEntityCollectionType);
PyModule_AddType(m, &mcrfpydef::PyUIGridType);
*/
return m;
}

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());
}

View File

@ -2,48 +2,31 @@
#include "Common.h"
#include "Python.h"
class PyTexture;
typedef struct {
PyObject_HEAD
std::shared_ptr<PyTexture> data;
} PyTextureObject;
class PyTexture : public std::enable_shared_from_this<PyTexture>
{
private:
sf::Texture texture;
std::string source;
int sheet_width, sheet_height;
public:
int sprite_width, sprite_height; // just use them read only, OK?
PyTexture(std::string filename, int sprite_w, int sprite_h);
sf::Sprite sprite(int index, sf::Vector2f pos = sf::Vector2f(0, 0), sf::Vector2f s = sf::Vector2f(1.0, 1.0));
PyObject* pyObject();
static Py_hash_t hash(PyObject*);
static int init(PyTextureObject*, PyObject*, PyObject*);
static PyObject* pynew(PyTypeObject* type, PyObject* args=NULL, PyObject* kwds=NULL);
};
namespace mcrfpydef {
class PyTexture;
typedef struct {
PyObject_HEAD
std::shared_ptr<PyTexture> data;
} PyTextureObject;
class PyTexture : public std::enable_shared_from_this<PyTexture>
{
private:
sf::Texture texture;
std::string source;
int sheet_width, sheet_height;
protected:
//PyObject* self = 0;
public:
int sprite_width, sprite_height; // just use them read only, OK?
PyTexture(std::string filename, int sprite_w, int sprite_h);
sf::Sprite sprite(int index, sf::Vector2f pos = sf::Vector2f(0, 0), sf::Vector2f s = sf::Vector2f(1.0, 1.0));
PyObject* pyObject();
static Py_hash_t hash(PyObject*);
static int init(PyTextureObject*, PyObject*, PyObject*);
static PyObject* pynew(PyTypeObject* type, PyObject* args=NULL, PyObject* kwds=NULL);
};
/*
static int PyTexture_init(PyTextureObject* self, PyObject* args, PyObject* kwds)
{
//std::cout << "Init called\n";
static const char* keywords[] = { "filename", "grid_size", "grid_width", "grid_height", nullptr };
char* filename;
int grid_size, grid_width, grid_height;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "siii", const_cast<char**>(keywords), &filename, &grid_size, &grid_width, &grid_height))
{
return -1;
}
self->data = std::make_shared<PyTexture>(filename, grid_size, grid_size);
return 0;
}
*/
static PyTypeObject PyTextureType = {
.tp_name = "mcrfpy.Texture",
.tp_basicsize = sizeof(PyTextureObject),
@ -53,12 +36,5 @@ namespace mcrfpydef {
.tp_doc = PyDoc_STR("SFML Texture Object"),
.tp_init = (initproc)PyTexture::init,
.tp_new = PyTexture::pynew,
/*
[](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject*
{
PyTextureObject* self = (PyTextureObject*)type->tp_alloc(type, 0);
return (PyObject*)self;
}
*/
};
}

View File

@ -2,8 +2,6 @@
#include "Resources.h"
#include "GameEngine.h"
using namespace mcrfpydef;
/* //callability fields & methods
PyObject* click_callable;
virtual UIDrawable* click_at(sf::Vector2f point);

View File

@ -8,8 +8,6 @@
#include "PyCallable.h"
#include "PyTexture.h"
using namespace mcrfpydef;
enum PyObjectsEnum : int
{
UIFRAME = 1,
@ -1082,58 +1080,6 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
*
*/
/*
*
* Begin PyTextureType defs
*
*/
/* // Definition moved to PyTexture.h
typedef struct {
PyObject_HEAD
std::shared_ptr<PyTexture> data;
} PyTextureObject;
static int PyTexture_init(PyTextureObject* self, PyObject* args, PyObject* kwds)
{
//std::cout << "Init called\n";
static const char* keywords[] = { "filename", "grid_size", "grid_width", "grid_height", nullptr };
char* filename;
int grid_size, grid_width, grid_height;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "siii", const_cast<char**>(keywords), &filename, &grid_size, &grid_width, &grid_height))
{
return -1;
}
//sf::Texture t = sf::Texture();
//t.loadFromFile((std::string)filename);
self->data = std::make_shared<PyTexture>(filename, grid_size, grid_size);
return 0;
}
static PyTypeObject PyTextureType = {
//PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "mcrfpy.Texture",
.tp_basicsize = sizeof(PyTextureObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("SFML Texture Object"),
.tp_init = (initproc)PyTexture_init,
.tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject*
{
PyTextureObject* self = (PyTextureObject*)type->tp_alloc(type, 0);
return (PyObject*)self;
}
};
*/
/*
*
* End PyTextureType defs
*
*/
/*
*
* Begin template generation for PyUISpriteType
@ -1218,7 +1164,6 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
static PyObject* PyUISprite_get_texture(PyUISpriteObject* self, void* closure)
{
std::cout << "Calling pyObject" << std::endl;
return self->data->getTexture()->pyObject();
}