我目前正在使用 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;
}
例如,当我收到 SDL_APP_WILLENTERBACKGROUND 时,应该在 HandleAppEvents() 或 main() 中放置什么样的代码来破坏内存或停止我的游戏循环?
假设 tex2 是可消耗的,如果应用程序收到 SDL_APP_LOWMEMORY,则可以将其删除。我如何从 HandleAppEvents() 中删除 tex2 而不会弄乱其他数据?
用户数据指针中有什么?
当我的应用程序进入后台时,我应该将我的纹理转换为表面,并将它们作为 bmps 保存在 ../tmp/ 目录中,还是当应用程序回到前台时它们仍然在内存中?
我希望我的令人困惑的问题有某种意义。如果有地方我可以找到 SDL2 的完整文档,那将是很高兴知道的。
谢谢参观!