我刚刚关注了lazyfoo的SDL教程,并运行了示例代码,如下所示:
#include <SDL2/SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main( int argc, char* args[] )
{
//The window we'll be rendering to
SDL_Window* window = NULL;
//The surface contained by the window
SDL_Surface* screenSurface = NULL;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Failed to initialise SDL! 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( "Failed to create window! 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, 255, 0, 0 ) );
//Update the surface
SDL_UpdateWindowSurface( window );
//Wait two seconds
SDL_Delay( 2000 );
}
}
//Destroy window
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}
但由于某些原因,没有显示真正的边框或标题栏,它只显示一个白色的屏幕。我尝试使用SDL_SetWindowBordered但它没有做任何事情。接下来我将背景颜色设置为红色,从这个图像中你可以看到有一个标题栏但是没有关闭或最小化按钮.有谁知道为什么会这样。它只是我还是mac的问题?
由于摆脱SDL_Delay
似乎有所帮助,我将尝试详细说明。如果我们看一下SDL_Delay
的代码,我们可以看到它基本上做了两件事:
nanosleep()
,它会睡一段时间;while
循环中,检查每次迭代已经过了多少时间,并且在足够的时间过去之后break
ing离开循环。现在,我必须说我从未亲自为osx编码,所以我不知道它是如何绘制窗口的。但是我可以假设,在操作系统设法绘制窗口的标题之前,代码中的SDL_Delay
会被调用(并且有效地阻止它被调用的线程),并且在延迟完成之后你会立即自行销毁窗口,因此标头从未正确绘制。
我知道答案已经解决,但对于想要一个简单解决方案的人来说。
例:
SDL_Delay(4000);
会变成
for(int i = 0; i < 4000; i++){
SDL_PumpEvents();
SDL_Delay(1);
}
实际上这根本与SDL_Delay()无关。
我测试了它,似乎在OSX上标题栏仅在每次轮询或抽取事件时更新。
这意味着SDL_Delay()会阻止标题栏渲染过程,如果它阻止您抽取事件。
要解决此问题,请每隔毫秒左右调用SDL_PumpEvents():
for(int i = 0; i < time_to_sleep; i++){
SDL_PumpEvents();
SDL_Delay(1);
}