【发布时间】:2020-05-25 11:07:44
【问题描述】:
我正在尝试开始使用 SDL2(将 CLion 作为我的 IDE),但我遇到了错误。我在 Pop!_OS 19.10(基于 ubuntu)
以下是相关的项目文件:
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(sdlpractice)
set(CMAKE_CXX_STANDARD 20)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(sdlpractice main.cpp)
target_link_libraries(sdlpractice ${SDL2_LIBRARIES})
Main.cpp
#include "SDL2/SDL.h"
#include "stdio.h"
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main(int argc, char* args[]) {
// The window we will be rendering to
SDL_Window * ptrWindow = NULL;
// The surface contained by the window
SDL_Surface * ptrScreenSurface = NULL;
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
} else {
// Create window
ptrWindow = SDL_CreateWindow("SDL Practice",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (ptrWindow == nullptr) {
printf("Window creation failed: %s\n", SDL_GetError());
}
// Get window surface
ptrScreenSurface = SDL_GetWindowSurface(ptrWindow);
// Fill the surface white
SDL_FillRect(ptrScreenSurface, NULL, SDL_MapRGB(ptrScreenSurface->format, 0xFF, 0xFF, 0xFF));
// Update the surface
SDL_UpdateWindowSurface(ptrWindow);
// Wait 2 seconds
SDL_Delay(2000);
// Destroy window, quit SDL subsystems
SDL_DestroyWindow(ptrWindow);
SDL_Quit();
return 0;
}
}
我收到以下错误:
SDL could not initialize! SDL_Error: No available video device
我尝试在 CLion 的运行配置中设置 DISPLAY=:0.0。相同的错误结果。而且,我跑了
echo $DISPLAY
:1
并尝试使用 :1,同样的错误仍然存在。
【问题讨论】:
-
你可以运行
DISPLAY=:1 glxgears吗?如果是这样,您可以在 CLion 之外运行已编译的sdlpractice二进制文件吗?如果是这样,您确定您正确设置了运行配置吗? -
@Botje glxgears 有效。在我的终端中运行 ./sdlpractice 会出现同样的错误。
-
此外,如果我将 SDL_VIDEODRIVER=x11 添加到环境中,我会收到错误 x11 not available。
-
您是从源代码还是使用
apt安装 SDL2?如果是第一个,你是如何配置的? -
很可能是您自己构建的那个禁用了关键视频模式,因为它缺少必需的标题。删除它(
/usr/local/bin/sdl2-config、/usr/local/include/SDL2和/usr/local/lib/libSDL2*)并使用 apt 中的版本重试。