iOS SDL 1.3 (SDL2) rotation on iOS causes screen offset.

I was having an issue with SDL on iOS (6). Whenever I rotated the screen rather than changing the orientation the rendering area seemed to stay where it was. This manifested itself as the origin appearing offset to the left of (-100 odd pixels) and below (+100 odd pixels) from the true origin.

This turned out to be an issue with the Rendering context. When the screen is rotated the UIKit SDL interface creates a new OpenGL rendering context, and this is what you should write to… However, turns out you can happily keep writing to your old context and it well be renderered, just in the wrong orientation. So this is what you have to do in your event loop:

if (event->type == SDL_WINDOWEVENT && (event->window.event == SDL_WINDOWEVENT_RESIZED) || (event->window.event == SDL_WINDOWEVENT_RESTORED)) {
    SDL_GetWindowSize(screen,&display_width,&display_height);
    SDL_DestroyRenderer(renderer);
    renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED);
    function_to_trash_all_SDL_Textures();
    SDL_StartTextInput();

    SDL_RaiseWindow(screen); // probably not required.
}

As you can see in addition to trashing the Renderer you will need to re-enable TextInput if you’re using it. All textures you’ve previously created will also be invalidated, you therefore need to delete and reinitialise them. I’m guess this means rotating the screen and be a significant overhead.

UPDATE: You also need to check for SDL_WINDOWEVENT_RESTORED this is used when the app wakes up from going into the background the renderer context can also become invalid in these situations.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>