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

使用SDL2的C语言程序运行时窗口重复创建问题求助

SDL2程序运行时窗口被创建两次的问题排查

编译运行一个基于SDL2的简单C语言程序时,出现窗口被创建两次的异常:先快速弹出并关闭一个窗口,随后另一个窗口弹出并停留2秒。

测试程序

#include <SDL2/SDL.h>
#include <stdio.h>

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

int main(int argc, char* args[]) {
  SDL_Window* window = NULL;
  SDL_Surface* screenSurface = NULL;
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
    return 1;
  }
  window = SDL_CreateWindow(
                "hello_sdl2",
                SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                SCREEN_WIDTH, SCREEN_HEIGHT,
                SDL_WINDOW_SHOWN
                );
  if (window == NULL) {
    fprintf(stderr, "could not create window: %s\n", SDL_GetError());
    return 1;
  }
  screenSurface = SDL_GetWindowSurface(window);
  SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
  SDL_UpdateWindowSurface(window);
  SDL_Delay(2000);
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}

编译命令

gcc -o hello_sdl2 hello_sdl2.c -lSDL2

相关版本信息

$ gcc --version
gcc (Debian 12.2.0-14) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ sdl2-config --version
2.26.5

补充信息(2024-08-27)

$ cat /etc/debian_version 
12.6
$ wmctrl -m
Name: Blackbox
Class: N/A
PID: N/A
Window manager's "showing the desktop" mode: N/A

可能的原因及解决方法

  • Blackbox窗口管理器兼容性问题:作为轻量窗口管理器,Blackbox在处理SDL_WINDOW_SHOWN标志时,可能触发额外的窗口创建/销毁流程。建议修改窗口创建标志为SDL_WINDOW_HIDDEN,再通过SDL_ShowWindow(window)显式显示窗口,避免隐式触发的异常行为。
  • SDL初始化临时窗口:SDL初始化视频子系统时,部分版本会创建临时窗口用于环境检测,Blackbox对这类临时窗口的处理不够平滑,导致视觉上出现两次窗口。通过显式控制窗口显示时机可规避该问题。
  • 编译参数不规范:手动指定-lSDL2可能缺少必要编译选项,改用sdl2-config生成的参数确保链接正确:
    gcc -o hello_sdl2 hello_sdl2.c $(sdl2-config --cflags --libs)
    

修改后的测试代码示例

#include <SDL2/SDL.h>
#include <stdio.h>

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

int main(int argc, char* args[]) {
  SDL_Window* window = NULL;
  SDL_Surface* screenSurface = NULL;
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
    return 1;
  }
  // 改为隐藏创建窗口
  window = SDL_CreateWindow(
                "hello_sdl2",
                SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                SCREEN_WIDTH, SCREEN_HEIGHT,
                SDL_WINDOW_HIDDEN
                );
  if (window == NULL) {
    fprintf(stderr, "could not create window: %s\n", SDL_GetError());
    return 1;
  }
  screenSurface = SDL_GetWindowSurface(window);
  SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
  SDL_UpdateWindowSurface(window);
  // 显式显示窗口
  SDL_ShowWindow(window);
  SDL_Delay(2000);
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}

内容的提问来源于stack exchange,提问作者August Karlstrom

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 02:59:57