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; }
例如,当我收到SDL_APP_WILLENTERBACKGROUND时,应该在HandleAppEvents()或main()中放置什么样的代码来销毁内存或停止我的游戏循环?
假设tex2是可消耗的,如果应用程序收到SDL_APP_LOWMEMORY,则可以将其删除.如何在不弄乱其他数据的情况下从HandleAppEvents()中删除tex2?
userdata指针中有什么?
当我的应用程序进入后台时,我应该将纹理转换为曲面,并将它们作为bmps保存在../tmp/目录中,还是当应用程序回到前台时它们仍然在内存中?
我希望我的混乱问题有某种意义.如果有一个地方我可以找到SDL2的全面文档,那就太棒了.
谢谢参观!
解决方法
这背后的技术原因是,对于这些类型的消息,iOS使用一系列回调,SDL为您提供并包装,以便尽可能无缝地实现跨平台体验,但事实仍然是iOS仍然期望您在从回调返回之前对他们采取行动.
因此,例如,如果我们只是将系统内存系统较低的消息放在队列中,而不是通过此机制将其直接传递给应用程序,并且在该事件通过队列之前您什么也不做,则轮询它等,iOS可以强制关闭您的应用程序,因为它不会按预期运行.
当应用程序转到后台时,您无需保存纹理. iOS会为你做这件事,如果它没有足够的内存来执行此操作,它只会杀死你的应用程序(你不会松开GL ES / ES2上下文,这可能会在某些Android设备上发生).
userdata指针将包含作为第二个参数传递给SDL_SetEventFilter的数据,因此如果使用SDL_SetEventFilter(HandleAppEvents,NULL),则userdata将为NULL.
在SDL_APP_WILLENTERBACKGROUND,如果我没记错的话,你不需要做任何事情.我有一段时间没有启动我的iOS应用程序,但我认为SDL自己处理其所有内部状态(包括阻止事件循环然后重新启动它).你必须自己处理的事件是SDL_APP_LOWMEMORY和SDL_APP_TERMINATING,你如何处理这是应用程序特定的(删除纹理,可用内存等,它超出了SDL专门)