You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SDL2程序无报错但无法弹出窗口问题求助

SDL2窗口在VMware Ubuntu18.04中无法显示但无报错的排查方案

我在VMware虚拟机的Ubuntu18.04系统中测试简单SDL2应用,程序运行无任何报错,但窗口始终无法显示。已尝试从源码编译SDL2,问题依旧。添加SDL_GetNumDisplayModes(0)验证显示模式,返回值为1,说明显示模式识别正常。测试代码如下:

// 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;

int main(int argc, char *args[])
{
    // The window we'll be rendering to
    SDL_Window *window = NULL;

    // The surface contained by the window
    SDL_Surface *screenSurface = NULL;

    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
    }
    else
    {
        int res = SDL_GetNumDisplayModes(0);
        printf("SDL_GetNumDisplayModes: %d\r\n", res);
        // Create window
        window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (window == NULL)
        {
            printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
        }
        else
        {
            // Get window surface
            screenSurface = SDL_GetWindowSurface(window);

            // Fill the surface white
            SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));

            // Update the surface
            SDL_UpdateWindowSurface(window);

            // 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;
                    }
                }
            }
        }
    }

    // Destroy window
    SDL_DestroyWindow(window);

    // Quit SDL subsystems
    SDL_Quit();

    return 0;
}

排查及解决步骤

  • 检查VMware显示设置
    • 开启3D图形加速:打开虚拟机设置→显示→勾选"加速3D图形",分配至少64MB显存。
    • 确保VMware Tools已安装:执行sudo apt install open-vm-tools-desktop,安装后重启虚拟机。用vmware-toolbox-cmd -v验证安装状态。
  • 强制指定SDL2视频后端
    • 运行程序时设置环境变量,强制使用X11后端:SDL_VIDEODRIVER=x11 ./your_program_name
    • 添加代码查看当前使用的视频驱动,在SDL_Init成功后插入:
      const char* driver = SDL_GetCurrentVideoDriver();
      printf("Current video driver: %s\n", driver);
      
      若输出不是x11,说明SDL未选择正确的桌面显示后端。
  • 修改窗口创建参数
    • 在SDL_CreateWindow中添加SDL_WINDOW_OPENGL标志,强制启用OpenGL上下文:
      window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
      
  • 系统环境检查
    • 确保在Ubuntu桌面环境中运行程序,避免纯终端环境执行。
    • 检查X11服务状态:systemctl status display-manager,确认lightdm/gdm服务正常运行。
  • 优化事件循环
    • 在外层while循环末尾添加延迟,避免CPU占用过高导致窗口渲染不及时:
      SDL_Delay(10);
      

内容的提问来源于stack exchange,提问作者Masoud Rahimi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 18:32:28