Improved the way rectangle are handled;

Some bug fixes.


git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1093 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
remi-k 2009-05-21 13:52:28 +00:00
parent caf7f915d5
commit 60e14e75b7
14 changed files with 145 additions and 115 deletions

View File

@ -230,7 +230,7 @@ PySfDrawable_TransformToGlobal(PySfDrawable* self, PyObject *args)
return Py_BuildValue("ff", result.x, result.y); return Py_BuildValue("ff", result.x, result.y);
} }
int PySfDrawable_SetAttr(PyObject* self, PyObject *attr_name, PyObject *v) int PySfDrawable_setattro(PyObject* self, PyObject *attr_name, PyObject *v)
{ {
#ifdef IS_PY3K #ifdef IS_PY3K
PyObject *string = PyUnicode_AsUTF8String(attr_name); PyObject *string = PyUnicode_AsUTF8String(attr_name);
@ -299,7 +299,7 @@ PyTypeObject PySfDrawableType = {
0, /*tp_call*/ 0, /*tp_call*/
0, /*tp_str*/ 0, /*tp_str*/
0, /*tp_getattro*/ 0, /*tp_getattro*/
PySfDrawable_SetAttr, /*tp_setattro*/ PySfDrawable_setattro, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"Abstract base class for every object that can be drawn into a render window.", /* tp_doc */ "Abstract base class for every object that can be drawn into a render window.", /* tp_doc */

View File

@ -167,13 +167,17 @@ PySfFont_GetCharacterSize(PySfFont* self)
static PyObject * static PyObject *
PySfFont_GetGlyph(PySfFont* self, PyObject *args) PySfFont_GetGlyph(PySfFont* self, PyObject *args)
{ {
PySfGlyph *PyGlyph = GetNewPySfGlyph(); PySfGlyph *Glyph = GetNewPySfGlyph();
sf::Glyph *Glyph = new sf::Glyph(self->obj->GetGlyph(PyLong_AsUnsignedLong(args))); Glyph->Owner = false;
PyGlyph->obj = Glyph; Glyph->Rectangle = GetNewPySfIntRect();
PyGlyph->Rectangle->obj = &(PyGlyph->obj->Rectangle); Glyph->Rectangle->Owner = false;
PyGlyph->TexCoords->obj = &(PyGlyph->obj->TexCoords); Glyph->TexCoords = GetNewPySfFloatRect();
PySfGlyphUpdateSelf(PyGlyph); Glyph->TexCoords->Owner = false;
return (PyObject *)PyGlyph; Glyph->obj = (sf::Glyph *) &(self->obj->GetGlyph(PyLong_AsUnsignedLong(args)));
Glyph->Rectangle->obj = &(Glyph->obj->Rectangle);
Glyph->TexCoords->obj = &(Glyph->obj->TexCoords);
PySfGlyphUpdateSelf(Glyph);
return (PyObject *)Glyph;
} }
static PyMethodDef PySfFont_methods[] = { static PyMethodDef PySfFont_methods[] = {

View File

@ -41,10 +41,8 @@ static PyMemberDef PySfGlyph_members[] = {
static void static void
PySfGlyph_dealloc(PySfGlyph *self) PySfGlyph_dealloc(PySfGlyph *self)
{ {
self->Rectangle->obj = new sf::IntRect(self->obj->Rectangle); Py_CLEAR(self->Rectangle);
Py_DECREF(self->Rectangle); Py_CLEAR(self->TexCoords);
self->TexCoords->obj = new sf::FloatRect(self->obj->TexCoords);
Py_DECREF(self->TexCoords);
delete self->obj; delete self->obj;
free_object(self); free_object(self);
} }
@ -53,30 +51,14 @@ void
PySfGlyphUpdateObj(PySfGlyph *self) PySfGlyphUpdateObj(PySfGlyph *self)
{ {
self->obj->Advance = self->Advance; self->obj->Advance = self->Advance;
PySfIntRectUpdateObj(self->Rectangle); PySfIntRectUpdateSelf(self->Rectangle);
PySfFloatRectUpdateObj(self->TexCoords); PySfFloatRectUpdateSelf(self->TexCoords);
self->obj->Rectangle.Left = self->Rectangle->Left;
self->obj->Rectangle.Top = self->Rectangle->Top;
self->obj->Rectangle.Right = self->Rectangle->Right;
self->obj->Rectangle.Bottom = self->Rectangle->Bottom;
self->obj->TexCoords.Left = self->TexCoords->Left;
self->obj->TexCoords.Top = self->TexCoords->Top;
self->obj->TexCoords.Right = self->TexCoords->Right;
self->obj->TexCoords.Bottom = self->TexCoords->Bottom;
} }
void void
PySfGlyphUpdateSelf(PySfGlyph *self) PySfGlyphUpdateSelf(PySfGlyph *self)
{ {
self->Advance = self->obj->Advance; self->Advance = self->obj->Advance;
self->Rectangle->Left = self->obj->Rectangle.Left;
self->Rectangle->Top = self->obj->Rectangle.Top;
self->Rectangle->Right = self->obj->Rectangle.Right;
self->Rectangle->Bottom = self->obj->Rectangle.Bottom;
self->TexCoords->Left = self->obj->TexCoords.Left;
self->TexCoords->Top = self->obj->TexCoords.Top;
self->TexCoords->Right = self->obj->TexCoords.Right;
self->TexCoords->Bottom = self->obj->TexCoords.Bottom;
PySfIntRectUpdateObj(self->Rectangle); PySfIntRectUpdateObj(self->Rectangle);
PySfFloatRectUpdateObj(self->TexCoords); PySfFloatRectUpdateObj(self->TexCoords);
} }
@ -88,16 +70,32 @@ PySfGlyph_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self = (PySfGlyph *)type->tp_alloc(type, 0); self = (PySfGlyph *)type->tp_alloc(type, 0);
if (self != NULL) if (self != NULL)
{ {
self->Advance = 0;
self->Rectangle = GetNewPySfIntRect(); self->Rectangle = GetNewPySfIntRect();
self->Rectangle->Owner = false;
self->TexCoords = GetNewPySfFloatRect(); self->TexCoords = GetNewPySfFloatRect();
self->TexCoords->Owner = false;
self->obj = new sf::Glyph(); self->obj = new sf::Glyph();
self->Owner = true;
self->Advance = self->obj->Advance;
self->Rectangle->obj = &(self->obj->Rectangle); self->Rectangle->obj = &(self->obj->Rectangle);
self->TexCoords->obj = &(self->obj->TexCoords); self->TexCoords->obj = &(self->obj->TexCoords);
PySfIntRectUpdateSelf(self->Rectangle);
PySfFloatRectUpdateSelf(self->TexCoords);
} }
return (PyObject *)self; return (PyObject *)self;
} }
int
PySfGlyph_setattro(PyObject* self, PyObject *attr_name, PyObject *v)
{
int result = PyObject_GenericSetAttr(self, attr_name, v);
PySfGlyph *Glyph = (PySfGlyph *)self;
Glyph->obj->Rectangle = *(Glyph->Rectangle->obj);
Glyph->obj->TexCoords = *(Glyph->TexCoords->obj);
Glyph->obj->Advance = Glyph->Advance;
return result;
}
PyTypeObject PySfGlyphType = { PyTypeObject PySfGlyphType = {
head_init head_init
"Glyph", /*tp_name*/ "Glyph", /*tp_name*/

View File

@ -34,6 +34,7 @@
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
bool Owner;
int Advance; int Advance;
PySfIntRect *Rectangle; PySfIntRect *Rectangle;
PySfFloatRect *TexCoords; PySfFloatRect *TexCoords;

View File

@ -78,10 +78,7 @@ PySfImage_CopyScreen(PySfImage* self, PyObject *args)
if (SourceRect) if (SourceRect)
{
PySfIntRectUpdateObj(SourceRect);
Result = self->obj->CopyScreen(*(RenderWindow->obj), *(SourceRect->obj)); Result = self->obj->CopyScreen(*(RenderWindow->obj), *(SourceRect->obj));
}
else else
Result = self->obj->CopyScreen(*(RenderWindow->obj)); Result = self->obj->CopyScreen(*(RenderWindow->obj));
if (Result) if (Result)
@ -242,18 +239,15 @@ PySfImage_GetTexCoords(PySfImage* self, PyObject *args)
if (!PyArg_ParseTuple(args, "O!|O:Image.GetTextCoords", &PySfIntRectType, &RectArg, &AdjustObj)) if (!PyArg_ParseTuple(args, "O!|O:Image.GetTextCoords", &PySfIntRectType, &RectArg, &AdjustObj))
return NULL; return NULL;
if (AdjustObj) if (AdjustObj != NULL)
if (PyObject_IsTrue(AdjustObj)) Adjust = PyBool_AsBool(AdjustObj);
Adjust = true;
PySfFloatRect *Rect; PySfFloatRect *Rect;
Rect = GetNewPySfFloatRect(); Rect = GetNewPySfFloatRect();
Rect->Owner = true;
Rect->obj = new sf::FloatRect(self->obj->GetTexCoords(*(RectArg->obj), Adjust)); Rect->obj = new sf::FloatRect(self->obj->GetTexCoords(*(RectArg->obj), Adjust));
Rect->Left = Rect->obj->Left; PySfFloatRectUpdateSelf(Rect);
Rect->Top = Rect->obj->Top;
Rect->Right = Rect->obj->Right;
Rect->Bottom = Rect->obj->Bottom;
return (PyObject *)Rect; return (PyObject *)Rect;
} }
@ -400,10 +394,7 @@ PySfImage_Copy(PySfImage* self, PyObject *args, PyObject *kwds)
ApplyAlpha = true; ApplyAlpha = true;
if (SourceRect) if (SourceRect)
{
PySfIntRectUpdateObj(SourceRect);
self->obj->Copy(*(Source->obj), DestX, DestY, *(SourceRect->obj), ApplyAlpha); self->obj->Copy(*(Source->obj), DestX, DestY, *(SourceRect->obj), ApplyAlpha);
}
else else
self->obj->Copy(*(Source->obj), DestX, DestY, sf::IntRect(0, 0, 0, 0), ApplyAlpha); self->obj->Copy(*(Source->obj), DestX, DestY, sf::IntRect(0, 0, 0, 0), ApplyAlpha);

View File

@ -49,6 +49,7 @@ static PyMemberDef PySfFloatRect_members[] = {
static void static void
PySfIntRect_dealloc(PySfIntRect* self) PySfIntRect_dealloc(PySfIntRect* self)
{ {
if (self->Owner)
delete self->obj; delete self->obj;
free_object(self); free_object(self);
} }
@ -56,6 +57,7 @@ PySfIntRect_dealloc(PySfIntRect* self)
static void static void
PySfFloatRect_dealloc(PySfFloatRect* self) PySfFloatRect_dealloc(PySfFloatRect* self)
{ {
if (self->Owner)
delete self->obj; delete self->obj;
free_object(self); free_object(self);
} }
@ -68,7 +70,8 @@ PySfIntRect_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self = (PySfIntRect *)type->tp_alloc(type, 0); self = (PySfIntRect *)type->tp_alloc(type, 0);
if (self != NULL) if (self != NULL)
{ {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiii:IntRect.__init__", (char **)kwlist, &(self->Left), &(self->Top), &(self->Right), &(self->Bottom))) self->Owner = true;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiii:IntRect.__new__", (char **)kwlist, &(self->Left), &(self->Top), &(self->Right), &(self->Bottom)))
return NULL; return NULL;
self->obj = new sf::IntRect(self->Left, self->Top, self->Right, self->Bottom); self->obj = new sf::IntRect(self->Left, self->Top, self->Right, self->Bottom);
} }
@ -83,7 +86,8 @@ PySfFloatRect_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self = (PySfFloatRect *)type->tp_alloc(type, 0); self = (PySfFloatRect *)type->tp_alloc(type, 0);
if (self != NULL) if (self != NULL)
{ {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffff:FloatRect.__init__", (char **)kwlist, &(self->Left), &(self->Top), &(self->Right), &(self->Bottom))) self->Owner = true;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffff:FloatRect.__new__", (char **)kwlist, &(self->Left), &(self->Top), &(self->Right), &(self->Bottom)))
return NULL; return NULL;
self->obj = new sf::FloatRect(self->Left, self->Top, self->Right, self->Bottom); self->obj = new sf::FloatRect(self->Left, self->Top, self->Right, self->Bottom);
} }
@ -93,14 +97,12 @@ PySfFloatRect_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject * static PyObject *
PySfIntRect_GetWidth(PySfIntRect *self) PySfIntRect_GetWidth(PySfIntRect *self)
{ {
PySfIntRectUpdateObj(self);
return PyLong_FromLong(self->obj->GetWidth()); return PyLong_FromLong(self->obj->GetWidth());
} }
static PyObject * static PyObject *
PySfIntRect_GetHeight(PySfIntRect *self) PySfIntRect_GetHeight(PySfIntRect *self)
{ {
PySfIntRectUpdateObj(self);
return PyLong_FromLong(self->obj->GetHeight()); return PyLong_FromLong(self->obj->GetHeight());
} }
@ -113,14 +115,12 @@ PySfIntRect_Intersects(PySfIntRect* self, PyObject *args);
static PyObject * static PyObject *
PySfFloatRect_GetWidth(PySfFloatRect *self) PySfFloatRect_GetWidth(PySfFloatRect *self)
{ {
PySfFloatRectUpdateObj(self);
return PyFloat_FromDouble(self->obj->GetWidth()); return PyFloat_FromDouble(self->obj->GetWidth());
} }
static PyObject * static PyObject *
PySfFloatRect_GetHeight(PySfFloatRect *self) PySfFloatRect_GetHeight(PySfFloatRect *self)
{ {
PySfFloatRectUpdateObj(self);
return PyFloat_FromDouble(self->obj->GetHeight()); return PyFloat_FromDouble(self->obj->GetHeight());
} }
@ -138,7 +138,6 @@ PySfIntRect_Offset(PySfIntRect* self, PyObject *args)
if (!PyArg_ParseTuple(args, "ii:IntRect.Offset", &OffsetX, &OffsetY)) if (!PyArg_ParseTuple(args, "ii:IntRect.Offset", &OffsetX, &OffsetY))
return NULL; return NULL;
PySfIntRectUpdateObj(self);
self->obj->Offset(OffsetX, OffsetY); self->obj->Offset(OffsetX, OffsetY);
PySfIntRectUpdateSelf(self); PySfIntRectUpdateSelf(self);
Py_RETURN_NONE; Py_RETURN_NONE;
@ -152,7 +151,6 @@ PySfFloatRect_Offset(PySfFloatRect* self, PyObject *args)
if (!PyArg_ParseTuple(args, "ff:FloatRect.Offset", &OffsetX, &OffsetY)) if (!PyArg_ParseTuple(args, "ff:FloatRect.Offset", &OffsetX, &OffsetY))
return NULL; return NULL;
PySfFloatRectUpdateObj(self);
self->obj->Offset(OffsetX, OffsetY); self->obj->Offset(OffsetX, OffsetY);
PySfFloatRectUpdateSelf(self); PySfFloatRectUpdateSelf(self);
Py_RETURN_NONE; Py_RETURN_NONE;
@ -202,6 +200,24 @@ Check intersection between two rectangles.\n\
{NULL} /* Sentinel */ {NULL} /* Sentinel */
}; };
int
PySfIntRect_setattro(PyObject* self, PyObject *attr_name, PyObject *v)
{
int result = PyObject_GenericSetAttr(self, attr_name, v);
PySfIntRect *Rect = (PySfIntRect *)self;
PySfIntRectUpdateObj(Rect);
return result;
}
int
PySfFloatRect_setattro(PyObject* self, PyObject *attr_name, PyObject *v)
{
int result = PyObject_GenericSetAttr(self, attr_name, v);
PySfFloatRect *Rect = (PySfFloatRect *)self;
PySfFloatRectUpdateObj(Rect);
return result;
}
PyTypeObject PySfIntRectType = { PyTypeObject PySfIntRectType = {
head_init head_init
"IntRect", /*tp_name*/ "IntRect", /*tp_name*/
@ -220,7 +236,7 @@ PyTypeObject PySfIntRectType = {
0, /*tp_call*/ 0, /*tp_call*/
0, /*tp_str*/ 0, /*tp_str*/
0, /*tp_getattro*/ 0, /*tp_getattro*/
0, /*tp_setattro*/ PySfIntRect_setattro, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"sf.IntRect is an utility class for manipulating rectangles.", /* tp_doc */ "sf.IntRect is an utility class for manipulating rectangles.", /* tp_doc */
@ -262,7 +278,7 @@ PyTypeObject PySfFloatRectType = {
0, /*tp_call*/ 0, /*tp_call*/
0, /*tp_str*/ 0, /*tp_str*/
0, /*tp_getattro*/ 0, /*tp_getattro*/
0, /*tp_setattro*/ PySfFloatRect_setattro, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"sf.FloatRect is an utility class for manipulating rectangles.", /* tp_doc */ "sf.FloatRect is an utility class for manipulating rectangles.", /* tp_doc */
@ -294,7 +310,6 @@ PySfFloatRect_Contains(PySfFloatRect* self, PyObject *args)
if (!PyArg_ParseTuple(args, "ff:FloatRect.Contains", &x, &y)) if (!PyArg_ParseTuple(args, "ff:FloatRect.Contains", &x, &y))
return NULL; return NULL;
PySfFloatRectUpdateObj(self);
return PyBool_FromLong(self->obj->Contains(x,y)); return PyBool_FromLong(self->obj->Contains(x,y));
} }
@ -307,7 +322,6 @@ PySfFloatRect_Intersects(PySfFloatRect* self, PyObject *args)
if (!PyArg_ParseTuple(args, "O!|O!:FloatRect.Intersects", &PySfFloatRectType, &Rect, &PySfFloatRectType, &OverlappingRect)) if (!PyArg_ParseTuple(args, "O!|O!:FloatRect.Intersects", &PySfFloatRectType, &Rect, &PySfFloatRectType, &OverlappingRect))
return NULL; return NULL;
PySfFloatRectUpdateObj(self);
if (OverlappingRect) if (OverlappingRect)
result = self->obj->Intersects(*(Rect->obj), (OverlappingRect->obj)); result = self->obj->Intersects(*(Rect->obj), (OverlappingRect->obj));
else else
@ -322,7 +336,6 @@ PySfIntRect_Contains(PySfIntRect* self, PyObject *args)
{ {
unsigned int x=0, y=0; unsigned int x=0, y=0;
PySfIntRectUpdateObj(self);
if (!PyArg_ParseTuple(args, "II:IntRect.Contains", &x, &y)) if (!PyArg_ParseTuple(args, "II:IntRect.Contains", &x, &y))
return NULL; return NULL;
@ -335,7 +348,6 @@ PySfIntRect_Intersects(PySfIntRect* self, PyObject *args)
PySfIntRect *Rect=NULL, *OverlappingRect=NULL; PySfIntRect *Rect=NULL, *OverlappingRect=NULL;
bool result; bool result;
PySfIntRectUpdateObj(self);
if (!PyArg_ParseTuple(args, "O!|O!:IntRect.Intersects", &PySfIntRectType, &Rect, &PySfIntRectType, &OverlappingRect)) if (!PyArg_ParseTuple(args, "O!|O!:IntRect.Intersects", &PySfIntRectType, &Rect, &PySfIntRectType, &OverlappingRect))
return NULL; return NULL;
@ -364,6 +376,7 @@ PySfFloatRectUpdateObj(PySfFloatRect *self)
self->obj->Top = self->Top; self->obj->Top = self->Top;
self->obj->Bottom = self->Bottom; self->obj->Bottom = self->Bottom;
} }
void void
PySfIntRectUpdateSelf(PySfIntRect *self) PySfIntRectUpdateSelf(PySfIntRect *self)
{ {

View File

@ -31,6 +31,7 @@
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
bool Owner;
int Left; int Left;
int Right; int Right;
int Top; int Top;
@ -40,6 +41,7 @@ typedef struct {
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
bool Owner;
float Left; float Left;
float Right; float Right;
float Top; float Top;

View File

@ -23,7 +23,6 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include "RenderWindow.hpp" #include "RenderWindow.hpp"
#include "View.hpp"
#include "Image.hpp" #include "Image.hpp"
#include "Window.hpp" #include "Window.hpp"
#include "Color.hpp" #include "Color.hpp"
@ -44,6 +43,7 @@ extern PyTypeObject PySfDrawableType;
static void static void
PySfRenderWindow_dealloc(PySfRenderWindow* self) PySfRenderWindow_dealloc(PySfRenderWindow* self)
{ {
Py_CLEAR(self->View);
delete self->obj; delete self->obj;
free_object(self); free_object(self);
} }
@ -54,7 +54,10 @@ PySfRenderWindow_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PySfRenderWindow *self; PySfRenderWindow *self;
self = (PySfRenderWindow *)type->tp_alloc(type, 0); self = (PySfRenderWindow *)type->tp_alloc(type, 0);
if (self != NULL) if (self != NULL)
{
self->obj = new sf::RenderWindow(); self->obj = new sf::RenderWindow();
self->View = NULL;
}
return (PyObject *)self; return (PyObject *)self;
} }
@ -153,12 +156,22 @@ PySfRenderWindow_Clear(PySfRenderWindow *self, PyObject *args)
static PyObject * static PyObject *
PySfRenderWindow_GetView(PySfRenderWindow *self) PySfRenderWindow_GetView(PySfRenderWindow *self)
{ {
if (self->View != NULL)
{
Py_INCREF(self->View);
return (PyObject *)(self->View);
}
else
{
PySfView *View; PySfView *View;
View = GetNewPySfView(); View = GetNewPySfView();
View->obj = new sf::View(self->obj->GetView()); View->Owner = false;
View->obj = (sf::View *)&(self->obj->GetView());
Py_INCREF(View);
self->View = View;
return (PyObject *)View; return (PyObject *)View;
}
} }
static PyObject * static PyObject *
@ -177,7 +190,10 @@ PySfRenderWindow_SetView(PySfRenderWindow* self, PyObject *args)
PyErr_SetString(PyExc_TypeError, "RenderWindow.SetView() Argument is not a sf.View"); PyErr_SetString(PyExc_TypeError, "RenderWindow.SetView() Argument is not a sf.View");
return NULL; return NULL;
} }
self->obj->SetView( *(View->obj)); Py_CLEAR(self->View);
Py_INCREF(View);
self->View = View;
self->obj->SetView(*(View->obj));
Py_RETURN_NONE; Py_RETURN_NONE;
} }

