From 26fd1b89e41eb5726ffb1c48b7d2fd33aa68a2fc Mon Sep 17 00:00:00 2001 From: remi-k Date: Sun, 8 Mar 2009 09:56:25 +0000 Subject: [PATCH] Bug fix (make shure a font object isn't destroyed as long as the string is using it) and code clean up in sf.String git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1043 4e206d99-4929-0410-ac5d-dfc041789085 --- python/src/String.cpp | 27 ++++++++++++++++++++++----- python/src/String.hpp | 3 +++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/python/src/String.cpp b/python/src/String.cpp index 7e4bc5c35..527233400 100644 --- a/python/src/String.cpp +++ b/python/src/String.cpp @@ -30,8 +30,6 @@ #include "compat.hpp" -extern PyTypeObject PySfColorType; -extern PyTypeObject PySfImageType; extern PyTypeObject PySfDrawableType; extern PyTypeObject PySfFontType; @@ -39,6 +37,7 @@ extern PyTypeObject PySfFontType; static void PySfString_dealloc(PySfString *self) { + Py_CLEAR(self->font); delete self->obj; free_object(self); } @@ -48,6 +47,8 @@ PySfString_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfString *self; self = (PySfString *)type->tp_alloc(type, 0); + if (self != NULL) + self->font = NULL; return (PyObject *)self; } @@ -64,7 +65,11 @@ PySfString_init(PySfString *self, PyObject *args, PyObject *kwds) return -1; if (FontTmp) + { Font = (FontTmp->obj); + Py_INCREF(FontTmp); + self->font = FontTmp; + } else Font = (sf::Font *)&(sf::Font::GetDefaultFont()); @@ -148,6 +153,9 @@ PySfString_SetFont(PySfString* self, PyObject *args) PyErr_SetString(PyExc_ValueError, "String.SetFont() Argument must be a sf.Font"); return NULL; } + Py_CLEAR(self->font); + Py_INCREF(args); + self->font = Font; self->obj->SetFont(*(Font->obj)); Py_RETURN_NONE; } @@ -192,9 +200,18 @@ PySfString_GetText(PySfString* self) static PyObject * PySfString_GetFont(PySfString* self) { - PySfFont *Font = GetNewPySfFont(); - Font->obj = new sf::Font(self->obj->GetFont()); - return (PyObject *)Font; + if (self->font == NULL) + { + PySfFont *Font = GetNewPySfFont(); + Font->obj = (sf::Font *)&(sf::Font::GetDefaultFont()); + Font->Owner = false; + return (PyObject *)Font; + } + else + { + Py_INCREF(self->font); + return (PyObject *)(self->font); + } } static PyObject * diff --git a/python/src/String.hpp b/python/src/String.hpp index a036f8e16..f32e14250 100644 --- a/python/src/String.hpp +++ b/python/src/String.hpp @@ -29,9 +29,12 @@ #include +#include "Font.hpp" + typedef struct { PyObject_HEAD sf::String *obj; + PySfFont *font; } PySfString; void PySfString_InitConst();