Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (529)
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  
  [LIBGDX] Texture content lost, black screen  (Read 452 times)
0 Members and 1 Guest are viewing this topic.
Offline imbrendino

Senior Newbie


Projects: 1



« Posted 2014-04-04 19:47:00 »

Hi all, I'm developing a game. When the game start I load all the textureAtlas:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
package com.me.mygdxgame.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.utils.Disposable;
import com.me.mygdxgame.util.Constants;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;

public class Assets implements Disposable, AssetErrorListener {

   public static final Assets instance = new Assets();
   private AssetManager assetManager;
   public AssetHead head;
   public AssetFloor floor;
   public AssetLevelDecoration levelDecoration;
   public AssetStecca interaStecca;
   public AssetFonts fonts;
   public AssetForce force;
   public AssetSounds sounds;
   public AssetMusic music;
   public TextureAtlas atlas;

   private Assets() {
   }

   public void init(AssetManager assetManager) {
      this.assetManager = assetManager;
      // set asset manager error handler
     assetManager.setErrorListener(this);
      // load texture atlas
     assetManager.load(Constants.TEXTURE_ATLAS_OBJECTS, TextureAtlas.class);
      // start loading assets and wait until finished
     assetManager.finishLoading();
      atlas = assetManager.get(Constants.TEXTURE_ATLAS_OBJECTS);
      assetManager.finishLoading();

      assetManager.load("sounds/jump.mp3", Sound.class);
      assetManager.finishLoading();
      assetManager.load("sounds/banana.mp3", Sound.class);
      assetManager.finishLoading();
      assetManager.load("sounds/urto.mp3", Sound.class);
      assetManager.finishLoading();
      assetManager.load("music/DST-1990.mp3", Music.class);
      assetManager.finishLoading();
      // enable texture filtering for pixel smoothing
     for (Texture t : atlas.getTextures())
         t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
      // create game resource objects
     head = new AssetHead(atlas);
      floor = new AssetFloor(atlas);
      levelDecoration = new AssetLevelDecoration(atlas);
      interaStecca = new AssetStecca(atlas);
      fonts = new AssetFonts();
      force = new AssetForce(atlas);
      music = new AssetMusic(assetManager);
      sounds = new AssetSounds(assetManager);
     

   }

   

   @Override
   public void dispose() {
      System.out.println("caro assets fai il dispose()");
      assetManager.dispose();
      atlas.dispose();
      fonts.defaultSmall.dispose();
      fonts.defaultNormal.dispose();
      fonts.defaultBig.dispose();

   }

   public class AssetHead {
      public final AtlasRegion head1, head2, head3, head4, head5, head6,
            head10, head12, head13, head14, head15, head11, headUp,
            headDown, head7, head8, head9;

      public AssetHead(TextureAtlas atlas) {
         head1 = atlas.findRegion("scimmia1");
         head2 = atlas.findRegion("scimmia2");
         head3 = atlas.findRegion("scimmia3");
         head4 = atlas.findRegion("scimmia4");
         head5 = atlas.findRegion("scimmia5");
         head6 = atlas.findRegion("scimmia6");
         head7 = atlas.findRegion("scimmia7");
         head8 = atlas.findRegion("scimmia8");
         head9 = atlas.findRegion("scimmia9");
         head10 = atlas.findRegion("scimmia10");
         head11 = atlas.findRegion("scimmia11");
         head12 = atlas.findRegion("scimmia12");
         head13 = atlas.findRegion("scimmia13");
         head14 = atlas.findRegion("scimmia14");
         head15 = atlas.findRegion("scimmia15");
         headUp = atlas.findRegion("scimmiaUp");
         headDown = atlas.findRegion("scimmiaDown");
      }
   }

   public class AssetFloor {
      public final AtlasRegion floor;

      public AssetFloor(TextureAtlas atlas) {
         floor = atlas.findRegion("pavimento");
      }
   }

   public class AssetStecca {
      public final AtlasRegion stecca;
      public final AtlasRegion varco, varco1;

      public AssetStecca(TextureAtlas atlas) {
         stecca = atlas.findRegion("muro");
         varco = atlas.findRegion("banana1");
         varco1 = atlas.findRegion("banana2");
      }
   }

   public class AssetLevelDecoration {
      public final AtlasRegion mountain;

      public AssetLevelDecoration(TextureAtlas atlas) {
         mountain = atlas.findRegion("JungleBackground1");
      }
   }

   public class AssetForce {
      public final AtlasRegion force;
      public final AtlasRegion force_interno;

      public AssetForce(TextureAtlas atlas) {
         force = atlas.findRegion("barra");
         force_interno = atlas.findRegion("rettangolo_interno");
      }
   }

   public class AssetFonts {
      public final BitmapFont defaultSmall;
      public final BitmapFont defaultNormal;
      public final BitmapFont defaultBig;

      public AssetFonts() {
         // create three fonts using Libgdx's 15px bitmap font
        defaultSmall = new BitmapFont(
               Gdx.files.internal("data/font.fnt"), true);
         defaultNormal = new BitmapFont(
               Gdx.files.internal("data/font.fnt"), true);
         defaultBig = new BitmapFont(
               Gdx.files.internal("data/font.fnt"), true);
         // set font sizes
        defaultSmall.setScale(0.75f);
         defaultNormal.setScale(1.0f);
         defaultBig.setScale(2.0f);
         // enable linear texture filtering for smooth fonts
        defaultSmall.getRegion().getTexture()
               .setFilter(TextureFilter.Linear, TextureFilter.Linear);
         defaultNormal.getRegion().getTexture()
               .setFilter(TextureFilter.Linear, TextureFilter.Linear);
         defaultBig.getRegion().getTexture()
               .setFilter(TextureFilter.Linear, TextureFilter.Linear);
      }

   }

   public class AssetSounds {
      public final Sound jump, banana, urto;

      public AssetSounds(AssetManager am) {
         jump = am.get("sounds/jump.mp3", Sound.class);
         banana = am.get("sounds/banana.mp3", Sound.class);
         urto = am.get("sounds/urto.mp3", Sound.class);

      }
   }

   public class AssetMusic {
      public final Music song01;

      public AssetMusic(AssetManager am) {
         song01 = am.get("music/DST-1990.mp3", Music.class);
      }
   }

   @Override
   public void error(AssetDescriptor asset, Throwable throwable) {
      // TODO Auto-generated method stub
     
   }
}


After there are a menu Screen. When I press Play Button:

1  
2  
3  
4  
private void onPlayClicked() {
      ScreenTransition transition = ScreenTransitionFade.init(.75f);
        game.setScreen(new GameScreen(game), transition);
   }



And Game started:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
package com.me.mygdxgame.screens;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL10;
import com.me.mygdxgame.game.Assets;
import com.me.mygdxgame.game.WorldController;
import com.me.mygdxgame.game.WorldRenderer;
import com.me.mygdxgame.util.GamePreferences;
public class GameScreen extends AbstractGameScreen{
   private WorldController worldController;
   private WorldRenderer worldRenderer;
   private boolean paused;
   // pausa
  //private Button btnPause;
  // schermata pausa

   public GameScreen(DirectedGame game) {
      super(game);
   }

   @Override
   public void render(float deltaTime) {
      // Do not update game world when paused.
     if (!paused) {
         // Update game world by the time that has passed
        // since last rendered frame.
        worldController.update(deltaTime);
      }
      // Sets the clear screen color to: Cornflower Blue
     Gdx.gl.glClearColor(0x64 / 255.0f, 0x95 / 255.0f, 0xed / 255.0f,
            0xff / 255.0f);
      // Clears the screen
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      // Render game world to screen
     worldRenderer.render();
   }

   @Override
   public void resize(int width, int height) {
      worldRenderer.resize(width, height);
   }

   @Override
   public void show() {
      GamePreferences.instance.load();
      worldController = new WorldController(game);
      worldRenderer = new WorldRenderer(worldController);
      Gdx.input.setCatchBackKey(true);
     
   }
   
   
   

   
   
   
   @Override
   public void hide() {
     
      System.out.println("walioooooooooooooooooooooooooo sei hideeeeeeeeeeeee");
      worldRenderer.dispose();
      Assets.instance.dispose();
      Gdx.input.setCatchBackKey(false);
     
   }

   @Override
   public void pause() {
      paused = true;
   }

   @Override
   public void resume() {
      super.resume();
      // Only called on Android!
     paused = false;
   }

   @Override
   public InputProcessor getInputProcessor() {
      // TODO Auto-generated method stub
     return worldController;
   }
}


WorldController:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public WorldController(DirectedGame game) {
      this.game=game;
      init();
   }

   private void init() {
      moveCam = 1;
      cameraHelper = new CameraHelper();
      score = 0;
      timeScore = 0;
      levelGenerator = new LevelGenerator();
      cameraHelper.setTarget(null);
      schiaccio = false;

   }


LevelGenerator use the image to build the game world.


When I died in the game:

1  
game.setScreen(new GameOver(game),transition);


And the GameOver class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
package com.me.mygdxgame.screens;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Stack;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.me.mygdxgame.game.Assets;
import com.me.mygdxgame.screens.transictions.ScreenTransition;
import com.me.mygdxgame.screens.transictions.ScreenTransitionSlice;
import com.me.mygdxgame.util.Constants;

public class GameOver extends AbstractGameScreen{
   
   
   private Stage stage;
   private Skin skinCanyonBunny;
   private Skin skinLibgdx;
   // menu
  private Image imgBackground;
   private Button btnMenuPlay;
   private Button btnMenuOptions;
   // debug
  private final float DEBUG_REBUILD_INTERVAL = 3.4f;
   private boolean debugEnabled = false;
   private float debugRebuildStage;

   public GameOver(DirectedGame game) {
      super(game);
     
   }

   
   
   @Override
   public void render(float deltaTime) {
      Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      if (debugEnabled) {
         debugRebuildStage -= deltaTime;
         if (debugRebuildStage <= 0) {
            debugRebuildStage = DEBUG_REBUILD_INTERVAL;
            rebuildStage();
         }
      }
      stage.act(deltaTime);
      stage.draw();
      Table.drawDebug(stage);
   }

   @Override
   public void resize(int width, int height) {
      stage.setViewport(Constants.VIEWPORT_GUI_WIDTH,
            Constants.VIEWPORT_GUI_HEIGHT, false);
   }

   @Override
   public void show() {

      stage = new Stage();
      rebuildStage();
   }

   @Override
   public void hide() {
      stage.dispose();
      skinCanyonBunny.dispose();
      skinLibgdx.dispose();
   }

   @Override
   public void pause() {
   }

   private void rebuildStage() {
      skinCanyonBunny = new Skin(Gdx.files.internal(Constants.SKIN_UI),
            new TextureAtlas(Constants.TEXTURE_ATLAS_UI));
      skinLibgdx = new Skin(Gdx.files.internal(Constants.SKIN_LIBGDX_UI),
            new TextureAtlas(Constants.TEXTURE_ATLAS_LIBGDX_UI));
      // build all layers
     Table layerBackground = buildBackgroundLayer();
      Table layerControls = buildControlsLayer();
      // assemble stage for menu screen
     stage.clear();
      Stack stack = new Stack();
      stage.addActor(stack);
      stack.setSize(Constants.VIEWPORT_GUI_WIDTH,
            Constants.VIEWPORT_GUI_HEIGHT);
      stack.add(layerBackground);
      stack.add(layerControls);
   }

   private Table buildBackgroundLayer() {
      Table layer = new Table();
      // + Background
     imgBackground = new Image(skinCanyonBunny, "background");
      layer.add(imgBackground);
      return layer;
   }

