我刚刚在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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句