SFML community forums

Help => Window => Topic started by: bonsairobo on April 16, 2013, 04:05:08 am

Title: Trying to wrap windowing functions in ctypes for Python; window wont stay open
Post by: bonsairobo on April 16, 2013, 04:05:08 am
Here is the library I wrote with SFML:

#include <string>
#include "SFML/Graphics.hpp"

class Window
{
public:
        sf::RenderWindow window;
        sf::Event event;

        Window(int width, int height, std::string title)
        {
                window.create(sf::VideoMode(width, height, 32), title);
        }

        void display()
        {
                window.display();
        }

        bool pollEvents()
        {
                while(window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                                return false;
                        }
                        else
                                return true;
                }
        }

        void clear()
        {
                window.clear();
        }
};

extern "C" Window* new_Window(int width, int height, char title[])
{
        return new Window(width, height, title);
}

extern "C" void display_Window(Window* window)
{
        window->display();
}

extern "C" void clear_Window(Window* window)
{
        window->clear();
}

extern "C" bool pollEvents_Window(Window* window)
{
        return window->pollEvents();
}

And here is the python wrapper:

from ctypes import *
windowLib = CDLL("./Window.so")

class Window(object):
        def __init__(self, width, height, title):
                self.obj = windowLib.new_Window(width, height, bytes(title, "ascii"))
               
        def display(self):
                windowLib.display_Window(self.obj)
               
        def clear(self):
                windowLib.clear_Window(self.obj)
               
        def pollEvents(self):
                return windowLib.pollEvents_Window(self.obj)
               
window = Window(800, 600, "Title")

windowIsOpen = True
while windowIsOpen:
        windowIsOpen = window.pollEvents()
        print(windowIsOpen)
        window.clear()
        window.display()

This python script works for opening the window, but the window closes shortly afterwords.  The print function manages to print "1 1 1 0" before the window closes itself.  I'm not sure what's causing my pollEvents function to return 0.
Title: Re: Trying to wrap windowing functions in ctypes for Python; window wont stay open
Post by: Laurent on April 16, 2013, 07:46:34 am
There's no return statement outside the while loop. So if there's no event, you return a random value (your compiler is badly configured if it missed that).

And your "else return true" is misplaced.

bool pollEvents()
{
    while(window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
        {
            window.close();
            return false;
        }
    }
    return true;
}
Title: Re: Trying to wrap windowing functions in ctypes for Python; window wont stay open
Post by: bonsairobo on April 16, 2013, 01:21:43 pm
Oh wow.  Thanks!  I guess I need to start using -Wall.