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

Author Topic: [PySFML/pygtk] RenderWindow from handle (patch+snippet)  (Read 5722 times)

0 Members and 1 Guest are viewing this topic.

Kaoron

  • Full Member
  • ***
  • Posts: 156
    • View Profile
[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()

 

anything