diff --git a/python/src/SoundStream.cpp b/python/src/SoundStream.cpp index bc75ecb6..e89c9fed 100644 --- a/python/src/SoundStream.cpp +++ b/python/src/SoundStream.cpp @@ -29,28 +29,39 @@ bool CustomSoundStream::OnStart() { + PyGILState_STATE gstate; bool result = false; + gstate = PyGILState_Ensure(); if (PyObject_HasAttrString(SoundStream, "OnStart")) { PyObject *OnStart = PyObject_GetAttrString(SoundStream, "OnStart"); - PyObject *Result = PyObject_CallFunction(OnStart, NULL); - result = PyBool_AsBool(Result); - Py_DECREF(OnStart); - Py_DECREF(Result); + if (OnStart != NULL) + { + PyObject *Result = PyObject_CallFunction(OnStart, NULL); + if (Result != NULL) + { + result = PyBool_AsBool(Result); + Py_CLEAR(Result); + } + Py_CLEAR(OnStart); + } } if (PyErr_Occurred()) { PyErr_Print(); - return false; + result = false; } + PyGILState_Release(gstate); return result; } bool CustomSoundStream::OnGetData(Chunk& Data) { + PyGILState_STATE gstate; bool result = false; - Py_CLEAR(PyData); - PyObject *Function = PyObject_GetAttrString(SoundStream, "OnGetData"); + PyObject *Function, *PyData; + gstate = PyGILState_Ensure(); + Function = PyObject_GetAttrString(SoundStream, "OnGetData"); if (Function != NULL) { PyData = PyObject_CallFunction(Function, NULL); @@ -62,15 +73,16 @@ bool CustomSoundStream::OnGetData(Chunk& Data) if (Data.NbSamples > 0) result = true; } + Py_CLEAR(PyData); } - Py_DECREF(Function); + Py_CLEAR(Function); } if (PyErr_Occurred()) { PyErr_Print(); - Py_CLEAR(PyData); - return false; + result = false; } + PyGILState_Release(gstate); return result; } @@ -94,7 +106,6 @@ PySfSoundStream_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (self != NULL) { self->obj = new CustomSoundStream(); - self->obj->PyData = NULL; self->obj->SoundStream = (PyObject *)self; } return (PyObject *)self; diff --git a/python/src/SoundStream.hpp b/python/src/SoundStream.hpp index 6569bb03..d9e0a377 100644 --- a/python/src/SoundStream.hpp +++ b/python/src/SoundStream.hpp @@ -33,7 +33,6 @@ 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/VideoMode.cpp b/python/src/VideoMode.cpp index 5b630546..b989cd43 100644 --- a/python/src/VideoMode.cpp +++ b/python/src/VideoMode.cpp @@ -54,25 +54,16 @@ PySfVideoMode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (self != NULL) { self->BitsPerPixel = 32; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "II|I:VideoMode.__init__", (char **)kwlist, &self->Width, &self->Height, &self->BitsPerPixel)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "II|I:VideoMode.__new__", (char **)kwlist, &self->Width, &self->Height, &self->BitsPerPixel)) return NULL; self->obj = new sf::VideoMode(self->Width, self->Height, self->BitsPerPixel); } return (PyObject *)self; } -void -PySfVideoModeUpdate(PySfVideoMode *self) -{ - self->obj->Width = self->Width; - self->obj->Height = self->Height; - self->obj->BitsPerPixel = self->BitsPerPixel; -} - static PyObject * PySfVideoMode_IsValid(PySfVideoMode* self) { - PySfVideoModeUpdate(self); return PyBool_FromLong(self->obj->IsValid()); } @@ -122,16 +113,37 @@ static PyMethodDef PySfVideoMode_methods[] = { {NULL} /* Sentinel */ }; -int PySfVideoMode_Compare(PyObject *o1, PyObject *o2) +PyObject * +PySfVideoMode_richcompare(PyObject *o1, PyObject *o2, int op) { - PySfVideoModeUpdate((PySfVideoMode *)o1); - PySfVideoModeUpdate((PySfVideoMode *)o2); - if (*(((PySfVideoMode *)o1)->obj) == *(((PySfVideoMode *)o2)->obj)) - return 0; - else - return 1; + if (*(((PySfVideoMode *)o1)->obj) == *(((PySfVideoMode *)o2)->obj)) + { + if (op==Py_EQ) + Py_RETURN_TRUE; + if (op==Py_NE) + Py_RETURN_FALSE; + } + else + { + if (op==Py_EQ) + Py_RETURN_FALSE; + if (op==Py_NE) + Py_RETURN_TRUE; + } + PyErr_SetString(PyExc_TypeError, "VideoMode comparison : only == and != make sens."); + return NULL; +} + +int +PySfVideoMode_setattro(PyObject* self, PyObject *attr_name, PyObject *v) +{ + int result = PyObject_GenericSetAttr(self, attr_name, v); + PySfVideoMode *Mode = (PySfVideoMode *)self; + Mode->obj->Width = Mode->Width; + Mode->obj->Height = Mode->Height; + Mode->obj->BitsPerPixel = Mode->BitsPerPixel; + return result; } - PyTypeObject PySfVideoModeType = { head_init @@ -142,7 +154,7 @@ PyTypeObject PySfVideoModeType = { 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ - PySfVideoMode_Compare, /*tp_compare*/ + 0, /*tp_compare*/ 0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ @@ -159,7 +171,7 @@ Default constructor : VideoMode()\n\ Construct the video mode with its attributes : VideoMode(ModeWidth, ModeHeight, ModeBpp = 32)\n ModeWidth : Width in pixels\n ModeHeight : Height in pixels\n ModeBpp : Pixel depths in bits per pixel (32 by default)", /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ - 0, /* tp_richcompare */ + PySfVideoMode_richcompare, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ diff --git a/python/src/VideoMode.hpp b/python/src/VideoMode.hpp index 59e3e274..3db195e8 100644 --- a/python/src/VideoMode.hpp +++ b/python/src/VideoMode.hpp @@ -37,9 +37,6 @@ typedef struct { sf::VideoMode *obj; } PySfVideoMode; -void -PySfVideoModeUpdate(PySfVideoMode *self); - PySfVideoMode * GetNewPySfVideoMode(); diff --git a/python/src/Window.cpp b/python/src/Window.cpp index bed8e702..7f32370e 100644 --- a/python/src/Window.cpp +++ b/python/src/Window.cpp @@ -131,8 +131,7 @@ PySfWindow_Create(PySfWindow* self, PyObject *args, PyObject *kwds) if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!s|IO!:Window.Create", (char **)kwlist, &PySfVideoModeType, &VideoModeTmp, &Title, &WindowStyle, &PySfWindowSettingsType, &Params)) return NULL; - VideoMode = ((PySfVideoMode *)VideoModeTmp)->obj; - PySfVideoModeUpdate((PySfVideoMode *)VideoModeTmp); + VideoMode = ((PySfVideoMode *)VideoModeTmp)->obj; if (Params) { diff --git a/python/src/main.cpp b/python/src/main.cpp index 517b7bda..91944ffa 100644 --- a/python/src/main.cpp +++ b/python/src/main.cpp @@ -294,6 +294,8 @@ initsf(void) PySfSoundStream_InitConst(); PySfString_InitConst(); + PyEval_InitThreads(); + #ifdef IS_PY3K return m; #endif