SDL2 未在 wsl2 中显示窗口

问题描述 投票:0回答:1

我正在使用 wsl2 Debian。在考虑安装 245 MB 的 libsdl2-dev apt install 后,我尝试了

g++ sdl.cpp -lSDL2 -o sdlbasic
并得到了这一大串文本,所有这些似乎都是一个组合错误。然后我重新安装了它,它居然编译了程序。但是当我运行 ./sdlbasic 时,我得到了这个错误:

error: XDG_RUNTIME_DIR not set in the environment.
The path /dev/dri/ cannot be opened or is not available
The path /dev/dri/ cannot be opened or is not available
error: XDG_RUNTIME_DIR not set in the environment.
The path /dev/dri/ cannot be opened or is not available
The path /dev/dri/ cannot be opened or is not available

我也遇到了延迟,尽管有错误,但实际上还是延迟了 10 秒。这是我的初学者代码,用于在屏幕中间绘制白色像素:

// g++ sdl.cpp -lSDL2 -o sdlbasic

#include <SDL2/SDL.h>
#define swidth 640
#define sheight 480

int main(){
    SDL_Window* window = nullptr;
    SDL_Renderer* renderer = nullptr;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_CreateWindowAndRenderer(swidth, sheight, 0, &window, &renderer); // width and height
    SDL_RenderSetScale(renderer, 4, 4); // x4 render size

    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // choose color to draw, rgba
    SDL_RenderClear(renderer);

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // white
    SDL_RenderDrawPoint(renderer, swidth/2, sheight/2); // draw pixel in center

    SDL_RenderPresent(renderer); // show renderer stuff
    SDL_Delay(10000); // delay until close
    return 0;
}

我试过了

  1. 重新编译程序
  2. 遵循这个堆栈交换教程,但我不知道 Nautilus 是什么 https://askubuntu.com/questions/456689/error-xdg-runtime-dir-not-set-in-the-environment-when-attempting-to-run-naut
c++ sdl-2 wsl-2
1个回答
0
投票

我通过更新最新版本的 WSL2 修复了错误,该版本具有内置的 Linux GUI 功能。 (真幸运。)要查看更多信息:https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

© www.soinside.com 2019 - 2024. All rights reserved.