【问题标题】:SDL window not showing?SDL 窗口不显示?
【发布时间】:2014-12-17 02:27:33
【问题描述】:

我有这个简单的 SDL 代码。

#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2_image/SDL_image.h>

using namespace std;

int main(int argc, char * argv[]){

if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
    return 1;
}

SDL_Window * win = SDL_CreateWindow("Window", 0, 0, 1920, 1080, SDL_WINDOW_FULLSCREEN_DESKTOP);

if(win == nullptr){
    return 1;
}


SDL_Quit();
return 0;
}

这些都没有显示窗口,我已经错误检查了所有内容。它以前可以工作,但现在它只是打开和关闭。如果有帮助,我正在 Xcode 上运行。

【问题讨论】:

  • 嗯,是的。您立即调用 SDL_Quit 并且没有主循环。
  • 是的,但它甚至没有出现在主循环中。
  • @anoyd_bi_me_grammers 所以如果你在SDL_Quat() 正上方放一个while(true){} 它不会显示窗口?
  • while(true){} 有时由于某种原因不起作用,但我让窗口出现了。

标签: c++ sdl sdl-2


【解决方案1】:

与其他状态一样,您的程序会立即终止,因此窗口应该会瞬间“闪烁”。您可以使用SDL_Delay 让窗口显示几秒钟:

SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (win == nullptr){
    std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
    SDL_Quit();
    return 1;
}

SDL_Delay(2000);

SDL_DestroyWindow(win);
SDL_Quit();

记得拨打SDL_DestroyWindow


while(true) {} 循环只会导致程序冻结。您可能需要类似以下的内容,以便它侦听事件,并且您可以在闲暇时关闭窗口。

SDL_Event e;
bool quit = false;
while (!quit){
    while (SDL_PollEvent(&e)){
        if (e.type == SDL_QUIT){
            quit = true;
        }
        if (e.type == SDL_KEYDOWN){
            quit = true;
        }
        if (e.type == SDL_MOUSEBUTTONDOWN){
            quit = true;
        }
    }
}

【讨论】:

  • 谢谢,但是为什么有时while(true){}不起作用,我把它放在SDL_Quit()之前。
  • @anoyd_bi_me_grammers 已修复。
  • 谢谢!不再有无响应和消失的窗口!
猜你喜欢
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多