窗口似乎被创建了两次

问题描述 投票:0回答:1

当我用 C 编译并运行一个使用 SDL 库(版本 2)的简单应用程序时,窗口似乎打开了两次;首先,我看到一个窗口,它打开和关闭的速度非常快,然后窗口升起并保持不动。有什么想法可能导致这种情况吗?

这是测试程序(可在here找到):

// SDL2 Hello, World!
// This should display a white screen for 2 seconds
// compile with: clang++ main.cpp -o hello_sdl2 -lSDL2
// run with: ./hello_sdl2
#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
c sdl sdl-2
1个回答
-1
投票

尝试添加检查,以防程序运行两次

int main(int argc, char* args[]) {
    printf("Program started!\n");
    //your code..
}
© www.soinside.com 2019 - 2024. All rights reserved.