From d4e7e7724f495673fe818d05baec98ed5e345033 Mon Sep 17 00:00:00 2001 From: remi-k Date: Sat, 14 Mar 2009 22:28:16 +0000 Subject: [PATCH] Split __init__ into __init__ and __new__, code clean up git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1056 4e206d99-4929-0410-ac5d-dfc041789085 --- python/PySFML/__init__.py | 1 + python/src/Clock.cpp | 13 +---- python/src/Color.cpp | 51 +++-------------- python/src/Drawable.cpp | 35 +++++------- python/src/Drawable.hpp | 4 +- python/src/Event.cpp | 80 ++++---------------------- python/src/Font.cpp | 14 ++--- python/src/Glyph.cpp | 18 ++---- python/src/Image.cpp | 87 ++++++++++++++-------------- python/src/Input.cpp | 14 +---- python/src/Listener.cpp | 10 +--- python/src/Music.cpp | 30 +++------- python/src/PostFX.cpp | 44 ++++++--------- python/src/PostFX.hpp | 1 + python/src/Rect.cpp | 57 ++++++------------- python/src/RenderWindow.cpp | 43 +++++--------- python/src/Shape.cpp | 20 +++---- python/src/Shape.hpp | 1 + python/src/Sound.cpp | 77 +++++++++++++------------ python/src/SoundBuffer.cpp | 41 ++++++-------- python/src/SoundBufferRecorder.cpp | 11 +--- python/src/SoundRecorder.cpp | 15 ++--- python/src/SoundStream.cpp | 17 +++--- python/src/Sprite.cpp | 3 +- python/src/Sprite.hpp | 1 + python/src/String.cpp | 91 ++++++++++++++---------------- python/src/String.hpp | 1 + python/src/VideoMode.cpp | 37 +++--------- python/src/Window.cpp | 9 +-- python/src/WindowSettings.cpp | 21 ++----- 30 files changed, 305 insertions(+), 542 deletions(-) diff --git a/python/PySFML/__init__.py b/python/PySFML/__init__.py index 8b1378917..48163e520 100644 --- a/python/PySFML/__init__.py +++ b/python/PySFML/__init__.py @@ -1 +1,2 @@ +__all__ = ['sf'] diff --git a/python/src/Clock.cpp b/python/src/Clock.cpp index 714a6603a..c6ba62880 100644 --- a/python/src/Clock.cpp +++ b/python/src/Clock.cpp @@ -41,21 +41,12 @@ PySfClock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) self = (PySfClock *)type->tp_alloc(type, 0); if (self != NULL) - { - } + self->obj = new sf::Clock(); return (PyObject *)self; } -static int -PySfClock_init(PySfClock *self, PyObject *args, PyObject *kwds) -{ - self->obj = new sf::Clock(); - return 0; -} - - static PyObject* PySfClock_GetElapsedTime(PySfClock *self) { @@ -111,7 +102,7 @@ PyTypeObject PySfClockType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfClock_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfClock_new, /* tp_new */ }; diff --git a/python/src/Color.cpp b/python/src/Color.cpp index be8f16754..567a2da93 100644 --- a/python/src/Color.cpp +++ b/python/src/Color.cpp @@ -57,52 +57,28 @@ static PyObject * PySfColor_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfColor *self; - self = (PySfColor *)type->tp_alloc(type, 0); - if (self != NULL) { self->r = 0; self->g = 0; self->b = 0; self->a = 255; + self->obj = new sf::Color(0, 0, 0, 255); } - return (PyObject *)self; } - -static int -PySfColor_init(PySfColor *self, PyObject *args, PyObject *kwds) +static int +PySfColor_init(PySfColor *self, PyObject *args, PyObject *kwds) { const char *kwlist[] = {"r", "g", "b", "a", NULL}; - - long int rgba=0; - - - if (PyTuple_Size(args) == 1) - { - if ( !PyArg_ParseTuple(args, "l", &rgba)) - return -1; - self->r = rgba & 0xff; - self->g = rgba>>8 & 0xff; - self->b = rgba>>16 & 0xff; - self->a = rgba>>24 & 0xff; - } - else if (PyTuple_Size(args) > 1) - if (! PyArg_ParseTupleAndKeywords(args, kwds, "BBB|B", (char **)kwlist, &(self->r), &(self->g), &(self->b), &(self->a))) - return -1; - - self->obj = new sf::Color(self->r, self->g, self->b, self->a); - + if (!PyArg_ParseTupleAndKeywords(args, kwds, "BBB|B:Color.__init__", (char **)kwlist, &(self->r), &(self->g), &(self->b), &(self->a))) + return -1; + PySfColorUpdate(self); return 0; } -static PyMethodDef PySfColor_methods[] = { - {NULL} /* Sentinel */ -}; - - PyTypeObject PySfColorType = { head_init "Color", /*tp_name*/ @@ -131,7 +107,7 @@ PyTypeObject PySfColorType = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - PySfColor_methods, /* tp_methods */ + 0, /* tp_methods */ PySfColor_members, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ @@ -147,7 +123,7 @@ PyTypeObject PySfColorType = { PySfColor * GetNewPySfColor() { - return (PySfColor *)PySfColor_new(&PySfColorType, NULL, NULL); + return PyObject_New(PySfColor, &PySfColorType); } void @@ -218,16 +194,5 @@ PySfColor_InitConst() Cyan->a = sf::Color::Cyan.a; PyDict_SetItemString(PySfColorType.tp_dict, "Cyan", (PyObject *)Cyan); Py_DECREF(Cyan); - -/* - static const Color Black; ///< Black predefined color - static const Color White; ///< White predefined color - static const Color Red; ///< Red predefined color - static const Color Green; ///< Green predefined color - static const Color Blue; ///< Blue predefined color - static const Color Yellow; ///< Yellow predefined color - static const Color Magenta; ///< Magenta predefined color - static const Color Cyan; ///< Cyan predefined color -*/ } diff --git a/python/src/Drawable.cpp b/python/src/Drawable.cpp index 99e6c419e..75ab0407c 100644 --- a/python/src/Drawable.cpp +++ b/python/src/Drawable.cpp @@ -36,7 +36,10 @@ void CustomDrawable::Render(sf::RenderTarget& Target) const if (RenderFunction) PyObject_CallFunction(RenderFunction, (char *)"O", RenderWindow); else + { PyErr_SetString(PyExc_RuntimeError, "Custom drawables must have a render method defined"); + PyErr_Print(); + } } static void @@ -51,21 +54,17 @@ PySfDrawable_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfDrawable *self; self = (PySfDrawable *)type->tp_alloc(type, 0); - return (PyObject *)self; -} - -static int -PySfDrawable_init(PySfDrawable *self, PyObject *args, PyObject *kwds) -{ - self->obj = new CustomDrawable(); - if (PyObject_HasAttrString((PyObject *)self, "Render")) + if (self != NULL) { - self->obj->RenderFunction = PyObject_GetAttrString((PyObject *)self, "Render"); + self->IsCustom = true; + self->obj = new CustomDrawable(); + if (PyObject_HasAttrString((PyObject *)self, "Render")) + self->obj->RenderFunction = PyObject_GetAttrString((PyObject *)self, "Render"); + else + self->obj->RenderFunction = NULL; + self->obj->RenderWindow = NULL; } - else - self->obj->RenderFunction = NULL; - self->obj->RenderWindow = NULL; - return 0; + return (PyObject *)self; } static PyObject * @@ -128,7 +127,7 @@ static PyObject * PySfDrawable_SetColor(PySfDrawable* self, PyObject *args) { PySfColor *Color = (PySfColor *)args; - if (! PyObject_TypeCheck(args, &PySfColorType)) + if (!PyObject_TypeCheck(args, &PySfColorType)) { PyErr_SetString(PyExc_TypeError, "Drawable.SetColor() Argument is not a sf.Color"); return NULL; @@ -318,15 +317,9 @@ PyTypeObject PySfDrawableType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfDrawable_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfDrawable_new, /* tp_new */ }; -PySfDrawable * -GetNewPySfDrawable() -{ - return (PySfDrawable *)PySfDrawable_new(&PySfDrawableType, NULL, NULL); -} - diff --git a/python/src/Drawable.hpp b/python/src/Drawable.hpp index 6e227271a..84049c5a3 100644 --- a/python/src/Drawable.hpp +++ b/python/src/Drawable.hpp @@ -44,11 +44,9 @@ public : typedef struct { PyObject_HEAD + bool IsCustom; CustomDrawable *obj; } PySfDrawable; -PySfDrawable * -GetNewPySfDrawable(); - #endif diff --git a/python/src/Event.cpp b/python/src/Event.cpp index e6a899c00..449295deb 100644 --- a/python/src/Event.cpp +++ b/python/src/Event.cpp @@ -51,12 +51,6 @@ PySfEventText_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return (PyObject *)self; } -int -PySfEventText_init(PySfEventText *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - void PySfEventText_dealloc(PySfEventText* self) { @@ -99,7 +93,7 @@ PyTypeObject PySfEventTextType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfEventText_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfEventText_new, /* tp_new */ }; @@ -129,12 +123,6 @@ PySfEventKey_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return (PyObject *)self; } -int -PySfEventKey_init(PySfEventKey *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - void PySfEventKey_dealloc(PySfEventKey* self) { @@ -185,7 +173,7 @@ PyTypeObject PySfEventKeyType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfEventKey_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfEventKey_new, /* tp_new */ }; @@ -210,12 +198,6 @@ PySfEventMouseMove_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return (PyObject *)self; } -int -PySfEventMouseMove_init(PySfEventMouseMove *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - void PySfEventMouseMove_dealloc(PySfEventMouseMove *self) { @@ -265,7 +247,7 @@ PyTypeObject PySfEventMouseMoveType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfEventMouseMove_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfEventMouseMove_new, /* tp_new */ }; @@ -291,12 +273,6 @@ PySfEventMouseButton_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return (PyObject *)self; } -int -PySfEventMouseButton_init(PySfEventMouseButton *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - void PySfEventMouseButton_dealloc(PySfEventMouseButton* self) { @@ -347,7 +323,7 @@ PyTypeObject PySfEventMouseButtonType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfEventMouseButton_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfEventMouseButton_new, /* tp_new */ }; @@ -371,12 +347,6 @@ PySfEventMouseWheel_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return (PyObject *)self; } -int -PySfEventMouseWheel_init(PySfEventMouseWheel *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - void PySfEventMouseWheel_dealloc(PySfEventMouseWheel* self) { @@ -424,7 +394,7 @@ PyTypeObject PySfEventMouseWheelType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfEventMouseWheel_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfEventMouseWheel_new, /* tp_new */ }; @@ -450,12 +420,6 @@ PySfEventJoyMove_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return (PyObject *)self; } -int -PySfEventJoyMove_init(PySfEventJoyMove *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - void PySfEventJoyMove_dealloc(PySfEventJoyMove* self) { @@ -506,7 +470,7 @@ PyTypeObject PySfEventJoyMoveType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfEventJoyMove_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfEventJoyMove_new, /* tp_new */ }; @@ -531,12 +495,6 @@ PySfEventJoyButton_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return (PyObject *)self; } -int -PySfEventJoyButton_init(PySfEventJoyButton *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - void PySfEventJoyButton_dealloc(PySfEventJoyButton* self) { @@ -586,7 +544,7 @@ PyTypeObject PySfEventJoyButtonType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfEventJoyButton_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfEventJoyButton_new, /* tp_new */ }; @@ -611,12 +569,6 @@ PySfEventSize_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return (PyObject *)self; } -int -PySfEventSize_init(PySfEventSize *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - void PySfEventSize_dealloc(PySfEventSize* self) { @@ -665,7 +617,7 @@ PyTypeObject PySfEventSizeType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfEventSize_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfEventSize_new, /* tp_new */ }; @@ -680,13 +632,6 @@ PyTypeObject PySfEventSizeType = { //////////////////////////////////// -static int -PySfEvent_init(PySfEvent *self, PyObject *args, PyObject *kwds) -{ - self->obj = new sf::Event(); - return 0; -} - static PyObject * PySfEvent_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { @@ -703,6 +648,7 @@ PySfEvent_new(PyTypeObject *type, PyObject *args, PyObject *kwds) self->JoyMove = (PySfEventJoyMove *)PySfEventJoyMove_new(&PySfEventJoyMoveType, NULL, NULL); self->JoyButton = (PySfEventJoyButton *)PySfEventJoyButton_new(&PySfEventJoyButtonType, NULL, NULL); self->Size = (PySfEventSize *)PySfEventSize_new(&PySfEventSizeType, NULL, NULL); + self->obj = new sf::Event(); } return (PyObject *)self; @@ -736,10 +682,6 @@ static PyMemberDef PySfEvent_members[] = { {NULL} /* Sentinel */ }; -static PyMethodDef PySfEvent_methods[] = { - {NULL} /* Sentinel */ -}; - PyTypeObject PySfEventType = { head_init "Event", /*tp_name*/ @@ -768,7 +710,7 @@ PyTypeObject PySfEventType = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - PySfEvent_methods, /* tp_methods */ + 0, /* tp_methods */ PySfEvent_members, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ @@ -776,7 +718,7 @@ PyTypeObject PySfEventType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfEvent_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfEvent_new, /* tp_new */ }; diff --git a/python/src/Font.cpp b/python/src/Font.cpp index b506456a1..cd6f7bd9f 100644 --- a/python/src/Font.cpp +++ b/python/src/Font.cpp @@ -42,17 +42,13 @@ PySfFont_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PySfFont *self; self = (PySfFont *)type->tp_alloc(type, 0); if (self != NULL) + { self->Owner = true; + self->obj = new sf::Font(); + } return (PyObject *)self; } -static int -PySfFont_init(PySfFont *self, PyObject *args, PyObject *kwds) -{ - self->obj = new sf::Font(); - return 0; -} - static PyObject * PySfFont_LoadFromFile(PySfFont* self, PyObject *args, PyObject *kwds) { @@ -238,7 +234,7 @@ PyTypeObject PySfFontType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfFont_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfFont_new, /* tp_new */ }; @@ -246,7 +242,7 @@ PyTypeObject PySfFontType = { PySfFont * GetNewPySfFont() { - return (PySfFont *)PySfFont_new(&PySfFontType, NULL, NULL); + return PyObject_New(PySfFont, &PySfFontType); } diff --git a/python/src/Glyph.cpp b/python/src/Glyph.cpp index 2f82b058f..d7fe9fc3f 100644 --- a/python/src/Glyph.cpp +++ b/python/src/Glyph.cpp @@ -91,21 +91,13 @@ PySfGlyph_new(PyTypeObject *type, PyObject *args, PyObject *kwds) self->Advance = 0; self->Rectangle = GetNewPySfIntRect(); self->TexCoords = GetNewPySfFloatRect(); + self->obj = new sf::Glyph(); + self->Rectangle->obj = &(self->obj->Rectangle); + self->TexCoords->obj = &(self->obj->TexCoords); } return (PyObject *)self; } - -static int -PySfGlyph_init(PySfGlyph *self, PyObject *args, PyObject *kwds) -{ - self->obj = new sf::Glyph(); - self->Rectangle->obj = &(self->obj->Rectangle); - self->TexCoords->obj = &(self->obj->TexCoords); - return 0; -} - - PyTypeObject PySfGlyphType = { head_init "Glyph", /*tp_name*/ @@ -142,7 +134,7 @@ PyTypeObject PySfGlyphType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfGlyph_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfGlyph_new, /* tp_new */ }; @@ -150,6 +142,6 @@ PyTypeObject PySfGlyphType = { PySfGlyph * GetNewPySfGlyph() { - return (PySfGlyph *)PySfGlyph_new(&PySfGlyphType, NULL, NULL); + return PyObject_New(PySfGlyph, &PySfGlyphType); } diff --git a/python/src/Image.cpp b/python/src/Image.cpp index 3d8b060e7..d98e4ea29 100644 --- a/python/src/Image.cpp +++ b/python/src/Image.cpp @@ -41,13 +41,7 @@ PySfImage_dealloc(PySfImage* self) } static PyObject * -PySfImage_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PySfImage *self; - self = (PySfImage *)type->tp_alloc(type, 0); - return (PyObject *)self; -} - +PySfImage_new(PyTypeObject *type, PyObject *args, PyObject *kwds); static PyObject * PySfImage_Create(PySfImage* self, PyObject *args, PyObject *kwds) @@ -57,7 +51,7 @@ PySfImage_Create(PySfImage* self, PyObject *args, PyObject *kwds) unsigned int Width=0, Height=0; const char *kwlist[] = {"Width", "Height", "Color", NULL}; - if (! PyArg_ParseTupleAndKeywords(args, kwds, "|IIO!:Image.Create", (char **)kwlist, &Width, &Height, &PySfColorType, &ColorTmp)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|IIO!:Image.Create", (char **)kwlist, &Width, &Height, &PySfColorType, &ColorTmp)) return NULL; if (ColorTmp) @@ -79,7 +73,7 @@ PySfImage_CopyScreen(PySfImage* self, PyObject *args) PySfIntRect *SourceRect=NULL; bool Result; - if (! PyArg_ParseTuple(args, "O!|O!:Image.CopyScreen", &PySfRenderWindowType, &RenderWindow, &PySfIntRectType, &SourceRect)) + if (!PyArg_ParseTuple(args, "O!|O!:Image.CopyScreen", &PySfRenderWindowType, &RenderWindow, &PySfIntRectType, &SourceRect)) return NULL; @@ -164,7 +158,7 @@ PySfImage_LoadFromMemory(PySfImage* self, PyObject *args) unsigned int SizeInBytes; char *Data; - if (! PyArg_ParseTuple(args, "s#:Image.LoadFromMemory", &Data, &SizeInBytes)) + if (!PyArg_ParseTuple(args, "s#:Image.LoadFromMemory", &Data, &SizeInBytes)) return NULL; return PyBool_FromLong(self->obj->LoadFromMemory(Data, (std::size_t) SizeInBytes)); @@ -205,9 +199,6 @@ PySfImage_SaveToFile (PySfImage *self, PyObject *args) save_to_file(self, args); } -static int -PySfImage_init(PySfImage *self, PyObject *args, PyObject *kwds); - static PyObject * PySfImage_Bind(PySfImage *self) { @@ -248,7 +239,7 @@ PySfImage_GetTexCoords(PySfImage* self, PyObject *args) bool Adjust = false; PyObject *AdjustObj = NULL; - if (! PyArg_ParseTuple(args, "O!|O", &PySfIntRectType, &RectArg, &AdjustObj)) + if (!PyArg_ParseTuple(args, "O!|O:Image.GetTextCoords", &PySfIntRectType, &RectArg, &AdjustObj)) return NULL; if (AdjustObj) @@ -258,7 +249,7 @@ PySfImage_GetTexCoords(PySfImage* self, PyObject *args) PySfFloatRect *Rect; Rect = GetNewPySfFloatRect(); - 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; Rect->Top = Rect->obj->Top; Rect->Right = Rect->obj->Right; @@ -267,8 +258,26 @@ PySfImage_GetTexCoords(PySfImage* self, PyObject *args) return (PyObject *)Rect; } +static int +PySfImage_init(PySfImage *self, PyObject *args, PyObject *kwds) +{ + int size = PyTuple_Size(args); + if (size > 0) + { + if (PySfImage_Create(self, args, kwds) == NULL) + { + if (size != 3) + return -1; + else if (PySfImage_LoadFromPixels(self, args) == NULL) + return -1; + else PyErr_Clear(); + } + } + return 0; +} + static PyObject * -PySfImage_Copy(PySfImage* self, PyObject *args); +PySfImage_Copy(PySfImage* self, PyObject *args, PyObject *kwds); static PyMethodDef PySfImage_methods[] = { {"Copy", (PyCFunction)PySfImage_Copy, METH_VARARGS, "Copy(Source, DestX, DestY, SourceRect = sf.IntRect(0,0,0,0))\n\ @@ -352,44 +361,38 @@ Copy constructor : sf.Image(Copy) where Copy is a sf.Image instance.", /* tp_doc PySfImage_new, /* tp_new */ }; -static int -PySfImage_init(PySfImage *self, PyObject *args, PyObject *kwds) +static PyObject * +PySfImage_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - int size = PyTuple_Size(args); - if (size == 1) + PySfImage *self; + self = (PySfImage *)type->tp_alloc(type, 0); + if (self != NULL) { - PySfImage *Image; - if (PyArg_ParseTuple(args, "O!", &PySfImageType, &Image)) + if (PyTuple_Size(args) == 1) { - self->obj = new sf::Image(*(Image->obj)); - return 0; - } - else PyErr_Clear(); - } - self->obj = new sf::Image(); - if (PyTuple_Size(args) > 0) - { - if (PySfImage_Create(self, args, kwds) == NULL) - { - if (size != 3) - return -1; - else if (PySfImage_LoadFromPixels(self, args) == NULL) - return -1; + PySfImage *Image; + if (PyArg_ParseTuple(args, "O!", &PySfImageType, &Image)) + { + self->obj = new sf::Image(*(Image->obj)); + } else PyErr_Clear(); } + else self->obj = new sf::Image(); } - return 0; + return (PyObject *)self; } static PyObject * -PySfImage_Copy(PySfImage* self, PyObject *args) +PySfImage_Copy(PySfImage* self, PyObject *args, PyObject *kwds) { + const char *kwlist[] = {"Source", "DestX", "DestY", "SourceRect", "ApplyAlpha", NULL}; PySfIntRect *SourceRect = NULL; PySfImage *Source = NULL; unsigned int DestX, DestY; - PyObject *PyApplyAlpha; - bool ApplyAlpha = false; - if (! PyArg_ParseTuple(args, "O!II|O!O:Image.Copy", &PySfImageType, &Source, &DestX, &DestY, &PySfIntRectType, &SourceRect, &PyApplyAlpha)) + PyObject *PyApplyAlpha = NULL; + bool ApplyAlpha = false; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!II|O!O:Image.Copy", (char **)kwlist, &PySfImageType, &Source, &DestX, &DestY, &PySfIntRectType, &SourceRect, &PyApplyAlpha)) return NULL; if (PyApplyAlpha) @@ -410,6 +413,6 @@ PySfImage_Copy(PySfImage* self, PyObject *args) PySfImage * GetNewPySfImage() { - return (PySfImage *)PySfImage_new(&PySfImageType, NULL, NULL); + return PyObject_New(PySfImage, &PySfImageType); } diff --git a/python/src/Input.cpp b/python/src/Input.cpp index c146f69f1..d3322a5bd 100644 --- a/python/src/Input.cpp +++ b/python/src/Input.cpp @@ -29,17 +29,9 @@ static PyObject * PySfInput_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PySfInput *self; - self = (PySfInput *)type->tp_alloc(type, 0); - return (PyObject *)self; -} - -static int -PySfInput_init(PySfInput *self, PyObject *args, PyObject *kwds) { PyErr_SetString(PyExc_RuntimeError, "You can't create an Input object yourself, because an Input object must always be associated to its window.\nThe only way to get an Input is by creating a window and calling : Input = MyWindow.GetInput()."); - return -1; + return NULL; } static PyObject* @@ -129,7 +121,7 @@ PyTypeObject PySfInputType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfInput_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfInput_new, /* tp_new */ }; @@ -137,6 +129,6 @@ PyTypeObject PySfInputType = { PySfInput * GetNewPySfInput() { - return (PySfInput *)PySfInput_new(&PySfInputType, NULL, NULL); + return PyObject_New(PySfInput, &PySfInputType); } diff --git a/python/src/Listener.cpp b/python/src/Listener.cpp index 4f2f877e0..c286411e6 100644 --- a/python/src/Listener.cpp +++ b/python/src/Listener.cpp @@ -27,14 +27,6 @@ #include "compat.hpp" -static PyObject * -PySfListener_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PySfListener *self; - self = (PySfListener *)type->tp_alloc(type, 0); - return (PyObject *)self; -} - static PyObject * PySfListener_SetGlobalVolume(PySfListener* self, PyObject *args) { @@ -130,7 +122,7 @@ PyTypeObject PySfListenerType = { 0, /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ - PySfListener_new, /* tp_new */ + 0, /* tp_new */ }; diff --git a/python/src/Music.cpp b/python/src/Music.cpp index ed54bf5f3..fae51b21a 100644 --- a/python/src/Music.cpp +++ b/python/src/Music.cpp @@ -40,37 +40,25 @@ PySfMusic_dealloc(PySfMusic *self) static PyObject * PySfMusic_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { + unsigned int BufferSize=44100; PySfMusic *self; self = (PySfMusic *)type->tp_alloc(type, 0); + if (self != NULL) + { + if (!PyArg_ParseTuple(args, "|I:Music.__new__", &BufferSize)) + return NULL; + self->obj = new sf::Music(BufferSize); + } return (PyObject *)self; } - -static int -PySfMusic_init(PySfMusic *self, PyObject *args, PyObject *kwds) -{ - unsigned int BufferSize=44100; - int size = PyTuple_Size(args); - if (size == 1) - { - if ( !PyArg_ParseTuple(args, "I:Music.Init", &BufferSize)) - return -1; - } - else if (size > 1) - { - PyErr_SetString(PyExc_TypeError, "Music.__init__() takes at most one argument"); - } - self->obj = new sf::Music(BufferSize); - return 0; -} - static PyObject* PySfMusic_OpenFromMemory(PySfMusic *self, PyObject *args) { unsigned int SizeInBytes; char *Data; - if (! PyArg_ParseTuple(args, "s#:Music.OpenFromMemory", &Data, &SizeInBytes)) + if (!PyArg_ParseTuple(args, "s#:Music.OpenFromMemory", &Data, &SizeInBytes)) return NULL; return PyBool_FromLong(self->obj->OpenFromMemory(Data, (std::size_t) SizeInBytes)); @@ -148,7 +136,7 @@ BufferSize : Size of the internal buffer, expressed in number of samples (ie. si 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfMusic_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfMusic_new, /* tp_new */ }; diff --git a/python/src/PostFX.cpp b/python/src/PostFX.cpp index 739920308..6d797a00e 100644 --- a/python/src/PostFX.cpp +++ b/python/src/PostFX.cpp @@ -41,12 +41,7 @@ PySfPostFX_dealloc(PySfPostFX *self) } static PyObject * -PySfPostFX_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PySfPostFX *self; - self = (PySfPostFX *)type->tp_alloc(type, 0); - return (PyObject *)self; -} +PySfPostFX_new(PyTypeObject *type, PyObject *args, PyObject *kwds); static PyObject * PySfPostFX_LoadFromFile (PySfPostFX *self, PyObject *args) @@ -73,10 +68,6 @@ PySfPostFX_LoadFromMemory (PySfPostFX *self, PyObject *args) return PyBool_FromLong(result); } -static int -PySfPostFX_init(PySfPostFX *self, PyObject *args); - - static PyObject * PySfPostFX_SetParameter(PySfPostFX* self, PyObject *args) { char *Name; float X, Y, Z, W; int size = PyTuple_Size(args); if (!PyArg_ParseTuple(args, "sf|fff:PostFX.SetParameter", &Name, &X, &Y, &Z, &W)) return NULL; switch (size) @@ -179,24 +170,25 @@ Copy constructor : sf.PostFX(Copy) where Copy is a sf.PostFX instance.", /* tp_d 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfPostFX_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfPostFX_new, /* tp_new */ }; -static int -PySfPostFX_init(PySfPostFX *self, PyObject *args) -{ - if (PyTuple_Size(args) == 1) - { - PySfPostFX *Copy; - if (PyArg_ParseTuple(args, "O!", &PySfPostFXType, &Copy)) - self->obj = new sf::PostFX(*(Copy->obj)); - else - return -1; - } - else - self->obj = new sf::PostFX(); - return 0; -} +static PyObject * +PySfPostFX_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PySfPostFX *self; + self = (PySfPostFX *)type->tp_alloc(type, 0); + if (self != NULL) + { + PySfPostFX *Copy = NULL; + self->IsCustom = false; + if (!PyArg_ParseTuple(args, "|O!", &PySfPostFXType, &Copy)) + return NULL; + if (Copy) self->obj = new sf::PostFX(*(Copy->obj)); + else self->obj = new sf::PostFX(); + } + return (PyObject *)self; +} diff --git a/python/src/PostFX.hpp b/python/src/PostFX.hpp index 080b3a0a5..9994885f7 100644 --- a/python/src/PostFX.hpp +++ b/python/src/PostFX.hpp @@ -31,6 +31,7 @@ typedef struct { PyObject_HEAD + bool IsCustom; sf::PostFX *obj; } PySfPostFX; diff --git a/python/src/Rect.cpp b/python/src/Rect.cpp index a05583e92..26a61b5ac 100644 --- a/python/src/Rect.cpp +++ b/python/src/Rect.cpp @@ -63,36 +63,33 @@ PySfFloatRect_dealloc(PySfFloatRect* self) static PyObject * PySfIntRect_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { + const char *kwlist[] = {"Left", "Top", "Right", "Bottom", NULL}; PySfIntRect *self; self = (PySfIntRect *)type->tp_alloc(type, 0); + if (self != NULL) + { + if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiii:IntRect.__init__", (char **)kwlist, &(self->Left), &(self->Top), &(self->Right), &(self->Bottom))) + return NULL; + self->obj = new sf::IntRect(self->Left, self->Top, self->Right, self->Bottom); + } return (PyObject *)self; } static PyObject * PySfFloatRect_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { + const char *kwlist[] = {"Left", "Top", "Right", "Bottom", NULL}; PySfFloatRect *self; self = (PySfFloatRect *)type->tp_alloc(type, 0); + if (self != NULL) + { + if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffff:FloatRect.__init__", (char **)kwlist, &(self->Left), &(self->Top), &(self->Right), &(self->Bottom))) + return NULL; + self->obj = new sf::FloatRect(self->Left, self->Top, self->Right, self->Bottom); + } return (PyObject *)self; } -static int -PySfIntRect_init(PySfIntRect *self, PyObject *args, PyObject *kwds) -{ - const char *kwlist[] = {"Left", "Top", "Right", "Bottom", NULL}; - int Left, Top, Right, Bottom; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiii:IntRect.__init__", (char **)kwlist, &Left, &Top, &Right, &Bottom)) - return -1; - - self->Left = Left; - self->Top = Top; - self->Right = Right; - self->Bottom = Bottom; - self->obj = new sf::IntRect(Left, Top, Right, Bottom); - return 0; -} - static PyObject * PySfIntRect_GetWidth(PySfIntRect *self) { @@ -133,24 +130,6 @@ PySfFloatRect_Contains(PySfFloatRect* self, PyObject *args); static PyObject * PySfFloatRect_Intersects(PySfFloatRect* self, PyObject *args); -static int -PySfFloatRect_init(PySfFloatRect *self, PyObject *args, PyObject *kwds) -{ - const char *kwlist[] = {"Left", "Top", "Right", "Bottom", NULL}; - float Left, Top, Right, Bottom; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffff:FloatRect.__init__", (char **)kwlist, &Left, &Top, &Right, &Bottom)) - return -1; - - self->Left = Left; - self->Top = Top; - self->Right = Right; - self->Bottom = Bottom; - self->obj = new sf::FloatRect(Left, Top, Right, Bottom); - return 0; -} - - static PyObject * PySfIntRect_Offset(PySfIntRect* self, PyObject *args) { @@ -259,7 +238,7 @@ PyTypeObject PySfIntRectType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfIntRect_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfIntRect_new, /* tp_new */ }; @@ -301,7 +280,7 @@ PyTypeObject PySfFloatRectType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfFloatRect_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfFloatRect_new, /* tp_new */ }; @@ -406,13 +385,13 @@ PySfFloatRectUpdateSelf(PySfFloatRect *self) PySfIntRect * GetNewPySfIntRect() { - return (PySfIntRect *)PySfIntRect_new(&PySfIntRectType, NULL, NULL); + return PyObject_New(PySfIntRect, &PySfIntRectType); } PySfFloatRect * GetNewPySfFloatRect() { - return (PySfFloatRect *)PySfFloatRect_new(&PySfFloatRectType, NULL, NULL); + return PyObject_New(PySfFloatRect, &PySfFloatRectType); } diff --git a/python/src/RenderWindow.cpp b/python/src/RenderWindow.cpp index 221eee557..598efd877 100644 --- a/python/src/RenderWindow.cpp +++ b/python/src/RenderWindow.cpp @@ -52,19 +52,11 @@ static PyObject * PySfRenderWindow_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfRenderWindow *self; - self = (PySfRenderWindow *)type->tp_alloc(type, 0); + self = (PySfRenderWindow *)type->tp_alloc(type, 0); + if (self != NULL) + self->obj = new sf::RenderWindow(); return (PyObject *)self; -} - -static int -PySfRenderWindow_init(PySfRenderWindow *self, PyObject *args, PyObject *kwds) -{ - self->obj = new sf::RenderWindow(); - if (PyTuple_Size(args) > 0) - if (PySfWindow_Create((PySfWindow *)self, args, kwds) == NULL) - return -1; - return 0; -} +} static PyObject * PySfRenderWindow_Capture(PySfRenderWindow *self) @@ -99,13 +91,13 @@ PySfRenderWindow_DrawObject(PySfRenderWindow *RenderWindow, PySfDrawable *Obj) { if (PyObject_TypeCheck((PyObject *)Obj, &PySfDrawableType)) { - if (PyObject_HasAttrString((PyObject *)Obj, "Render")) + if (Obj->IsCustom) { Py_CLEAR(Obj->obj->RenderWindow); Py_INCREF(RenderWindow); Obj->obj->RenderWindow = RenderWindow; } - RenderWindow->obj->Draw( *(Obj->obj) ); + RenderWindow->obj->Draw(*(Obj->obj)); return true; } return false; @@ -146,23 +138,14 @@ PySfRenderWindow_Draw(PySfRenderWindow *self, PyObject *args) static PyObject * PySfRenderWindow_Clear(PySfRenderWindow *self, PyObject *args) { - PySfColor *Color; - int size = PyTuple_Size(args); - if (size == 1) - { - if (!PyArg_ParseTuple(args, "O!:RenderWindow.Clear", &PySfColorType, &Color)) - return NULL; - PySfColorUpdate(Color); - self->obj->Clear(*(Color->obj)); - } - else if (size == 0) - { - self->obj->Clear(sf::Color::Black); - } + PySfColor *Color = NULL; + if (!PyArg_ParseTuple(args, "|O!:RenderWindow.Clear", &PySfColorType, &Color)) + return NULL; + if (Color == NULL) self->obj->Clear(sf::Color::Black); else { - PyErr_SetString(PyExc_TypeError, "RenderWindow.Clear() takes one or zero argument"); - return NULL; + PySfColorUpdate(Color); + self->obj->Clear(*(Color->obj)); } Py_RETURN_NONE; } @@ -281,7 +264,7 @@ Parameters:\n\ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfRenderWindow_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfRenderWindow_new, /* tp_new */ }; diff --git a/python/src/Shape.cpp b/python/src/Shape.cpp index 89c016ee9..6ac2dd4a5 100644 --- a/python/src/Shape.cpp +++ b/python/src/Shape.cpp @@ -45,16 +45,14 @@ PySfShape_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfShape *self; self = (PySfShape *)type->tp_alloc(type, 0); + if (self != NULL) + { + self->obj = new sf::Shape(); + self->IsCustom = false; + } return (PyObject *)self; } -static int -PySfShape_init(PySfShape *self, PyObject *args) -{ - self->obj = new sf::Shape(); - return 0; -} - // void AddPoint(float X, float Y, const Color& Col = Color(255, 255, 255), const Color& OutlineCol = Color(0, 0, 0)); static PyObject * PySfShape_AddPoint(PySfShape* self, PyObject *args, PyObject *kwds) @@ -231,7 +229,7 @@ PySfShape_SetPointOutlineColor(PySfShape* self, PyObject *args) { unsigned int Index; PySfColor *Color; - if (!PyArg_ParseTuple(args, "IO!:Shape:SetPointOutlineColor", &Index, &PySfColorType, &Color)) + if (!PyArg_ParseTuple(args, "IO!:Shape.SetPointOutlineColor", &Index, &PySfColorType, &Color)) return NULL; PySfColorUpdate(Color); self->obj->SetPointOutlineColor(Index, *(Color->obj)); @@ -365,7 +363,7 @@ PyTypeObject PySfShapeType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfShape_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfShape_new, /* tp_new */ }; @@ -374,6 +372,8 @@ PyTypeObject PySfShapeType = { PySfShape * GetNewPySfShape() { - return (PySfShape *)PySfShape_new(&PySfShapeType, NULL, NULL); + PySfShape *Shape = PyObject_New(PySfShape, &PySfShapeType); + Shape->IsCustom = false; + return Shape; } diff --git a/python/src/Shape.hpp b/python/src/Shape.hpp index ee2d2ce1c..7e21bfc94 100644 --- a/python/src/Shape.hpp +++ b/python/src/Shape.hpp @@ -32,6 +32,7 @@ typedef struct { PyObject_HEAD + bool IsCustom; sf::Shape *obj; } PySfShape; diff --git a/python/src/Sound.cpp b/python/src/Sound.cpp index 494d7fb43..bd3418a9c 100644 --- a/python/src/Sound.cpp +++ b/python/src/Sound.cpp @@ -39,23 +39,38 @@ PySfSound_dealloc(PySfSound *self) } static PyObject * -PySfSound_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PySfSound *self; - self = (PySfSound *)type->tp_alloc(type, 0); - return (PyObject *)self; +PySfSound_new(PyTypeObject *type, PyObject *args, PyObject *kwds); + +static int +PySfSound_init(PySfSound *self, PyObject *args, PyObject *kwds) +{ + const char *kwlist[] = {"Buffer", "Loop", "Pitch", "Volume", "X", "Y", "Z", NULL}; + PySfSoundBuffer *Buffer=NULL; + PyObject *Loop=NULL; + float Pitch=1.f, Volume=100.f, X=0.f, Y=0.f, Z=0.f; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O!Offfff:Sound.__new__", (char **)kwlist, &PySfSoundBufferType, &Buffer, &Loop, &Pitch, &Volume, &X, &Y, &Z)) + return -1; + { + if (Loop) + self->obj->SetLoop(PyBool_AsBool(Loop)); + if (Buffer) + self->obj->SetBuffer(*(Buffer->obj)); + self->obj->SetPitch(Pitch); + self->obj->SetVolume(Volume); + self->obj->SetPosition(X, Y, Z); + } + return 0; } - -static int -PySfSound_init(PySfSound *self, PyObject *args, PyObject *kwds); - static PyObject* PySfSound_SetBuffer(PySfSound *self, PyObject *args) { PySfSoundBuffer *Buffer = (PySfSoundBuffer *)args; if (!PyObject_TypeCheck(args, &PySfSoundBufferType)) + { PyErr_SetString(PyExc_TypeError, "Sound.SetBuffer() The argument must be a sf.SoundBuffer."); + return NULL; + } self->obj->SetBuffer(*(Buffer->obj)); Py_RETURN_NONE; @@ -269,38 +284,26 @@ Copy constructor : Sound(Copy) where Copy is a sf.Sound instance.", /* tp_doc */ PySfSound_new, /* tp_new */ }; -static int -PySfSound_init(PySfSound *self, PyObject *args, PyObject *kwds) -{ - const char *kwlist[] = {"Buffer", "Loop", "Pitch", "Volume", "X", "Y", "Z", NULL}; - PySfSoundBuffer *Buffer=NULL; - bool Loop=false; - PyObject *LoopObj=Py_False; - float Pitch=1.f, Volume=100.f, X=0.f, Y=0.f, Z=0.f; - - if (PyTuple_Size(args) == 1) +static PyObject * +PySfSound_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PySfSound *self; + self = (PySfSound *)type->tp_alloc(type, 0); + if (self != NULL) { - PySfSound *Copy; - if (PyArg_ParseTuple(args, "O!:Sound.__init__", &PySfSoundType, &Copy)) + if (PyTuple_Size(args) == 1) { - self->obj = new sf::Sound(*(Copy->obj)); - return 0; + PySfSound *Copy; + if (PyArg_ParseTuple(args, "O!:Sound.__new__", &PySfSoundType, &Copy)) + { + self->obj = new sf::Sound(*(Copy->obj)); + return (PyObject *)self; + } + else PyErr_Clear(); } - else PyErr_Clear(); - } - if (PyTuple_Size(args) > 0) - { - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|Offfff:Sound.__init__", (char **)kwlist, &PySfSoundBufferType, &Buffer, &LoopObj, &Pitch, &Volume, &X, &Y, &Z)) - return -1; - if (PyObject_IsTrue(LoopObj)) - Loop = true; - - self->obj = new sf::Sound(*(Buffer->obj), Loop, Pitch, Volume, sf::Vector3f(X, Y, Z)); - } - else self->obj = new sf::Sound(); - - return 0; + } + return (PyObject *)self; } void diff --git a/python/src/SoundBuffer.cpp b/python/src/SoundBuffer.cpp index e3c95238c..bf4ac168b 100644 --- a/python/src/SoundBuffer.cpp +++ b/python/src/SoundBuffer.cpp @@ -35,15 +35,7 @@ PySfSoundBuffer_dealloc(PySfSoundBuffer *self) } static PyObject * -PySfSoundBuffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PySfSoundBuffer *self; - self = (PySfSoundBuffer *)type->tp_alloc(type, 0); - return (PyObject *)self; -} - -static int -PySfSoundBuffer_init(PySfSoundBuffer *self, PyObject *args, PyObject *kwds); +PySfSoundBuffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds); static PyObject* PySfSoundBuffer_LoadFromFile(PySfSoundBuffer *self, PyObject *args) @@ -170,32 +162,33 @@ Copy constructor : SoundBuffer(Copy) where Copy is a sf.SoundBuffer instance.", 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfSoundBuffer_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfSoundBuffer_new, /* tp_new */ }; -static int -PySfSoundBuffer_init(PySfSoundBuffer *self, PyObject *args, PyObject *kwds) +static PyObject * +PySfSoundBuffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - int size = PyTuple_Size(args); - if (size == 1) + PySfSoundBuffer *self; + self = (PySfSoundBuffer *)type->tp_alloc(type, 0); + if (self != NULL) { - PySfSoundBuffer *Copy; - if (!PyArg_ParseTuple(args, "O!:SoundBuffer.__init__", &PySfSoundBufferType, &Copy)) - return -1; - self->obj = new sf::SoundBuffer(*(Copy->obj)); - } - else if (size == 0) + PySfSoundBuffer *Copy=NULL; + if (PyArg_ParseTuple(args, "O!:SoundBuffer.__init__", &PySfSoundBufferType, &Copy)) + { + self->obj = new sf::SoundBuffer(*(Copy->obj)); + return (PyObject *)self; + } + PyErr_Clear(); self->obj = new sf::SoundBuffer(); - else - PyErr_SetString(PyExc_TypeError, "SoundBuffer.__init__() takes 0 or 1 argument"); - return 0; + } + return (PyObject *)self; } PySfSoundBuffer * GetNewPySfSoundBuffer() { - return (PySfSoundBuffer *)PySfSoundBuffer_new(&PySfSoundBufferType, NULL, NULL); + return PyObject_New(PySfSoundBuffer, &PySfSoundBufferType); } diff --git a/python/src/SoundBufferRecorder.cpp b/python/src/SoundBufferRecorder.cpp index 5da462ae3..05978f6fb 100644 --- a/python/src/SoundBufferRecorder.cpp +++ b/python/src/SoundBufferRecorder.cpp @@ -43,16 +43,11 @@ PySfSoundBufferRecorder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfSoundBufferRecorder *self; self = (PySfSoundBufferRecorder *)type->tp_alloc(type, 0); + if (self != NULL) + self->obj = new sf::SoundBufferRecorder(); return (PyObject *)self; } -static int -PySfSoundBufferRecorder_init(PySfSoundBufferRecorder *self, PyObject *args) -{ - self->obj = new sf::SoundBufferRecorder(); - return 0; -} - static PyObject * PySfSoundBufferRecorder_GetBuffer(PySfSoundBufferRecorder* self) { @@ -103,7 +98,7 @@ PyTypeObject PySfSoundBufferRecorderType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfSoundBufferRecorder_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfSoundBufferRecorder_new, /* tp_new */ }; diff --git a/python/src/SoundRecorder.cpp b/python/src/SoundRecorder.cpp index e45ef1828..46f7ae199 100644 --- a/python/src/SoundRecorder.cpp +++ b/python/src/SoundRecorder.cpp @@ -76,17 +76,14 @@ PySfSoundRecorder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfSoundRecorder *self; self = (PySfSoundRecorder *)type->tp_alloc(type, 0); + if (self != NULL) + { + self->obj = new CustomSoundRecorder(); + self->obj->SoundRecorder = (PyObject *)self; + } return (PyObject *)self; } -static int -PySfSoundRecorder_init(PySfSoundRecorder *self, PyObject *args) -{ - self->obj = new CustomSoundRecorder(); - self->obj->SoundRecorder = (PyObject *)self; - return 0; -} - static PyObject * PySfSoundRecorder_Start(PySfSoundRecorder* self, PyObject *args) { @@ -162,7 +159,7 @@ Construct the sound recorder with a callback function for processing captured sa 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfSoundRecorder_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfSoundRecorder_new, /* tp_new */ }; diff --git a/python/src/SoundStream.cpp b/python/src/SoundStream.cpp index 65bcb43eb..2a2f0d54c 100644 --- a/python/src/SoundStream.cpp +++ b/python/src/SoundStream.cpp @@ -81,15 +81,6 @@ void CustomSoundStream::Init(unsigned int ChannelsCount, unsigned int SampleRate Initialize(ChannelsCount, SampleRate); } -static int -PySfSoundStream_init(PySfSoundStream *self, PyObject *args, PyObject *kwds) -{ - self->obj = new CustomSoundStream(); - self->obj->PyData = NULL; - self->obj->SoundStream = (PyObject *)self; - return 0; -} - static void PySfSoundStream_dealloc(PySfSoundStream *self) { @@ -102,6 +93,12 @@ PySfSoundStream_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfSoundStream *self; self = (PySfSoundStream *)type->tp_alloc(type, 0); + if (self != NULL) + { + self->obj = new CustomSoundStream(); + self->obj->PyData = NULL; + self->obj->SoundStream = (PyObject *)self; + } return (PyObject *)self; } @@ -310,7 +307,7 @@ or for streaming sound from the network", /* tp_doc */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfSoundStream_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfSoundStream_new, /* tp_new */ }; diff --git a/python/src/Sprite.cpp b/python/src/Sprite.cpp index 3070e47c8..5482acc7d 100644 --- a/python/src/Sprite.cpp +++ b/python/src/Sprite.cpp @@ -56,7 +56,8 @@ PySfSprite_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (self != NULL) { - self->Image = NULL; + self->Image = NULL; + self->IsCustom = false; } return (PyObject *)self; diff --git a/python/src/Sprite.hpp b/python/src/Sprite.hpp index aaab6d635..ae83bbf31 100644 --- a/python/src/Sprite.hpp +++ b/python/src/Sprite.hpp @@ -33,6 +33,7 @@ typedef struct { PyObject_HEAD + bool IsCustom; sf::Sprite *obj; PySfImage *Image; } PySfSprite; diff --git a/python/src/String.cpp b/python/src/String.cpp index 527233400..ecb168508 100644 --- a/python/src/String.cpp +++ b/python/src/String.cpp @@ -48,59 +48,14 @@ PySfString_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PySfString *self; self = (PySfString *)type->tp_alloc(type, 0); if (self != NULL) + { self->font = NULL; + self->IsCustom = false; + self->obj = new sf::String(); + } return (PyObject *)self; } -static int -PySfString_init(PySfString *self, PyObject *args, PyObject *kwds) -{ - const char *kwlist[] = {"Text", "Font", "Size", NULL}; - float Size = 30.f; - PyObject *Text=NULL; - PySfFont *FontTmp = NULL; - sf::Font *Font; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO!f:String.__init__", (char **)kwlist, &Text, &PySfFontType, &FontTmp, &Size)) - return -1; - - if (FontTmp) - { - Font = (FontTmp->obj); - Py_INCREF(FontTmp); - self->font = FontTmp; - } - else - Font = (sf::Font *)&(sf::Font::GetDefaultFont()); - - if (Text != NULL) - { - if (PyUnicode_Check(Text)) - { -#if Py_UNICODE_SIZE == 4 - self->obj = new sf::String((sf::Uint32 *)PyUnicode_AS_UNICODE(Text), *Font, Size); -#else - self->obj = new sf::String((sf::Uint16 *)PyUnicode_AS_UNICODE(Text), *Font, Size); -#endif - } -#ifdef IS_PY3K - else if (PyBytes_Check(Text)) - self->obj = new sf::String(sf::Unicode::UTF8String((sf::Uint8 *)PyBytes_AsString(Text)), *Font, Size); -#else - else if (PyString_Check(Text)) - self->obj = new sf::String(sf::Unicode::UTF8String((sf::Uint8 *)PyString_AsString(Text)), *Font, Size); -#endif - else - { - PyErr_SetString(PyExc_TypeError, "String.__init__() first argument must be str"); - return -1; - } - } - else - self->obj = new sf::String("", *Font, Size); - return 0; -} - static PyObject * PySfString_SetText(PySfString* self, PyObject *args) { @@ -236,6 +191,44 @@ PySfString_GetCharacterPos(PySfString* self, PyObject *args) return Py_BuildValue("ff", Pos.x, Pos.y); } +static int +PySfString_init(PySfString *self, PyObject *args, PyObject *kwds) +{ + const char *kwlist[] = {"Text", "Font", "Size", NULL}; + float Size = 30.f; + PyObject *Text=NULL; + PySfFont *Font = NULL; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO!f:String.__new__", (char **)kwlist, &Text, &PySfFontType, &Font, &Size)) + return -1; + + if (Text != NULL) + { + if (PyUnicode_Check(Text)) + { +#if Py_UNICODE_SIZE == 4 + self->obj->SetText((sf::Uint32 *)PyUnicode_AS_UNICODE(Text)); +#else + self->obj->SetText((sf::Uint16 *)PyUnicode_AS_UNICODE(Text)); +#endif + } +#ifdef IS_PY3K + else if (PyBytes_Check(Text)) + self->obj->SetText(sf::Unicode::UTF8String((sf::Uint8 *)PyBytes_AsString(Text))); +#else + else if (PyString_Check(Text)) + self->obj->SetText(sf::Unicode::UTF8String((sf::Uint8 *)PyString_AsString(Text))); +#endif + else + { + PyErr_SetString(PyExc_TypeError, "String.__init__() first argument must be str"); + return -1; + } + } + if (Font) PySfString_SetFont(self, (PyObject *)Font); + self->obj->SetSize(Size); + return 0; +} + static PyMethodDef PySfString_methods[] = { {"GetCharacterPos", (PyCFunction)PySfString_GetCharacterPos, METH_O, "GetCharacterPos(Index)\n\ diff --git a/python/src/String.hpp b/python/src/String.hpp index f32e14250..ef26c6a61 100644 --- a/python/src/String.hpp +++ b/python/src/String.hpp @@ -33,6 +33,7 @@ typedef struct { PyObject_HEAD + bool IsCustom; sf::String *obj; PySfFont *font; } PySfString; diff --git a/python/src/VideoMode.cpp b/python/src/VideoMode.cpp index 70cefbb5c..5b630546f 100644 --- a/python/src/VideoMode.cpp +++ b/python/src/VideoMode.cpp @@ -48,17 +48,16 @@ PySfVideoMode_dealloc(PySfVideoMode* self) static PyObject * PySfVideoMode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { + const char *kwlist[] = {"Width", "Height", "BitsPerPixel", NULL}; PySfVideoMode *self; - self = (PySfVideoMode *)type->tp_alloc(type, 0); - if (self != NULL) { - self->Width = 0; - self->Height = 0; self->BitsPerPixel = 32; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "II|I:VideoMode.__init__", (char **)kwlist, &self->Width, &self->Height, &self->BitsPerPixel)) + return NULL; + self->obj = new sf::VideoMode(self->Width, self->Height, self->BitsPerPixel); } - return (PyObject *)self; } @@ -70,27 +69,10 @@ PySfVideoModeUpdate(PySfVideoMode *self) self->obj->BitsPerPixel = self->BitsPerPixel; } -static int -PySfVideoMode_init(PySfVideoMode *self, PyObject *args, PyObject *kwds) -{ - const char *kwlist[] = {"Width", "Height", "BitsPerPixel", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "II|I:VideoMode.__init__", (char **)kwlist, &self->Width, &self->Height, &self->BitsPerPixel)) - return -1; - - self->obj = new sf::VideoMode(self->Width, self->Height, self->BitsPerPixel); - - return 0; -} - - - static PyObject * PySfVideoMode_IsValid(PySfVideoMode* self) { - self->obj->Width = self->Width; - self->obj->Height = self->Height; - self->obj->BitsPerPixel = self->BitsPerPixel; + PySfVideoModeUpdate(self); return PyBool_FromLong(self->obj->IsValid()); } @@ -100,7 +82,7 @@ PySfVideoMode_GetDesktopMode(PySfVideoMode* self) PySfVideoMode *VideoMode; VideoMode = GetNewPySfVideoMode(); - VideoMode->obj = new sf::VideoMode ( sf::VideoMode::GetDesktopMode() ); + VideoMode->obj = new sf::VideoMode(sf::VideoMode::GetDesktopMode()); VideoMode->Width = VideoMode->obj->Width; VideoMode->Height = VideoMode->obj->Height; VideoMode->BitsPerPixel = VideoMode->obj->BitsPerPixel; @@ -117,7 +99,7 @@ PySfVideoMode_GetMode(PySfVideoMode* self, PyObject *args) index = (std::size_t)PyLong_AsLong(args); VideoMode = GetNewPySfVideoMode(); - VideoMode->obj = new sf::VideoMode ( sf::VideoMode::GetMode(index) ); + VideoMode->obj = new sf::VideoMode(sf::VideoMode::GetMode(index)); VideoMode->Width = VideoMode->obj->Width; VideoMode->Height = VideoMode->obj->Height; VideoMode->BitsPerPixel = VideoMode->obj->BitsPerPixel; @@ -132,7 +114,6 @@ PySfVideoMode_GetModesCount(PySfVideoMode* self) } - static PyMethodDef PySfVideoMode_methods[] = { {"IsValid", (PyCFunction)PySfVideoMode_IsValid, METH_NOARGS, "IsValid()\nTell whether or not the video mode is supported."}, {"GetDesktopMode", (PyCFunction)PySfVideoMode_GetDesktopMode, METH_STATIC | METH_NOARGS, "GetDesktopMode()\nGet the current desktop video mode."}, @@ -190,7 +171,7 @@ Construct the video mode with its attributes : VideoMode(ModeWidth, ModeHeight, 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfVideoMode_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfVideoMode_new, /* tp_new */ }; @@ -199,6 +180,6 @@ Construct the video mode with its attributes : VideoMode(ModeWidth, ModeHeight, PySfVideoMode * GetNewPySfVideoMode() { - return (PySfVideoMode *)PySfVideoMode_new(&PySfVideoModeType, NULL, NULL); + return PyObject_New(PySfVideoMode, &PySfVideoModeType); } diff --git a/python/src/Window.cpp b/python/src/Window.cpp index 9844ddf68..481eaf42b 100644 --- a/python/src/Window.cpp +++ b/python/src/Window.cpp @@ -50,7 +50,9 @@ static PyObject * PySfWindow_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfWindow *self; - self = (PySfWindow *)type->tp_alloc(type, 0); + self = (PySfWindow *)type->tp_alloc(type, 0); + if (self != NULL) + self->obj = new sf::Window(); return (PyObject *)self; } @@ -128,9 +130,8 @@ PySfWindow_Create(PySfWindow* self, PyObject *args, PyObject *kwds) static int PySfWindow_init(PySfWindow *self, PyObject *args, PyObject *kwds) -{ - self->obj = new sf::Window(); - if (PyTuple_Size(args) > 0) +{ + if (args != NULL) if (PySfWindow_Create(self, args, kwds) == NULL) return -1; return 0; diff --git a/python/src/WindowSettings.cpp b/python/src/WindowSettings.cpp index 9a6579624..5c4a4f275 100644 --- a/python/src/WindowSettings.cpp +++ b/python/src/WindowSettings.cpp @@ -55,6 +55,7 @@ PySfWindowSettingsUpdate(PySfWindowSettings *self) static PyObject * PySfWindowSettings_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { + const char *kwlist[] = {"DepthBits", "StencilBits", "AntialiasingLevel", NULL}; PySfWindowSettings *self; self = (PySfWindowSettings *)type->tp_alloc(type, 0); if (self != NULL) @@ -62,23 +63,13 @@ PySfWindowSettings_new(PyTypeObject *type, PyObject *args, PyObject *kwds) self->DepthBits = 24; self->StencilBits = 8; self->AntialiasingLevel = 0; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|III:WindowSettings.__init__", (char **)kwlist, &(self->DepthBits), &(self->StencilBits), &(self->AntialiasingLevel))) + return NULL; + self->obj = new sf::WindowSettings(self->DepthBits, self->StencilBits, self->AntialiasingLevel); } return (PyObject *)self; } - -static int -PySfWindowSettings_init(PySfWindowSettings *self, PyObject *args, PyObject *kwds) -{ - const char *kwlist[] = {"DepthBits", "StencilBits", "AntialiasingLevel", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|III:WindowSettings.__init__", (char **)kwlist, &(self->DepthBits), &(self->StencilBits), &(self->AntialiasingLevel))) - return -1; - self->obj = new sf::WindowSettings(self->DepthBits, self->StencilBits, self->AntialiasingLevel); - - return 0; -} - - PyTypeObject PySfWindowSettingsType = { head_init "WindowSettings", /*tp_name*/ @@ -115,7 +106,7 @@ PyTypeObject PySfWindowSettingsType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfWindowSettings_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfWindowSettings_new, /* tp_new */ }; @@ -123,6 +114,6 @@ PyTypeObject PySfWindowSettingsType = { PySfWindowSettings * GetNewPySfWindowSettings() { - return (PySfWindowSettings *)PySfWindowSettings_new(&PySfWindowSettingsType, NULL, NULL); + return PyObject_New(PySfWindowSettings, &PySfWindowSettingsType); }