From af3dd7c63008854cbfa2600cc2636541aa246006 Mon Sep 17 00:00:00 2001 From: remi-k Date: Thu, 26 Feb 2009 12:36:06 +0000 Subject: [PATCH] More code clean-up and python3 compliance git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1022 4e206d99-4929-0410-ac5d-dfc041789085 --- python/src/Blend.cpp | 56 ++--------- python/src/Blend.hpp | 8 ++ python/src/Font.cpp | 21 ++--- python/src/Font.hpp | 6 +- python/src/Glyph.cpp | 21 ++--- python/src/Glyph.hpp | 6 +- python/src/Image.cpp | 32 +------ python/src/Joy.cpp | 62 +++--------- python/src/Joy.hpp | 8 ++ python/src/Listener.cpp | 38 ++------ python/src/Listener.hpp | 6 +- python/src/Mouse.cpp | 58 +++--------- python/src/Mouse.hpp | 8 ++ python/src/PostFX.cpp | 23 +---- python/src/Shape.cpp | 45 +++------ python/src/Shape.hpp | 6 +- python/src/Sound.cpp | 55 ++++------- python/src/Sound.hpp | 6 +- python/src/SoundBuffer.cpp | 59 +++++------- python/src/SoundBuffer.hpp | 6 +- python/src/SoundBufferRecorder.cpp | 24 ++--- python/src/SoundBufferRecorder.hpp | 6 +- python/src/SoundRecorder.cpp | 61 ++++++------ python/src/SoundRecorder.hpp | 6 +- python/src/SoundStream.cpp | 62 ++++++------ python/src/SoundStream.hpp | 3 +- python/src/String.cpp | 37 ++++---- python/src/String.hpp | 6 +- python/src/VideoMode.cpp | 20 ++-- python/src/VideoMode.hpp | 8 +- python/src/View.cpp | 24 ++--- python/src/View.hpp | 8 +- python/src/Window.cpp | 145 ++++++++--------------------- python/src/Window.hpp | 10 +- python/src/WindowSettings.cpp | 21 ++--- python/src/WindowSettings.hpp | 6 +- python/src/WindowStyle.cpp | 59 +++--------- python/src/WindowStyle.hpp | 8 ++ python/src/compat.hpp | 27 ++++++ python/src/main.cpp | 7 +- 40 files changed, 364 insertions(+), 714 deletions(-) diff --git a/python/src/Blend.cpp b/python/src/Blend.cpp index 7398e4621..dc43ca5c6 100644 --- a/python/src/Blend.cpp +++ b/python/src/Blend.cpp @@ -22,62 +22,26 @@ // //////////////////////////////////////////////////////////// -#include - -#include -#include - #include "Blend.hpp" +#include "compat.hpp" -typedef struct { - PyObject_HEAD -} PySfBlend; - - - -static PyMemberDef PySfBlend_members[] = { - {NULL} /* Sentinel */ -}; - - -static void -PySfBlend_dealloc(PySfBlend *self) -{ - self->ob_type->tp_free((PyObject*)self); -} static PyObject * PySfBlend_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfBlend *self; - self = (PySfBlend *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } -static int -PySfBlend_init(PySfBlend *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - -static PyMethodDef PySfBlend_methods[] = { - {NULL} /* Sentinel */ -}; - PyTypeObject PySfBlendType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Blend", /*tp_name*/ sizeof(PySfBlend), /*tp_basicsize*/ 0, /*tp_itemsize*/ - (destructor)PySfBlend_dealloc, /*tp_dealloc*/ + 0, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -104,15 +68,15 @@ None No blending.", /* tp_doc */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - PySfBlend_methods, /* tp_methods */ - PySfBlend_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)PySfBlend_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfBlend_new, /* tp_new */ }; @@ -120,16 +84,16 @@ None No blending.", /* tp_doc */ void PySfBlend_InitConst() { PyObject *obj; - obj = PyInt_FromLong(sf::Blend::Alpha); + obj = PyLong_FromLong(sf::Blend::Alpha); PyDict_SetItemString(PySfBlendType.tp_dict, "Alpha", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Blend::Add); + obj = PyLong_FromLong(sf::Blend::Add); PyDict_SetItemString(PySfBlendType.tp_dict, "Add", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Blend::Multiply); + obj = PyLong_FromLong(sf::Blend::Multiply); PyDict_SetItemString(PySfBlendType.tp_dict, "Multiply", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Blend::None); + obj = PyLong_FromLong(sf::Blend::None); PyDict_SetItemString(PySfBlendType.tp_dict, "None", obj); Py_DECREF(obj); } diff --git a/python/src/Blend.hpp b/python/src/Blend.hpp index e47ed9c53..f61087369 100644 --- a/python/src/Blend.hpp +++ b/python/src/Blend.hpp @@ -25,6 +25,14 @@ #ifndef __PYBLEND_HPP #define __PYBLEND_HPP +#include + +#include + +typedef struct { + PyObject_HEAD +} PySfBlend; + void PySfBlend_InitConst(); diff --git a/python/src/Font.cpp b/python/src/Font.cpp index e9abe5527..c6f49860d 100644 --- a/python/src/Font.cpp +++ b/python/src/Font.cpp @@ -25,10 +25,7 @@ #include "Font.hpp" #include "Glyph.hpp" -static PyMemberDef PySfFont_members[] = { - {NULL} /* Sentinel */ -}; - +#include "compat.hpp" static void @@ -36,21 +33,16 @@ PySfFont_dealloc(PySfFont *self) { if (self->Owner) delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfFont_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfFont *self; - self = (PySfFont *)type->tp_alloc(type, 0); - if (self != NULL) - { self->Owner = true; - } - return (PyObject *)self; } @@ -71,7 +63,7 @@ PySfFont_LoadFromFile(PySfFont* self, PyObject *args, PyObject *kwds) int CharsetSize; bool Result; - if (! PyArg_ParseTupleAndKeywords(args, kwds, "s|Is#", (char **)kwlist, &Filename, &Charsize, &CharsetTmp, &CharsetSize)) + if (! PyArg_ParseTupleAndKeywords(args, kwds, "s|Is#:Font.LoadFromFile", (char **)kwlist, &Filename, &Charsize, &CharsetTmp, &CharsetSize)) return NULL; if (CharsetTmp) @@ -100,7 +92,7 @@ PySfFont_LoadFromMemory(PySfFont* self, PyObject *args, PyObject *kwds) int CharsetSize; bool Result; - if (! PyArg_ParseTupleAndKeywords(args, kwds, "s#|Is#", (char **)kwlist, &Data, &Size, &Charsize, &CharsetTmp, &CharsetSize)) + if (! PyArg_ParseTupleAndKeywords(args, kwds, "s#|Is#:Font.LoadFromMemory", (char **)kwlist, &Data, &Size, &Charsize, &CharsetTmp, &CharsetSize)) return NULL; if (CharsetTmp) @@ -169,8 +161,7 @@ Get the description of a glyph (character) given by its unicode value. Returns g PyTypeObject PySfFontType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Font", /*tp_name*/ sizeof(PySfFont), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -198,7 +189,7 @@ PyTypeObject PySfFontType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfFont_methods, /* tp_methods */ - PySfFont_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/Font.hpp b/python/src/Font.hpp index a7851a463..a8105504b 100644 --- a/python/src/Font.hpp +++ b/python/src/Font.hpp @@ -25,13 +25,9 @@ #ifndef __PYFONT_HPP #define __PYFONT_HPP -#include -#include - #include -#include -#include "offsetof.hpp" +#include typedef struct { diff --git a/python/src/Glyph.cpp b/python/src/Glyph.cpp index 21c93a344..2f82b058f 100644 --- a/python/src/Glyph.cpp +++ b/python/src/Glyph.cpp @@ -24,6 +24,12 @@ #include "Glyph.hpp" +#include + +#include "offsetof.hpp" +#include "compat.hpp" + + static PyMemberDef PySfGlyph_members[] = { {(char *)"Advance", T_INT, offsetof(PySfGlyph, Advance), 0, (char *)"Offset to move horizontically to the next character."}, {(char *)"Rectangle", T_OBJECT, offsetof(PySfGlyph, Rectangle), 0, (char *)"Bounding rectangle of the glyph, in relative coordinates."}, @@ -32,7 +38,6 @@ static PyMemberDef PySfGlyph_members[] = { }; - static void PySfGlyph_dealloc(PySfGlyph *self) { @@ -41,7 +46,7 @@ PySfGlyph_dealloc(PySfGlyph *self) self->TexCoords->obj = new sf::FloatRect(self->obj->TexCoords); Py_DECREF(self->TexCoords); delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } void @@ -80,16 +85,13 @@ static PyObject * PySfGlyph_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfGlyph *self; - self = (PySfGlyph *)type->tp_alloc(type, 0); - if (self != NULL) { self->Advance = 0; self->Rectangle = GetNewPySfIntRect(); self->TexCoords = GetNewPySfFloatRect(); } - return (PyObject *)self; } @@ -103,14 +105,9 @@ PySfGlyph_init(PySfGlyph *self, PyObject *args, PyObject *kwds) return 0; } -static PyMethodDef PySfGlyph_methods[] = { - {NULL} /* Sentinel */ -}; - PyTypeObject PySfGlyphType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Glyph", /*tp_name*/ sizeof(PySfGlyph), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -137,7 +134,7 @@ PyTypeObject PySfGlyphType = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - PySfGlyph_methods, /* tp_methods */ + 0, /* tp_methods */ PySfGlyph_members, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ diff --git a/python/src/Glyph.hpp b/python/src/Glyph.hpp index 0b35a8866..5b83575d8 100644 --- a/python/src/Glyph.hpp +++ b/python/src/Glyph.hpp @@ -25,13 +25,9 @@ #ifndef __PYGLYPH_HPP #define __PYGLYPH_HPP -#include -#include - #include -#include -#include "offsetof.hpp" +#include #include "Rect.hpp" diff --git a/python/src/Image.cpp b/python/src/Image.cpp index 9f8abe689..3d8b060e7 100644 --- a/python/src/Image.cpp +++ b/python/src/Image.cpp @@ -196,39 +196,13 @@ PySfImage_GetPixels(PySfImage *self) static PyObject * PySfImage_LoadFromFile (PySfImage *self, PyObject *args) { - 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); + load_from_file(self, args); } static PyObject * PySfImage_SaveToFile (PySfImage *self, PyObject *args) { - 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); + save_to_file(self, args); } static int @@ -390,6 +364,7 @@ PySfImage_init(PySfImage *self, PyObject *args, PyObject *kwds) self->obj = new sf::Image(*(Image->obj)); return 0; } + else PyErr_Clear(); } self->obj = new sf::Image(); if (PyTuple_Size(args) > 0) @@ -400,6 +375,7 @@ PySfImage_init(PySfImage *self, PyObject *args, PyObject *kwds) return -1; else if (PySfImage_LoadFromPixels(self, args) == NULL) return -1; + else PyErr_Clear(); } } return 0; diff --git a/python/src/Joy.cpp b/python/src/Joy.cpp index b43b29d64..7f20f68f3 100644 --- a/python/src/Joy.cpp +++ b/python/src/Joy.cpp @@ -22,62 +22,26 @@ // //////////////////////////////////////////////////////////// -#include - -#include -#include - #include "Joy.hpp" +#include "compat.hpp" -typedef struct { - PyObject_HEAD -} PySfJoy; - - - -static PyMemberDef PySfJoy_members[] = { - {NULL} /* Sentinel */ -}; - - -static void -PySfJoy_dealloc(PySfJoy *self) -{ - self->ob_type->tp_free((PyObject*)self); -} static PyObject * PySfJoy_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfJoy *self; - self = (PySfJoy *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } -static int -PySfJoy_init(PySfJoy *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - -static PyMethodDef PySfJoy_methods[] = { - {NULL} /* Sentinel */ -}; - PyTypeObject PySfJoyType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Joy", /*tp_name*/ sizeof(PySfJoy), /*tp_basicsize*/ 0, /*tp_itemsize*/ - (destructor)PySfJoy_dealloc, /*tp_dealloc*/ + 0, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -100,15 +64,15 @@ PyTypeObject PySfJoyType = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - PySfJoy_methods, /* tp_methods */ - PySfJoy_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)PySfJoy_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfJoy_new, /* tp_new */ }; @@ -116,25 +80,25 @@ PyTypeObject PySfJoyType = { void PySfJoy_InitConst() { PyObject *obj; - obj = PyInt_FromLong(sf::Joy::AxisX); + obj = PyLong_FromLong(sf::Joy::AxisX); PyDict_SetItemString(PySfJoyType.tp_dict, "AxisX", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Joy::AxisY); + obj = PyLong_FromLong(sf::Joy::AxisY); PyDict_SetItemString(PySfJoyType.tp_dict, "AxisY", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Joy::AxisZ); + obj = PyLong_FromLong(sf::Joy::AxisZ); PyDict_SetItemString(PySfJoyType.tp_dict, "AxisZ", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Joy::AxisR); + obj = PyLong_FromLong(sf::Joy::AxisR); PyDict_SetItemString(PySfJoyType.tp_dict, "AxisR", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Joy::AxisU); + obj = PyLong_FromLong(sf::Joy::AxisU); PyDict_SetItemString(PySfJoyType.tp_dict, "AxisU", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Joy::AxisV); + obj = PyLong_FromLong(sf::Joy::AxisV); PyDict_SetItemString(PySfJoyType.tp_dict, "AxisV", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Joy::AxisPOV); + obj = PyLong_FromLong(sf::Joy::AxisPOV); PyDict_SetItemString(PySfJoyType.tp_dict, "AxisPOV", obj); Py_DECREF(obj); } diff --git a/python/src/Joy.hpp b/python/src/Joy.hpp index 14805ade9..6daf4bb38 100644 --- a/python/src/Joy.hpp +++ b/python/src/Joy.hpp @@ -25,6 +25,14 @@ #ifndef __PYJOY_HPP #define __PYJOY_HPP +#include + +#include + +typedef struct { + PyObject_HEAD +} PySfJoy; + void PySfJoy_InitConst(); diff --git a/python/src/Listener.cpp b/python/src/Listener.cpp index 5a56143d2..4f2f877e0 100644 --- a/python/src/Listener.cpp +++ b/python/src/Listener.cpp @@ -24,40 +24,17 @@ #include "Listener.hpp" +#include "compat.hpp" -static PyMemberDef PySfListener_members[] = { - {NULL} /* Sentinel */ -}; - - - -static void -PySfListener_dealloc(PySfListener* self) -{ - self->ob_type->tp_free((PyObject*)self); -} static PyObject * PySfListener_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfListener *self; - self = (PySfListener *)type->tp_alloc(type, 0); - - if (self != NULL) - { - } - return (PyObject *)self; } -static int -PySfListener_init(PySfListener *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - - static PyObject * PySfListener_SetGlobalVolume(PySfListener* self, PyObject *args) { @@ -75,7 +52,7 @@ static PyObject * PySfListener_SetPosition(PySfListener* self, PyObject *args) { float X, Y, Z; - if (! PyArg_ParseTuple(args, "fff", &X, &Y, &Z)) + if (!PyArg_ParseTuple(args, "fff:Listener.SetPosition", &X, &Y, &Z)) return NULL; sf::Listener::SetPosition(X, Y, Z); Py_RETURN_NONE; @@ -92,7 +69,7 @@ static PyObject * PySfListener_SetTarget(PySfListener* self, PyObject *args) { float X, Y, Z; - if (! PyArg_ParseTuple(args, "fff", &X, &Y, &Z)) + if (!PyArg_ParseTuple(args, "fff:Listener.SetTarget", &X, &Y, &Z)) return NULL; sf::Listener::SetTarget(X, Y, Z); Py_RETURN_NONE; @@ -116,12 +93,11 @@ static PyMethodDef PySfListener_methods[] = { }; PyTypeObject PySfListenerType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Listener", /*tp_name*/ sizeof(PySfListener), /*tp_basicsize*/ 0, /*tp_itemsize*/ - (destructor)PySfListener_dealloc, /*tp_dealloc*/ + 0, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -145,14 +121,14 @@ PyTypeObject PySfListenerType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfListener_methods, /* tp_methods */ - PySfListener_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 */ - (initproc)PySfListener_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfListener_new, /* tp_new */ }; diff --git a/python/src/Listener.hpp b/python/src/Listener.hpp index 6cd72504d..123956b6b 100644 --- a/python/src/Listener.hpp +++ b/python/src/Listener.hpp @@ -25,13 +25,9 @@ #ifndef __PYLISTENER_HPP #define __PYLISTENER_HPP -#include -#include - #include -#include -#include "offsetof.hpp" +#include typedef struct { diff --git a/python/src/Mouse.cpp b/python/src/Mouse.cpp index 490e7e3ad..d0165efc2 100644 --- a/python/src/Mouse.cpp +++ b/python/src/Mouse.cpp @@ -22,62 +22,26 @@ // //////////////////////////////////////////////////////////// -#include - -#include -#include - #include "Mouse.hpp" +#include "compat.hpp" -typedef struct { - PyObject_HEAD -} PySfMouse; - - - -static PyMemberDef PySfMouse_members[] = { - {NULL} /* Sentinel */ -}; - - -static void -PySfMouse_dealloc(PySfMouse *self) -{ - self->ob_type->tp_free((PyObject*)self); -} static PyObject * PySfMouse_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfMouse *self; - self = (PySfMouse *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } -static int -PySfMouse_init(PySfMouse *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - -static PyMethodDef PySfMouse_methods[] = { - {NULL} /* Sentinel */ -}; - PyTypeObject PySfMouseType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Mouse", /*tp_name*/ sizeof(PySfMouse), /*tp_basicsize*/ 0, /*tp_itemsize*/ - (destructor)PySfMouse_dealloc, /*tp_dealloc*/ + 0, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -100,15 +64,15 @@ PyTypeObject PySfMouseType = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - PySfMouse_methods, /* tp_methods */ - PySfMouse_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)PySfMouse_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfMouse_new, /* tp_new */ }; @@ -116,19 +80,19 @@ PyTypeObject PySfMouseType = { void PySfMouse_InitConst() { PyObject *obj; - obj = PyInt_FromLong(sf::Mouse::Left); + obj = PyLong_FromLong(sf::Mouse::Left); PyDict_SetItemString(PySfMouseType.tp_dict, "Left", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Mouse::Right); + obj = PyLong_FromLong(sf::Mouse::Right); PyDict_SetItemString(PySfMouseType.tp_dict, "Right", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Mouse::Middle); + obj = PyLong_FromLong(sf::Mouse::Middle); PyDict_SetItemString(PySfMouseType.tp_dict, "Middle", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Mouse::XButton1); + obj = PyLong_FromLong(sf::Mouse::XButton1); PyDict_SetItemString(PySfMouseType.tp_dict, "XButton1", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Mouse::XButton2); + obj = PyLong_FromLong(sf::Mouse::XButton2); PyDict_SetItemString(PySfMouseType.tp_dict, "XButton2", obj); Py_DECREF(obj); } diff --git a/python/src/Mouse.hpp b/python/src/Mouse.hpp index 0876840f9..3e0ca33a3 100644 --- a/python/src/Mouse.hpp +++ b/python/src/Mouse.hpp @@ -25,6 +25,14 @@ #ifndef __PYMOUSE_HPP #define __PYMOUSE_HPP +#include + +#include + +typedef struct { + PyObject_HEAD +} PySfMouse; + void PySfMouse_InitConst(); diff --git a/python/src/PostFX.cpp b/python/src/PostFX.cpp index 5962972a7..739920308 100644 --- a/python/src/PostFX.cpp +++ b/python/src/PostFX.cpp @@ -51,35 +51,22 @@ PySfPostFX_new(PyTypeObject *type, PyObject *args, PyObject *kwds) static PyObject * PySfPostFX_LoadFromFile (PySfPostFX *self, PyObject *args) { - 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); + load_from_file(self, args); } static PyObject * PySfPostFX_LoadFromMemory (PySfPostFX *self, PyObject *args) { - char *path; + char *effect; #ifdef IS_PY3K PyObject *string = PyUnicode_AsUTF8String(args); if (string == NULL) return NULL; - path = PyBytes_AsString(string); + effect = PyBytes_AsString(string); #else - path = PyString_AsString(args); + effect = PyString_AsString(args); #endif - bool result = self->obj->LoadFromMemory(path); + bool result = self->obj->LoadFromMemory(effect); #ifdef IS_PY3K Py_DECREF(string); #endif diff --git a/python/src/Shape.cpp b/python/src/Shape.cpp index 622e0f90e..fc4142cdf 100644 --- a/python/src/Shape.cpp +++ b/python/src/Shape.cpp @@ -27,33 +27,24 @@ #include #include "Color.hpp" +#include "compat.hpp" + extern PyTypeObject PySfColorType; extern PyTypeObject PySfDrawableType; -static PyMemberDef PySfShape_members[] = { - {NULL} /* Sentinel */ -}; - - static void PySfShape_dealloc(PySfShape* self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfShape_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfShape *self; - self = (PySfShape *)type->tp_alloc(type, 0); - - if (self != NULL) - { - } - return (PyObject *)self; } @@ -64,7 +55,6 @@ PySfShape_init(PySfShape *self, PyObject *args) 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) @@ -73,7 +63,7 @@ PySfShape_AddPoint(PySfShape* self, PyObject *args, PyObject *kwds) float X, Y; sf::Color *Col, *OutlineCol; PySfColor *ColTmp=NULL, *OutlineColTmp=NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "ff|O!O!", (char **)kwlist, &X, &Y, &PySfColorType, &ColTmp, &PySfColorType, &OutlineColTmp)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "ff|O!O!:Shape.AddPoint", (char **)kwlist, &X, &Y, &PySfColorType, &ColTmp, &PySfColorType, &OutlineColTmp)) return NULL; if (ColTmp) @@ -119,7 +109,7 @@ PySfShape_Line(PySfShape* self, PyObject *args, PyObject *kwds) float X0, Y0, X1, Y1, Thickness, Outline = 0.f; sf::Color *OutlineCol; PySfColor *ColTmp, *OutlineColTmp=NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "fffffO!|fO!", (char **)kwlist, &X0, &Y0, &X1, &Y1, &Thickness, &PySfColorType, &ColTmp, &Outline, &PySfColorType, &OutlineColTmp)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "fffffO!|fO!:Shape.Line", (char **)kwlist, &X0, &Y0, &X1, &Y1, &Thickness, &PySfColorType, &ColTmp, &Outline, &PySfColorType, &OutlineColTmp)) return NULL; if (OutlineColTmp) { @@ -143,7 +133,7 @@ PySfShape_Rectangle(PySfShape* self, PyObject *args, PyObject *kwds) float X0, Y0, X1, Y1, Outline = 0.f; sf::Color *OutlineCol; PySfColor *ColTmp, *OutlineColTmp=NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffffO!|fO!", (char **)kwlist, &X0, &Y0, &X1, &Y1, &PySfColorType, &ColTmp, &Outline, &PySfColorType, &OutlineColTmp)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffffO!|fO!:Shape.Rectangle", (char **)kwlist, &X0, &Y0, &X1, &Y1, &PySfColorType, &ColTmp, &Outline, &PySfColorType, &OutlineColTmp)) return NULL; if (OutlineColTmp) { @@ -167,7 +157,7 @@ PySfShape_Circle(PySfShape* self, PyObject *args, PyObject *kwds) float X, Y, Radius, Outline = 0.f; sf::Color *OutlineCol; PySfColor *ColTmp, *OutlineColTmp=NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "fffO!|fO!", (char **)kwlist, &X, &Y, &Radius, &PySfColorType, &ColTmp, &Outline, &PySfColorType, &OutlineColTmp)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "fffO!|fO!:Shape.Circle", (char **)kwlist, &X, &Y, &Radius, &PySfColorType, &ColTmp, &Outline, &PySfColorType, &OutlineColTmp)) return NULL; if (OutlineColTmp) { @@ -218,7 +208,7 @@ PySfShape_SetPointPosition(PySfShape* self, PyObject *args) { unsigned int Index; float X, Y; - if (!PyArg_ParseTuple(args, "Iff", &Index, &X, &Y)) + if (!PyArg_ParseTuple(args, "Iff:Shape.SetPointPosition", &Index, &X, &Y)) return NULL; self->obj->SetPointPosition(Index, X, Y); Py_RETURN_NONE; @@ -229,7 +219,7 @@ PySfShape_SetPointColor(PySfShape* self, PyObject *args) { unsigned int Index; PySfColor *Color; - if (!PyArg_ParseTuple(args, "IO!", &Index, &PySfColorType, &Color)) + if (!PyArg_ParseTuple(args, "IO!:Shape.SetPointColor", &Index, &PySfColorType, &Color)) return NULL; PySfColorUpdate(Color); self->obj->SetPointColor(Index, *(Color->obj)); @@ -241,7 +231,7 @@ PySfShape_SetPointOutlineColor(PySfShape* self, PyObject *args) { unsigned int Index; PySfColor *Color; - if (!PyArg_ParseTuple(args, "IO!", &Index, &PySfColorType, &Color)) + if (!PyArg_ParseTuple(args, "IO!:Shape:SetPointOutlineColor", &Index, &PySfColorType, &Color)) return NULL; PySfColorUpdate(Color); self->obj->SetPointOutlineColor(Index, *(Color->obj)); @@ -257,20 +247,14 @@ PySfShape_GetNbPoints(PySfShape* self, PyObject *args) static PyObject * PySfShape_EnableFill(PySfShape* self, PyObject *args) { - if (PyObject_IsTrue(args)) - self->obj->EnableFill(true); - else - self->obj->EnableFill(false); + self->obj->EnableFill(PyBool_AsBool(args)); Py_RETURN_NONE; } static PyObject * PySfShape_EnableOutline(PySfShape* self, PyObject *args) { - if (PyObject_IsTrue(args)) - self->obj->EnableOutline(true); - else - self->obj->EnableOutline(false); + self->obj->EnableOutline(PyBool_AsBool(args)); Py_RETURN_NONE; } @@ -346,8 +330,7 @@ Create a shape made of a single circle.\n\ PyTypeObject PySfShapeType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Shape", /*tp_name*/ sizeof(PySfShape), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -375,7 +358,7 @@ PyTypeObject PySfShapeType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfShape_methods, /* tp_methods */ - PySfShape_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ &PySfDrawableType, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/Shape.hpp b/python/src/Shape.hpp index 05bab545b..ee2d2ce1c 100644 --- a/python/src/Shape.hpp +++ b/python/src/Shape.hpp @@ -25,13 +25,9 @@ #ifndef __PYSHAPE_HPP #define __PYSHAPE_HPP -#include -#include - #include -#include -#include "offsetof.hpp" +#include typedef struct { diff --git a/python/src/Sound.cpp b/python/src/Sound.cpp index d755d4485..494d7fb43 100644 --- a/python/src/Sound.cpp +++ b/python/src/Sound.cpp @@ -23,33 +23,26 @@ //////////////////////////////////////////////////////////// #include "Sound.hpp" - #include "SoundBuffer.hpp" -extern PyTypeObject PySfSoundBufferType; +#include "compat.hpp" -static PyMemberDef PySfSound_members[] = { - {NULL} /* Sentinel */ -}; + +extern PyTypeObject PySfSoundBufferType; static void PySfSound_dealloc(PySfSound *self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfSound_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfSound *self; - self = (PySfSound *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } @@ -61,21 +54,17 @@ static PyObject* PySfSound_SetBuffer(PySfSound *self, PyObject *args) { PySfSoundBuffer *Buffer = (PySfSoundBuffer *)args; - if ( ! PyObject_TypeCheck(args, &PySfSoundBufferType)) - PyErr_SetString(PyExc_TypeError, "The argument must be a sfSoundBuffer."); + if (!PyObject_TypeCheck(args, &PySfSoundBufferType)) + PyErr_SetString(PyExc_TypeError, "Sound.SetBuffer() The argument must be a sf.SoundBuffer."); self->obj->SetBuffer(*(Buffer->obj)); - Py_RETURN_NONE; } static PyObject* PySfSound_SetLoop(PySfSound *self, PyObject *args) { - if (PyObject_IsTrue(args)) - self->obj->SetLoop(true); - else - self->obj->SetLoop(false); + self->obj->SetLoop(PyBool_AsBool(args)); Py_RETURN_NONE; } @@ -147,10 +136,7 @@ PySfSound_GetPlayingOffset(PySfSound *self) static PyObject* PySfSound_GetLoop(PySfSound *self) { - if (self->obj->GetLoop()) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(self->obj->GetLoop()); } static PyObject* @@ -184,7 +170,7 @@ static PyObject* PySfSound_SetPosition(PySfSound *self, PyObject *args) { float X, Y, Z; - if (! PyArg_ParseTuple(args, "fff", &X, &Y, &Z)) + if (!PyArg_ParseTuple(args, "fff:Sound.SetPosition", &X, &Y, &Z)) return NULL; self->obj->SetPosition(X, Y, Z); Py_RETURN_NONE; @@ -233,8 +219,7 @@ static PyMethodDef PySfSound_methods[] = { }; PyTypeObject PySfSoundType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Sound", /*tp_name*/ sizeof(PySfSound), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -272,7 +257,7 @@ Copy constructor : Sound(Copy) where Copy is a sf.Sound instance.", /* tp_doc */ 0, /* tp_iter */ 0, /* tp_iternext */ PySfSound_methods, /* tp_methods */ - PySfSound_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ @@ -286,9 +271,7 @@ Copy constructor : Sound(Copy) where Copy is a sf.Sound instance.", /* tp_doc */ static int PySfSound_init(PySfSound *self, PyObject *args, PyObject *kwds) -{ -// Sound(const SoundBuffer& Buffer, bool Loop = false, float Pitch = 1.f, float Volume = 100.f, float X = 0.f, float Y = 0.f, float Z = 0.f); - +{ const char *kwlist[] = {"Buffer", "Loop", "Pitch", "Volume", "X", "Y", "Z", NULL}; PySfSoundBuffer *Buffer=NULL; bool Loop=false; @@ -298,19 +281,17 @@ PySfSound_init(PySfSound *self, PyObject *args, PyObject *kwds) if (PyTuple_Size(args) == 1) { PySfSound *Copy; - if (PyArg_ParseTuple(args, "O!", &PySfSoundType, &Copy)) + if (PyArg_ParseTuple(args, "O!:Sound.__init__", &PySfSoundType, &Copy)) { self->obj = new sf::Sound(*(Copy->obj)); return 0; } - else - PyErr_Clear(); + else PyErr_Clear(); } if (PyTuple_Size(args) > 0) { - if ( !PyArg_ParseTupleAndKeywords(args, kwds, "O!|Offfff", (char **)kwlist, &PySfSoundBufferType, &Buffer, &LoopObj, &Pitch, &Volume, &X, &Y, &Z)) + 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; @@ -326,13 +307,13 @@ void PySfSound_InitConst() { PyObject *obj; - obj = PyInt_FromLong(sf::Sound::Stopped); + obj = PyLong_FromLong(sf::Sound::Stopped); PyDict_SetItemString(PySfSoundType.tp_dict, "Stopped", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Sound::Paused); + obj = PyLong_FromLong(sf::Sound::Paused); PyDict_SetItemString(PySfSoundType.tp_dict, "Paused", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Sound::Playing); + obj = PyLong_FromLong(sf::Sound::Playing); PyDict_SetItemString(PySfSoundType.tp_dict, "Playing", obj); Py_DECREF(obj); } diff --git a/python/src/Sound.hpp b/python/src/Sound.hpp index d0c38230a..4992a5d7f 100644 --- a/python/src/Sound.hpp +++ b/python/src/Sound.hpp @@ -25,11 +25,9 @@ #ifndef __PYSOUND_HPP #define __PYSOUND_HPP -#include -#include - #include -#include + +#include typedef struct { PyObject_HEAD diff --git a/python/src/SoundBuffer.cpp b/python/src/SoundBuffer.cpp index 2d0fd5522..e3c95238c 100644 --- a/python/src/SoundBuffer.cpp +++ b/python/src/SoundBuffer.cpp @@ -24,29 +24,21 @@ #include "SoundBuffer.hpp" - -static PyMemberDef PySfSoundBuffer_members[] = { - {NULL} /* Sentinel */ -}; +#include "compat.hpp" static void PySfSoundBuffer_dealloc(PySfSoundBuffer *self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfSoundBuffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfSoundBuffer *self; - self = (PySfSoundBuffer *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } @@ -56,11 +48,7 @@ PySfSoundBuffer_init(PySfSoundBuffer *self, PyObject *args, PyObject *kwds); static PyObject* PySfSoundBuffer_LoadFromFile(PySfSoundBuffer *self, PyObject *args) { - char *path = PyString_AsString(args); - if (self->obj->LoadFromFile(path)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + load_from_file(self, args); } static PyObject * @@ -69,13 +57,10 @@ PySfSoundBuffer_LoadFromMemory(PySfSoundBuffer* self, PyObject *args) unsigned int SizeInBytes; char *Data; - if (! PyArg_ParseTuple(args, "s#", &Data, &SizeInBytes)) + if (!PyArg_ParseTuple(args, "s#:SoundBuffer.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 * @@ -84,29 +69,26 @@ PySfSoundBuffer_LoadFromSamples(PySfSoundBuffer* self, PyObject *args) unsigned int SizeInBytes, ChannelsCount, SampleRate; char *Data; - if (! PyArg_ParseTuple(args, "s#II", &Data, &SizeInBytes, &ChannelsCount, &SampleRate)) + if (!PyArg_ParseTuple(args, "s#II:SoundBuffer.LoadFromSamples", &Data, &SizeInBytes, &ChannelsCount, &SampleRate)) return NULL; - if (self->obj->LoadFromSamples((const sf::Int16*)Data, (std::size_t) SizeInBytes/2, ChannelsCount, SampleRate)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(self->obj->LoadFromSamples((const sf::Int16*)Data, (std::size_t) SizeInBytes/2, ChannelsCount, SampleRate)); } static PyObject* PySfSoundBuffer_GetSamples(PySfSoundBuffer *self) { +#ifdef IS_PY3K + return PyBytes_FromStringAndSize((const char *)(self->obj->GetSamples()), self->obj->GetSamplesCount()*2); +#else return PyString_FromStringAndSize((const char *)(self->obj->GetSamples()), self->obj->GetSamplesCount()*2); +#endif } static PyObject* PySfSoundBuffer_SaveToFile(PySfSoundBuffer *self, PyObject *args) { - char *path = PyString_AsString(args); - if (self->obj->SaveToFile(path)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + save_to_file(self, args); } static PyObject* @@ -151,8 +133,7 @@ static PyMethodDef PySfSoundBuffer_methods[] = { }; PyTypeObject PySfSoundBufferType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "SoundBuffer", /*tp_name*/ sizeof(PySfSoundBuffer), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -182,7 +163,7 @@ Copy constructor : SoundBuffer(Copy) where Copy is a sf.SoundBuffer instance.", 0, /* tp_iter */ 0, /* tp_iternext */ PySfSoundBuffer_methods, /* tp_methods */ - PySfSoundBuffer_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ @@ -197,16 +178,18 @@ Copy constructor : SoundBuffer(Copy) where Copy is a sf.SoundBuffer instance.", static int PySfSoundBuffer_init(PySfSoundBuffer *self, PyObject *args, PyObject *kwds) { - if (PyTuple_Size(args) == 1) + int size = PyTuple_Size(args); + if (size == 1) { PySfSoundBuffer *Copy; - if (PyArg_ParseTuple(args, "O!", &PySfSoundBufferType, &Copy)) - self->obj = new sf::SoundBuffer(*(Copy->obj)); - else + if (!PyArg_ParseTuple(args, "O!:SoundBuffer.__init__", &PySfSoundBufferType, &Copy)) return -1; + self->obj = new sf::SoundBuffer(*(Copy->obj)); } - else + else if (size == 0) self->obj = new sf::SoundBuffer(); + else + PyErr_SetString(PyExc_TypeError, "SoundBuffer.__init__() takes 0 or 1 argument"); return 0; } diff --git a/python/src/SoundBuffer.hpp b/python/src/SoundBuffer.hpp index 2420bf909..4a35fb3e4 100644 --- a/python/src/SoundBuffer.hpp +++ b/python/src/SoundBuffer.hpp @@ -25,11 +25,9 @@ #ifndef __PYSOUNDBUFFER_HPP #define __PYSOUNDBUFFER_HPP -#include -#include - #include -#include + +#include typedef struct { PyObject_HEAD diff --git a/python/src/SoundBufferRecorder.cpp b/python/src/SoundBufferRecorder.cpp index 37d87e8d5..5da462ae3 100644 --- a/python/src/SoundBufferRecorder.cpp +++ b/python/src/SoundBufferRecorder.cpp @@ -23,35 +23,26 @@ //////////////////////////////////////////////////////////// #include "SoundBufferRecorder.hpp" - #include "SoundBuffer.hpp" +#include "compat.hpp" + + extern PyTypeObject PySfSoundRecorderType; -static PyMemberDef PySfSoundBufferRecorder_members[] = { - {NULL} /* Sentinel */ -}; - - static void PySfSoundBufferRecorder_dealloc(PySfSoundBufferRecorder *self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfSoundBufferRecorder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfSoundBufferRecorder *self; - self = (PySfSoundBufferRecorder *)type->tp_alloc(type, 0); - - if (self != NULL) - { - } - return (PyObject *)self; } @@ -66,7 +57,7 @@ static PyObject * PySfSoundBufferRecorder_GetBuffer(PySfSoundBufferRecorder* self) { PySfSoundBuffer *SoundBuffer = GetNewPySfSoundBuffer(); - SoundBuffer->obj = new sf::SoundBuffer( self->obj->GetBuffer() ); + SoundBuffer->obj = new sf::SoundBuffer(self->obj->GetBuffer()); return (PyObject *)SoundBuffer; } @@ -77,8 +68,7 @@ static PyMethodDef PySfSoundBufferRecorder_methods[] = { }; PyTypeObject PySfSoundBufferRecorderType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "SoundBufferRecorder", /*tp_name*/ sizeof(PySfSoundBufferRecorder), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -106,7 +96,7 @@ PyTypeObject PySfSoundBufferRecorderType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfSoundBufferRecorder_methods, /* tp_methods */ - PySfSoundBufferRecorder_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ &PySfSoundRecorderType, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/SoundBufferRecorder.hpp b/python/src/SoundBufferRecorder.hpp index 966243299..4feadbe38 100644 --- a/python/src/SoundBufferRecorder.hpp +++ b/python/src/SoundBufferRecorder.hpp @@ -25,13 +25,9 @@ #ifndef __PYSOUNDBUFFERRECORDER_HPP #define __PYSOUNDBUFFERRECORDER_HPP -#include -#include - #include -#include -#include "offsetof.hpp" +#include typedef struct { PyObject_HEAD diff --git a/python/src/SoundRecorder.cpp b/python/src/SoundRecorder.cpp index 887c0272e..e45ef1828 100644 --- a/python/src/SoundRecorder.cpp +++ b/python/src/SoundRecorder.cpp @@ -24,57 +24,58 @@ #include "SoundRecorder.hpp" - - -static PyMemberDef PySfSoundRecorder_members[] = { - {NULL} /* Sentinel */ -}; - +#include "compat.hpp" bool CustomSoundRecorder::OnStart() { + bool result = false; if (PyObject_HasAttrString(SoundRecorder, "OnStart")) - if (PyObject_IsTrue(PyObject_CallFunction(PyObject_GetAttrString(SoundRecorder, "OnStart"), NULL))) - return true; - return false; + { + PyObject *OnStart = PyObject_GetAttrString(SoundRecorder, "OnStart"); + PyObject *Result = PyObject_CallFunction(OnStart, NULL); + result = PyBool_AsBool(Result); + Py_DECREF(OnStart); + Py_DECREF(Result); + } + return result; } bool CustomSoundRecorder::OnProcessSamples(const sf::Int16* Samples, std::size_t SamplesCount) { + bool result = false; if (PyObject_HasAttrString(SoundRecorder, "OnGetData")) { - if (PyObject_IsTrue(PyObject_CallFunction(PyObject_GetAttrString(SoundRecorder, "OnGetData"), (char *)"#s", (char *)Samples, SamplesCount*2))) - { - return true; - } + PyObject *OnGetData = PyObject_GetAttrString(SoundRecorder, "OnGetData"); + PyObject *Result = PyObject_CallFunction(OnGetData, (char *)"#s", (char *)Samples, SamplesCount*2); + result = PyBool_AsBool(Result); + Py_DECREF(OnGetData); + Py_DECREF(Result); } - return false; + return result; } void CustomSoundRecorder::OnStop() { if (PyObject_HasAttrString(SoundRecorder, "OnStop")) - PyObject_CallFunction(PyObject_GetAttrString(SoundRecorder, "OnStop"), NULL); + { + PyObject *OnStop = PyObject_GetAttrString(SoundRecorder, "OnStop"); + PyObject_CallFunction(OnStop, NULL); + Py_DECREF(OnStop); + } } static void PySfSoundRecorder_dealloc(PySfSoundRecorder* self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfSoundRecorder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfSoundRecorder *self; - self = (PySfSoundRecorder *)type->tp_alloc(type, 0); - - if (self != NULL) - { - } - return (PyObject *)self; } @@ -86,11 +87,10 @@ PySfSoundRecorder_init(PySfSoundRecorder *self, PyObject *args) return 0; } - static PyObject * PySfSoundRecorder_Start(PySfSoundRecorder* self, PyObject *args) { - self->obj->Start( PyInt_AsLong(args) ); + self->obj->Start(PyLong_AsLong(args)); Py_RETURN_NONE; } @@ -104,20 +104,16 @@ PySfSoundRecorder_Stop(PySfSoundRecorder* self) static PyObject * PySfSoundRecorder_GetSampleRate(PySfSoundRecorder* self) { - return PyInt_FromLong(self->obj->GetSampleRate()); + return PyLong_FromLong(self->obj->GetSampleRate()); } static PyObject * PySfSoundRecorder_CanCapture(PySfSoundRecorder* self) { - if (sf::SoundRecorder::CanCapture()) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(sf::SoundRecorder::CanCapture()); } - static PyMethodDef PySfSoundRecorder_methods[] = { {"Start", (PyCFunction)PySfSoundRecorder_Start, METH_O, "Start(SampleRate=44100)\nStart the capture. Warning : only one capture can happen at the same time.\n SampleRate : Sound frequency (the more samples, the higher the quality) (44100 by default = CD quality)."}, {"Stop", (PyCFunction)PySfSoundRecorder_Stop, METH_NOARGS, "Stop()\nStop the capture."}, @@ -128,8 +124,7 @@ static PyMethodDef PySfSoundRecorder_methods[] = { PyTypeObject PySfSoundRecorderType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "SoundRecorder", /*tp_name*/ sizeof(PySfSoundRecorder), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -160,7 +155,7 @@ Construct the sound recorder with a callback function for processing captured sa 0, /* tp_iter */ 0, /* tp_iternext */ PySfSoundRecorder_methods, /* tp_methods */ - PySfSoundRecorder_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/SoundRecorder.hpp b/python/src/SoundRecorder.hpp index 356262d21..cc3600240 100644 --- a/python/src/SoundRecorder.hpp +++ b/python/src/SoundRecorder.hpp @@ -25,13 +25,9 @@ #ifndef __PYSOUNDRECORDER_HPP #define __PYSOUNDRECORDER_HPP -#include -#include - #include -#include -#include "offsetof.hpp" +#include class CustomSoundRecorder : public sf::SoundRecorder { diff --git a/python/src/SoundStream.cpp b/python/src/SoundStream.cpp index a52b81fc1..89b040840 100644 --- a/python/src/SoundStream.cpp +++ b/python/src/SoundStream.cpp @@ -24,32 +24,46 @@ #include "SoundStream.hpp" +#include "compat.hpp" + bool CustomSoundStream::OnStart() { + bool result = false; if (PyObject_HasAttrString(SoundStream, "OnStart")) - if (PyObject_IsTrue(PyObject_CallFunction(PyObject_GetAttrString(SoundStream, "OnStart"), NULL))) - return true; - return false; + { + PyObject *OnStart = PyObject_GetAttrString(SoundStream, "OnStart"); + PyObject *Result = PyObject_CallFunction(OnStart, NULL); + result = PyBool_AsBool(Result); + Py_DECREF(OnStart); + Py_DECREF(Result); + } + return result; } bool CustomSoundStream::OnGetData(Chunk& Data) { + bool result = false; + if (PyData != NULL) { + Py_DECREF(PyData); + } if (PyObject_HasAttrString(SoundStream, "OnGetData")) { - PyObject *PyData=NULL; + PyObject *Function = PyObject_GetAttrString(SoundStream, "OnGetData"); Data.NbSamples = 0; - if ((PyData = PyObject_CallFunction(PyObject_GetAttrString(SoundStream, "OnGetData"), NULL))) + PyData = PyObject_CallFunction(Function, NULL); + if (PyData != NULL) { if (PyArg_Parse(PyData, "s#", &(Data.Samples), &(Data.NbSamples))) { Data.NbSamples /= 2; if (Data.NbSamples > 0) - return true; + result = true; } } + Py_DECREF(Function); } - return false; + return result; } void CustomSoundStream::Init(unsigned int ChannelsCount, unsigned int SampleRate) @@ -57,16 +71,11 @@ void CustomSoundStream::Init(unsigned int ChannelsCount, unsigned int SampleRate Initialize(ChannelsCount, SampleRate); } - -static PyMemberDef PySfSoundStream_members[] = { - {NULL} /* Sentinel */ -}; - - 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; } @@ -75,7 +84,7 @@ static void PySfSoundStream_dealloc(PySfSoundStream *self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * @@ -90,7 +99,7 @@ static PyObject * PySfSoundStream_Initialize(PySfSoundStream *self, PyObject *args) { unsigned int ChannelsCount, SampleRate; - if (!PyArg_ParseTuple(args, "II", &ChannelsCount, &SampleRate)) + if (!PyArg_ParseTuple(args, "II:SoundStream.Initialize", &ChannelsCount, &SampleRate)) return NULL; self->obj->Init(ChannelsCount, SampleRate); Py_RETURN_NONE; @@ -192,7 +201,7 @@ static PyObject* PySfSoundStream_SetPosition(PySfSoundStream *self, PyObject *args) { float X, Y, Z; - if (! PyArg_ParseTuple(args, "fff", &X, &Y, &Z)) + if (!PyArg_ParseTuple(args, "fff:SoundStream.SetPosition", &X, &Y, &Z)) return NULL; self->obj->SetPosition(X, Y, Z); Py_RETURN_NONE; @@ -207,20 +216,14 @@ PySfSoundStream_GetStatus(PySfSoundStream *self) static PyObject* PySfSoundStream_SetLoop(PySfSoundStream *self, PyObject *args) { - if (PyObject_IsTrue(args)) - self->obj->SetLoop(true); - else - self->obj->SetLoop(false); + self->obj->SetLoop(PyBool_AsBool(args)); Py_RETURN_NONE; } static PyObject* PySfSoundStream_GetLoop(PySfSoundStream *self) { - if (self->obj->GetLoop()) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(self->obj->GetLoop()); } static PyObject* @@ -259,8 +262,7 @@ Set the audio stream parameters, you must call it before Play()\n\ PyTypeObject PySfSoundStreamType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "SoundStream", /*tp_name*/ sizeof(PySfSoundStream), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -291,7 +293,7 @@ or for streaming sound from the network", /* tp_doc */ 0, /* tp_iter */ 0, /* tp_iternext */ PySfSoundStream_methods, /* tp_methods */ - PySfSoundStream_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ @@ -308,13 +310,13 @@ void PySfSoundStream_InitConst() { PyObject *obj; - obj = PyInt_FromLong(sf::SoundStream::Stopped); + obj = PyLong_FromLong(sf::SoundStream::Stopped); PyDict_SetItemString(PySfSoundStreamType.tp_dict, "Stopped", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::SoundStream::Paused); + obj = PyLong_FromLong(sf::SoundStream::Paused); PyDict_SetItemString(PySfSoundStreamType.tp_dict, "Paused", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::SoundStream::Playing); + obj = PyLong_FromLong(sf::SoundStream::Playing); PyDict_SetItemString(PySfSoundStreamType.tp_dict, "Playing", obj); Py_DECREF(obj); } diff --git a/python/src/SoundStream.hpp b/python/src/SoundStream.hpp index 8c7b26f4c..6569bb035 100644 --- a/python/src/SoundStream.hpp +++ b/python/src/SoundStream.hpp @@ -26,15 +26,14 @@ #define __PYSOUNDSTREAM_HPP #include -#include #include -#include class CustomSoundStream : public sf::SoundStream { public : PyObject *SoundStream; + PyObject *PyData; virtual bool OnStart(); virtual bool OnGetData(Chunk& Data); void Init(unsigned int ChannelsCount, unsigned int SampleRate); diff --git a/python/src/String.cpp b/python/src/String.cpp index 8d2fa7f2f..09b9f8d62 100644 --- a/python/src/String.cpp +++ b/python/src/String.cpp @@ -27,36 +27,27 @@ #include "Color.hpp" #include "Rect.hpp" +#include "compat.hpp" + extern PyTypeObject PySfColorType; extern PyTypeObject PySfImageType; extern PyTypeObject PySfDrawableType; extern PyTypeObject PySfFontType; -static PyMemberDef PySfString_members[] = { - {NULL} /* Sentinel */ -}; - - static void PySfString_dealloc(PySfString *self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfString_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfString *self; - self = (PySfString *)type->tp_alloc(type, 0); - - if (self != NULL) - { - } - return (PyObject *)self; } @@ -121,7 +112,7 @@ PySfString_SetFont(PySfString* self, PyObject *args) { PySfFont *Font = (PySfFont *)args; if (!PyObject_TypeCheck(Font, &PySfFontType)) - PyErr_SetString(PyExc_ValueError, "Argument must be a sf.Font"); + PyErr_SetString(PyExc_ValueError, "String.SetFont() Argument must be a sf.Font"); self->obj->SetFont(*(Font->obj)); Py_RETURN_NONE; } @@ -155,7 +146,12 @@ PySfString_GetStyle(PySfString* self) static PyObject * PySfString_GetText(PySfString* self) { - return PyString_FromString((std::string(self->obj->GetText())).c_str()); + std::string Text = (std::string(self->obj->GetText())); +#ifdef IS_PY3K + return PyUnicode_DecodeUTF8(Text.c_str(), (Py_ssize_t)Text.length(), "replace"); +#else + return PyString_FromString(Text.c_str()); +#endif } static PyObject * @@ -206,8 +202,7 @@ Return the visual position (a tuple of two floats) of the Index-th character of }; PyTypeObject PySfStringType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "String", /*tp_name*/ sizeof(PySfString), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -236,7 +231,7 @@ Default constructor : String ()\nConstruct the string from a utf-8 or a utf-16 s 0, /* tp_iter */ 0, /* tp_iternext */ PySfString_methods, /* tp_methods */ - PySfString_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ &PySfDrawableType, /* tp_base */ 0, /* tp_dict */ @@ -253,16 +248,16 @@ Default constructor : String ()\nConstruct the string from a utf-8 or a utf-16 s void PySfString_InitConst() { PyObject *obj; - obj = PyInt_FromLong(sf::String::Regular); + obj = PyLong_FromLong(sf::String::Regular); PyDict_SetItemString(PySfStringType.tp_dict, "Regular", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::String::Bold); + obj = PyLong_FromLong(sf::String::Bold); PyDict_SetItemString(PySfStringType.tp_dict, "Bold", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::String::Italic); + obj = PyLong_FromLong(sf::String::Italic); PyDict_SetItemString(PySfStringType.tp_dict, "Italic", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::String::Underlined); + obj = PyLong_FromLong(sf::String::Underlined); PyDict_SetItemString(PySfStringType.tp_dict, "Underlined", obj); Py_DECREF(obj); } diff --git a/python/src/String.hpp b/python/src/String.hpp index 312eb4bbe..a036f8e16 100644 --- a/python/src/String.hpp +++ b/python/src/String.hpp @@ -25,11 +25,9 @@ #ifndef __PYSTRING_HPP #define __PYSTRING_HPP -#include -#include - #include -#include + +#include typedef struct { PyObject_HEAD diff --git a/python/src/VideoMode.cpp b/python/src/VideoMode.cpp index 19abe2fc8..70cefbb5c 100644 --- a/python/src/VideoMode.cpp +++ b/python/src/VideoMode.cpp @@ -22,8 +22,12 @@ // //////////////////////////////////////////////////////////// -#include "VideoMode.hpp" - +#include "VideoMode.hpp" + +#include + +#include "offsetof.hpp" +#include "compat.hpp" static PyMemberDef PySfVideoMode_members[] = { @@ -34,12 +38,11 @@ static PyMemberDef PySfVideoMode_members[] = { }; - static void PySfVideoMode_dealloc(PySfVideoMode* self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * @@ -72,7 +75,7 @@ PySfVideoMode_init(PySfVideoMode *self, PyObject *args, PyObject *kwds) { const char *kwlist[] = {"Width", "Height", "BitsPerPixel", NULL}; - if (! PyArg_ParseTupleAndKeywords(args, kwds, "II|I", (char **)kwlist, &self->Width, &self->Height, &self->BitsPerPixel)) + 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); @@ -111,7 +114,7 @@ PySfVideoMode_GetMode(PySfVideoMode* self, PyObject *args) std::size_t index; PySfVideoMode *VideoMode; - index = (std::size_t)PyInt_AsLong(args); + index = (std::size_t)PyLong_AsLong(args); VideoMode = GetNewPySfVideoMode(); VideoMode->obj = new sf::VideoMode ( sf::VideoMode::GetMode(index) ); @@ -125,7 +128,7 @@ PySfVideoMode_GetMode(PySfVideoMode* self, PyObject *args) static PyObject * PySfVideoMode_GetModesCount(PySfVideoMode* self) { - return PyInt_FromLong(sf::VideoMode::GetModesCount()); + return PyLong_FromLong(sf::VideoMode::GetModesCount()); } @@ -150,8 +153,7 @@ int PySfVideoMode_Compare(PyObject *o1, PyObject *o2) PyTypeObject PySfVideoModeType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "VideoMode", /*tp_name*/ sizeof(PySfVideoMode), /*tp_basicsize*/ 0, /*tp_itemsize*/ diff --git a/python/src/VideoMode.hpp b/python/src/VideoMode.hpp index d7a3dec6a..59e3e274e 100644 --- a/python/src/VideoMode.hpp +++ b/python/src/VideoMode.hpp @@ -25,15 +25,9 @@ #ifndef __PYVIDEOMODE_HPP #define __PYVIDEOMODE_HPP -#include -#include -#include +#include #include -#include - -#include "offsetof.hpp" - typedef struct { PyObject_HEAD diff --git a/python/src/View.cpp b/python/src/View.cpp index d2587f02f..45ffd0798 100644 --- a/python/src/View.cpp +++ b/python/src/View.cpp @@ -23,20 +23,21 @@ //////////////////////////////////////////////////////////// #include "View.hpp" +#include "Rect.hpp" + +#include "offsetof.hpp" +#include "compat.hpp" + extern PyTypeObject PySfFloatRectType; -static PyMemberDef PySfView_members[] = { - {NULL} /* Sentinel */ -}; - static void PySfView_dealloc(PySfView *self) { if (self->Owner) delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * @@ -57,7 +58,7 @@ static int PySfView_init(PySfView *self, PyObject *args, PyObject *kwds) { PySfFloatRect *Rect=NULL; - if (! PyArg_ParseTuple(args, "|O!", &PySfFloatRectType, &Rect)) + if (!PyArg_ParseTuple(args, "|O!:View.__init__", &PySfFloatRectType, &Rect)) return -1; if (Rect != NULL) @@ -98,7 +99,7 @@ static PyObject * PySfView_Move(PySfView* self, PyObject *args) { float x, y; - if ( !PyArg_ParseTuple(args, "ff", &x, &y) ) + if (!PyArg_ParseTuple(args, "ff:View.Move", &x, &y) ) return NULL; self->obj->Move(x, y); Py_RETURN_NONE; @@ -108,7 +109,7 @@ static PyObject * PySfView_SetCenter(PySfView* self, PyObject *args) { float x, y; - if ( !PyArg_ParseTuple(args, "ff", &x, &y) ) + if (!PyArg_ParseTuple(args, "ff:View.SetCenter", &x, &y) ) return NULL; self->obj->SetCenter(x, y); Py_RETURN_NONE; @@ -118,7 +119,7 @@ static PyObject * PySfView_SetHalfSize(PySfView* self, PyObject *args) { float x, y; - if ( !PyArg_ParseTuple(args, "ff", &x, &y) ) + if (!PyArg_ParseTuple(args, "ff:View.SetHalfSize", &x, &y) ) return NULL; self->obj->SetHalfSize(x, y); Py_RETURN_NONE; @@ -145,8 +146,7 @@ static PyMethodDef PySfView_methods[] = { }; PyTypeObject PySfViewType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "View", /*tp_name*/ sizeof(PySfView), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -174,7 +174,7 @@ PyTypeObject PySfViewType = { 0, /* tp_iter */ 0, /* tp_iternext */ PySfView_methods, /* tp_methods */ - PySfView_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/View.hpp b/python/src/View.hpp index af55275f9..2e8955c3a 100644 --- a/python/src/View.hpp +++ b/python/src/View.hpp @@ -25,15 +25,9 @@ #ifndef __PYVIEW_HPP #define __PYVIEW_HPP -#include -#include - -#include "Rect.hpp" - #include -#include -#include "offsetof.hpp" +#include typedef struct { PyObject_HEAD diff --git a/python/src/Window.cpp b/python/src/Window.cpp index 791012460..54da51f43 100644 --- a/python/src/Window.cpp +++ b/python/src/Window.cpp @@ -22,39 +22,38 @@ // //////////////////////////////////////////////////////////// -#include "Window.hpp" +#include "Window.hpp" + +#include "Event.hpp" +#include "VideoMode.hpp" +#include "Input.hpp" +#include "WindowSettings.hpp" -#include "SFML/Window/WindowStyle.hpp" +#include + +#include "compat.hpp" + extern PyTypeObject PySfEventType; extern PyTypeObject PySfWindowSettingsType; -extern PyTypeObject PySfVideoModeType; +extern PyTypeObject PySfVideoModeType; -static PyMemberDef PySfWindow_members[] = { - {NULL} /* Sentinel */ -}; static void PySfWindow_dealloc(PySfWindow* self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } static PyObject * PySfWindow_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfWindow *self; - self = (PySfWindow *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } - static PyObject* PySfWindow_GetEvent(PySfWindow *self, PyObject *args) @@ -63,7 +62,7 @@ PySfWindow_GetEvent(PySfWindow *self, PyObject *args) if (! PyObject_TypeCheck(PyEvent, &PySfEventType)) { - PyErr_SetString(PyExc_TypeError, "Argument is not a sfEvent"); + PyErr_SetString(PyExc_TypeError, "Window.GetEvent() Argument is not a sfEvent"); return NULL; } @@ -71,43 +70,13 @@ PySfWindow_GetEvent(PySfWindow *self, PyObject *args) { PyEvent->Type = PyEvent->obj->Type; PyEvent->Text->Unicode = PyEvent->obj->Text.Unicode; - PyEvent->Key->Code = PyEvent->obj->Key.Code; - if (PyEvent->obj->Key.Alt && PyEvent->Key->Alt == Py_False) - { - Py_DECREF(Py_False); - Py_INCREF(Py_True); - PyEvent->Key->Alt = Py_True; - } - else if (PyEvent->Key->Alt == Py_True) - { - Py_DECREF(Py_True); - Py_INCREF(Py_False); - PyEvent->Key->Alt = Py_False; - } - if (PyEvent->obj->Key.Control && PyEvent->Key->Control == Py_False) - { - Py_DECREF(Py_False); - Py_INCREF(Py_True); - PyEvent->Key->Control = Py_True; - } - else if (PyEvent->Key->Control == Py_True) - { - Py_DECREF(Py_True); - Py_INCREF(Py_False); - PyEvent->Key->Control = Py_False; - } - if (PyEvent->obj->Key.Shift && PyEvent->Key->Shift == Py_False) - { - Py_DECREF(Py_False); - Py_INCREF(Py_True); - PyEvent->Key->Shift = Py_True; - } - else if (PyEvent->Key->Shift == Py_True) - { - Py_DECREF(Py_True); - Py_INCREF(Py_False); - PyEvent->Key->Shift = Py_False; - } + PyEvent->Key->Code = PyEvent->obj->Key.Code; + Py_DECREF(PyEvent->Key->Alt); + PyEvent->Key->Alt = PyBool_FromLong(PyEvent->obj->Key.Alt); + Py_DECREF(PyEvent->Key->Control); + PyEvent->Key->Control = PyBool_FromLong(PyEvent->obj->Key.Control); + Py_DECREF(PyEvent->Key->Shift); + PyEvent->Key->Shift = PyBool_FromLong(PyEvent->obj->Key.Shift); PyEvent->MouseButton->Button = PyEvent->obj->MouseButton.Button; PyEvent->MouseButton->X = PyEvent->obj->MouseButton.X; PyEvent->MouseButton->Y = PyEvent->obj->MouseButton.Y; @@ -136,30 +105,23 @@ PySfWindow_Create(PySfWindow* self, PyObject *args, PyObject *kwds) sf::VideoMode *VideoMode; char *Title=NULL; unsigned long WindowStyle = sf::Style::Resize | sf::Style::Close; - PySfWindowSettings *ParamsTmp=NULL; - sf::WindowSettings *Params = NULL; + PySfWindowSettings *Params=NULL; const char *kwlist[] = {"VideoMode", "Title", "WindowStyle", "Params", NULL}; - if (! PyArg_ParseTupleAndKeywords(args, kwds, "O!s|IO!", (char **)kwlist, &PySfVideoModeType, &VideoModeTmp, &Title, &WindowStyle, &PySfWindowSettingsType, &ParamsTmp)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!s|IO!:Window.Create", (char **)kwlist, &PySfVideoModeType, &VideoModeTmp, &Title, &WindowStyle, &PySfWindowSettingsType, &Params)) return NULL; - if (VideoModeTmp) { - VideoMode = ((PySfVideoMode *)VideoModeTmp)->obj; - PySfVideoModeUpdate((PySfVideoMode *)VideoModeTmp); - } - else - return NULL; + VideoMode = ((PySfVideoMode *)VideoModeTmp)->obj; + PySfVideoModeUpdate((PySfVideoMode *)VideoModeTmp); - if (ParamsTmp) + if (Params) { - PySfWindowSettingsUpdate(ParamsTmp); - Params = ParamsTmp->obj; + PySfWindowSettingsUpdate(Params); + self->obj->Create(*VideoMode, Title, WindowStyle, *(Params->obj)); } - else - Params = new sf::WindowSettings(); - - self->obj->Create(*VideoMode, Title, WindowStyle, *Params); + else + self->obj->Create(*VideoMode, Title, WindowStyle); Py_RETURN_NONE; } @@ -183,10 +145,7 @@ PySfWindow_Close(PySfWindow *self) static PyObject * PySfWindow_IsOpened(PySfWindow *self) { - if (self->obj->IsOpened()) - Py_RETURN_TRUE; - else - Py_RETURN_NONE; + return PyBool_FromLong(self->obj->IsOpened()); } static PyObject * PySfWindow_GetWidth(PySfWindow *self) @@ -202,32 +161,20 @@ PySfWindow_GetHeight(PySfWindow *self) static PyObject * PySfWindow_UseVerticalSync(PySfWindow *self, PyObject *args) { - bool Enabled = false; - if (PyObject_IsTrue(args)) - Enabled = true; - self->obj->UseVerticalSync(Enabled); + self->obj->UseVerticalSync(PyBool_AsBool(args)); Py_RETURN_NONE; } static PyObject * PySfWindow_ShowMouseCursor(PySfWindow *self, PyObject *args) { - bool Show = false; - if (PyObject_IsTrue(args)) - Show = true; - self->obj->ShowMouseCursor(Show); + self->obj->ShowMouseCursor(PyBool_AsBool(args)); Py_RETURN_NONE; } static PyObject * PySfWindow_SetActive(PySfWindow *self, PyObject *args) { - bool Active = false; - if (PyObject_IsTrue(args)) - Active = true; - if (self->obj->SetActive(Active)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; + return PyBool_FromLong(self->obj->SetActive(PyBool_AsBool(args))); } static PyObject * PySfWindow_Display(PySfWindow *self) @@ -266,9 +213,8 @@ static PyObject * PySfWindow_SetPosition(PySfWindow* self, PyObject *args) { int Left=0, Top=0; - if (! PyArg_ParseTuple(args, "ii", &Left, &Top)) + if (!PyArg_ParseTuple(args, "ii:Window.SetPosition", &Left, &Top)) return NULL; - self->obj->SetPosition(Left,Top); Py_RETURN_NONE; } @@ -283,20 +229,14 @@ PySfWindow_SetFramerateLimit(PySfWindow *self, PyObject *args) static PyObject * PySfWindow_Show(PySfWindow *self, PyObject *args) { - if (PyObject_IsTrue(args)) - self->obj->Show(true); - else - self->obj->Show(false); + self->obj->Show(PyBool_AsBool(args)); Py_RETURN_NONE; } static PyObject * PySfWindow_EnableKeyRepeat(PySfWindow *self, PyObject *args) { - if (PyObject_IsTrue(args)) - self->obj->EnableKeyRepeat(true); - else - self->obj->EnableKeyRepeat(false); + self->obj->EnableKeyRepeat(PyBool_AsBool(args)); Py_RETURN_NONE; } @@ -304,9 +244,8 @@ static PyObject * PySfWindow_SetCursorPosition(PySfWindow* self, PyObject *args) { unsigned int Left=0, Top=0; - if (! PyArg_ParseTuple(args, "II", &Left, &Top)) + if (!PyArg_ParseTuple(args, "II:Window.SetCursorPosition", &Left, &Top)) return NULL; - self->obj->SetCursorPosition(Left,Top); Py_RETURN_NONE; } @@ -315,9 +254,8 @@ static PyObject * PySfWindow_SetSize(PySfWindow* self, PyObject *args) { unsigned int Width=0, Height=0; - if (! PyArg_ParseTuple(args, "II", &Width, &Height)) + if (!PyArg_ParseTuple(args, "II:Window.SetSize", &Width, &Height)) return NULL; - self->obj->SetSize(Width, Height); Py_RETURN_NONE; } @@ -335,7 +273,7 @@ PySfWindow_SetIcon(PySfWindow* self, PyObject *args) unsigned int Width, Height, Size; char *Data; - if (! PyArg_ParseTuple(args, "IIs#", &Width, &Height, &Data, &Size)) + if (! PyArg_ParseTuple(args, "IIs#:Window.SetIcon", &Width, &Height, &Data, &Size)) return NULL; self->obj->SetIcon(Width, Height, (sf::Uint8*) Data); @@ -378,8 +316,7 @@ Change the window's icon.\n\ }; PyTypeObject PySfWindowType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Window", /*tp_name*/ sizeof(PySfWindow), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -414,7 +351,7 @@ Construct a new window : sf.Window(Mode, Title, sf.Style.Resize | sf.Style.Close 0, /* tp_iter */ 0, /* tp_iternext */ PySfWindow_methods, /* tp_methods */ - PySfWindow_members, /* tp_members */ + 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ diff --git a/python/src/Window.hpp b/python/src/Window.hpp index b42084b0b..5b26b5191 100644 --- a/python/src/Window.hpp +++ b/python/src/Window.hpp @@ -25,17 +25,9 @@ #ifndef __PYWINDOW_HPP #define __PYWINDOW_HPP -#include -#include -#include - #include -#include -#include "Event.hpp" -#include "VideoMode.hpp" -#include "Input.hpp" -#include "WindowSettings.hpp" +#include typedef struct { diff --git a/python/src/WindowSettings.cpp b/python/src/WindowSettings.cpp index 807e7d847..9a6579624 100644 --- a/python/src/WindowSettings.cpp +++ b/python/src/WindowSettings.cpp @@ -24,6 +24,11 @@ #include "WindowSettings.hpp" +#include + +#include "offsetof.hpp" +#include "compat.hpp" + static PyMemberDef PySfWindowSettings_members[] = { {(char *)"DepthBits", T_UINT, offsetof(PySfWindowSettings, DepthBits), 0, (char *)"Depth buffer bits (24 by default)"}, {(char *)"StencilBits", T_UINT, offsetof(PySfWindowSettings, StencilBits), 0, (char *)"Stencil buffer bits (8 by default)"}, @@ -36,7 +41,7 @@ static void PySfWindowSettings_dealloc(PySfWindowSettings *self) { delete self->obj; - self->ob_type->tp_free((PyObject*)self); + free_object(self); } void @@ -51,16 +56,13 @@ static PyObject * PySfWindowSettings_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfWindowSettings *self; - self = (PySfWindowSettings *)type->tp_alloc(type, 0); - if (self != NULL) { self->DepthBits = 24; self->StencilBits = 8; self->AntialiasingLevel = 0; } - return (PyObject *)self; } @@ -69,21 +71,16 @@ static int PySfWindowSettings_init(PySfWindowSettings *self, PyObject *args, PyObject *kwds) { const char *kwlist[] = {"DepthBits", "StencilBits", "AntialiasingLevel", NULL}; - if (! PyArg_ParseTupleAndKeywords(args, kwds, "|III", (char **)kwlist, &(self->DepthBits), &(self->StencilBits), &(self->AntialiasingLevel))) + 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; } -static PyMethodDef PySfWindowSettings_methods[] = { - {NULL} /* Sentinel */ -}; - PyTypeObject PySfWindowSettingsType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "WindowSettings", /*tp_name*/ sizeof(PySfWindowSettings), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -110,7 +107,7 @@ PyTypeObject PySfWindowSettingsType = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - PySfWindowSettings_methods, /* tp_methods */ + 0, /* tp_methods */ PySfWindowSettings_members, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ diff --git a/python/src/WindowSettings.hpp b/python/src/WindowSettings.hpp index ac6135619..e185084e3 100644 --- a/python/src/WindowSettings.hpp +++ b/python/src/WindowSettings.hpp @@ -25,13 +25,9 @@ #ifndef __PYWINDOWSETTINGS_HPP #define __PYWINDOWSETTINGS_HPP -#include -#include - #include -#include -#include "offsetof.hpp" +#include typedef struct { diff --git a/python/src/WindowStyle.cpp b/python/src/WindowStyle.cpp index 3846f1501..03976e500 100644 --- a/python/src/WindowStyle.cpp +++ b/python/src/WindowStyle.cpp @@ -22,62 +22,25 @@ // //////////////////////////////////////////////////////////// -#include - -#include -#include - #include "WindowStyle.hpp" +#include "compat.hpp" -typedef struct { - PyObject_HEAD -} PySfStyle; - - - -static PyMemberDef PySfStyle_members[] = { - {NULL} /* Sentinel */ -}; - - -static void -PySfStyle_dealloc(PySfStyle *self) -{ - self->ob_type->tp_free((PyObject*)self); -} static PyObject * PySfStyle_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PySfStyle *self; - self = (PySfStyle *)type->tp_alloc(type, 0); - if (self != NULL) - { - } - return (PyObject *)self; } - -static int -PySfStyle_init(PySfStyle *self, PyObject *args, PyObject *kwds) -{ - return 0; -} - -static PyMethodDef PySfStyle_methods[] = { - {NULL} /* Sentinel */ -}; - PyTypeObject PySfStyleType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + head_init "Style", /*tp_name*/ sizeof(PySfStyle), /*tp_basicsize*/ 0, /*tp_itemsize*/ - (destructor)PySfStyle_dealloc, /*tp_dealloc*/ + 0, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -105,15 +68,15 @@ Fullscreen Fullscreen mode (this flag and all others are mutually exclusive).", 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - PySfStyle_methods, /* tp_methods */ - PySfStyle_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)PySfStyle_init, /* tp_init */ + 0, /* tp_init */ 0, /* tp_alloc */ PySfStyle_new, /* tp_new */ }; @@ -121,19 +84,19 @@ Fullscreen Fullscreen mode (this flag and all others are mutually exclusive).", void PySfStyle_InitConst() { PyObject *obj; - obj = PyInt_FromLong(sf::Style::None); + obj = PyLong_FromLong(sf::Style::None); PyDict_SetItemString(PySfStyleType.tp_dict, "None", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Style::Titlebar); + obj = PyLong_FromLong(sf::Style::Titlebar); PyDict_SetItemString(PySfStyleType.tp_dict, "Titlebar", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Style::Resize); + obj = PyLong_FromLong(sf::Style::Resize); PyDict_SetItemString(PySfStyleType.tp_dict, "Resize", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Style::Close); + obj = PyLong_FromLong(sf::Style::Close); PyDict_SetItemString(PySfStyleType.tp_dict, "Close", obj); Py_DECREF(obj); - obj = PyInt_FromLong(sf::Style::Fullscreen); + obj = PyLong_FromLong(sf::Style::Fullscreen); PyDict_SetItemString(PySfStyleType.tp_dict, "Fullscreen", obj); Py_DECREF(obj); } diff --git a/python/src/WindowStyle.hpp b/python/src/WindowStyle.hpp index 042c1b83a..d93516efa 100644 --- a/python/src/WindowStyle.hpp +++ b/python/src/WindowStyle.hpp @@ -25,6 +25,14 @@ #ifndef __PYWINDOWSTYLE_HPP #define __PYWINDOWSTYLE_HPP +#include + +#include + +typedef struct { + PyObject_HEAD +} PySfStyle; + void PySfStyle_InitConst(); diff --git a/python/src/compat.hpp b/python/src/compat.hpp index 173ab54a8..8439f5891 100644 --- a/python/src/compat.hpp +++ b/python/src/compat.hpp @@ -26,17 +26,44 @@ #define __PYCOMPAT_HPP #if PY_MAJOR_VERSION >= 3 + #define IS_PY3K #define head_init PyVarObject_HEAD_INIT(NULL, 0) + +#define save_to_file(self, args) \ + PyObject *string = PyUnicode_AsUTF8String(args); \ + if (string == NULL) return NULL; \ + char *path = PyBytes_AsString(string); \ + bool result = self->obj->SaveToFile(path); \ + Py_DECREF(string); \ + return PyBool_FromLong(result) + +#define load_from_file(self, args) \ + PyObject *string = PyUnicode_AsUTF8String(args); \ + if (string == NULL) return NULL; \ + char *path = PyBytes_AsString(string); \ + bool result = self->obj->LoadFromFile(path); \ + Py_DECREF(string); \ + return PyBool_FromLong(result) + #else + +#define save_to_file(self, args) \ + return PyBool_FromLong(self->obj->SaveToFile(PyString_AsString(args))) +#define load_from_file(self, args) \ + return PyBool_FromLong(self->obj->LoadFromFile(PyString_AsString(args))) + #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 6dc7d06d8..b1d5fefa1 100644 --- a/python/src/main.cpp +++ b/python/src/main.cpp @@ -69,8 +69,7 @@ extern PyTypeObject PySfGlyphType; extern PyTypeObject PySfStringType; extern PyTypeObject PySfPostFXType; -extern PyTypeObject PySfImageType; - +extern PyTypeObject PySfImageType; extern PyTypeObject PySfColorType; extern PyTypeObject PySfShapeType; @@ -96,8 +95,8 @@ static PyMethodDef module_methods[] = { #define INITERROR return NULL static PyModuleDef module_def = { PyModuleDef_HEAD_INIT, - "noddy", - "Example module that creates an extension type.", + "sf", + "Python binding for sfml (Simple Fast Media Library)", -1, module_methods, NULL, NULL, NULL, NULL };