我正在使用 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;
}
我试过了
我通过更新最新版本的 WSL2 修复了错误,该版本具有内置的 Linux GUI 功能。 (真幸运。)要查看更多信息:https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps