我目前正在使用SDL2库和C语言编写一个iPhone应用程序,大部分情况下都进展顺利。不幸的是,文档在某些方面似乎相当薄弱,特别是iOS专有函数。我刚开始使用SDL2,这使得事情非常困难。到目前为止,一切都很顺利,但我遇到了一个难题。SDL2定义了六种事件类型,专门用于移动应用程序。README-ios.txt文件描述了它们,并如下使用:
int HandleAppEvents(void *userdata, SDL_Event *event)
{
switch (event->type)
{
case SDL_APP_TERMINATING:
/* Terminate the app.
Shut everything down before returning from this function.
*/
return 0;
case SDL_APP_LOWMEMORY:
/* You will get this when your app is paused and iOS wants more memory.
Release as much memory as possible.
*/
return 0;
case SDL_APP_WILLENTERBACKGROUND:
/* Prepare your app to go into the background. Stop loops, etc.
This gets called when the user hits the home button, or gets a call.
*/
return 0;
case SDL_APP_DIDENTERBACKGROUND:
/* This will get called if the user accepted whatever sent your app to the background.
If the user got a phone call and canceled it, you'll instead get an SDL_APP_DIDENTERFOREGROUND event and restart your loops.
When you get this, you have 5 seconds to save all your state or the app will be terminated.
Your app is NOT active at this point.
*/
return 0;
case SDL_APP_WILLENTERFOREGROUND:
/* This call happens when your app is coming back to the foreground.
Restore all your state here.
*/
return 0;
case SDL_APP_DIDENTERFOREGROUND:
/* Restart your loops here.
Your app is interactive and getting CPU again.
*/
return 0;
default:
/* No special processing, add it to the event queue */
return 1;
}
}
int main(int argc, char *argv[])
{
SDL_SetEventFilter(HandleAppEvents, NULL);
//... run your main loop
return 0;
}
我对这段代码有几个问题。
SDL_SetEventFilter() 函数是做什么的?我阅读了 SDL Wiki 页面,但似乎解释得不够清晰。
HandleAppEvents() 函数在实践中是如何工作的?例如,如果我的代码如下:
int main(int argc, char* argv[])
{
//Initialize SDL, etc...
SDL_SetEventFilter(HandleAppEvents, NULL);
//I've got some SDL_Textures and windows and things...
SDL_Window* my_window;
SDL_Renderer* windowrend;
SDL_Texture* tex1, tex2, tex3;
//Primitive game loop
while(game_is_running){
handle_input();
do_logic();
update_screen();
}
destroy_all_my_data();
SDL_Quit();
return 0;
}
什么样的代码应该放在HandleAppEvents()或main()中,以便在收到SDL_APP_WILLENTERBACKGROUND等消息时销毁内存或停止游戏循环?
例如,如果应用程序收到SDL_APP_LOWMEMORY,则可以删除tex2。如何在HandleAppEvents()中删除tex2而不影响其他数据? userdata指针中有什么?
当我的应用程序进入后台时,我应该将纹理转换为表面,并将它们保存在../tmp/目录中作为bmp文件,还是当应用程序回到前台时它们仍然在内存中?
希望我的问题没有让您感到困惑。如果有一个地方可以找到关于SDL2的详尽文档,那就太好了。
谢谢您的查阅!