Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Kaoron

Pages: [1]
1
Python / Threading PySFML (Tank, remi, anyone familiar with GIL ? )
« on: April 15, 2010, 01:42:29 am »
This is a follow-up of a thread (!) in the french forum :
http://www.sfml-dev.org/forum-fr/viewtopic.php?t=2454

Basically, it says "PySFML locks all threads up".
I first investigated on how to work around this limitation on the python side, resulting in the use of sleep() inside the PySFML thread - maybe any IO function could do the trick here.

Now, I wish to make the wrapper more thread-friendly, there comes the GIL, the (in)famous Global Interpreter Lock. To let python threads execute while pysfml is doing time consuming IO stuff (like say... displaying things), the module has to release the lock so it is on it's own, then reacquire it when it wants to discuss with python again.

I made a test wrapping the Display call between python ALLOW_THREADS macros, but I don't have a very deep understanding on how the GIL works, and how extension modules can be safely threaded.

Here is the trick (in Window.cpp) :
Code: [Select]
static PyObject *
PySfWindow_Display(PySfWindow *self)
{
    Py_BEGIN_ALLOW_THREADS
    self->obj->Display();
    Py_END_ALLOW_THREADS
    Py_RETURN_NONE;
}


Pretty simple, isn't it ? Yet maybe awfully stupid regarding concurrent programming design, so that's the very rationale of this topic : let's discuss it to make sure what's the way to go.

2
Python / [PySFML/pygtk] RenderWindow from handle (patch+snippet)
« on: April 06, 2010, 03:52:52 pm »
Hi, I'm trying to integrate a RenderWindow to a Gtk widget, but it looks like it doesn't work with PySFML. I had to modify Window.cpp to get this OK.

Here is my mods to python/src/Window.cpp :

Code: [Select]
Index: src/Window.cpp
===================================================================
--- src/Window.cpp (révision 1509)
+++ src/Window.cpp (copie de travail)
@@ -144,6 +144,26 @@
  Py_RETURN_NONE;
 }

+static PyObject *
+PySfWindow_CreateFromHandle(PySfWindow* self, PyObject *args, PyObject *kwds)
+{
+    long Handle;
+    PySfContextSettings *Params=NULL;
+ if (self != NULL)
+ {
+    if (!PyArg_ParseTuple(args, "l|O!:Window.__new__", &Handle, &PySfContextSettingsType, &Params))
+        return NULL;
+    if (Params)
+    {
+    PySfContextSettingsUpdate(Params);
+    self->obj->Create((sf::WindowHandle)Handle, *(Params->obj));
+    }
+    else
+    self->obj->Create((sf::WindowHandle)Handle);
+ }
+ Py_RETURN_NONE;
+}
+
 static int
 PySfWindow_init(PySfWindow *self, PyObject *args, PyObject *kwds)
 {
@@ -155,7 +175,10 @@
  if (PyTuple_Size(args) == 0)
  return 0;
  if (PyArg_ParseTuple(args, "l|O!:Window.__new__", &Handle, &PySfContextSettingsType, &Params))
+ {
+ PySfWindow_CreateFromHandle(self, args, kwds);
  return 0;
+ }
  PyErr_Clear();
  if (PySfWindow_Create(self, args, kwds) == NULL)
  return -1;
@@ -315,6 +338,7 @@
  Title : Title of the window\n\
  WindowStyle : Window style (Resize | Close by default)\n\
  Params : Creation parameters (see default constructor for default values)"},
+ {"CreateFromHandle", (PyCFunction)PySfWindow_CreateFromHandle, METH_VARARGS, "CreateFromHandle(Handle)\nCreate a window using the given native ID"},
  {"Display", (PyCFunction)PySfWindow_Display, METH_NOARGS, "Display()\nDisplay the window on screen."},
  {"EnableKeyRepeat", (PyCFunction)PySfWindow_EnableKeyRepeat, METH_O, "EnableKeyRepeat(Enable)\nEnable or disable automatic key-repeat. Automatic key-repeat is enabled by default.\n Enabled : True to enable, false to disable"},
  {"GetEvent", (PyCFunction)PySfWindow_GetEvent, METH_O, "GetEvent(Event)\nGet the event on top of events stack, if any, and pop it. Returns True if an event was returned, False if events stack was empty.\n EventReceived : Event to fill, if any."},


and here is a pygtk+pysfml snippet :

Code: [Select]
import gtk, gobject
from PySFML import sf
class SFMLWidget(gtk.DrawingArea):
    def __init__(self):
        gtk.DrawingArea.__init__(self)
        self.connect("realize",self.realize)
        self.connect("unrealize",self.unrealize)
    def realize(self, event):
        self.set_double_buffered(False)
        self.sfml_canvas = sf.RenderWindow(self.window.xid)
    def unrealize(self,event):
        self.sfml_canvas.Close()


def main():
    window = gtk.Window()
    widget = SFMLWidget()

    window.add(widget)
    window.connect("destroy", gtk.main_quit)
    window.show_all()
    def display():
        print "Display"
        widget.sfml_canvas.Clear()
        widget.sfml_canvas.Display()
        return True
    display_tag = gobject.timeout_add(5,display)

    gtk.main()

if __name__ == "__main__":
    main()

Pages: [1]
anything