【发布时间】:2020-11-08 03:51:57
【问题描述】:
#include "SDL.h"
#include "SDL_image.h"
#include <iostream>
const int width = 800, height = 600;
int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cout << "SDL couldn't initialise! Error: " << SDL_GetError() << std::endl;
}
SDL_Window* window = SDL_CreateWindow("New Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_ALLOW_HIGHDPI);
if (window == NULL)
{
std::cout << "Window couldn't be created! Error: " << SDL_GetError() << std::endl;
return EXIT_FAILURE;
}
SDL_Event windowEvent;
while (true)
{
if (SDL_PollEvent(&windowEvent))
{
if (SDL_QUIT == windowEvent.type)
{
break;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
那么在这个小程序中,窗口是如何保持打开状态的呢?我已经尝试过什么让它保持打开,发现它是这部分:
while (true)
{
if (SDL_PollEvent(&windowEvent))
{
if (SDL_QUIT == windowEvent.type)
{
break;
}
}
}
更具体地说,循环在“true”时循环。但我不明白什么是真的,也不知道它是如何循环的。有人可以解释它是如何工作的吗? 如果有人基本上逐步解释了这里的所有内容,我将不胜感激。 (例如“SDL_Window”是什么,SDL_Event 是什么等)
【问题讨论】:
-
while (true)表示“无限循环”。或者直到break被触发。 “我不明白什么是真的” 这是一个布尔值。例如,1 < 2是true。或42 == 42.