View File

@ -29,9 +29,12 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "View.hpp"
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
sf::RenderWindow *obj; sf::RenderWindow *obj;
PySfView *View;
} PySfRenderWindow; } PySfRenderWindow;

View File

@ -24,7 +24,6 @@
#include "Sprite.hpp" #include "Sprite.hpp"
#include "Drawable.hpp" #include "Drawable.hpp"
#include "Rect.hpp"
#include "Color.hpp" #include "Color.hpp"
#include "compat.hpp" #include "compat.hpp"
@ -39,7 +38,8 @@ extern PyTypeObject PySfDrawableType;
static void static void
PySfSprite_dealloc(PySfSprite *self) PySfSprite_dealloc(PySfSprite *self)
{ {
Py_XDECREF(self->Image); Py_CLEAR(self->Image);
Py_CLEAR(self->SubRect);
delete self->obj; delete self->obj;
free_object(self); free_object(self);
} }
@ -54,6 +54,7 @@ PySfSprite_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (self != NULL) if (self != NULL)
{ {
self->Image = NULL; self->Image = NULL;
self->SubRect = NULL;
self->IsCustom = false; self->IsCustom = false;
} }
@ -141,27 +142,36 @@ PySfSprite_Resize(PySfSprite* self, PyObject *args)
static PyObject * static PyObject *
PySfSprite_GetSubRect(PySfSprite* self) PySfSprite_GetSubRect(PySfSprite* self)
{ {
if (self->SubRect != NULL)
{
Py_INCREF(self->SubRect);
return (PyObject *)(self->SubRect);
}
else
{
PySfIntRect *Rect; PySfIntRect *Rect;
Rect = GetNewPySfIntRect(); Rect = GetNewPySfIntRect();
Rect->obj = new sf::IntRect(self->obj->GetSubRect()); Rect->Owner = false;
Rect->Left = Rect->obj->Left; Rect->obj = (sf::IntRect *) &(self->obj->GetSubRect());
Rect->Top = Rect->obj->Top; PySfIntRectUpdateSelf(Rect);
Rect->Right = Rect->obj->Right; Py_INCREF(Rect);
Rect->Bottom = Rect->obj->Bottom; self->SubRect = Rect;
return (PyObject *)Rect; return (PyObject *)Rect;
}
} }
static PyObject * static PyObject *
PySfSprite_SetSubRect(PySfSprite* self, PyObject *args) PySfSprite_SetSubRect(PySfSprite* self, PyObject *args)
{ {
PySfIntRect *Rect = (PySfIntRect *)args; PySfIntRect *Rect = (PySfIntRect *)args;
if (! PyObject_TypeCheck(Rect, &PySfIntRectType)) if (!PyObject_TypeCheck(Rect, &PySfIntRectType))
{ {
PyErr_SetString(PyExc_TypeError, "Sprite.SetSubRect() Argument is not a sf.IntRect instance"); PyErr_SetString(PyExc_TypeError, "Sprite.SetSubRect() Argument is not a sf.IntRect instance");
return NULL; return NULL;
} }
Py_CLEAR(self->SubRect);
Py_INCREF(Rect);
self->SubRect = Rect;
self->obj->SetSubRect(*(Rect->obj)); self->obj->SetSubRect(*(Rect->obj));
Py_RETURN_NONE; Py_RETURN_NONE;
} }

