SFML/SFML

SFML on WSL creates window but doesn't draw anything in it

lucas-yotsui opened this issue · 14 comments

Subject of the issue

I've been trying to create an app for Linux with a graphical interface based on SFML. I'm currently working through WSL2 (please don't give up yet) and I'm having some issues with it. I can use many graphical interface apps on WSL2 since it is currently supported, however, I can't seem to make my SFML-based app work.

Your environment

  • Your Ubuntu 22.04 (WSL2)
  • SFML version 2.6.x
  • GCC 11.4.0

Steps to reproduce

I've been using the following code as a starting point just to see if I can get anything to compile and show up on the window. This is my current main.cpp file:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode({640, 480}), "ImGui + SFML = <3");
   
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);
    shape.setPosition(100.f, 100.f);

    sf::Clock deltaClock;
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {

            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }

        window.clear();
        window.draw(shape);
        window.display();
    }
}

To compile this project, I've been using CMake with the following CMakeLists.txt:

# ------------------------------------ Project definitions ------------------------------------ #
cmake_minimum_required(VERSION 3.24)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)

set(COMPILER_WARNINGS -Wall -Wextra -pedantic -MD -fno-exceptions)

include(FetchContent)
# --------------------------------------------------------------------------------------------- #

# ----------------------------------- Dependencies section ------------------------------------ #
set(BUILD_SHARED_LIBS OFF)

message(STATUS " --------------------------------------------------------------- ")
message(STATUS "|         Fetching sfml and adding it to the project.           |")
message(STATUS " --------------------------------------------------------------- ")
FetchContent_Declare(
    sfml
    GIT_REPOSITORY https://github.com/SFML/SFML
    GIT_TAG 2.6.x
    SYSTEM
)
set(SFML_STATIC ON)    
set(SFML_BUILD_EXAMPLES OFF)
set(SFML_BUILD_DOC OFF)
set(SFML_BUILD_NETWORK OFF)
set(SFML_BUILD_AUDIO OFF)
set(SFML_BUILD_GRAPHICS ON)
set(SFML_BUILD_WINDOW ON)
FetchContent_MakeAvailable(sfml)
list(APPEND REMOTE_LIBS 
    sfml-system
    sfml-window
    sfml-graphics
)
# --------------------------------------------------------------------------------------------- #

# --------------------------------------- Build section --------------------------------------- #
project(
  tinker-iase
  VERSION 0.1
  DESCRIPTION "Programa de Emulação do Hardware IASE"
)

# I have the SOURCES and HEADERS variables set, but I've omitted it since for this first attempt I'm only using the main.cpp file (which is included in the SOURCES variable)
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})

target_compile_options(${PROJECT_NAME} PRIVATE ${COMPILER_WARNINGS})
target_link_libraries(${PROJECT_NAME} ${REMOTE_LIBS})
# --------------------------------------------------------------------------------------------- #

Expected behavior

This was supposed to show a standing green circle, which happens when I compile this same code on Windows and run it there.

Actual behavior

When compiling this code on WSL2, the window is created but the circle is not rendered at all, the window just stays blank.

What do you get when you enter wsl --version?

Versão do WSL: 1.2.5.0
Versão do kernel: 5.15.90.1
Versão do WSLg: 1.0.51
Versão do MSRDC: 1.2.3770
Versão do Direct3D: 1.608.2-61064218
Versão do DXCore: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Versão do Windows: 10.0.22621.2428

I've had SFML rendering in WSL2 for a long while, I believe the guide for setting it up differs between Win 10 & Win 11. Have you ensured you have the steps done for Win10?

Are you talking about setting up SFML or WSL?
I'm currently using Win 11, but I do not recall seeing any configuration specific to each version when I was setting up my WSL.
As for SFML I haven't found anything recent on how to set it up for WSL, but I assumed it would work just like in regular Linux.

I followed the steps on: https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps then setup SFML using the template project. I have been able to reliably build & run my own projects on WSL2 since the setup. Just wondering if you followed this guide?

Sorry it took me so long to get back to this, I intend to use SFML for a side project and my current work has been occupying all of my time these past weeks.

I followed those steps as well, I regularly use other GUI applications through WSL and I've even managed to create a GUI application of my own with the wxWidgets library that renders just fine.

Update: It seems like the issue is related to SFML dependencies like X11. I had already previously installed all SFML dependencies I could find in the documentation and my code compiled just fine, apparently not lacking any libraries. However I stumbled upon a script someone wrote that installed all SFML dependencies and I decided to try it. For some reason it worked, I couldn't find which libraries were not installed yet but it worked. My example application rendered fine and ran perfectly.
However afterwards it stopped working again out of the blue and I still couldn't find why. I uninstalled all SFML dependencies, those in the documentation and those that the script installed and installed them again, but I can't get it to render again.
Still no idea what happened, why it worked in the first place and why it suddenly stopped.

I can confirm that I've been using WSL2 + SFML for some time now and never had any issues with it other than SFML 2.6.x needing libFLAC.so.12 for some reason. Other than that, for me, it works like any Linux machine.
My versions:
WSL version: 2.0.9.0
Kernel version: 5.15.133.1-1
WSLg version: 1.0.59
MSRDC version: 1.2.4677
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.19045.3693

What's your output on wsl -l -v? It's totally possible you've got WSL2 installed, but your distribution still runs WSL1.

Was just curious, because I've never tried X11 inside WSL so far, and in my case it works just fine with latest master:
Example output

And something I forgot earlier, could you try to install mesa-utils and run glxgears? Does it render?

What's your output on wsl -l -v? It's totally possible you've got WSL2 installed, but your distribution still runs WSL1.

  NAME            STATE           VERSION
* Ubuntu-22.04    Running         2

And something I forgot earlier, could you try to install mesa-utils and run glxgears? Does it render?

I've installed it and it runs just fine, I think my issue is related to my CMake script, probably linking something wrong I guess. I haven't had much time to work on this but I'll give it another try and see if I can make it work

Version looks good. Maybe everything is actually right, but you're unintentionally linking older builds of sorts (e.g. building static, but linking older dynamic or vice-versa)?

As so far nobody has been able to reproduce this, I'll close the issue for now. If we do find out it's a general issue, I'll be happy to re-open it again.