简体   繁体   中英

Shared library in Python won't open window on macOS

I am currently developing a Reinforcement Learning environment game in C++. The C++ code controls both the graphics and game logic, while I have Python bindings (imported via a shared library) controlling the flow of the game (decision making, stepping, etc). I have no problem running the environment on my Linux machine: it correctly opens a GUI display when I run the bindings in Python. However, on my macOS machine, the environment will not render. Oddly enough, when I compile the C++ code as an executable, rather than a shared library, and run it, the GUI window correctly displays the environment. I suspect there is something different in the way Python handles shared libraries on macOS.

I am using CMake as the build system, SFML as the GUI, and pybind11 for the Python bindings. Does anyone have any ideas on how I could debug this problem?

Here's some snippets of my code to illustrate my point:

C++ Bindings

#include <SFML/Graphics.hpp>                                                       
#include <pybind11/pybind11.h> 
#include <iostream>                                      
                                                                                   
namespace py = pybind11;                                                           
                                                                                   
void openWindow() {                                                                
    sf::RenderWindow window(sf::VideoMode(500, 500), "SFML");                      
    while (window.isOpen())                                                        
    {                                                                              
        // Update the window
        std::cout << 1;                    
        window.clear();                                                            
        window.display();                                                          
    }                                                                              
}                                                                                  
                                                                                   
PYBIND11_MODULE(test, handle) {                                                    
    handle.def("openWindow", &openWindow);                                         
} 

Python Code

from build.test import *                                                           
                                                                                   
def main():                                                                        
    openWindow()                                                                   
                                                                                   
if __name__ == "__main__":                                                         
    main() 

And CMakeLists.txt

make_minimum_required(VERSION 3.10)                                               
                                                                                   
project(main)                                                                      
                                                                                   
find_package(pybind11)                                                             
find_package(SFML 2.5 COMPONENTS REQUIRED graphics system)                         
                                                                                   
pybind11_add_module(test main.cpp)                                                 
target_link_libraries(test PUBLIC sfml-graphics sfml-system)

I know that the loop for rendering the window is working, rather than the program crashing completely, because 1 is outputted throughout the duration of the program's execution. However, I still don't know why the window won't display, or even how to approach debugging this problem.

Here's the solution. Your code needs to poll an SFML event, otherwise the window won't render on macOS.

Insert the following line in your code if you want it render.

sf::Event event;
window.pollEvent(event);

Once again, I'm not sure why my code worked fine without these lines on Linux, but for some reason polling an event is necessary on macOS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM