The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Android - Pause/Resume issue
XT95


Joined: 28 Oct 2015
Posts: 4
Hi all,

I can't find a good way to getting back to my application after pressing the home button.
I have read few (old) posts about that on the web, and I try many options..

Here is a little test app which do a black screen on my Wiko Cink Five after resuming the app :
Code:

#include <iostream>
#include <SDL.h>
#include <GLES2/gl2.h>


SDL_Window* window(0);
SDL_GLContext context(0);
bool run=true,minimize=false;

int EventFilter(void *, SDL_Event * event)
{
    //Application states
    switch (event->type)
    {
        case SDL_APP_TERMINATING:
           run = false;
           return 0;
           break;
        case SDL_QUIT:
           run = false;
           break;
        case SDL_APP_LOWMEMORY:
           return 0;
           break;
        case SDL_APP_WILLENTERBACKGROUND:
           SDL_Log("SDLEVENT : SDL_APP_WILLENTERBACKGROUND");
            minimize = true;
            glFlush();
            glFinish();
          SDL_GL_DeleteContext(context);
          return 0;
           break;
        case SDL_APP_DIDENTERBACKGROUND:
           SDL_Log("SDLEVENT : SDL_APP_DIDENTERBACKGROUND");
          return 0;
           break;
        case SDL_APP_WILLENTERFOREGROUND:
           SDL_Log("SDLEVENT : SDL_APP_WILLENTERFOREGROUND");
          return 0;
           break;
        case SDL_APP_DIDENTERFOREGROUND:
           SDL_Log("SDLEVENT : SDL_APP_DIDENTERFOREGROUND");
           context = SDL_GL_CreateContext(window);
          if(context == 0)
          {
              SDL_Log("ERROR CREATE CONTEXT : %s", SDL_GetError());
          }

           if( SDL_GL_MakeCurrent(window,context) != 0 ) //Error if I don't recreate the context.
              SDL_Log("Make current error : %s", SDL_GetError());

           minimize = false;
          return 0;
           break;
    }
   return 1;
}

int main(int argc, char **argv)
{
   SDL_Log("Main()");
    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        std::cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << std::endl;
        SDL_Quit();
       
        return -1;
    }
   
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);

    SDL_DisplayMode mode;
    SDL_GetDisplayMode(0,0,&mode);
    int width = mode.w;
    int height = mode.h;
   
    SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL,1);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
   
   
    window = SDL_CreateWindow("Laroost", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN);
   
    if(window == 0)
    {
        SDL_Log("%s", SDL_GetError());
        SDL_Quit();
       
        return -1;
    }
   
   
    context = SDL_GL_CreateContext(window);
   
    if(context == 0)
    {
        SDL_Log("ERROR CREATE CONTEXT : %s", SDL_GetError());
        SDL_DestroyWindow(window);
        SDL_Quit();
       
        return -1;
    }
   
    SDL_SetEventFilter(EventFilter, NULL);

    while(run)
    {
        //Render
        if(minimize)
           SDL_Delay(100);
        else
        {
           float t = float(SDL_GetTicks())/1000.f;
           glClearColor( cos(t)*.5+.5, sin(t)*.5+.5, 0.,1. );
           glClear(GL_COLOR_BUFFER_BIT);
           SDL_GL_SwapWindow(window);
       }
    }
   
    SDL_GL_DeleteContext(context);
    SDL_DestroyWindow(window);
    SDL_Quit();
   
    return 0;
}


Many thanks for you help !
MartinCaine


Joined: 08 Sep 2015
Posts: 4
Location: Wakefield, UK
I'd try without using SDL_SetEventFilter. It could be that you're receiving the events on another thread, and running GL calls on another thread is definitely not recommended.
XT95


Joined: 28 Oct 2015
Posts: 4
MartinCaine : You are right !! Many thanks!