Ubuntu 22.04.2下SDL2程序编译正常但运行时窗口不显示求助
SDL程序编译正常但窗口不显示的问题解决(Ubuntu 22.04.2)
问题描述
我在Ubuntu 22.04.2系统中按官方指南安装了SDL,VS Code已正确引入SDL2库,程序能正常编译,但运行时窗口始终不出现——终端显示程序在运行,却没有窗口弹出,也没收到任何错误信息。相关代码如下:
/*This source code copyrighted by Lazy Foo' Productions (2004-2022) and may not be redistributed without written permission.*/ //Using SDL and standard IO #include <SDL2/SDL.h> #include <stdio.h> //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; //Starts up SDL and creates window bool init(); //Loads media bool loadMedia(); //Frees media and shuts down SDL void close(); //The window we'll be rendering to SDL_Window* gWindow = NULL; //The surface contained by the window SDL_Surface* gScreenSurface = NULL; //The image we will load and show on the screen SDL_Surface* gHelloWorld = NULL; bool init() { //Initialization flag bool success = true; //Initialize SDL if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() ); success = false; } else { //Create window gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); if( gWindow == NULL ) { printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); success = false; } else { //Get window surface gScreenSurface = SDL_GetWindowSurface( gWindow ); } } return success; } bool loadMedia() { //Loading success flag bool success = true; //Load splash image gHelloWorld = SDL_LoadBMP( "hello_world.bmp" ); if( gHelloWorld == NULL ) { printf( "Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() ); success = false; } return success; } void close() { //Deallocate surface SDL_FreeSurface( gHelloWorld ); gHelloWorld = NULL; //Destroy window SDL_DestroyWindow( gWindow ); gWindow = NULL; //Quit SDL subsystems SDL_Quit(); } int main( int argc, char* args[] ) { //Start up SDL and create window if( !init() ) { printf( "Failed to initialize!\n" ); } else { //Load media if( !loadMedia() ) { printf( "Failed to load media!\n" ); } else { //Apply the image SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL ); //Update the surface SDL_UpdateWindowSurface( gWindow ); //Hack to get window to stay up SDL_Event e; bool quit = false; while( quit == false ) { while( SDL_PollEvent( &e ) ) { if( e.type == SDL_QUIT ) quit = true; } } } } //Free resources and close SDL close(); return 0; }
排查与解决办法
1. 强制指定SDL图形后端
Ubuntu桌面环境下,SDL默认后端可能存在兼容性问题,可强制使用X11后端:
- 运行程序时添加环境变量:
SDL_VIDEODRIVER=x11 ./你的程序文件名 - 或在代码中
SDL_Init前添加:
putenv("SDL_VIDEODRIVER=x11");
2. 调整窗口创建参数
尝试修改SDL_CreateWindow的参数,添加OpenGL标识并设置窗口居中,确保窗口能被正确渲染:
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL );
3. 验证图像加载路径
若hello_world.bmp路径错误,程序会进入事件循环但窗口因未更新内容而不可见:
- 确认图片放在程序运行的当前工作目录,或使用绝对路径加载
- 可在
SDL_BlitSurface后添加日志输出,验证该步骤是否执行:
printf("图像已绘制到窗口表面\n");
4. 检查终端环境变量
VS Code内置终端可能未正确继承桌面显示变量,导致窗口无法显示:
- 直接在系统终端(如GNOME Terminal)中编译运行程序
- 确认
DISPLAY变量正确,执行echo $DISPLAY,正常输出应为:0或:1
5. 优化事件循环
当前事件循环无延迟,可能导致CPU占用过高且窗口渲染不及时,添加延迟降低循环频率:
while( quit == false ) { while( SDL_PollEvent( &e ) ) { if( e.type == SDL_QUIT ) quit = true; } SDL_Delay(10); // 添加10毫秒延迟 }
内容的提问来源于stack exchange,提问作者user21569762
相关产品推荐
相关产品推荐

