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

Author Topic: Trying to wrap windowing functions in ctypes for Python; window wont stay open  (Read 2180 times)

0 Members and 1 Guest are viewing this topic.

bonsairobo

  • Newbie
  • *
  • Posts: 11
    • View Profile
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
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;
}
« Last Edit: April 16, 2013, 07:48:10 am by Laurent »
Laurent Gomila - SFML developer

bonsairobo

  • Newbie
  • *
  • Posts: 11
    • View Profile
Oh wow.  Thanks!  I guess I need to start using -Wall.

 

anything