How to use iOS SDK application events?

I am currently using the SDL2 and C library to write an application for the iPhone, and in most cases everything is going well. Unfortunately, the documentation seems rather subtle in some areas, especially for iOS-specific features. I am new to using SDL2 and this complicates the job. So far, everything worked, but I ran into one problem. SDL2 defines six types of events that will be used specifically for mobile applications. The README-ios.txt file describes them and uses them as such:

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; } 

I have a few questions about this code.

What does SDL_SetEventFilter () do? I read the Wiki Wiki page and it looked especially vague.

In practice, how does the HandleAppEvents () function work? For example, if I have a code like this:

 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; } 

What code should be placed in HandleAppEvents () or main () to destroy memory or stop my game loop when I get SDL_APP_WILLENTERBACKGROUND, for example?

Let's say tex2 is consumable and can be deleted if the application receives SDL_APP_LOWMEMORY. How can I remove tex2 from HandleAppEvents () without using other data?

What is in the userdata pointer?

When my application enters the background, do I need to convert my textures to surfaces and save them in the ../ tmp / directory as bmps, or will they still be in memory when the application returns to the foreground?

Hope my confusing questions make any sense. If there is a place where I can find detailed documentation for SDL2, it would be great to know.

Thanks for watching!

+6
source share
2 answers

SDL_SetEventFilter is a way to “speed up" the SDL event queue. Basically, you receive events as they are received before they get into the queue, and in the case of iOS, you should immediately respond to them.

The technical reason for this is that for such messages, iOS uses a series of callbacks that the SDL receives and wraps for you to make the cross platform as seamless as possible, but the fact remains: iOS still expects you to act on them. before returning from the callback.

So, for example, if we just put a message saying that the system is in the queue, the queue is in the queue, instead of transferring it directly to the application through this mechanism, and you just do nothing until this event passes through the queue , you polled it, etc., iOS may force close your application because it does not behave as expected.

When the application goes into the background, you do not need to save your textures. iOS does this for you, and if it doesn't have enough memory, it just kills your application (you never lose GL ES / ES2 contexts, which can happen on some Android devices).

The userdata pointer will contain the data that you pass as the second parameter to the SDL_SetEventFilter, so if you use SDL_SetEventFilter (HandleAppEvents, NULL), then the user data will be NULL.

In SDL_APP_WILLENTERBACKGROUND, if I remember correctly, you do not need to do anything. I have not run the iOS application for a while, but I think the SDL handles all its internal state (including blocking the event loop, and then restarts it). The events you have are SDL_APP_LOWMEMORY and SDL_APP_TERMINATING, how you deal with a particular application (delete textures, free memory, etc., This is beyond the scope of SDL)

+5
source

As you can see from this thread , you must stop the game loop on SDL_APP_WILLENTERBACKGROUND and resume it on SDL_APP_WILLENTERFOREGROUND to avoid crashes.

+1
source

Source: https://habr.com/ru/post/952942/


All Articles