OpenGL and apps in background in iOS 5.1


#1

I’ve stumbled upon this issue here: http://stackoverflow.com/questions/10620287/opengl-es-crash-on-move-background-ios-5-1 . As far as I can see, there’s no way to currently react to the user pressing the home button to suspend/resume a JUCE application. This has become necessary when using OpenGL for rendering, as all rendering has to be stopped when the app is suspended, as described in the link. I think what should be done is to implement the applicationDidEnterBackground and applicationWillEnterForeground methods in juce_ios_windowing.mm , and then to add similar functions to the JUCEApplication class where one could start or stop OpenGL playback (unless I’ve overlooked another, already existing possibility).


#2

FWIW, I added a suspend and resume to the juce base app class. Then wired it up for iOS and Android. I needed to close and restore some network connections. I did not make the pure virtual, just virtual, so the changes wouldn’t break any other juce apps.

I’m sure Jules will get to suspend/resume eventually, but, in the mean time, the changes are pretty minor.


#3

Yeah, sorry chaps, I’ve just not got around to this yet. If anyone wants to post some suggested code that works on iOS and android, that might kick me into action :wink:


#4

Well, I haven’t researched the Android part yet, but what works on iOS is the following: add the applicationDidEnterBackground and applicationWillEnterForeground methods in the startup delegate in juce_ios_Windowing.mm like this:

[code]
@interface JuceAppStartupDelegate : NSObject
{
}

  • (void) applicationDidFinishLaunching: (UIApplication*) application;
  • (void) applicationWillTerminate: (UIApplication*) application;
  • (void)applicationDidEnterBackground:(UIApplication *)application;
  • (void)applicationWillEnterForeground:(UIApplication *)application;

@end

@implementation JuceAppStartupDelegate

  • (void) applicationDidFinishLaunching: (UIApplication*) application
    {
    initialiseJuce_GUI();
    JUCEApplication* app = dynamic_cast <JUCEApplication*> (JUCEApplicationBase::createInstance());
    if (! app->initialiseApp (String::empty))
    exit (0);
    }

  • (void) applicationWillTerminate: (UIApplication*) application
    {
    JUCEApplicationBase::appWillTerminateByForce();
    }

  • (void)applicationDidEnterBackground:(UIApplication )application
    {
    JUCEApplication
    app = JUCEApplication::getInstance();
    app->suspend();
    }

  • (void)applicationWillEnterForeground:(UIApplication )application
    {
    JUCEApplication
    app = JUCEApplicationBase::getInstance();
    app->resume();
    }[/code]

Then, in juce_Application.h you need to add the suspend and resume methods by

virtual void suspend(){};
virtual void resume(){};

As jfitzpat said, they should be virtual to ensure compatibility. Now in your actual application, add whatever needs to be done in suspend() and resume() (like, in my case, attaching and detaching the OpenGL context).


#5

Sorry, I missed the request yesterday. Here is juce_android_Windowing.cpp, juce_ApplicationBase.cpp, juce_ApplicationBase.h, juce_ios_Windowing.mm, and JuceAppActivity.java. I just merged with today’s tip. Suspend and Resume should be the only changes.

** Note: I didn’t grab all four iOS delegates like above. I intended to just generalize it as suspend/resume across all five platforms, but never got around to adding the desktops.


#6

Cool stuff, thanks chaps! I’ll check something in shortly…