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
This commit is contained in:
remi-k 2009-03-08 09:56:25 +00:00
parent 06b5299c2b
commit 26fd1b89e4
2 changed files with 25 additions and 5 deletions

View File

@ -30,8 +30,6 @@
#include "compat.hpp" #include "compat.hpp"
extern PyTypeObject PySfColorType;
extern PyTypeObject PySfImageType;
extern PyTypeObject PySfDrawableType; extern PyTypeObject PySfDrawableType;
extern PyTypeObject PySfFontType; extern PyTypeObject PySfFontType;
@ -39,6 +37,7 @@ extern PyTypeObject PySfFontType;
static void static void
PySfString_dealloc(PySfString *self) PySfString_dealloc(PySfString *self)
{ {
Py_CLEAR(self->font);
delete self->obj; delete self->obj;
free_object(self); free_object(self);
} }
@ -48,6 +47,8 @@ PySfString_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ {
PySfString *self; PySfString *self;
self = (PySfString *)type->tp_alloc(type, 0); self = (PySfString *)type->tp_alloc(type, 0);
if (self != NULL)
self->font = NULL;
return (PyObject *)self; return (PyObject *)self;
} }
@ -64,7 +65,11 @@ PySfString_init(PySfString *self, PyObject *args, PyObject *kwds)
return -1; return -1;
if (FontTmp) if (FontTmp)
{
Font = (FontTmp->obj); Font = (FontTmp->obj);
Py_INCREF(FontTmp);
self->font = FontTmp;
}
else else
Font = (sf::Font *)&(sf::Font::GetDefaultFont()); 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"); PyErr_SetString(PyExc_ValueError, "String.SetFont() Argument must be a sf.Font");
return NULL; return NULL;
} }
Py_CLEAR(self->font);
Py_INCREF(args);
self->font = Font;
self->obj->SetFont(*(Font->obj)); self->obj->SetFont(*(Font->obj));
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -192,9 +200,18 @@ PySfString_GetText(PySfString* self)
static PyObject * static PyObject *
PySfString_GetFont(PySfString* self) PySfString_GetFont(PySfString* self)
{ {
PySfFont *Font = GetNewPySfFont(); if (self->font == NULL)
Font->obj = new sf::Font(self->obj->GetFont()); {
return (PyObject *)Font; 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 * static PyObject *

View File

@ -29,9 +29,12 @@
#include <SFML/Graphics/String.hpp> #include <SFML/Graphics/String.hpp>
#include "Font.hpp"
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
sf::String *obj; sf::String *obj;
PySfFont *font;
} PySfString; } PySfString;
void PySfString_InitConst(); void PySfString_InitConst();