Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Detect context loss (libGDX on Android)  (Read 486 times)
0 Members and 1 Guest are viewing this topic.
Offline KevinWorkman

JGO Knight


Medals: 21
Projects: 11
Exp: 12 years


klaatu barada nikto


« Posted 2013-11-24 17:27:15 »

Hey all,

I have a little program that simply draws a small image to a larger image and then draws the large image to the screen. It's for a live wallpaper on Android, and it pretty much works.

I'm working with these variables:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
private Pixmap smallPixmap;

private SpriteBatch batch;

private Pixmap screenPixmap;
private Texture screenTexture;

public void create(){
      batch = new SpriteBatch();
      batch.enableBlending();

      smallPixmap = new Pixmap(Gdx.files.internal("small.png"));
      screenPixmap = new Pixmap(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), Format.RGBA8888);
      screenTexture = new Texture(screenPixmap);
}


And in the render() method, I simple draw the small image to the large image and then redraw the large image:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void render(float delta) {   

      Gdx.gl.glClearColor(0, 0, 0, 1f);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      screenPixmap.drawPixmap(smallPixmap, 0, 0, smallPixmap.getWidth(), smallPixmap.getHeight(), MathUtils.random(screenPixmap.getWidth()), MathUtils.random(Gdx.graphics.getHeight()), smallPixmap.getWidth(), smallPixmap.getHeight());

      //THIS IS THE LINE
     screenTexture.dispose();
      screenTexture = new Texture(screenPixmap);
     
      batch.begin();
      batch.draw(screenTexture, 0, y, screenTexture.getWidth(), screenTexture.getHeight());
      batch.end();  
}


This all seems to work. The only problem is the above commented line where I dispose of the screen texture and then recreate it. I have to do this on Android because the app (in this case a live wallpaper) can lose the openGL context, which invalidates the texture. Without that line (if I just draw the screenPixmap to the screenTexture instead), the screen either displays all white or something like this:



However, it seems like overkill to create a new texture instance every single frame. My question is, is there a way to detect whether the context has been lost, so I only recreate the texture when I need to? Something like this:

1  
2  
3  
4  
5  
6  
7  
if(contextWasLost){
      screenTexture.dispose();
      screenTexture = new Texture(screenPixmap);
}
else{
      screenTexture.draw(screenPixmap, 0, 0);
}


I've tried googling, but I haven't been able to find anything conclusive. And it's further complicated that it works okay on some devices (including mine), but exhibits the above errors on other phones!

Static Void Games - Play indie games, learn game programming, upload your own games!
Offline davedes
« Reply #1 - Posted 2013-11-24 17:34:07 »

Why are you drawing a pixmap to another pixmap? Why not just do this:
1  
2  
3  
4  
5  
6  
7  
.... init
    myTex = new Texture("blah.png");

.... render
    batch.begin();
    batch.draw(myTex, 50, 50);
    batch.end();


If you create a Texture with a path like that it will be correctly managed on context loss. If you create a Pixmap with the width and height constructor, it will be unmanaged. You will need to re-create the pixmap on resume() (which is called on context restore).

Lots more info here:
https://github.com/mattdesl/lwjgl-basics/wiki/LibGDX-Textures

Offline KevinWorkman

JGO Knight


Medals: 21
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #2 - Posted 2013-11-24 17:51:29 »

Yeah, this is an unmanaged texture, since I'm drawing to it over and over again. I'll try the resume() function, thanks!

Static Void Games - Play indie games, learn game programming, upload your own games!
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (11 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (108 views)
2014-04-08 23:06:04

tom_mai78101 (208 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!