   private Table buildControlsLayer() {
      Table layer = new Table();
      layer.center();
      // + Play Button
     btnMenuPlay = new Button(skinCanyonBunny, "repeat");
      layer.add(btnMenuPlay);
      btnMenuPlay.addListener(new ChangeListener() {
         @Override
         public void changed(ChangeEvent event, Actor actor) {
            onPlayClicked();
         }
      });
      layer.row();
      // + Options Button
     btnMenuOptions = new Button(skinCanyonBunny, "stop");
      layer.add(btnMenuOptions);
      btnMenuOptions.addListener(new ChangeListener() {
         @Override
         public void changed(ChangeEvent event, Actor actor) {
            onOptionsClicked();

         }
      });
      if (debugEnabled)
         layer.debug();
      return layer;
   }

   
   private void onPlayClicked() {
        game.setScreen(new GameScreen(game));
   }

   private void onOptionsClicked() {
      ScreenTransition transition = ScreenTransitionSlice.init(.75f,ScreenTransitionSlice.UP_DOWN, 10, Interpolation.pow5Out);
        game.setScreen(new MenuScreen(game), transition);
   }



   @Override
   public InputProcessor getInputProcessor() {
      // TODO Auto-generated method stub
     return stage;
   }

}



I died and repeat the game 4-5 times. After these the images aren't loaded, the screen is all black and the game is very slow. Only the score and musics are loaded. The score use the font  (data/font.fnt).

The image references are lost.


help me please!!!

Offline ewg

Junior Member


Medals: 4


Java games rock!


« Reply #1 - Posted 2014-04-05 15:33:58 »


   @Override
   public void hide() {
     
      System.out.println("walioooooooooooooooooooooooooo sei hideeeeeeeeeeeee");
      worldRenderer.dispose();
      Assets.instance.dispose();                     <------------------------------------------------------------------ This looks suspicious
      Gdx.input.setCatchBackKey(false);
     
   }

Offline Gibbo3771
« Reply #2 - Posted 2014-04-05 15:37:56 »


   
1  
2  
3  
4  
5  
6  
7  
8  
9  
@Override
   public void hide() {
     
      System.out.println("walioooooooooooooooooooooooooo sei hideeeeeeeeeeeee");
      worldRenderer.dispose();
      Assets.instance.dispose();                     <------------------------------------------------------------------ This looks suspicious
      Gdx.input.setCatchBackKey(false);
     
   }




Lol yep, your disposing everything when the screen is changing :p

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline imbrendino

Senior Newbie


Projects: 1



« Reply #3 - Posted 2014-04-06 16:04:40 »

nono!
I insert this to try. In both case the result is the same
Offline imbrendino

Senior Newbie


Projects: 1



« Reply #4 - Posted 2014-04-06 16:30:52 »

The problem occurs after 4-5 times that I repeat the game. The Assets class loses the references.
Offline ewg

Junior Member


Medals: 4


Java games rock!


« Reply #5 - Posted 2014-04-07 04:46:16 »

If you post the code in a zip file in a way that is easy to compile and run with a .bat file or something I will take a look at it.
Offline imbrendino

Senior Newbie


Projects: 1



« Reply #6 - Posted 2014-04-07 20:24:35 »

in this moment I can send you the .apk file. with you can see the problem.


https://www.dropbox.com/s/ktujkqj57u4dgqh/jump-android.apk
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.

ctomni231 (31 views)
2014-07-18 06:55:21

Zero Volt (27 views)
2014-07-17 23:47:54

danieldean (23 views)
2014-07-17 23:41:23

MustardPeter (24 views)
2014-07-16 23:30:00

Cero (39 views)
2014-07-16 00:42:17

Riven (41 views)
2014-07-14 18:02:53

OpenGLShaders (28 views)
2014-07-14 16:23:47

Riven (28 views)
2014-07-14 11:51:35

quew8 (25 views)
2014-07-13 13:57:52

SHC (61 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!