View File

@ -30,12 +30,14 @@
#include <SFML/Graphics/Sprite.hpp> #include <SFML/Graphics/Sprite.hpp>
#include "Image.hpp" #include "Image.hpp"
#include "Rect.hpp"
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
bool IsCustom; bool IsCustom;
sf::Sprite *obj; sf::Sprite *obj;
PySfImage *Image; PySfImage *Image;
PySfIntRect *SubRect;
} PySfSprite; } PySfSprite;
#endif #endif

View File

@ -175,11 +175,9 @@ PySfString_GetRect(PySfString* self)
PySfFloatRect *Rect; PySfFloatRect *Rect;
Rect = GetNewPySfFloatRect(); Rect = GetNewPySfFloatRect();
Rect->Owner = true;
Rect->obj = new sf::FloatRect (self->obj->GetRect()); Rect->obj = new sf::FloatRect (self->obj->GetRect());
Rect->Left = Rect->obj->Left; PySfFloatRectUpdateSelf(Rect);
Rect->Top = Rect->obj->Top;
Rect->Right = Rect->obj->Right;
Rect->Bottom = Rect->obj->Bottom;
return (PyObject *)Rect; return (PyObject *)Rect;
} }

View File

@ -23,7 +23,6 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include "View.hpp" #include "View.hpp"
#include "Rect.hpp"
#include "offsetof.hpp" #include "offsetof.hpp"
#include "compat.hpp" #include "compat.hpp"
@ -49,24 +48,17 @@ PySfView_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (self != NULL) if (self != NULL)
{ {
self->Owner = true; self->Owner = true;
} PySfFloatRect *Rect = NULL;
if (!PyArg_ParseTuple(args, "|O!:View.__new__", &PySfFloatRectType, &Rect))
return (PyObject *)self; return NULL;
}
static int
PySfView_init(PySfView *self, PyObject *args, PyObject *kwds)
{
PySfFloatRect *Rect=NULL;
if (!PyArg_ParseTuple(args, "|O!:View.__init__", &PySfFloatRectType, &Rect))
return -1;
if (Rect != NULL) if (Rect != NULL)
self->obj = new sf::View( (const sf::FloatRect) *(Rect->obj)); self->obj = new sf::View( (const sf::FloatRect) *(Rect->obj));
else else
self->obj = new sf::View(); self->obj = new sf::View();
}
return 0; return (PyObject *)self;
} }
static PyObject * static PyObject *
@ -87,11 +79,9 @@ static PyObject *
PySfView_GetRect(PySfView* self) PySfView_GetRect(PySfView* self)
{ {
PySfFloatRect *Rect = GetNewPySfFloatRect(); PySfFloatRect *Rect = GetNewPySfFloatRect();
Rect->obj = new sf::FloatRect(self->obj->GetRect()); Rect->Owner = false;
Rect->Left = Rect->obj->Left; Rect->obj = (sf::FloatRect *) &(self->obj->GetRect());
Rect->Right = Rect->obj->Right; PySfFloatRectUpdateSelf(Rect);
Rect->Top = Rect->obj->Top;
Rect->Bottom = Rect->obj->Bottom;
return (PyObject *)Rect; return (PyObject *)Rect;
} }
@ -181,7 +171,7 @@ PyTypeObject PySfViewType = {
0, /* tp_descr_get */ 0, /* tp_descr_get */
0, /* tp_descr_set */ 0, /* tp_descr_set */
0, /* tp_dictoffset */ 0, /* tp_dictoffset */
(initproc)PySfView_init, /* tp_init */ 0, /* tp_init */
0, /* tp_alloc */ 0, /* tp_alloc */
PySfView_new, /* tp_new */ PySfView_new, /* tp_new */
}; };
@ -189,6 +179,6 @@ PyTypeObject PySfViewType = {
PySfView * PySfView *
GetNewPySfView() GetNewPySfView()
{ {
return (PySfView *)PySfView_new(&PySfViewType, NULL, NULL); return PyObject_New(PySfView, &PySfViewType);
} }

View File

@ -29,6 +29,8 @@
#include <SFML/Graphics/View.hpp> #include <SFML/Graphics/View.hpp>
#include "Rect.hpp"
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
bool Owner; bool Owner;