From 79bf5c62522937b61e2cf07b79e3759debf279fb Mon Sep 17 00:00:00 2001 From: remi-k Date: Wed, 25 Feb 2009 10:40:38 +0000 Subject: [PATCH] * Began supporting python3 * Code clean-up git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1021 4e206d99-4929-0410-ac5d-dfc041789085 --- python/samples/opengl.py | 10 +- python/src/Clock.cpp | 21 +-- python/src/Clock.hpp | 8 +- python/src/Color.cpp | 8 +- python/src/Color.hpp | 5 +- python/src/Drawable.cpp | 23 ++-- python/src/Drawable.hpp | 7 +- python/src/Event.cpp | 134 +++++++++---------- python/src/Event.hpp | 7 +- python/src/Image.cpp | 118 +++++++++-------- python/src/Image.hpp | 9 +- python/src/Input.cpp | 49 ++----- python/src/Input.hpp | 7 +- python/src/Key.cpp | 252 +++++++++++++++--------------------- python/src/Key.hpp | 8 ++ python/src/Music.cpp | 58 +++++---- python/src/Music.hpp | 6 +- python/src/PostFX.cpp | 90 +++++++------ python/src/PostFX.hpp | 7 +- python/src/Rect.cpp | 67 ++++------ python/src/Rect.hpp | 7 +- python/src/RenderTarget.cpp | 35 ++--- python/src/RenderTarget.hpp | 5 +- python/src/RenderWindow.cpp | 42 +++--- python/src/RenderWindow.hpp | 6 +- python/src/Sleep.hpp | 6 +- python/src/Sprite.cpp | 45 +++---- python/src/Sprite.hpp | 9 +- python/src/Window.cpp | 3 +- python/src/compat.hpp | 42 ++++++ python/src/main.cpp | 122 ++++++++++------- 31 files changed, 572 insertions(+), 644 deletions(-) create mode 100644 python/src/compat.hpp diff --git a/python/samples/opengl.py b/python/samples/opengl.py index 6442b8399..e35df9e5f 100644 --- a/python/samples/opengl.py +++ b/python/samples/opengl.py @@ -30,11 +30,7 @@ def main(): Texture = glGenTextures(1) # instead of glGenTextures(1, &Texture); glBindTexture(GL_TEXTURE_2D, Texture) # It is almost the same line there, except in C++, the last argument was Image.GetPixelsPtr(). - # With GetPixelsPtr, PySFML returns a PyCObject: "an opaque value, useful for C extension - # modules who need to pass an opaque value (as a void* pointer) through Python code to other C code". - # However, gluBuild2DMipmaps' python version takes a string as last argument (which is normally a - # pointer to pixels data). This is why Image.GetPixelsPtr is replaced by Image.GetPixelsString. - # This function (that doesn't exist in C++ SFML) returns a string that contains the pixels data. + # In python, GetPixels simply returns a string. gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Image.GetWidth(), Image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, Image.GetPixels()) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) @@ -73,7 +69,7 @@ def main(): # Adjust the viewport when the window is resized if Event.Type == sf.Event.Resized: - glViewport(0, 0, Event.Size.Width, Event.Size.Height); + glViewport(0, 0, Event.Size.Width, Event.Size.Height) # Draw background App.Draw(Background) @@ -81,7 +77,7 @@ def main(): # Clear depth buffer glClear(GL_DEPTH_BUFFER_BIT) - # Apply some transf.ormations + # Apply some transformations glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTranslatef(0, 0, -200) diff --git a/python/src/Clock.cpp b/python/src/Clock.cpp index 7c28c3141..714a6603a 100644 --- a/python/src/Clock.cpp +++ b/python/src/Clock.cpp @@ -24,24 +24,14 @@ #include "Clock.hpp" - -typedef struct { - PyObject_HEAD - sf::Clock *obj; -} PySfClock; - - - -static PyMemberDef PySfClock_members[] = { - {NULL} /* Sentinel */ -}; +#include "compat.hpp" static void PySfClock_dealloc(PySfClock *self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * @@ -86,8 +76,7 @@ static PyMethodDef PySfClock_methods[] = { }; PyTypeObject PySfClockType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Clock", /*tp_name*/ sizeof(PySfClock), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -95,7 +84,7 @@ PyTypeObject PySfClockType = { 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ - 0, /*tp_compare*/ + 0, /*tp_compare (tp_reserved in py3k)*/ 0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ @@ -115,7 +104,7 @@ PyTypeObject PySfClockType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfClock_methods, /* tp_methods */ - PySfClock_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/Clock.hpp b/python/src/Clock.hpp index 4b2c9d8ca..d0a32f65c 100644 --- a/python/src/Clock.hpp +++ b/python/src/Clock.hpp @@ -25,11 +25,13 @@ #ifndef __PYCLOCK_HPP #define __PYCLOCK_HPP +#include #include -#include -#include -#include +typedef struct { + PyObject_HEAD + sf::Clock *obj; +} PySfClock; #endif diff --git a/python/src/Color.cpp b/python/src/Color.cpp index 1b3043299..be8f16754 100644 --- a/python/src/Color.cpp +++ b/python/src/Color.cpp @@ -24,6 +24,9 @@ #include "Color.hpp" +#include "offsetof.hpp" +#include "compat.hpp" + static PyMemberDef PySfColor_members[] = { {(char *)"r", T_UBYTE, offsetof(PySfColor, r), 0, (char *)"Red component."}, {(char *)"g", T_UBYTE, offsetof(PySfColor, g), 0, (char *)"Green component."}, @@ -38,7 +41,7 @@ static void PySfColor_dealloc(PySfColor *self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } void @@ -101,8 +104,7 @@ static PyMethodDef PySfColor_methods[] = { PyTypeObject PySfColorType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Color", /*tp_name*/ sizeof(PySfColor), /*tp_basicsize*/ 0, /*tp_itemsize*/ diff --git a/python/src/Color.hpp b/python/src/Color.hpp index 19379e3bd..d86ed7f42 100644 --- a/python/src/Color.hpp +++ b/python/src/Color.hpp @@ -25,13 +25,10 @@ #ifndef __PYCOLOR_HPP #define __PYCOLOR_HPP -#include -#include - #include #include -#include "offsetof.hpp" +#include typedef struct { diff --git a/python/src/Drawable.cpp b/python/src/Drawable.cpp index 008e469a9..d0fb70042 100644 --- a/python/src/Drawable.cpp +++ b/python/src/Drawable.cpp @@ -23,6 +23,13 @@ //////////////////////////////////////////////////////////// #include "Drawable.hpp" +#include "Color.hpp" + +#include "compat.hpp" + + +extern PyTypeObject PySfColorType; + void CustomDrawable::Render (sf::RenderTarget& Target) const { @@ -30,20 +37,11 @@ void CustomDrawable::Render (sf::RenderTarget& Target) const PyObject_CallFunction(RenderFunction, (char *)"O", RenderWindow); } - -extern PyTypeObject PySfColorType; - -static PyMemberDef PySfDrawable_members[] = { - {NULL} /* Sentinel */ -}; - - - static void PySfDrawable_dealloc(PySfDrawable *self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * @@ -261,8 +259,7 @@ Transform a point from local coordinates into global coordinates (ie it applies }; PyTypeObject PySfDrawableType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Drawable", /*tp_name*/ sizeof(PySfDrawable), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -290,7 +287,7 @@ PyTypeObject PySfDrawableType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfDrawable_methods, /* tp_methods */ - PySfDrawable_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/Drawable.hpp b/python/src/Drawable.hpp index 2553b640a..6e227271a 100644 --- a/python/src/Drawable.hpp +++ b/python/src/Drawable.hpp @@ -25,13 +25,10 @@ #ifndef __PYDRAWABLE_H #define __PYDRAWABLE_H -#include -#include - #include -#include -#include "Color.hpp" +#include + #include "RenderWindow.hpp" diff --git a/python/src/Event.cpp b/python/src/Event.cpp index 954916901..e6a899c00 100644 --- a/python/src/Event.cpp +++ b/python/src/Event.cpp @@ -24,13 +24,16 @@ #include "Event.hpp" +#include + +#include "compat.hpp" //////////////////////////////// // Text Events Parameters //////////////////////////////// PyMemberDef PySfEventText_members[] = { - {(char *)"Unicode", T_USHORT, offsetof(PySfEventText, Unicode), RO, (char *)""}, + {(char *)"Unicode", T_USHORT, offsetof(PySfEventText, Unicode), READONLY, (char *)""}, {NULL} /* Sentinel */ }; @@ -57,12 +60,11 @@ PySfEventText_init(PySfEventText *self, PyObject *args, PyObject *kwds) void PySfEventText_dealloc(PySfEventText* self) { - self->ob_type->tp_free((PyObject*)self); + free_object(self); } PyTypeObject PySfEventTextType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Event.Text", /*tp_name*/ sizeof(PySfEventText), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -136,20 +138,19 @@ PySfEventKey_init(PySfEventKey *self, PyObject *args, PyObject *kwds) void PySfEventKey_dealloc(PySfEventKey* self) { - self->ob_type->tp_free((PyObject*)self); + free_object(self); } PyMemberDef PySfEventKey_members[] = { - {(char *)"Alt", T_OBJECT, offsetof(PySfEventKey, Alt), RO, (char *)""}, - {(char *)"Control", T_OBJECT, offsetof(PySfEventKey, Control), RO, (char *)""}, - {(char *)"Shift", T_OBJECT, offsetof(PySfEventKey, Shift), RO, (char *)""}, - {(char *)"Code", T_UINT, offsetof(PySfEventKey, Code), RO, (char *)""}, + {(char *)"Alt", T_OBJECT, offsetof(PySfEventKey, Alt), READONLY, (char *)""}, + {(char *)"Control", T_OBJECT, offsetof(PySfEventKey, Control), READONLY, (char *)""}, + {(char *)"Shift", T_OBJECT, offsetof(PySfEventKey, Shift), READONLY, (char *)""}, + {(char *)"Code", T_UINT, offsetof(PySfEventKey, Code), READONLY, (char *)""}, {NULL} /* Sentinel */ }; PyTypeObject PySfEventKeyType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Event.Key", /*tp_name*/ sizeof(PySfEventKey), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -218,19 +219,18 @@ PySfEventMouseMove_init(PySfEventMouseMove *self, PyObject *args, PyObject *kwds void PySfEventMouseMove_dealloc(PySfEventMouseMove *self) { - self->ob_type->tp_free((PyObject*)self); + free_object(self); } PyMemberDef PySfEventMouseMove_members[] = { - {(char *)"X", T_INT, offsetof(PySfEventMouseMove, X), RO, (char *)""}, - {(char *)"Y", T_INT, offsetof(PySfEventMouseMove, Y), RO, (char *)""}, + {(char *)"X", T_INT, offsetof(PySfEventMouseMove, X), READONLY, (char *)""}, + {(char *)"Y", T_INT, offsetof(PySfEventMouseMove, Y), READONLY, (char *)""}, {NULL} /* Sentinel */ }; PyTypeObject PySfEventMouseMoveType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Event.MouseMove", /*tp_name*/ sizeof(PySfEventMouseMove), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -300,20 +300,19 @@ PySfEventMouseButton_init(PySfEventMouseButton *self, PyObject *args, PyObject * void PySfEventMouseButton_dealloc(PySfEventMouseButton* self) { - self->ob_type->tp_free((PyObject*)self); + free_object(self); } PyMemberDef PySfEventMouseButton_members[] = { - {(char *)"Button", T_UINT, offsetof(PySfEventMouseButton, Button), RO, (char *)""}, - {(char *)"X", T_INT, offsetof(PySfEventMouseButton, X), RO, (char *)""}, - {(char *)"Y", T_INT, offsetof(PySfEventMouseButton, Y), RO, (char *)""}, + {(char *)"Button", T_UINT, offsetof(PySfEventMouseButton, Button), READONLY, (char *)""}, + {(char *)"X", T_INT, offsetof(PySfEventMouseButton, X), READONLY, (char *)""}, + {(char *)"Y", T_INT, offsetof(PySfEventMouseButton, Y), READONLY, (char *)""}, {NULL} /* Sentinel */ }; PyTypeObject PySfEventMouseButtonType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Event.MouseButton", /*tp_name*/ sizeof(PySfEventMouseButton), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -381,17 +380,16 @@ PySfEventMouseWheel_init(PySfEventMouseWheel *self, PyObject *args, PyObject *kw void PySfEventMouseWheel_dealloc(PySfEventMouseWheel* self) { - self->ob_type->tp_free((PyObject*)self); + free_object(self); } PyMemberDef PySfEventMouseWheel_members[] = { - {(char *)"Delta", T_INT, offsetof(PySfEventMouseWheel,Delta), RO, (char *)""}, + {(char *)"Delta", T_INT, offsetof(PySfEventMouseWheel,Delta), READONLY, (char *)""}, {NULL} /* Sentinel */ }; PyTypeObject PySfEventMouseWheelType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Event.MouseWheel", /*tp_name*/ sizeof(PySfEventMouseWheel), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -461,20 +459,19 @@ PySfEventJoyMove_init(PySfEventJoyMove *self, PyObject *args, PyObject *kwds) void PySfEventJoyMove_dealloc(PySfEventJoyMove* self) { - self->ob_type->tp_free((PyObject*)self); + free_object(self); } PyMemberDef PySfEventJoyMove_members[] = { - {(char *)"JoystickId", T_UINT, offsetof(PySfEventJoyMove,JoystickId), RO, (char *)""}, - {(char *)"Axis", T_UINT, offsetof(PySfEventJoyMove,Axis), RO, (char *)""}, - {(char *)"Position", T_FLOAT, offsetof(PySfEventJoyMove,Position), RO, (char *)""}, + {(char *)"JoystickId", T_UINT, offsetof(PySfEventJoyMove,JoystickId), READONLY, (char *)""}, + {(char *)"Axis", T_UINT, offsetof(PySfEventJoyMove,Axis), READONLY, (char *)""}, + {(char *)"Position", T_FLOAT, offsetof(PySfEventJoyMove,Position), READONLY, (char *)""}, {NULL} /* Sentinel */ }; PyTypeObject PySfEventJoyMoveType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Event.JoyMove", /*tp_name*/ sizeof(PySfEventJoyMove), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -543,19 +540,18 @@ PySfEventJoyButton_init(PySfEventJoyButton *self, PyObject *args, PyObject *kwds void PySfEventJoyButton_dealloc(PySfEventJoyButton* self) { - self->ob_type->tp_free((PyObject*)self); + free_object(self); } PyMemberDef PySfEventJoyButton_members[] = { - {(char *)"JoystickId", T_UINT, offsetof(PySfEventJoyButton, JoystickId), RO, (char *)""}, - {(char *)"Button", T_UINT, offsetof(PySfEventJoyButton, Button), RO, (char *)""}, + {(char *)"JoystickId", T_UINT, offsetof(PySfEventJoyButton, JoystickId), READONLY, (char *)""}, + {(char *)"Button", T_UINT, offsetof(PySfEventJoyButton, Button), READONLY, (char *)""}, {NULL} /* Sentinel */ }; PyTypeObject PySfEventJoyButtonType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Event.JoyButton", /*tp_name*/ sizeof(PySfEventJoyButton), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -624,18 +620,17 @@ PySfEventSize_init(PySfEventSize *self, PyObject *args, PyObject *kwds) void PySfEventSize_dealloc(PySfEventSize* self) { - self->ob_type->tp_free((PyObject*)self); + free_object(self); } PyMemberDef PySfEventSize_members[] = { - {(char *)"Width", T_UINT, offsetof(PySfEventSize, Width), RO, (char *)""}, - {(char *)"Height", T_UINT, offsetof(PySfEventSize, Height), RO, (char *)""}, + {(char *)"Width", T_UINT, offsetof(PySfEventSize, Width), READONLY, (char *)""}, + {(char *)"Height", T_UINT, offsetof(PySfEventSize, Height), READONLY, (char *)""}, {NULL} /* Sentinel */ }; PyTypeObject PySfEventSizeType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Event.Size", /*tp_name*/ sizeof(PySfEventSize), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -725,19 +720,19 @@ PySfEvent_dealloc(PySfEvent* self) Py_DECREF(self->JoyButton); Py_DECREF(self->Size); delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyMemberDef PySfEvent_members[] = { - {(char *)"Text", T_OBJECT, offsetof(PySfEvent, Text), RO, (char *)"Text Events Parameters"}, - {(char *)"Key", T_OBJECT, offsetof(PySfEvent, Key), RO, (char *)"Keyboard Events Parameters"}, - {(char *)"MouseMove", T_OBJECT, offsetof(PySfEvent, MouseMove), RO, (char *)"MouseMove Events Parameters"}, - {(char *)"MouseButton", T_OBJECT, offsetof(PySfEvent, MouseButton), RO, (char *)"MouseButton Events Parameters"}, - {(char *)"MouseWheel", T_OBJECT, offsetof(PySfEvent, MouseWheel), RO, (char *)"MouseWheel Events Parameters"}, - {(char *)"JoyMove", T_OBJECT, offsetof(PySfEvent, JoyMove), RO, (char *)"JoyMove Events Parameters"}, - {(char *)"JoyButton", T_OBJECT, offsetof(PySfEvent, JoyButton), RO, (char *)"JoyButton Events Parameters"}, - {(char *)"Size", T_OBJECT, offsetof(PySfEvent, Size), RO, (char *)"Size Events Parameters"}, - {(char *)"Type", T_UINT, offsetof(PySfEvent, Type), RO, (char *)"Type Events Parameters"}, + {(char *)"Text", T_OBJECT, offsetof(PySfEvent, Text), READONLY, (char *)"Text Events Parameters"}, + {(char *)"Key", T_OBJECT, offsetof(PySfEvent, Key), READONLY, (char *)"Keyboard Events Parameters"}, + {(char *)"MouseMove", T_OBJECT, offsetof(PySfEvent, MouseMove), READONLY, (char *)"MouseMove Events Parameters"}, + {(char *)"MouseButton", T_OBJECT, offsetof(PySfEvent, MouseButton), READONLY, (char *)"MouseButton Events Parameters"}, + {(char *)"MouseWheel", T_OBJECT, offsetof(PySfEvent, MouseWheel), READONLY, (char *)"MouseWheel Events Parameters"}, + {(char *)"JoyMove", T_OBJECT, offsetof(PySfEvent, JoyMove), READONLY, (char *)"JoyMove Events Parameters"}, + {(char *)"JoyButton", T_OBJECT, offsetof(PySfEvent, JoyButton), READONLY, (char *)"JoyButton Events Parameters"}, + {(char *)"Size", T_OBJECT, offsetof(PySfEvent, Size), READONLY, (char *)"Size Events Parameters"}, + {(char *)"Type", T_UINT, offsetof(PySfEvent, Type), READONLY, (char *)"Type Events Parameters"}, {NULL} /* Sentinel */ }; @@ -746,8 +741,7 @@ static PyMethodDef PySfEvent_methods[] = { }; PyTypeObject PySfEventType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Event", /*tp_name*/ sizeof(PySfEvent), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -792,52 +786,52 @@ void PySfEvent_InitConst() { PyObject *obj; - obj = PyInt_FromLong(sf::Event::KeyReleased); + obj = PyLong_FromLong(sf::Event::KeyReleased); PyDict_SetItemString(PySfEventType.tp_dict, "KeyReleased", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::LostFocus); + obj = PyLong_FromLong(sf::Event::LostFocus); PyDict_SetItemString(PySfEventType.tp_dict, "LostFocus", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::GainedFocus); + obj = PyLong_FromLong(sf::Event::GainedFocus); PyDict_SetItemString(PySfEventType.tp_dict, "GainedFocus", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::KeyPressed); + obj = PyLong_FromLong(sf::Event::KeyPressed); PyDict_SetItemString(PySfEventType.tp_dict, "KeyPressed", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::MouseWheelMoved); + obj = PyLong_FromLong(sf::Event::MouseWheelMoved); PyDict_SetItemString(PySfEventType.tp_dict, "MouseWheelMoved", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::TextEntered); + obj = PyLong_FromLong(sf::Event::TextEntered); PyDict_SetItemString(PySfEventType.tp_dict, "TextEntered", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::MouseMoved); + obj = PyLong_FromLong(sf::Event::MouseMoved); PyDict_SetItemString(PySfEventType.tp_dict, "MouseMoved", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::JoyButtonPressed); + obj = PyLong_FromLong(sf::Event::JoyButtonPressed); PyDict_SetItemString(PySfEventType.tp_dict, "JoyButtonPressed", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::MouseButtonReleased); + obj = PyLong_FromLong(sf::Event::MouseButtonReleased); PyDict_SetItemString(PySfEventType.tp_dict, "MouseButtonReleased", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::Closed); + obj = PyLong_FromLong(sf::Event::Closed); PyDict_SetItemString(PySfEventType.tp_dict, "Closed", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::MouseButtonPressed); + obj = PyLong_FromLong(sf::Event::MouseButtonPressed); PyDict_SetItemString(PySfEventType.tp_dict, "MouseButtonPressed", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::JoyMoved); + obj = PyLong_FromLong(sf::Event::JoyMoved); PyDict_SetItemString(PySfEventType.tp_dict, "JoyMoved", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::JoyButtonReleased); + obj = PyLong_FromLong(sf::Event::JoyButtonReleased); PyDict_SetItemString(PySfEventType.tp_dict, "JoyButtonReleased", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::Resized); + obj = PyLong_FromLong(sf::Event::Resized); PyDict_SetItemString(PySfEventType.tp_dict, "Resized", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::MouseEntered); + obj = PyLong_FromLong(sf::Event::MouseEntered); PyDict_SetItemString(PySfEventType.tp_dict, "MouseEntered", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Event::MouseLeft); + obj = PyLong_FromLong(sf::Event::MouseLeft); PyDict_SetItemString(PySfEventType.tp_dict, "MouseLeft", obj); Py_DECREF(obj); } diff --git a/python/src/Event.hpp b/python/src/Event.hpp index c0f4ad896..fa8f8fa4a 100644 --- a/python/src/Event.hpp +++ b/python/src/Event.hpp @@ -25,12 +25,9 @@ #ifndef __PYEVENT_HPP #define __PYEVENT_HPP -#include -#include -#include - #include -#include + +#include typedef struct { diff --git a/python/src/Image.cpp b/python/src/Image.cpp index e3ac4d8b7..9f8abe689 100644 --- a/python/src/Image.cpp +++ b/python/src/Image.cpp @@ -24,34 +24,27 @@ #include "Image.hpp" #include "RenderWindow.hpp" +#include "Color.hpp" +#include "Rect.hpp" + +#include "compat.hpp" extern PyTypeObject PySfColorType; extern PyTypeObject PySfIntRectType; extern PyTypeObject PySfRenderWindowType; -static PyMemberDef PySfImage_members[] = { - {NULL} /* Sentinel */ -}; - - static void PySfImage_dealloc(PySfImage* self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfImage_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfImage *self; - self = (PySfImage *)type->tp_alloc(type, 0); - - if (self != NULL) - { - } - return (PyObject *)self; } @@ -64,7 +57,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!", (char **)kwlist, &Width, &Height, &PySfColorType, &ColorTmp)) + if (! PyArg_ParseTupleAndKeywords(args, kwds, "|IIO!:Image.Create", (char **)kwlist, &Width, &Height, &PySfColorType, &ColorTmp)) return NULL; if (ColorTmp) @@ -82,12 +75,11 @@ PySfImage_Create(PySfImage* self, PyObject *args, PyObject *kwds) static PyObject * PySfImage_CopyScreen(PySfImage* self, PyObject *args) { -// bool CopyScreen(RenderWindow& Window, const IntRect& SourceRect = IntRect(0, 0, 0, 0)); PySfRenderWindow *RenderWindow; PySfIntRect *SourceRect=NULL; bool Result; - if (! PyArg_ParseTuple(args, "O!|O!", &PySfRenderWindowType, &RenderWindow, &PySfIntRectType, &SourceRect)) + if (! PyArg_ParseTuple(args, "O!|O!:Image.CopyScreen", &PySfRenderWindowType, &RenderWindow, &PySfIntRectType, &SourceRect)) return NULL; @@ -113,7 +105,7 @@ PySfImage_SetPixel(PySfImage* self, PyObject *args, PyObject *kwds) const char *kwlist[] = {"x", "y", "Color", NULL}; - if (! PyArg_ParseTupleAndKeywords(args, kwds, "II|O!", (char **)kwlist, &x, &y, &PySfColorType, &ColorTmp)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "II|O!:Image.SetPixel", (char **)kwlist, &x, &y, &PySfColorType, &ColorTmp)) return NULL; @@ -134,7 +126,7 @@ PySfImage_GetPixel(PySfImage* self, PyObject *args) unsigned int x=0, y=0; - if (! PyArg_ParseTuple(args, "II", &x, &y)) + if (!PyArg_ParseTuple(args, "II:Image.GetPixel", &x, &y)) return NULL; @@ -154,8 +146,11 @@ PySfImage_CreateMaskFromColor(PySfImage* self, PyObject *args) PySfColor *ColorTmp= (PySfColor *)args; sf::Color *Color; - if ( ! PyObject_TypeCheck(ColorTmp, &PySfColorType)) - PyErr_SetString(PyExc_ValueError, "Argument must be a sf.Color"); + if (!PyObject_TypeCheck(ColorTmp, &PySfColorType)) + { + PyErr_SetString(PyExc_TypeError, "Image.CreateMaskFromColor() Argument must be a sf.Color"); + return NULL; + } Color = ColorTmp->obj; PySfColorUpdate(ColorTmp); self->obj->CreateMaskFromColor(*Color); @@ -169,14 +164,10 @@ PySfImage_LoadFromMemory(PySfImage* self, PyObject *args) unsigned int SizeInBytes; char *Data; - if (! PyArg_ParseTuple(args, "s#", &Data, &SizeInBytes)) + if (! PyArg_ParseTuple(args, "s#:Image.LoadFromMemory", &Data, &SizeInBytes)) return NULL; - if (self->obj->LoadFromMemory(Data, (std::size_t) SizeInBytes)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; - + return PyBool_FromLong(self->obj->LoadFromMemory(Data, (std::size_t) SizeInBytes)); } static PyObject * @@ -185,7 +176,7 @@ PySfImage_LoadFromPixels(PySfImage* self, PyObject *args) unsigned int Width, Height, Size; char *Data; - if (! PyArg_ParseTuple(args, "IIs#", &Width, &Height, &Data, &Size)) + if (! PyArg_ParseTuple(args, "IIs#:Image.LoadFromPixels", &Width, &Height, &Data, &Size)) return NULL; self->obj->LoadFromPixels(Width, Height, (sf::Uint8*) Data); @@ -195,29 +186,49 @@ PySfImage_LoadFromPixels(PySfImage* self, PyObject *args) static PyObject * PySfImage_GetPixels(PySfImage *self) { +#ifdef IS_PY3K + return PyBytes_FromStringAndSize((const char *)(self->obj->GetPixelsPtr()), self->obj->GetWidth()*self->obj->GetHeight()*4); +#else return PyString_FromStringAndSize((const char *)(self->obj->GetPixelsPtr()), self->obj->GetWidth()*self->obj->GetHeight()*4); +#endif } static PyObject * PySfImage_LoadFromFile (PySfImage *self, PyObject *args) { - char *path = PyString_AsString(args); - - if (self->obj->LoadFromFile(path)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + char *path; +#ifdef IS_PY3K + PyObject *string = PyUnicode_AsUTF8String(args); + if (string == NULL) + return NULL; + path = PyBytes_AsString(string); +#else + path = PyString_AsString(args); +#endif + bool result = self->obj->LoadFromFile(path); +#ifdef IS_PY3K + Py_DECREF(string); +#endif + return PyBool_FromLong(result); } static PyObject * PySfImage_SaveToFile (PySfImage *self, PyObject *args) { - char *path = PyString_AsString(args); - - if (self->obj->SaveToFile(path)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + char *path; +#ifdef IS_PY3K + PyObject *string = PyUnicode_AsUTF8String(args); + if (string == NULL) + return NULL; + path = PyBytes_AsString(string); +#else + path = PyString_AsString(args); +#endif + bool result = self->obj->SaveToFile(path); +#ifdef IS_PY3K + Py_DECREF(string); +#endif + return PyBool_FromLong(result); } static int @@ -233,10 +244,7 @@ PySfImage_Bind(PySfImage *self) static PyObject * PySfImage_SetSmooth (PySfImage *self, PyObject *args) { - bool arg=false; - if (PyObject_IsTrue(args)) - arg = true; - self->obj->SetSmooth(arg); + self->obj->SetSmooth(PyBool_AsBool(args)); Py_RETURN_NONE; } @@ -269,8 +277,9 @@ PySfImage_GetTexCoords(PySfImage* self, PyObject *args) if (! PyArg_ParseTuple(args, "O!|O", &PySfIntRectType, &RectArg, &AdjustObj)) return NULL; - if (PyObject_IsTrue(AdjustObj)) - Adjust = true; + if (AdjustObj) + if (PyObject_IsTrue(AdjustObj)) + Adjust = true; PySfFloatRect *Rect; @@ -326,8 +335,7 @@ Create the image from the current contents of the given window. Return True if c }; PyTypeObject PySfImageType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Image", /*tp_name*/ sizeof(PySfImage), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -358,7 +366,7 @@ Copy constructor : sf.Image(Copy) where Copy is a sf.Image instance.", /* tp_doc 0, /* tp_iter */ 0, /* tp_iternext */ PySfImage_methods, /* tp_methods */ - PySfImage_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ @@ -373,7 +381,8 @@ Copy constructor : sf.Image(Copy) where Copy is a sf.Image instance.", /* tp_doc static int PySfImage_init(PySfImage *self, PyObject *args, PyObject *kwds) { - if (PyTuple_Size(args) == 1) + int size = PyTuple_Size(args); + if (size == 1) { PySfImage *Image; if (PyArg_ParseTuple(args, "O!", &PySfImageType, &Image)) @@ -386,8 +395,12 @@ PySfImage_init(PySfImage *self, PyObject *args, PyObject *kwds) if (PyTuple_Size(args) > 0) { if (PySfImage_Create(self, args, kwds) == NULL) - if (PySfImage_LoadFromPixels(self, args) == NULL) + { + if (size != 3) return -1; + else if (PySfImage_LoadFromPixels(self, args) == NULL) + return -1; + } } return 0; } @@ -400,11 +413,12 @@ PySfImage_Copy(PySfImage* self, PyObject *args) unsigned int DestX, DestY; PyObject *PyApplyAlpha; bool ApplyAlpha = false; - if (! PyArg_ParseTuple(args, "O!II|O!O", &PySfImageType, &Source, &DestX, &DestY, &PySfIntRectType, &SourceRect, &PyApplyAlpha)) + if (! PyArg_ParseTuple(args, "O!II|O!O:Image.Copy", &PySfImageType, &Source, &DestX, &DestY, &PySfIntRectType, &SourceRect, &PyApplyAlpha)) return NULL; - if (PyObject_IsTrue(PyApplyAlpha)) - ApplyAlpha = true; + if (PyApplyAlpha) + if (PyObject_IsTrue(PyApplyAlpha)) + ApplyAlpha = true; if (SourceRect) { diff --git a/python/src/Image.hpp b/python/src/Image.hpp index 84713e0ef..ff195c7ae 100644 --- a/python/src/Image.hpp +++ b/python/src/Image.hpp @@ -25,16 +25,9 @@ #ifndef __PYIMAGE_HPP #define __PYIMAGE_HPP -#include -#include - #include -#include -#include "Color.hpp" -#include "Rect.hpp" - -#include "offsetof.hpp" +#include typedef struct { diff --git a/python/src/Input.cpp b/python/src/Input.cpp index 7116731e4..c146f69f1 100644 --- a/python/src/Input.cpp +++ b/python/src/Input.cpp @@ -24,85 +24,61 @@ #include "Input.hpp" +#include "compat.hpp" -static PyMemberDef PySfInput_members[] = { - {NULL} /* Sentinel */ -}; - - -static void -PySfInput_dealloc(PySfInput *self) -{ - self->ob_type->tp_free((PyObject*)self); -} static PyObject * PySfInput_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfInput *self; - self = (PySfInput *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } - static int PySfInput_init(PySfInput *self, PyObject *args, PyObject *kwds) { - PyErr_SetString(PyExc_StandardError, "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()."); + 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; } static PyObject* PySfInput_IsKeyDown(PySfInput *self, PyObject *args) { - if (self->obj->IsKeyDown( (sf::Key::Code) PyInt_AsLong(args) )) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(self->obj->IsKeyDown( (sf::Key::Code) PyLong_AsLong(args) )); } static PyObject* PySfInput_IsMouseButtonDown(PySfInput *self, PyObject *args) { - if (self->obj->IsMouseButtonDown( (sf::Mouse::Button) PyInt_AsLong(args) )) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(self->obj->IsMouseButtonDown( (sf::Mouse::Button) PyLong_AsLong(args) )); } static PyObject* PySfInput_IsJoystickButtonDown(PySfInput *self, PyObject *args) { unsigned int JoyId, Button; - if (! PyArg_ParseTuple (args, "II", &JoyId, &Button)) + if (! PyArg_ParseTuple (args, "II:Input.IsJoystickButtonDown", &JoyId, &Button)) return NULL; - if (self->obj->IsJoystickButtonDown(JoyId, Button)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(self->obj->IsJoystickButtonDown(JoyId, Button)); } static PyObject* PySfInput_GetMouseX(PySfInput *self) { - return PyInt_FromLong(self->obj->GetMouseX()); + return PyLong_FromLong(self->obj->GetMouseX()); } static PyObject* PySfInput_GetMouseY(PySfInput *self) { - return PyInt_FromLong(self->obj->GetMouseY()); + return PyLong_FromLong(self->obj->GetMouseY()); } static PyObject* PySfInput_GetJoystickAxis(PySfInput *self, PyObject *args) { unsigned int JoyId, Axis; - if (! PyArg_ParseTuple (args, "II", &JoyId, &Axis)) + if (! PyArg_ParseTuple (args, "II:Input.GetJoystickAxis", &JoyId, &Axis)) return NULL; return PyFloat_FromDouble(self->obj->GetJoystickAxis(JoyId, (sf::Joy::Axis) Axis)); } @@ -118,12 +94,11 @@ static PyMethodDef PySfInput_methods[] = { }; PyTypeObject PySfInputType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Input", /*tp_name*/ sizeof(PySfInput), /*tp_basicsize*/ 0, /*tp_itemsize*/ - (destructor)PySfInput_dealloc, /*tp_dealloc*/ + 0, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -147,7 +122,7 @@ PyTypeObject PySfInputType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfInput_methods, /* tp_methods */ - PySfInput_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/Input.hpp b/python/src/Input.hpp index ad5209e0e..73df36523 100644 --- a/python/src/Input.hpp +++ b/python/src/Input.hpp @@ -25,12 +25,9 @@ #ifndef __PYINPUT_HPP #define __PYINPUT_HPP -#include -#include -#include - #include -#include + +#include typedef struct { PyObject_HEAD diff --git a/python/src/Key.cpp b/python/src/Key.cpp index f209c376e..9b807cfa6 100644 --- a/python/src/Key.cpp +++ b/python/src/Key.cpp @@ -22,62 +22,24 @@ // //////////////////////////////////////////////////////////// -#include - -#include -#include - #include "Key.hpp" - -typedef struct { - PyObject_HEAD -} PySfKey; - - - -static PyMemberDef PySfKey_members[] = { - {NULL} /* Sentinel */ -}; - - -static void -PySfKey_dealloc(PySfKey *self) -{ - self->ob_type->tp_free((PyObject*)self); -} +#include "compat.hpp" static PyObject * PySfKey_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfKey *self; - self = (PySfKey *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } - -static int -PySfKey_init(PySfKey *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - -static PyMethodDef PySfKey_methods[] = { - {NULL} /* Sentinel */ -}; - PyTypeObject PySfKeyType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Key", /*tp_name*/ sizeof(PySfKey), /*tp_basicsize*/ 0, /*tp_itemsize*/ - (destructor)PySfKey_dealloc, /*tp_dealloc*/ + 0, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -100,15 +62,15 @@ PyTypeObject PySfKeyType = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - PySfKey_methods, /* tp_methods */ - PySfKey_members, /* tp_members */ + 0, /* tp_methods */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - (initproc)PySfKey_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfKey_new, /* tp_new */ }; @@ -116,307 +78,307 @@ PyTypeObject PySfKeyType = { void PySfKey_InitConst() { PyObject *obj; - obj = PyInt_FromLong(sf::Key::Numpad2); + obj = PyLong_FromLong(sf::Key::Numpad2); PyDict_SetItemString(PySfKeyType.tp_dict, "Numpad2", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Numpad3); + obj = PyLong_FromLong(sf::Key::Numpad3); PyDict_SetItemString(PySfKeyType.tp_dict, "Numpad3", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Numpad0); + obj = PyLong_FromLong(sf::Key::Numpad0); PyDict_SetItemString(PySfKeyType.tp_dict, "Numpad0", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Numpad1); + obj = PyLong_FromLong(sf::Key::Numpad1); PyDict_SetItemString(PySfKeyType.tp_dict, "Numpad1", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Numpad6); + obj = PyLong_FromLong(sf::Key::Numpad6); PyDict_SetItemString(PySfKeyType.tp_dict, "Numpad6", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Numpad7); + obj = PyLong_FromLong(sf::Key::Numpad7); PyDict_SetItemString(PySfKeyType.tp_dict, "Numpad7", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Numpad4); + obj = PyLong_FromLong(sf::Key::Numpad4); PyDict_SetItemString(PySfKeyType.tp_dict, "Numpad4", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Numpad5); + obj = PyLong_FromLong(sf::Key::Numpad5); PyDict_SetItemString(PySfKeyType.tp_dict, "Numpad5", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Numpad8); + obj = PyLong_FromLong(sf::Key::Numpad8); PyDict_SetItemString(PySfKeyType.tp_dict, "Numpad8", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Numpad9); + obj = PyLong_FromLong(sf::Key::Numpad9); PyDict_SetItemString(PySfKeyType.tp_dict, "Numpad9", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::RAlt); + obj = PyLong_FromLong(sf::Key::RAlt); PyDict_SetItemString(PySfKeyType.tp_dict, "RAlt", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::PageUp); + obj = PyLong_FromLong(sf::Key::PageUp); PyDict_SetItemString(PySfKeyType.tp_dict, "PageUp", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Multiply); + obj = PyLong_FromLong(sf::Key::Multiply); PyDict_SetItemString(PySfKeyType.tp_dict, "Multiply", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::D); + obj = PyLong_FromLong(sf::Key::D); PyDict_SetItemString(PySfKeyType.tp_dict, "D", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::SemiColon); + obj = PyLong_FromLong(sf::Key::SemiColon); PyDict_SetItemString(PySfKeyType.tp_dict, "SemiColon", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::H); + obj = PyLong_FromLong(sf::Key::H); PyDict_SetItemString(PySfKeyType.tp_dict, "H", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::L); + obj = PyLong_FromLong(sf::Key::L); PyDict_SetItemString(PySfKeyType.tp_dict, "L", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::P); + obj = PyLong_FromLong(sf::Key::P); PyDict_SetItemString(PySfKeyType.tp_dict, "P", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Num7); + obj = PyLong_FromLong(sf::Key::Num7); PyDict_SetItemString(PySfKeyType.tp_dict, "Num7", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::T); + obj = PyLong_FromLong(sf::Key::T); PyDict_SetItemString(PySfKeyType.tp_dict, "T", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::X); + obj = PyLong_FromLong(sf::Key::X); PyDict_SetItemString(PySfKeyType.tp_dict, "X", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::RSystem); + obj = PyLong_FromLong(sf::Key::RSystem); PyDict_SetItemString(PySfKeyType.tp_dict, "RSystem", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F5); + obj = PyLong_FromLong(sf::Key::F5); PyDict_SetItemString(PySfKeyType.tp_dict, "F5", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Num4); + obj = PyLong_FromLong(sf::Key::Num4); PyDict_SetItemString(PySfKeyType.tp_dict, "Num4", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Num5); + obj = PyLong_FromLong(sf::Key::Num5); PyDict_SetItemString(PySfKeyType.tp_dict, "Num5", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Num6); + obj = PyLong_FromLong(sf::Key::Num6); PyDict_SetItemString(PySfKeyType.tp_dict, "Num6", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Right); + obj = PyLong_FromLong(sf::Key::Right); PyDict_SetItemString(PySfKeyType.tp_dict, "Right", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Num0); + obj = PyLong_FromLong(sf::Key::Num0); PyDict_SetItemString(PySfKeyType.tp_dict, "Num0", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Num1); + obj = PyLong_FromLong(sf::Key::Num1); PyDict_SetItemString(PySfKeyType.tp_dict, "Num1", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Num2); + obj = PyLong_FromLong(sf::Key::Num2); PyDict_SetItemString(PySfKeyType.tp_dict, "Num2", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Num3); + obj = PyLong_FromLong(sf::Key::Num3); PyDict_SetItemString(PySfKeyType.tp_dict, "Num3", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::LControl); + obj = PyLong_FromLong(sf::Key::LControl); PyDict_SetItemString(PySfKeyType.tp_dict, "LControl", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Num8); + obj = PyLong_FromLong(sf::Key::Num8); PyDict_SetItemString(PySfKeyType.tp_dict, "Num8", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Num9); + obj = PyLong_FromLong(sf::Key::Num9); PyDict_SetItemString(PySfKeyType.tp_dict, "Num9", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Tab); + obj = PyLong_FromLong(sf::Key::Tab); PyDict_SetItemString(PySfKeyType.tp_dict, "Tab", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::RBracket); + obj = PyLong_FromLong(sf::Key::RBracket); PyDict_SetItemString(PySfKeyType.tp_dict, "RBracket", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::End); + obj = PyLong_FromLong(sf::Key::End); PyDict_SetItemString(PySfKeyType.tp_dict, "End", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::BackSlash); + obj = PyLong_FromLong(sf::Key::BackSlash); PyDict_SetItemString(PySfKeyType.tp_dict, "BackSlash", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::LShift); + obj = PyLong_FromLong(sf::Key::LShift); PyDict_SetItemString(PySfKeyType.tp_dict, "LShift", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::E); + obj = PyLong_FromLong(sf::Key::E); PyDict_SetItemString(PySfKeyType.tp_dict, "E", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::C); + obj = PyLong_FromLong(sf::Key::C); PyDict_SetItemString(PySfKeyType.tp_dict, "C", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::G); + obj = PyLong_FromLong(sf::Key::G); PyDict_SetItemString(PySfKeyType.tp_dict, "G", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::K); + obj = PyLong_FromLong(sf::Key::K); PyDict_SetItemString(PySfKeyType.tp_dict, "K", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Up); + obj = PyLong_FromLong(sf::Key::Up); PyDict_SetItemString(PySfKeyType.tp_dict, "Up", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::O); + obj = PyLong_FromLong(sf::Key::O); PyDict_SetItemString(PySfKeyType.tp_dict, "O", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::S); + obj = PyLong_FromLong(sf::Key::S); PyDict_SetItemString(PySfKeyType.tp_dict, "S", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::W); + obj = PyLong_FromLong(sf::Key::W); PyDict_SetItemString(PySfKeyType.tp_dict, "W", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F12); + obj = PyLong_FromLong(sf::Key::F12); PyDict_SetItemString(PySfKeyType.tp_dict, "F12", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F13); + obj = PyLong_FromLong(sf::Key::F13); PyDict_SetItemString(PySfKeyType.tp_dict, "F13", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F10); + obj = PyLong_FromLong(sf::Key::F10); PyDict_SetItemString(PySfKeyType.tp_dict, "F10", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F11); + obj = PyLong_FromLong(sf::Key::F11); PyDict_SetItemString(PySfKeyType.tp_dict, "F11", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F14); + obj = PyLong_FromLong(sf::Key::F14); PyDict_SetItemString(PySfKeyType.tp_dict, "F14", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Delete); + obj = PyLong_FromLong(sf::Key::Delete); PyDict_SetItemString(PySfKeyType.tp_dict, "Delete", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Back); + obj = PyLong_FromLong(sf::Key::Back); PyDict_SetItemString(PySfKeyType.tp_dict, "Back", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Tilde); + obj = PyLong_FromLong(sf::Key::Tilde); PyDict_SetItemString(PySfKeyType.tp_dict, "Tilde", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Home); + obj = PyLong_FromLong(sf::Key::Home); PyDict_SetItemString(PySfKeyType.tp_dict, "Home", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Pause); + obj = PyLong_FromLong(sf::Key::Pause); PyDict_SetItemString(PySfKeyType.tp_dict, "Pause", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Add); + obj = PyLong_FromLong(sf::Key::Add); PyDict_SetItemString(PySfKeyType.tp_dict, "Add", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F15); + obj = PyLong_FromLong(sf::Key::F15); PyDict_SetItemString(PySfKeyType.tp_dict, "F15", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Subtract); + obj = PyLong_FromLong(sf::Key::Subtract); PyDict_SetItemString(PySfKeyType.tp_dict, "Subtract", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::B); + obj = PyLong_FromLong(sf::Key::B); PyDict_SetItemString(PySfKeyType.tp_dict, "B", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F); + obj = PyLong_FromLong(sf::Key::F); PyDict_SetItemString(PySfKeyType.tp_dict, "F", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::J); + obj = PyLong_FromLong(sf::Key::J); PyDict_SetItemString(PySfKeyType.tp_dict, "J", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::N); + obj = PyLong_FromLong(sf::Key::N); PyDict_SetItemString(PySfKeyType.tp_dict, "N", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::LBracket); + obj = PyLong_FromLong(sf::Key::LBracket); PyDict_SetItemString(PySfKeyType.tp_dict, "LBracket", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::R); + obj = PyLong_FromLong(sf::Key::R); PyDict_SetItemString(PySfKeyType.tp_dict, "R", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::V); + obj = PyLong_FromLong(sf::Key::V); PyDict_SetItemString(PySfKeyType.tp_dict, "V", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::LSystem); + obj = PyLong_FromLong(sf::Key::LSystem); PyDict_SetItemString(PySfKeyType.tp_dict, "LSystem", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Z); + obj = PyLong_FromLong(sf::Key::Z); PyDict_SetItemString(PySfKeyType.tp_dict, "Z", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Left); + obj = PyLong_FromLong(sf::Key::Left); PyDict_SetItemString(PySfKeyType.tp_dict, "Left", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F1); + obj = PyLong_FromLong(sf::Key::F1); PyDict_SetItemString(PySfKeyType.tp_dict, "F1", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F2); + obj = PyLong_FromLong(sf::Key::F2); PyDict_SetItemString(PySfKeyType.tp_dict, "F2", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F3); + obj = PyLong_FromLong(sf::Key::F3); PyDict_SetItemString(PySfKeyType.tp_dict, "F3", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F4); + obj = PyLong_FromLong(sf::Key::F4); PyDict_SetItemString(PySfKeyType.tp_dict, "F4", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Divide); + obj = PyLong_FromLong(sf::Key::Divide); PyDict_SetItemString(PySfKeyType.tp_dict, "Divide", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F6); + obj = PyLong_FromLong(sf::Key::F6); PyDict_SetItemString(PySfKeyType.tp_dict, "F6", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F7); + obj = PyLong_FromLong(sf::Key::F7); PyDict_SetItemString(PySfKeyType.tp_dict, "F7", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F8); + obj = PyLong_FromLong(sf::Key::F8); PyDict_SetItemString(PySfKeyType.tp_dict, "F8", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::F9); + obj = PyLong_FromLong(sf::Key::F9); PyDict_SetItemString(PySfKeyType.tp_dict, "F9", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Period); + obj = PyLong_FromLong(sf::Key::Period); PyDict_SetItemString(PySfKeyType.tp_dict, "Period", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Down); + obj = PyLong_FromLong(sf::Key::Down); PyDict_SetItemString(PySfKeyType.tp_dict, "Down", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::PageDown); + obj = PyLong_FromLong(sf::Key::PageDown); PyDict_SetItemString(PySfKeyType.tp_dict, "PageDown", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Space); + obj = PyLong_FromLong(sf::Key::Space); PyDict_SetItemString(PySfKeyType.tp_dict, "Space", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Menu); + obj = PyLong_FromLong(sf::Key::Menu); PyDict_SetItemString(PySfKeyType.tp_dict, "Menu", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::RControl); + obj = PyLong_FromLong(sf::Key::RControl); PyDict_SetItemString(PySfKeyType.tp_dict, "RControl", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Slash); + obj = PyLong_FromLong(sf::Key::Slash); PyDict_SetItemString(PySfKeyType.tp_dict, "Slash", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Return); + obj = PyLong_FromLong(sf::Key::Return); PyDict_SetItemString(PySfKeyType.tp_dict, "Return", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Quote); + obj = PyLong_FromLong(sf::Key::Quote); PyDict_SetItemString(PySfKeyType.tp_dict, "Quote", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::A); + obj = PyLong_FromLong(sf::Key::A); PyDict_SetItemString(PySfKeyType.tp_dict, "A", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Insert); + obj = PyLong_FromLong(sf::Key::Insert); PyDict_SetItemString(PySfKeyType.tp_dict, "Insert", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::RShift); + obj = PyLong_FromLong(sf::Key::RShift); PyDict_SetItemString(PySfKeyType.tp_dict, "RShift", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::I); + obj = PyLong_FromLong(sf::Key::I); PyDict_SetItemString(PySfKeyType.tp_dict, "I", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Escape); + obj = PyLong_FromLong(sf::Key::Escape); PyDict_SetItemString(PySfKeyType.tp_dict, "Escape", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::M); + obj = PyLong_FromLong(sf::Key::M); PyDict_SetItemString(PySfKeyType.tp_dict, "M", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Equal); + obj = PyLong_FromLong(sf::Key::Equal); PyDict_SetItemString(PySfKeyType.tp_dict, "Equal", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Q); + obj = PyLong_FromLong(sf::Key::Q); PyDict_SetItemString(PySfKeyType.tp_dict, "Q", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::U); + obj = PyLong_FromLong(sf::Key::U); PyDict_SetItemString(PySfKeyType.tp_dict, "U", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Y); + obj = PyLong_FromLong(sf::Key::Y); PyDict_SetItemString(PySfKeyType.tp_dict, "Y", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Dash); + obj = PyLong_FromLong(sf::Key::Dash); PyDict_SetItemString(PySfKeyType.tp_dict, "Dash", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::Comma); + obj = PyLong_FromLong(sf::Key::Comma); PyDict_SetItemString(PySfKeyType.tp_dict, "Comma", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Key::LAlt); + obj = PyLong_FromLong(sf::Key::LAlt); PyDict_SetItemString(PySfKeyType.tp_dict, "LAlt", obj); Py_DECREF(obj); } diff --git a/python/src/Key.hpp b/python/src/Key.hpp index 2126ee279..1af17a218 100644 --- a/python/src/Key.hpp +++ b/python/src/Key.hpp @@ -25,6 +25,14 @@ #ifndef __PYKEY_HPP #define __PYKEY_HPP +#include + +#include + +typedef struct { + PyObject_HEAD +} PySfKey; + void PySfKey_InitConst(); diff --git a/python/src/Music.cpp b/python/src/Music.cpp index 9801262ea..ed54bf5f3 100644 --- a/python/src/Music.cpp +++ b/python/src/Music.cpp @@ -24,32 +24,24 @@ #include "Music.hpp" +#include "compat.hpp" + extern PyTypeObject PySfSoundStreamType; -static PyMemberDef PySfMusic_members[] = { - {NULL} /* Sentinel */ -}; - - static void PySfMusic_dealloc(PySfMusic *self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfMusic_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfMusic *self; - self = (PySfMusic *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } @@ -58,11 +50,16 @@ static int PySfMusic_init(PySfMusic *self, PyObject *args, PyObject *kwds) { unsigned int BufferSize=44100; - if (PyTuple_Size(args) == 1) + int size = PyTuple_Size(args); + if (size == 1) { - if ( !PyArg_ParseTuple(args, "I", &BufferSize)) + 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; } @@ -73,23 +70,29 @@ PySfMusic_OpenFromMemory(PySfMusic *self, PyObject *args) unsigned int SizeInBytes; char *Data; - if (! PyArg_ParseTuple(args, "s#", &Data, &SizeInBytes)) + if (! PyArg_ParseTuple(args, "s#:Music.OpenFromMemory", &Data, &SizeInBytes)) return NULL; - if (self->obj->OpenFromMemory(Data, (std::size_t) SizeInBytes)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(self->obj->OpenFromMemory(Data, (std::size_t) SizeInBytes)); } static PyObject* PySfMusic_OpenFromFile(PySfMusic *self, PyObject *args) { - char *path = PyString_AsString(args); - if (self->obj->OpenFromFile(path)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + char *path; +#ifdef IS_PY3K + PyObject *string = PyUnicode_AsUTF8String(args); + if (string == NULL) + return NULL; + path = PyBytes_AsString(string); +#else + path = PyString_AsString(args); +#endif + bool result = self->obj->OpenFromFile(path); +#ifdef IS_PY3K + Py_DECREF(string); +#endif + return PyBool_FromLong(result); } static PyObject* @@ -108,8 +111,7 @@ static PyMethodDef PySfMusic_methods[] = { PyTypeObject PySfMusicType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Music", /*tp_name*/ sizeof(PySfMusic), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -129,7 +131,9 @@ PyTypeObject PySfMusicType = { 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - "sf.Music defines a big sound played using streaming, so usually what we call a music :)", /* tp_doc */ + "sf.Music defines a big sound played using streaming, so usually what we call a music :).\n\ +Constructor: sf.Music(BufferSize=44100)\n\ +BufferSize : Size of the internal buffer, expressed in number of samples (ie. size taken by the music in memory) (44100 by default)", /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -137,7 +141,7 @@ PyTypeObject PySfMusicType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfMusic_methods, /* tp_methods */ - PySfMusic_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ &PySfSoundStreamType, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/Music.hpp b/python/src/Music.hpp index 9d2fba400..a69c47d17 100644 --- a/python/src/Music.hpp +++ b/python/src/Music.hpp @@ -25,13 +25,9 @@ #ifndef __PYMUSIC_HPP #define __PYMUSIC_HPP -#include -#include - #include -#include - +#include typedef struct { PyObject_HEAD diff --git a/python/src/PostFX.cpp b/python/src/PostFX.cpp index fb2f343d7..5962972a7 100644 --- a/python/src/PostFX.cpp +++ b/python/src/PostFX.cpp @@ -22,69 +22,75 @@ // //////////////////////////////////////////////////////////// -#include "PostFX.hpp" +#include "PostFX.hpp" +#include "Drawable.hpp" +#include "Image.hpp" + +#include "compat.hpp" extern PyTypeObject PySfImageType; -extern PyTypeObject PySfDrawableType; - -static PyMemberDef PySfPostFX_members[] = { - {NULL} /* Sentinel */ -}; - - +extern PyTypeObject PySfDrawableType; + static void PySfPostFX_dealloc(PySfPostFX *self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfPostFX_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfPostFX *self; - self = (PySfPostFX *)type->tp_alloc(type, 0); - - if (self != NULL) - { - } - return (PyObject *)self; } static PyObject * PySfPostFX_LoadFromFile (PySfPostFX *self, PyObject *args) -{ - if (self->obj->LoadFromFile(PyString_AsString(args))) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; +{ + char *path; +#ifdef IS_PY3K + PyObject *string = PyUnicode_AsUTF8String(args); + if (string == NULL) + return NULL; + path = PyBytes_AsString(string); +#else + path = PyString_AsString(args); +#endif + bool result = self->obj->LoadFromFile(path); +#ifdef IS_PY3K + Py_DECREF(string); +#endif + return PyBool_FromLong(result); } static PyObject * PySfPostFX_LoadFromMemory (PySfPostFX *self, PyObject *args) -{ - if (self->obj->LoadFromMemory(PyString_AsString(args))) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; +{ + char *path; +#ifdef IS_PY3K + PyObject *string = PyUnicode_AsUTF8String(args); + if (string == NULL) + return NULL; + path = PyBytes_AsString(string); +#else + path = PyString_AsString(args); +#endif + bool result = self->obj->LoadFromMemory(path); +#ifdef IS_PY3K + Py_DECREF(string); +#endif + return PyBool_FromLong(result); } static int -PySfPostFX_init(PySfPostFX *self, PyObject *args); +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", &Name, &X, &Y, &Z, &W)) - return NULL; +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) { @@ -120,7 +126,7 @@ PySfPostFX_SetTexture(PySfPostFX* self, PyObject *args) { if (!PyObject_TypeCheck(Image, &PySfImageType)) { - PyErr_SetString(PyExc_TypeError, "Argument 2, if specified, must be a sf.Image instance or None."); + PyErr_SetString(PyExc_TypeError, "PostFX.SetTexture() Argument 2, if specified, must be a sf.Image instance or None."); return NULL; } self->obj->SetTexture(Name, Image->obj); @@ -131,10 +137,7 @@ PySfPostFX_SetTexture(PySfPostFX* self, PyObject *args) static PyObject * PySfPostFX_CanUsePostFX(PySfPostFX* self, PyObject *args) { - if (sf::PostFX::CanUsePostFX()) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(sf::PostFX::CanUsePostFX()); } @@ -152,8 +155,7 @@ static PyMethodDef PySfPostFX_methods[] = { }; PyTypeObject PySfPostFXType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "PostFX", /*tp_name*/ sizeof(PySfPostFX), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -173,7 +175,9 @@ PyTypeObject PySfPostFXType = { 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - "sf.PostFX is used to apply a post effect to a window. ", /* tp_doc */ + "sf.PostFX is used to apply a post effect to a window.\n\ +Default constructor : sf.PostFX()\n\ +Copy constructor : sf.PostFX(Copy) where Copy is a sf.PostFX instance.", /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -181,7 +185,7 @@ PyTypeObject PySfPostFXType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfPostFX_methods, /* tp_methods */ - PySfPostFX_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ &PySfDrawableType, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/PostFX.hpp b/python/src/PostFX.hpp index 20541ace6..080b3a0a5 100644 --- a/python/src/PostFX.hpp +++ b/python/src/PostFX.hpp @@ -25,14 +25,9 @@ #ifndef __PYPOSTFX_HPP #define __PYPOSTFX_HPP -#include -#include - #include -#include -#include "Drawable.hpp" -#include "Image.hpp" +#include typedef struct { PyObject_HEAD diff --git a/python/src/Rect.cpp b/python/src/Rect.cpp index 46d03722b..a05583e92 100644 --- a/python/src/Rect.cpp +++ b/python/src/Rect.cpp @@ -24,6 +24,12 @@ #include "Rect.hpp" +#include + +#include "compat.hpp" +#include "offsetof.hpp" + + static PyMemberDef PySfIntRect_members[] = { {(char *)"Left", T_INT, offsetof(PySfIntRect, Left), 0, (char *)"Left coordinate of the rectangle."}, {(char *)"Top", T_INT, offsetof(PySfIntRect, Top), 0, (char *)"Top coordinate of the rectangle."}, @@ -44,26 +50,21 @@ static void PySfIntRect_dealloc(PySfIntRect* self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static void PySfFloatRect_dealloc(PySfFloatRect* self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfIntRect_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfIntRect *self; - self = (PySfIntRect *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } @@ -71,12 +72,7 @@ static PyObject * PySfFloatRect_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfFloatRect *self; - self = (PySfFloatRect *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } @@ -86,7 +82,7 @@ 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", (char **)kwlist, &Left, &Top, &Right, &Bottom)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiii:IntRect.__init__", (char **)kwlist, &Left, &Top, &Right, &Bottom)) return -1; self->Left = Left; @@ -101,14 +97,14 @@ static PyObject * PySfIntRect_GetWidth(PySfIntRect *self) { PySfIntRectUpdateObj(self); - return PyInt_FromLong(self->obj->GetWidth()); + return PyLong_FromLong(self->obj->GetWidth()); } static PyObject * PySfIntRect_GetHeight(PySfIntRect *self) { PySfIntRectUpdateObj(self); - return PyInt_FromLong(self->obj->GetHeight()); + return PyLong_FromLong(self->obj->GetHeight()); } static PyObject * @@ -143,7 +139,7 @@ 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", (char **)kwlist, &Left, &Top, &Right, &Bottom)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffff:FloatRect.__init__", (char **)kwlist, &Left, &Top, &Right, &Bottom)) return -1; self->Left = Left; @@ -160,7 +156,7 @@ PySfIntRect_Offset(PySfIntRect* self, PyObject *args) { int OffsetX, OffsetY; - if (!PyArg_ParseTuple(args, "ii", &OffsetX, &OffsetY)) + if (!PyArg_ParseTuple(args, "ii:IntRect.Offset", &OffsetX, &OffsetY)) return NULL; PySfIntRectUpdateObj(self); @@ -174,7 +170,7 @@ PySfFloatRect_Offset(PySfFloatRect* self, PyObject *args) { float OffsetX, OffsetY; - if (!PyArg_ParseTuple(args, "ff", &OffsetX, &OffsetY)) + if (!PyArg_ParseTuple(args, "ff:FloatRect.Offset", &OffsetX, &OffsetY)) return NULL; PySfFloatRectUpdateObj(self); @@ -228,8 +224,7 @@ Check intersection between two rectangles.\n\ }; PyTypeObject PySfIntRectType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "IntRect", /*tp_name*/ sizeof(PySfIntRect), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -271,8 +266,7 @@ PyTypeObject PySfIntRectType = { PyTypeObject PySfFloatRectType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "FloatRect", /*tp_name*/ sizeof(PySfFloatRect), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -318,14 +312,11 @@ PySfFloatRect_Contains(PySfFloatRect* self, PyObject *args) { float x=0, y=0; - if (! PyArg_ParseTuple(args, "ff", &x, &y)) + if (!PyArg_ParseTuple(args, "ff:FloatRect.Contains", &x, &y)) return NULL; PySfFloatRectUpdateObj(self); - if (self->obj->Contains(x,y)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(self->obj->Contains(x,y)); } static PyObject * @@ -334,8 +325,7 @@ PySfFloatRect_Intersects(PySfFloatRect* self, PyObject *args) PySfFloatRect *Rect=NULL, *OverlappingRect=NULL; bool result; - - if (! PyArg_ParseTuple(args, "O!|O!", &PySfFloatRectType, &Rect, &PySfFloatRectType, &OverlappingRect)) + if (!PyArg_ParseTuple(args, "O!|O!:FloatRect.Intersects", &PySfFloatRectType, &Rect, &PySfFloatRectType, &OverlappingRect)) return NULL; PySfFloatRectUpdateObj(self); @@ -344,10 +334,7 @@ PySfFloatRect_Intersects(PySfFloatRect* self, PyObject *args) else result = self->obj->Intersects(*(Rect->obj)); - if (result) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(result); } @@ -357,13 +344,10 @@ PySfIntRect_Contains(PySfIntRect* self, PyObject *args) unsigned int x=0, y=0; PySfIntRectUpdateObj(self); - if (! PyArg_ParseTuple(args, "II", &x, &y)) + if (!PyArg_ParseTuple(args, "II:IntRect.Contains", &x, &y)) return NULL; - if (self->obj->Contains(x,y)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(self->obj->Contains(x,y)); } static PyObject * @@ -373,7 +357,7 @@ PySfIntRect_Intersects(PySfIntRect* self, PyObject *args) bool result; PySfIntRectUpdateObj(self); - if (! PyArg_ParseTuple(args, "O!|O!", &PySfIntRectType, &Rect, &PySfIntRectType, &OverlappingRect)) + if (!PyArg_ParseTuple(args, "O!|O!:IntRect.Intersects", &PySfIntRectType, &Rect, &PySfIntRectType, &OverlappingRect)) return NULL; if (OverlappingRect) @@ -381,10 +365,7 @@ PySfIntRect_Intersects(PySfIntRect* self, PyObject *args) else result = self->obj->Intersects(*(Rect->obj)); - if (result) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(result); } void diff --git a/python/src/Rect.hpp b/python/src/Rect.hpp index 59856f929..4b4ae63f9 100644 --- a/python/src/Rect.hpp +++ b/python/src/Rect.hpp @@ -25,14 +25,9 @@ #ifndef __PYRECT_HPP #define __PYRECT_HPP +#include #include -#include - -#include -#include - -#include "offsetof.hpp" typedef struct { PyObject_HEAD diff --git a/python/src/RenderTarget.cpp b/python/src/RenderTarget.cpp index 8762cdaa9..5b2afe243 100644 --- a/python/src/RenderTarget.cpp +++ b/python/src/RenderTarget.cpp @@ -25,33 +25,26 @@ #include "RenderTarget.hpp" #include "Color.hpp" #include "View.hpp" + +#include "compat.hpp" + extern PyTypeObject PySfColorType; extern PyTypeObject PySfViewType; -static PyMemberDef PySfRenderTarget_members[] = { - {NULL} /* Sentinel */ -}; - static void PySfRenderTarget_dealloc(PySfRenderTarget *self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfRenderTarget_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfRenderTarget *self; - self = (PySfRenderTarget *)type->tp_alloc(type, 0); - - if (self != NULL) - { - } - return (PyObject *)self; } @@ -62,11 +55,8 @@ PySfRenderTarget_Clear(PySfRenderTarget *self, PyObject *args) int size = PyTuple_Size(args); if (size == 1) { - if (! PyArg_ParseTuple(args, "O!", &PySfColorType, &Color)) - { - PyErr_SetString(PyExc_TypeError, "Argument is not a sf.Color"); + if (!PyArg_ParseTuple(args, "O!:RenderTarget.Clear", &PySfColorType, &Color)) return NULL; - } PySfColorUpdate(Color); self->obj->Clear(*(Color->obj)); } @@ -97,9 +87,7 @@ PySfRenderTarget_GetView(PySfRenderTarget *self) static PyObject * PySfRenderTarget_PreserveOpenGLStates(PySfRenderTarget *self, PyObject *args) { - bool Optimize = false; - if (PyObject_IsTrue(args)) - Optimize = true; + bool Optimize = PyBool_AsBool(args); self->obj->PreserveOpenGLStates(Optimize); Py_RETURN_NONE; } @@ -108,9 +96,9 @@ static PyObject * PySfRenderTarget_SetView(PySfRenderTarget* self, PyObject *args) { PySfView *View = (PySfView *)args; - if (! PyObject_TypeCheck(View, &PySfViewType)) + if (!PyObject_TypeCheck(View, &PySfViewType)) { - PyErr_SetString(PyExc_TypeError, "Argument is not a sf.View"); + PyErr_SetString(PyExc_TypeError, "RenderTarget.SetView() Argument is not a sf.View"); return NULL; } self->obj->SetView( *(View->obj)); @@ -146,8 +134,7 @@ Change the current active view. View must be a sf.View instance."}, }; PyTypeObject PySfRenderTargetType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "RenderTarget", /*tp_name*/ sizeof(PySfRenderTarget), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -175,14 +162,14 @@ PyTypeObject PySfRenderTargetType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfRenderTarget_methods, /* tp_methods */ - PySfRenderTarget_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - 0, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfRenderTarget_new, /* tp_new */ }; diff --git a/python/src/RenderTarget.hpp b/python/src/RenderTarget.hpp index 25a4ef40c..e57a5a552 100644 --- a/python/src/RenderTarget.hpp +++ b/python/src/RenderTarget.hpp @@ -25,12 +25,9 @@ #ifndef __PYRENDERTARGET_H #define __PYRENDERTARGET_H -#include - #include -#include -#include +#include typedef struct { PyObject_HEAD diff --git a/python/src/RenderWindow.cpp b/python/src/RenderWindow.cpp index 71f293e3f..1e486a55f 100644 --- a/python/src/RenderWindow.cpp +++ b/python/src/RenderWindow.cpp @@ -23,7 +23,6 @@ //////////////////////////////////////////////////////////// #include "RenderWindow.hpp" - #include "Event.hpp" #include "VideoMode.hpp" #include "Drawable.hpp" @@ -31,9 +30,12 @@ #include "Rect.hpp" #include "View.hpp" #include "Image.hpp" -#include "Window.hpp" +#include "Window.hpp" +#include "WindowSettings.hpp" + +#include "compat.hpp" -#include "SFML/Window/WindowStyle.hpp" +#include extern PyTypeObject PySfEventType; @@ -43,31 +45,21 @@ extern PyTypeObject PySfWindowType; extern PyTypeObject PySfWindowSettingsType; extern PyTypeObject PySfVideoModeType; extern PyTypeObject PySfDrawableType; - extern PyTypeObject PySfRenderTargetType; -static PyMemberDef PySfRenderWindow_members[] = { - {NULL} /* Sentinel */ -}; - static void PySfRenderWindow_dealloc(PySfRenderWindow* self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfRenderWindow_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfRenderWindow *self; - self = (PySfRenderWindow *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } @@ -76,7 +68,8 @@ PySfRenderWindow_init(PySfRenderWindow *self, PyObject *args, PyObject *kwds) { self->obj = new sf::RenderWindow(); if (PyTuple_Size(args) > 0) - PySfWindow_Create((PySfWindow *)self, args, kwds); + if (PySfWindow_Create((PySfWindow *)self, args, kwds) == NULL) + return -1; return 0; } @@ -98,7 +91,7 @@ PySfRenderWindow_ConvertCoords(PySfRenderWindow *self, PyObject *args) sf::View *TargetView = NULL; sf::Vector2f Vect; - if (! PyArg_ParseTuple(args, "II|O!", &WindowX, &WindowY, &PySfViewType, &PyTargetView)) + if (!PyArg_ParseTuple(args, "II|O!:RenderWindow.ConvertCoords", &WindowX, &WindowY, &PySfViewType, &PyTargetView)) return NULL; if (PyTargetView) @@ -154,9 +147,8 @@ PySfRenderWindow_Draw(PySfRenderWindow *self, PyObject *args) Py_DECREF(iterator); - if (PyErr_Occurred()) { + if (PyErr_Occurred()) return NULL; - } } Py_RETURN_NONE; } @@ -175,8 +167,7 @@ Draw something on the window. The argument can be a drawable or any object suppo }; PyTypeObject PySfRenderWindowType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "RenderWindow", /*tp_name*/ sizeof(PySfRenderWindow), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -196,7 +187,14 @@ PyTypeObject PySfRenderWindowType = { 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - "Simple wrapper for sf.Window that allows easy 2D rendering.", /* tp_doc */ + "Simple wrapper for sf.Window that allows easy 2D rendering.\n\ +Default constructor : sf.RenderWindow()\n\ +Other constructor : sf.RenderWindow(Mode, Title, Style::Resize|Style::Close, Params = WindowSettings())\n\ +Parameters:\n\ + Mode : Video mode to use\n\ + Title : Title of the window\n\ + WindowStyle : Window style (Resize | Close by default)\n\ + Params : Creation parameters (see default constructor for default values)", /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -204,7 +202,7 @@ PyTypeObject PySfRenderWindowType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfRenderWindow_methods, /* tp_methods */ - PySfRenderWindow_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/RenderWindow.hpp b/python/src/RenderWindow.hpp index c484b891e..37e62afb6 100644 --- a/python/src/RenderWindow.hpp +++ b/python/src/RenderWindow.hpp @@ -25,13 +25,9 @@ #ifndef __PYRENDERWINDOW_HPP #define __PYRENDERWINDOW_HPP -#include -#include - #include -#include -#include "WindowSettings.hpp" +#include typedef struct { PyObject_HEAD diff --git a/python/src/Sleep.hpp b/python/src/Sleep.hpp index ad2b7e0e9..3c78fd733 100644 --- a/python/src/Sleep.hpp +++ b/python/src/Sleep.hpp @@ -25,11 +25,9 @@ #ifndef __PYSLEEP_HPP #define __PYSLEEP_HPP -#include -#include - #include -#include + +#include PyObject * PySFML_Sleep (PyObject *self, PyObject *args); diff --git a/python/src/Sprite.cpp b/python/src/Sprite.cpp index c783ccdd1..3070e47c8 100644 --- a/python/src/Sprite.cpp +++ b/python/src/Sprite.cpp @@ -22,7 +22,12 @@ // //////////////////////////////////////////////////////////// -#include "Sprite.hpp" +#include "Sprite.hpp" +#include "Drawable.hpp" +#include "Rect.hpp" +#include "Color.hpp" + +#include "compat.hpp" extern PyTypeObject PySfColorType; @@ -30,19 +35,16 @@ extern PyTypeObject PySfImageType; extern PyTypeObject PySfIntRectType; extern PyTypeObject PySfDrawableType; -static PyMemberDef PySfSprite_members[] = { - {NULL} /* Sentinel */ -}; - - static void PySfSprite_dealloc(PySfSprite *self) { if (self->Image != NULL) - Py_DECREF(self->Image); + { + Py_DECREF(self->Image); + } delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * @@ -69,7 +71,7 @@ PySfSprite_init(PySfSprite *self, PyObject *args, PyObject *kwds) PySfImage *Image=NULL; PySfColor *Color=NULL; - if (! PyArg_ParseTupleAndKeywords(args, kwds, "O!|fffffO!", (char **)kwlist, &PySfImageType, &Image, &X, &Y, &ScaleX, &ScaleY, &Rotation, &PySfColorType, &Color)) + if (! PyArg_ParseTupleAndKeywords(args, kwds, "O!|fffffO!:Sprite.__init__", (char **)kwlist, &PySfImageType, &Image, &X, &Y, &ScaleX, &ScaleY, &Rotation, &PySfColorType, &Color)) return -1; Py_INCREF(Image); @@ -79,7 +81,6 @@ PySfSprite_init(PySfSprite *self, PyObject *args, PyObject *kwds) else self->obj = new sf::Sprite(*(Image->obj), sf::Vector2f(X, Y), sf::Vector2f(ScaleX, ScaleY), Rotation); - return 0; } @@ -91,7 +92,7 @@ PySfSprite_SetImage(PySfSprite* self, PyObject *args) PySfImage *Image = (PySfImage *)args; if (! PyObject_TypeCheck(Image, &PySfImageType)) { - PyErr_SetString(PyExc_TypeError, "Argument is not a sfImage"); + PyErr_SetString(PyExc_TypeError, "Sprite.SetImage() Argument is not a sf.Image"); return NULL; } Py_DECREF(self->Image); @@ -114,8 +115,7 @@ PySfSprite_GetPixel(PySfSprite* self, PyObject *args) PySfColor *Color; unsigned int x=0, y=0; - - if (! PyArg_ParseTuple(args, "II", &x, &y)) + if (!PyArg_ParseTuple(args, "II:Sprite.GetPixel", &x, &y)) return NULL; Color = GetNewPySfColor(); @@ -133,7 +133,7 @@ PySfSprite_Resize(PySfSprite* self, PyObject *args) { float W=0, H=0; - if (! PyArg_ParseTuple(args, "ff", &W, &H)) + if (! PyArg_ParseTuple(args, "ff:Sprite.Resize", &W, &H)) return NULL; self->obj->Resize(W,H); @@ -161,7 +161,7 @@ PySfSprite_SetSubRect(PySfSprite* self, PyObject *args) PySfIntRect *Rect = (PySfIntRect *)args; if (! PyObject_TypeCheck(Rect, &PySfIntRectType)) { - PyErr_SetString(PyExc_TypeError, "Argument is not a sf.IntRect instance"); + PyErr_SetString(PyExc_TypeError, "Sprite.SetSubRect() Argument is not a sf.IntRect instance"); return NULL; } self->obj->SetSubRect(*(Rect->obj)); @@ -171,20 +171,14 @@ PySfSprite_SetSubRect(PySfSprite* self, PyObject *args) static PyObject * PySfSprite_FlipX(PySfSprite* self, PyObject *args) { - bool Flip = false; - if (PyObject_IsTrue(args)) - Flip = true; - self->obj->FlipX(Flip); + self->obj->FlipX(PyBool_AsBool(args)); Py_RETURN_NONE; } static PyObject * PySfSprite_FlipY(PySfSprite* self, PyObject *args) { - bool Flip = false; - if (PyObject_IsTrue(args)) - Flip = true; - self->obj->FlipY(Flip); + self->obj->FlipY(PyBool_AsBool(args)); Py_RETURN_NONE; } @@ -211,8 +205,7 @@ static PyMethodDef PySfSprite_methods[] = { }; PyTypeObject PySfSpriteType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Sprite", /*tp_name*/ sizeof(PySfSprite), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -240,7 +233,7 @@ PyTypeObject PySfSpriteType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfSprite_methods, /* tp_methods */ - PySfSprite_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ &PySfDrawableType, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/Sprite.hpp b/python/src/Sprite.hpp index 97cd58b2d..aaab6d635 100644 --- a/python/src/Sprite.hpp +++ b/python/src/Sprite.hpp @@ -25,16 +25,11 @@ #ifndef __PYSPRITE_HPP #define __PYSPRITE_HPP -#include -#include -#include - #include -#include -#include "Drawable.hpp" +#include + #include "Image.hpp" -#include "Rect.hpp" typedef struct { PyObject_HEAD diff --git a/python/src/Window.cpp b/python/src/Window.cpp index 1f4b17f80..791012460 100644 --- a/python/src/Window.cpp +++ b/python/src/Window.cpp @@ -169,7 +169,8 @@ PySfWindow_init(PySfWindow *self, PyObject *args, PyObject *kwds) { self->obj = new sf::Window(); if (PyTuple_Size(args) > 0) - PySfWindow_Create(self, args, kwds); + if (PySfWindow_Create(self, args, kwds) == NULL) + return -1; return 0; } diff --git a/python/src/compat.hpp b/python/src/compat.hpp new file mode 100644 index 000000000..173ab54a8 --- /dev/null +++ b/python/src/compat.hpp @@ -0,0 +1,42 @@ +//////////////////////////////////////////////////////////// +// +// PySFML - Python binding for SFML (Simple and Fast Multimedia Library) +// Copyright (C) 2007, 2008 RĂ©mi Koenig (remi.k2620@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +#ifndef __PYCOMPAT_HPP +#define __PYCOMPAT_HPP + +#if PY_MAJOR_VERSION >= 3 +#define IS_PY3K +#define head_init PyVarObject_HEAD_INIT(NULL, 0) +#else +#define Py_TYPE(a) a->ob_type +#define head_init PyObject_HEAD_INIT(NULL) 0, +#define PyBytes_FromStringAndSize PyString_FromStringAndSize +#endif + +#define free_object(a) Py_TYPE(a)->tp_free((PyObject*)a) + +#define PyBool_AsBool(a) ((PyObject_IsTrue(a))?true:false) + +#endif + diff --git a/python/src/main.cpp b/python/src/main.cpp index 7df0428b1..6dc7d06d8 100644 --- a/python/src/main.cpp +++ b/python/src/main.cpp @@ -33,7 +33,9 @@ #include "Blend.hpp" #include "Sound.hpp" #include "String.hpp" -#include "SoundStream.hpp" +#include "SoundStream.hpp" + +#include "compat.hpp" extern PyTypeObject PySfClockType; @@ -88,109 +90,129 @@ extern PyTypeObject PySfListenerType; static PyMethodDef module_methods[] = { {"Sleep", (PyCFunction)PySFML_Sleep, METH_O, "Sleep(Duration)\nMake the current thread sleep for a given time.\n Duration : Time to sleep, in seconds"}, {NULL} /* Sentinel */ -}; +}; +#ifdef IS_PY3K +#define INITERROR return NULL +static PyModuleDef module_def = { + PyModuleDef_HEAD_INIT, + "noddy", + "Example module that creates an extension type.", + -1, + module_methods, NULL, NULL, NULL, NULL +}; + +PyObject * +PyInit_sf(void) +#else +#define INITERROR return + #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ #define PyMODINIT_FUNC void #endif -PyMODINIT_FUNC -initsf(void) +PyMODINIT_FUNC +initsf(void) +#endif { PyObject *m; if (PyType_Ready(&PySfClockType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfWindowType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfWindowSettingsType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfStyleType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfRenderTargetType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfRenderWindowType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfVideoModeType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfViewType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfInputType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfEventType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfEventTextType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfEventKeyType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfEventMouseMoveType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfEventMouseButtonType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfEventMouseWheelType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfEventJoyMoveType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfEventJoyButtonType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfEventSizeType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfKeyType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfJoyType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfMouseType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfDrawableType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfBlendType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfSpriteType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfFontType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfGlyphType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfStringType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfPostFXType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfImageType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfShapeType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfColorType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfIntRectType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfFloatRectType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfMusicType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfSoundType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfSoundBufferType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfSoundBufferRecorderType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfSoundRecorderType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfSoundStreamType) < 0) - return; + INITERROR; if (PyType_Ready(&PySfListenerType) < 0) - return; - - m = Py_InitModule3("sf", module_methods, "Python binding for sfml (Simple Fast Media Library)"); + INITERROR; + +#ifdef IS_PY3K + m = PyModule_Create(&module_def); +#else + m = Py_InitModule3("sf", module_methods, "Python binding for sfml (Simple Fast Media Library)"); +#endif if (m == NULL) - return; + INITERROR; Py_INCREF(&PySfClockType); PyModule_AddObject(m, "Clock", (PyObject *)&PySfClockType); @@ -274,6 +296,10 @@ initsf(void) PySfBlend_InitConst(); PySfSound_InitConst(); PySfSoundStream_InitConst(); - PySfString_InitConst(); + PySfString_InitConst(); + +#ifdef IS_PY3K + return m; +#endif }