c++ - 为什么mac os sdl2程序窗口没有响应?
问题描述
我刚刚在我的 mac 上设置了 SDL2 框架,但是编译和运行程序成功,窗口没有响应(我复制了创建一些矩形的代码)。
我使用 xcode 并 一步一步地从这里http://lazyfoo.net/tutorials/SDL/01_hello_SDL/mac/xcode/index.php学习教程。
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
{
//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 );
cout << " Ok" << endl;
//Wait two seconds
SDL_Delay( 20000 );
}
}
//Destroy window
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
为什么会出现这个问题?先感谢您
解决方案
为了让 SDL 编写的程序“响应”操作系统,您应该将控制权交还给 SDL,以便它处理系统消息并将它们作为 SDL 事件(鼠标事件、键盘事件等)交还给您。
为此,您必须添加一个使用 的循环SDL_PollEvent
,它应该看起来像
while(true)
{
SDL_Event e;
while (SDL_PollEvent(&e))
{
// Decide what to do with events here
}
// Put the code that is executed every "frame".
// Under "frame" I mean any logic that is run every time there is no app events to process
}
有一些特殊事件,例如SDL_QuiEvent
您需要处理这些事件才能有办法关闭您的应用程序。如果你想处理它,你应该修改你的代码看起来像这样:
while(true)
{
SDL_Event e;
while (SDL_PollEvent(&e))
{
if(e.type == SDL_QUIT)
{
break;
}
// Handle events
}
// "Frame" logic
}
推荐阅读
- python - 查找自定义python模块的源路径
- python - 如何在 Heroku 项目中获得自己的 Pytesseract 字体?
- amazon-web-services - 如何处理两个 Amazon S3 存储桶的 CORS 问题
- google-chrome-extension - 我如何在没有 devtools-network 的情况下获得 m3u8 地址?
- android - 当滚动到达第一个位置顶部时,Recycleview 加载更多数据
- html - 如何使用不同的 IONIC CSS 边距和填充
- python - 遇到 ParseError: not well-formed (invalid token): 将 XML 转换为 DataFrame 时出现问题
- java - 当调用多个静态方法时,使用 Mockito 验证静态方法调用
- azure - 从 Azure FHIR 服务请求 FHIR 资源时出现身份验证错误
- c# - 如何在解决方案中获取特定文件夹的路径