# SDL 2.0 OpenGL ES 1.1, NDK, home bt losing the draw screen

**URL:** <https://discourse.libsdl.org/t/sdl-2-0-opengl-es-1-1-ndk-home-bt-losing-the-draw-screen/19584>\
**Category:** SDL Development\
**Created:** [May 9, 2013, 2:20am UTC](https://discourse.libsdl.org/t/sdl-2-0-opengl-es-1-1-ndk-home-bt-losing-the-draw-screen/19584 "2013-05-09T02:20:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Timodor](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/c0e974/32.png) [@Timodor](https://discourse.libsdl.org/u/Timodor)\
**Post date:** [May 9, 2013, 2:20am UTC](https://discourse.libsdl.org/t/sdl-2-0-opengl-es-1-1-ndk-home-bt-losing-the-draw-screen/19584/1 "2013-05-09T02:20:14Z")

</div>

Hey Dudes,

I’ve now decided to move onto SDL 2.0 and scrapping SDL 1.2 because of On-pause Issues and Asset-Management issues.  
Since I’m doing everything in OpenGL, all I need to do is get and set the resolution and do the events, which shouldn’t be much work.  
I’m hoping at the end of this assets are working fine and onPause works perfectly. Which would mean I would have 100% working base to develop.

I just got my first OPENGL window to draw on SDL 2.0!

Seems like you need this for it to draw:

// TO CREATE SCREEN  
SDL\_Window \*win;  
win = SDL\_CreateWindow(“Hello World!”, 100, 100, 480, 800, SDL\_WINDOW\_OPENGL);

// NEEDED TO DRAW OPENGL  
SDL\_GLContext glcontext = SDL\_GL\_CreateContext(win);

//LIKE THE OLD SWAP BUUFERS FUNCTION  
SDL\_GL\_SwapWindow(win);

It’s drawing! Which is good.  
But when I press the home button on the phone Android 4.0 and then go back to it, the screen is lost. It’s all BLACK. It’s like it has no SDL\_GLContext;

I need to figure this out, then move onto events to make sure they also don’t get lost. Anyone have any idea why it’s going back?

Code is below.

#include \<SDL.h\>  
#include \<stdlib.h\>  
#include \<stdio.h\>  
#include \<time.h\>  
#include \<GLES/gl.h\>

int main(int argc, char\*\* argv)  
{  
if (SDL\_Init(SDL\_INIT\_EVERYTHING) == -1)  
{  
}

```
int SCREEN_WIDTH = 480;
int SCREEN_HEIGHT = 800;

unsigned int iTexture[1];

SDL_Window *win;
win = SDL_CreateWindow("Hello World!", 100, 100, 480, 800, SDL_WINDOW_OPENGL);

SDL_GLContext glcontext = SDL_GL_CreateContext(win);

glViewport( 0.f, 0.f, SCREEN_WIDTH, SCREEN_HEIGHT );
//Initialize Projection Matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrthof( 0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, 0.0, 1.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

float fXpos = 200;
float fYpos = 200;

GLfloat box[] = {
   0, 0,
   0, 64,
   64, 0,
   64, 64};

GLfloat tex[] = {0, 0,
				 0, 1,
				 1, 0,
				 1, 1};

		bool bGame = true;

		int i = 0;

		//glGenTextures(1, &iTexture[0]); // generate texture object
		//glBindTexture(GL_TEXTURE_2D, iTexture[0]); // enable our texture object
		//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, player->pixels);

		int iDeg;

		while (bGame == true)
		{
			box[0] = fXpos - 64;
			box[1] = fYpos - 64;
			box[2] = fXpos - 64;
			box[3] = fYpos + 64;
			box[4] = fXpos + 64;
			box[5] = fYpos - 64;
			box[6] = fXpos + 64;
			box[7] = fYpos + 64;

			glLoadIdentity();

			glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

			glEnableClientState(GL_VERTEX_ARRAY);
			glVertexPointer(2, GL_FLOAT, 0, box);

			glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

			glDisableClientState(GL_VERTEX_ARRAY);

			SDL_GL_SwapWindow(win);
			}

```

}

---

<div class="post-metadata">

**Author:** ![Jared\_Maddox](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@Jared\_Maddox](https://discourse.libsdl.org/u/Jared_Maddox)\
**Post date:** [May 9, 2013, 5:57pm UTC](https://discourse.libsdl.org/t/sdl-2-0-opengl-es-1-1-ndk-home-bt-losing-the-draw-screen/19584/2 "2013-05-09T17:57:05Z")

</div>

> screen  
> Message-ID: \<1368066013.m2f.37020 at [forums.libsdl.org](http://forums.libsdl.org)\>  
> Content-Type: text/plain; charset=“iso-8859-1”
> 
> Hey Dudes,
> 
> I’ve now decided to move onto SDL 2.0 and scrapping SDL 1.2 because of  
> On-pause Issues and Asset-Management issues.  
> Since I’m doing everything in OpenGL, all I need to do is get and set the  
> resolution and do the events, which shouldn’t be much work.  
> I’m hoping at the end of this assets are working fine and onPause works  
> perfectly. Which would mean I would have 100% working base to develop.
> 
> I just got my first OPENGL window to draw on SDL 2.0!
> 
> Seems like you need this for it to draw:
> 
> // TO CREATE SCREEN  
> SDL\_Window \*win;  
> win = SDL\_CreateWindow(“Hello World!”, 100, 100, 480, 800,  
> SDL\_WINDOW\_OPENGL);
> 
> // NEEDED TO DRAW OPENGL  
> SDL\_GLContext glcontext = SDL\_GL\_CreateContext(win);
> 
> //LIKE THE OLD SWAP BUUFERS FUNCTION  
> SDL\_GL\_SwapWindow(win);
> 
> It’s drawing! Which is good.  
> But when I press the home button on the phone Android 4.0 and then go back  
> to it, the screen is lost. It’s all BLACK. It’s like it has no  
> SDL\_GLContext;
> 
> I need to figure this out, then move onto events to make sure they also  
> don’t get lost. Anyone have any idea why it’s going back?
> 
> Code is below.

Some platforms destroy the OpenGL context (or it’s textures, or…)  
when the current window loses focus. I don’t know that Android is one  
of these, but I think it might be, and I know that this specifically  
happens on some of the smart-phone OSes. The solution is to keep an  
eye out for certain events, and when they happen, set a flag so that  
the program will know to reinitialize part or all of it’s OpenGL  
configuration the next time that it makes a render pass. As a result,  
I’d suggest starting on events, and just stick a note somewhere in the  
event loop to remind yourself of this later on.\> Date: Wed, 08 May 2013 19:20:14 -0700

> From: “Timodor”   
> To: sdl at [lists.libsdl.org](http://lists.libsdl.org)  
> Subject: [SDL] SDL 2.0 OpenGL ES 1.1, NDK, home bt losing the draw

---

<div class="post-metadata">

**Author:** ![Timodor](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/c0e974/32.png) [@Timodor](https://discourse.libsdl.org/u/Timodor)\
**Post date:** [May 9, 2013, 10:54pm UTC](https://discourse.libsdl.org/t/sdl-2-0-opengl-es-1-1-ndk-home-bt-losing-the-draw-screen/19584/3 "2013-05-09T22:54:33Z")

</div>

I was trying that last night with the event, but how does one know the application is on-pause? How does the event determine that?

I also think I’m using an old version base version SDL 1.2 and SDL 2.0, maybe that’s why on pause is also not working.

I’m not sure what is supposed recreated, which parts of OpenGL need to be reloaded.

---

<div class="post-metadata">

**Author:** ![Jared\_Maddox](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@Jared\_Maddox](https://discourse.libsdl.org/u/Jared_Maddox)\
**Post date:** [May 10, 2013, 6:04pm UTC](https://discourse.libsdl.org/t/sdl-2-0-opengl-es-1-1-ndk-home-bt-losing-the-draw-screen/19584/4 "2013-05-10T18:04:53Z")

</div>

> screen  
> Message-ID: \<1368140072.m2f.37024 at [forums.libsdl.org](http://forums.libsdl.org)\>  
> Content-Type: text/plain; charset=“iso-8859-1”
> 
> I was trying that last night with the event, but how does one know the  
> application is on-pause? How does the event determine that?
> 
> I also think I’m using an old version base version SDL 1.2 and SDL 2.0,  
> maybe that’s why on pause is also not working.
> 
> I’m not sure what is supposed recreated, which parts of OpenGL need to be  
> reloaded.

I believe this is all buried in the mailing list archives. I’ll try to  
look it up later, but I don’t have time at the moment.\> Date: Thu, 09 May 2013 15:54:33 -0700

> From: “Timodor”   
> To: sdl at [lists.libsdl.org](http://lists.libsdl.org)  
> Subject: Re: [SDL] SDL 2.0 OpenGL ES 1.1, NDK, home bt losing the draw

---

<div class="post-metadata">

**Author:** ![Jared\_Maddox](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@Jared\_Maddox](https://discourse.libsdl.org/u/Jared_Maddox)\
**Post date:** [May 11, 2013, 8:51am UTC](https://discourse.libsdl.org/t/sdl-2-0-opengl-es-1-1-ndk-home-bt-losing-the-draw-screen/19584/5 "2013-05-11T08:51:56Z")

</div>

> screen  
> Message-ID:  
> \<CABXS6\_m\_5nfLeaRX2=Uax-w7jZ\_8ZCDxBdEAHzE7Acd4xocGzg at [mail.gmail.com](http://mail.gmail.com)\>  
> Content-Type: text/plain; charset=ISO-8859-1
> 
> > screen  
> > Message-ID: \<1368140072.m2f.37024 at [forums.libsdl.org](http://forums.libsdl.org)\>  
> > Content-Type: text/plain; charset=“iso-8859-1”
> > 
> > I was trying that last night with the event, but how does one know the  
> > application is on-pause? How does the event determine that?
> > 
> > I also think I’m using an old version base version SDL 1.2 and SDL 2.0,  
> > maybe that’s why on pause is also not working.
> > 
> > I’m not sure what is supposed recreated, which parts of OpenGL need to be  
> > reloaded.
> 
> I believe this is all buried in the mailing list archives. I’ll try to  
> look it up later, but I don’t have time at the moment.

Not certain that I’ve found the right stuff, but I didn’t want to  
leave this TOO long. It’s mostly my hope that this will give you the  
references that you need to find the info you actually want.

Firstly, make certain that you understand the “Pause / Resume  
behaviour” portion of README.android … unfortunately, I don’t know  
whether it’s fully up to date. Why isn’t anyone that currently  
compiles for Android chiming in on the events/needed actions?

Next, a set of mailing list archive messages. Note that some iOS  
support is relevant here. I’ve basically tried to just grab all of the  
relevant conversations, instead of specific posts, so you’ll want to  
browse a little.  
[http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-April/084358.html](http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-April/084358.html)  
[http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-April/084369.html](http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-April/084369.html)  
[http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-April/084445.html](http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-April/084445.html)  
[http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-May/084743.html](http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-May/084743.html)  
[http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-June/084935.html](http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-June/084935.html)  
[http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-July/084998.html](http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-July/084998.html)  
[http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-August/085378.html](http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-August/085378.html)  
[http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-August/085380.html](http://lists.libsdl.org/pipermail/sdl-libsdl.org/2012-August/085380.html)

Finally, two forum posts:  
[http://forums.libsdl.org/viewtopic.php?t=7926](http://forums.libsdl.org/viewtopic.php?t=7926)  
[http://forums.libsdl.org/viewtopic.php?t=7990&sid=85572573f88be2948c3f1b55be16e519](http://forums.libsdl.org/viewtopic.php?t=7990&sid=85572573f88be2948c3f1b55be16e519)

In all of that, I noticed that SDL2 is supposed to deal with this  
stuff itself, but that on some versions of Android that apparently  
doesn’t actually work. Did you mention your Android version? Have you  
checked inside the Android files for SDL2 to see what Android-specific  
events and window messages are specified? What events are you actually  
getting leading up to, and following, an application pause? The more  
information you can provide, the more likely we are to figure out the  
problem.\> Date: Fri, 10 May 2013 13:04:53 -0500

> From: Jared Maddox \<@Jared\_Maddox\>  
> To: sdl at [lists.libsdl.org](http://lists.libsdl.org)  
> Subject: Re: [SDL] SDL 2.0 OpenGL ES 1.1, NDK, home bt losing the draw
> 
> > Date: Thu, 09 May 2013 15:54:33 -0700  
> > From: “Timodor”   
> > To: sdl at [lists.libsdl.org](http://lists.libsdl.org)  
> > Subject: Re: [SDL] SDL 2.0 OpenGL ES 1.1, NDK, home bt losing the draw

---

<div class="post-metadata">

**Author:** ![Timodor](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/c0e974/32.png) [@Timodor](https://discourse.libsdl.org/u/Timodor)\
**Post date:** [May 11, 2013, 11:53am UTC](https://discourse.libsdl.org/t/sdl-2-0-opengl-es-1-1-ndk-home-bt-losing-the-draw-screen/19584/6 "2013-05-11T11:53:02Z")

</div>

Hey Jared

Thanks for taking the time to look up that information for me.

But you know what I finally fixed it all. I suspect I was using an old version of SDL 2.0 which id downloaded from internet (which was android eclipse project) before the on pause was fixed.  
Not only that assets now load from APK!!!, on pause works totally fine now. goes in and out.  
The game is still running, but still there must be some type of event issue to pause the game, if no events with in 5 seconds pause game or something.  
I downloaded latest SDL 2.0 Source today and complied them. I’ve come to the conclusion SDL 2.0 is the way to go for android. I literally jumped from SDL 1.2 to SDL 2.0 with like 5 lines of code, which was all the setting of the screen and OpenGL context. Everything else, events, mouse still using the old 1.2 commands 🙂

If your using OpenGL moving from SDL 1.2 and SDL 2.0 is like 5 lines of code. Which is pretty amazing.

I’ve had a good day today.

I guess now I need to figure out how to dodual touch. I suspect maybe it’s the right mouse button instead of the left?

---

<div class="post-metadata">

**Author:** ![Gabriel\_Jacobo](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/gabriel_jacobo/32/1968_2.png) [@Gabriel\_Jacobo](https://discourse.libsdl.org/u/Gabriel_Jacobo)\
**Post date:** [May 11, 2013, 12:06pm UTC](https://discourse.libsdl.org/t/sdl-2-0-opengl-es-1-1-ndk-home-bt-losing-the-draw-screen/19584/7 "2013-05-11T12:06:43Z")

</div>

Not certain that I’ve found the right stuff, but I didn’t want to

> leave this TOO long. It’s mostly my hope that this will give you the  
> references that you need to find the info you actually want.
> 
> Firstly, make certain that you understand the “Pause / Resume  
> behaviour” portion of README.android … unfortunately, I don’t know  
> whether it’s fully up to date. Why isn’t anyone that currently  
> compiles for Android chiming in on the events/needed actions?

I haven’t because most of this stuff has already been discussed and is  
readily available via the forum search or Google.

> In all of that, I noticed that SDL2 is supposed to deal with this  
> stuff itself, but that on some versions of Android that apparently  
> doesn’t actually work.

It does work AFAIK. If it doesn’t, that’s a bug worth reporting! ;)–  
Gabriel.
