Font mostly working, just a few weird bugs with the types of the default items added to the module
This commit is contained in:
parent
fbf263a038
commit
159658521c
|
@ -9,6 +9,9 @@ std::vector<sf::SoundBuffer> McRFPy_API::soundbuffers;
|
|||
sf::Music McRFPy_API::music;
|
||||
sf::Sound McRFPy_API::sfx;
|
||||
|
||||
std::shared_ptr<PyFont> McRFPy_API::default_font;
|
||||
std::shared_ptr<PyTexture> McRFPy_API::default_texture;
|
||||
|
||||
static PyMethodDef mcrfpyMethods[] = {
|
||||
{"registerPyAction", McRFPy_API::_registerPyAction, METH_VARARGS,
|
||||
"Register a callable Python object to correspond to an action string. (actionstr, callable)"},
|
||||
|
@ -39,8 +42,15 @@ static PyMethodDef mcrfpyMethods[] = {
|
|||
};
|
||||
|
||||
static PyModuleDef mcrfpyModule = {
|
||||
PyModuleDef_HEAD_INIT, "mcrfpy", NULL, -1, mcrfpyMethods,
|
||||
NULL, NULL, NULL, NULL
|
||||
PyModuleDef_HEAD_INIT, /* m_base - Always initialize this member to PyModuleDef_HEAD_INIT. */
|
||||
"mcrfpy", /* m_name */
|
||||
NULL, /* m_doc - Docstring for the module; usually a docstring variable created with PyDoc_STRVAR is used. */
|
||||
-1, /* m_size - Setting m_size to -1 means that the module does not support sub-interpreters, because it has global state. */
|
||||
mcrfpyMethods, /* m_methods */
|
||||
NULL, /* m_slots - An array of slot definitions ... When using single-phase initialization, m_slots must be NULL. */
|
||||
NULL, /* traverseproc m_traverse - A traversal function to call during GC traversal of the module object */
|
||||
NULL, /* inquiry m_clear - A clear function to call during GC clearing of the module object */
|
||||
NULL /* freefunc m_free - A function to call during deallocation of the module object */
|
||||
};
|
||||
|
||||
// Module initializer fn, passed to PyImport_AppendInittab
|
||||
|
@ -77,6 +87,11 @@ PyObject* PyInit_mcrfpy()
|
|||
t = pytypes[i++];
|
||||
}
|
||||
|
||||
// Add default_font and default_texture to module
|
||||
McRFPy_API::default_font = std::make_shared<PyFont>("assets/JetbrainsMono.ttf");
|
||||
McRFPy_API::default_texture = std::make_shared<PyTexture>("assets/kenney_tinydungeon.png", 16, 16);
|
||||
PyModule_AddObject(m, "default_font", McRFPy_API::default_font->pyObject());
|
||||
PyModule_AddObject(m, "default_texture", McRFPy_API::default_texture->pyObject());
|
||||
return m;
|
||||
}
|
||||
|
||||
|
@ -131,6 +146,7 @@ PyStatus init_python(const char *program_name)
|
|||
return status;
|
||||
}
|
||||
|
||||
/*
|
||||
void McRFPy_API::setSpriteTexture(int ti)
|
||||
{
|
||||
int tx = ti % texture_width, ty = ti / texture_width;
|
||||
|
@ -139,6 +155,7 @@ void McRFPy_API::setSpriteTexture(int ti)
|
|||
ty * texture_size,
|
||||
texture_size, texture_size));
|
||||
}
|
||||
*/
|
||||
|
||||
// functionality
|
||||
//void McRFPy_API::
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
#include "Python.h"
|
||||
#include <list>
|
||||
|
||||
#include "PyFont.h"
|
||||
#include "PyTexture.h"
|
||||
|
||||
class GameEngine; // forward declared (circular members)
|
||||
|
||||
class McRFPy_API
|
||||
|
@ -14,10 +17,13 @@ private:
|
|||
|
||||
McRFPy_API();
|
||||
|
||||
|
||||
public:
|
||||
inline static sf::Sprite sprite;
|
||||
inline static sf::Texture texture;
|
||||
static void setSpriteTexture(int);
|
||||
static std::shared_ptr<PyFont> default_font;
|
||||
static std::shared_ptr<PyTexture> default_texture;
|
||||
//inline static sf::Sprite sprite;
|
||||
//inline static sf::Texture texture;
|
||||
//static void setSpriteTexture(int);
|
||||
inline static GameEngine* game;
|
||||
static void api_init();
|
||||
static void api_shutdown();
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
#include "PyFont.h"
|
||||
|
||||
|
||||
PyFont::PyFont(std::string filename)
|
||||
: source(filename)
|
||||
{
|
||||
font = sf::Font();
|
||||
font.loadFromFile(source);
|
||||
}
|
||||
|
||||
PyObject* PyFont::pyObject()
|
||||
{
|
||||
PyObject* obj = PyType_GenericAlloc(&mcrfpydef::PyFontType, 0);
|
||||
try {
|
||||
((PyFontObject*)obj)->data = shared_from_this();
|
||||
}
|
||||
catch (std::bad_weak_ptr& e)
|
||||
{
|
||||
std::cout << "Bad weak ptr: shared_from_this() failed in PyFont::pyObject(); did you create a PyFont 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;
|
||||
}
|
||||
|
||||
Py_hash_t PyFont::hash(PyObject* obj)
|
||||
{
|
||||
auto self = (PyFontObject*)obj;
|
||||
return reinterpret_cast<Py_hash_t>(self->data.get());
|
||||
}
|
||||
|
||||
int PyFont::init(PyFontObject* self, PyObject* args, PyObject* kwds)
|
||||
{
|
||||
static const char* keywords[] = { "filename", nullptr };
|
||||
char* filename;
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", const_cast<char**>(keywords), &filename))
|
||||
return -1;
|
||||
self->data = std::make_shared<PyFont>(filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject* PyFont::pynew(PyTypeObject* type, PyObject* args, PyObject* kwds)
|
||||
{
|
||||
return (PyObject*)type->tp_alloc(type, 0);
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
#include "Common.h"
|
||||
#include "Python.h"
|
||||
|
||||
class PyFont;
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
std::shared_ptr<PyFont> data;
|
||||
} PyFontObject;
|
||||
|
||||
class PyFont : public std::enable_shared_from_this<PyFont>
|
||||
{
|
||||
private:
|
||||
std::string source;
|
||||
public:
|
||||
PyFont(std::string filename);
|
||||
sf::Font font;
|
||||
PyObject* pyObject();
|
||||
static Py_hash_t hash(PyObject*);
|
||||
static int init(PyFontObject*, PyObject*, PyObject*);
|
||||
static PyObject* pynew(PyTypeObject* type, PyObject* args=NULL, PyObject* kwds=NULL);
|
||||
};
|
||||
|
||||
namespace mcrfpydef {
|
||||
static PyTypeObject PyFontType = {
|
||||
.tp_name = "mcrfpy.Font",
|
||||
.tp_basicsize = sizeof(PyFontObject),
|
||||
.tp_itemsize = 0,
|
||||
//.tp_hash = PyFont::hash,
|
||||
.tp_flags = Py_TPFLAGS_DEFAULT,
|
||||
.tp_doc = PyDoc_STR("SFML Font Object"),
|
||||
.tp_init = (initproc)PyFont::init,
|
||||
.tp_new = PyFont::pynew,
|
||||
};
|
||||
}
|
7
src/UI.h
7
src/UI.h
|
@ -13,6 +13,7 @@
|
|||
#include "PyColor.h"
|
||||
//#include "PyLinkedColor.h"
|
||||
#include "PyVector.h"
|
||||
#include "PyFont.h"
|
||||
|
||||
enum PyObjectsEnum : int
|
||||
{
|
||||
|
@ -409,7 +410,7 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
|
|||
* Begin PyFontType defs
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
std::shared_ptr<sf::Font> data;
|
||||
|
@ -444,6 +445,7 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
|
|||
return (PyObject*)self;
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
|
@ -792,13 +794,14 @@ static int PyUIDrawable_set_click(PyUIGridObject* self, PyObject* value, void* c
|
|||
//
|
||||
// Set Font
|
||||
//
|
||||
std::cout << PyUnicode_AsUTF8(PyObject_Repr(font)) << std::endl;
|
||||
if (font != NULL && !PyObject_IsInstance(font, (PyObject*)&PyFontType)){
|
||||
PyErr_SetString(PyExc_TypeError, "font must be a mcrfpy.Font instance");
|
||||
return -1;
|
||||
} else if (font != NULL)
|
||||
{
|
||||
auto font_obj = (PyFontObject*)font;
|
||||
self->data->text.setFont(*font_obj->data);
|
||||
self->data->text.setFont(font_obj->data->font);
|
||||
self->font = font;
|
||||
Py_INCREF(font);
|
||||
} else
|
||||
|
|
Loading…
Reference in New Issue