libGDX manager.update() blocking animations - java

I successfully implemented a gifdecoder in my project and wrote a gifloader for it to use it with the assetmanager. I am able to load the gif in the assetmanager and I am also able to draw it. But everytime I try to load gifs in my assetmanager with manager.update() I can not draw animations anymore, only non animated textures.
Here is my code I hope somebody has an idea
public class Load implements Screen {
private SpriteBatch batch;
private Animation<TextureRegion> loadingAnimation;
private TextureAtlas atlasLoading = new TextureAtlas(Gdx.files.internal("atlas/loadingAnimation.atlas"));
float stateTime;
MainExecute lr;
public FileHandleResolver resolver = new InternalFileHandleResolver();
public AssetManager manager = new AssetManager();
private final Animation animation = GifDecoder.loadGIFAnimation(Animation.PlayMode.NORMAL, Gdx.files.internal("data/loading.gif").read());
public Load(MainExecute lr){
this.lr = lr;
}
public Texture Bg1;
#Override
public void show() {
Bg1 = new Texture(Gdx.files.internal("bggamescreen.png"));
batch = new SpriteBatch();
manager.setLoader(GIF.class,new Gifloader(resolver));
manager.load("data/BackgroundAnim.gif",GIF.class);
stateTime = 0f;
}
#Override
public void render(float delta) {
if (manager.update())
{
lr.setScreen(new MainMenu(lr));
}
stateTime += delta;
// loadingAnimation = new Animation<TextureRegion>(0.5f, atlasLoading.findRegions("loading"),Animation.PlayMode.LOOP);
TextureRegion currentFrame = (TextureRegion) animation.getKeyFrame(stateTime, true);
batch.begin();
batch.draw(Bg1,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
batch.draw(currentFrame, 0, 0, 200, 200);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}

Strange code, you are drawing animation before it finish loading. You need to waiting to load your animation, try smth like this.
if (manager.update())
{
loadingAnimation = new Animation<TextureRegion>(0.5f, atlasLoading.findRegions("loading"),Animation.PlayMode.LOOP);
TextureRegion currentFrame = (TextureRegion) animation.getKeyFrame(stateTime, true);
lr.setScreen(new MainMenu(lr));
}

Related

Problems loading assets in libgdx

I'm new to the world of app development, but I already have some specific knowledge. But my issue is the following involving libgdx and Android Studio:
When trying to load a texture through the Assets Manager method, the following error occurs:
`
E/AndroidRuntime: FATAL EXCEPTION: GLThread 593
Process: br.com.tpgames.dbz, PID: 19495
com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: bater_1/goku2.png
at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:172)
at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:143)
at br.com.tpgames.dbz.SplashScreen.<init>(SplashScreen.java:53)
`
My codes:
main class:
`
public class MainClass extends Game {
public AssetManager manager;
public void create () {
setScreen(new SplashScreen (this, manager));
}
}
`
Splash screen
`
public class SplashScreen implements Screen {
private Game game;
private AssetManager manager;
private float time = 0;
private SpriteBatch batch;
private Texture tex;
long startime;
public Texture goku;
public SplashScreen(Game game, AssetManager manager){
this.game = game;
this.manager = manager;
batch = new SpriteBatch();
tex = new Texture("logo.png");
startime = TimeUtils.millis();
manager = new AssetManager();
for (int i=1;i<=5;i++){
manager.load("bater_1/"+i+".png",Texture.class);
}
boolean load = manager.isLoaded("bater_1/goku1.png");
Gdx.app.log("Log", String.valueOf(load));
goku = manager.get("bater_1/goku2.png",Texture.class);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
time+=delta;
if (manager.update() && time >=2){ //Se o tempo for maior que 2 segundos, vai abrir a tela principal
game.setScreen(new Tela_Principal(game,manager));
}
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(tex, (float) (screenx*0.26), (float) (screeny*0.1), (float) (screenx*0.5), (float) (screeny*0.8));
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
`
Constants
`
public class Constantes {
public static int screenx = Gdx.graphics.getWidth();
public static int screeny = Gdx.graphics.getHeight();
}
`
Main screen
`
public class Tela_Principal implements Screen {
private Game game;
private AssetManager manager;
private SpriteBatch batch;
private SplashScreen splashScreen;
// private Texture goku;
public Tela_Principal(Game game, AssetManager manager){
this.game = game;
this.manager = manager;
splashScreen = new SplashScreen(game,manager);
for (int i=1;i<=5;i++){
manager.load("bater_1/goku"+i+".png",Texture.class);
}
batch = new SpriteBatch();
// goku = manager.get("bater_1/goku1.png",Texture.class);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0,1,0,0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(splashScreen.goku, (float) (screenx *0.1), (float) (screeny*0.1));
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
`
I thank you for your help!!
manager.load() will queue your asset, not immediately load it.
Only when manager.update() returns true will the assets be available.
Looks like your problem is in SplashScreen
for (int i=1;i<=5;i++){
manager.load("bater_1/"+i+".png",Texture.class);
}
//not allowed
goku = manager.get("bater_1/goku2.png",Texture.class);
AssetManager is well explained here https://libgdx.com/wiki/managing-your-assets

LIBGDX font different positions on different platform

When I run the android version of my app, the text that is drawn using Bitmap is displayed in a (way) different position in my desktop version than in my android version of the app. I have already looked this issue up and read that I had to set the Projectmatrix to combined, but this hasn't helped. No clue what could be the reason that it displays the texts in different positions crossplatform-wise.
public class HighScoresScreen implements Screen{
private CrossplatformApp game;
private Stage stage;
private TextButton backButton;
private OrthographicCamera camera;
private Table table;
BitmapFont font = new BitmapFont();
private Skin skin;
private Texture background;
private String[] data;
private String[] playerscores;
String nm = "";
String sc = "";
public HighScoresScreen (CrossplatformApp game) {
this.game = game;
this.camera = new OrthographicCamera(Constants.WIDTH, Constants.HEIGHT);
this.camera.position.set(Constants.WIDTH/2, Constants.HEIGHT/2, 0);
this.camera.update();
game.batch.setProjectionMatrix(this.camera.combined);
this.skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
this.background = new Texture("Screens/HighscoresScreen/highscores.png");
}
#Override
public void show() {
backButton = new TextButton("back", skin);
backButton.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new MenuScreen(game));
MenuScreen.musicHandler.stopMusic();
}
});
Gdx.input.setInputProcessor(stage);
JSONfunctions json = new JSONfunctions();
parseJSON parse = new parseJSON(json.doInBackground());
for (String i : parse.getNames()){
if(i != null) {
nm += i;
nm += "\n\n";
} else {
break;
}
}
System.out.println(nm);
//
for (String i : parse.getScores()){
if(i != null) {
sc += i;
sc += "\n\n";
} else {
break;
}
}
table = new Table();
table.setFillParent(true);
table.bottom();
table.add(backButton).size(100, 100);
stage.addActor(table);
}
#Override
public void render(float delta) {
game.batch.begin();
game.batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
font.draw(game.batch, nm, 100, 300);
font.draw(game.batch, sc, 480, 300);
game.batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
stage.dispose();
}
}
You need to update viewport of camera and then update projectionmatrix of SpriteBatch with updated viewport.
#Override
public void resize(int width, int height) {
camera.setToOrtho(false, width, height);
game.batch.setProjectionMatrix(camera.combined);
}
Also you need to update Stage's viewport with screen with and height.
You should use Label for nm and sc text.
It's better to use ExtendViewprot instead of FillViewport, take a look of this answer.

LibGdx: Shaperenderer Rect not being drawn on Screen

Trying to create a simple loading Screen. The below code prints the correct progress, so I know that part works. But the rectangle is not being drawn. Not sure what is wrong.
Full LoadingScreen:
public class LoadingScreen implements Screen {
private static final float PROGRESS_BAR_WIDTH = MyGdxGame.WIDTH / 2f;
private static final float PROGRESS_BAR_HEIGHT = 50f;
GdxAssetManager assetManager;
Stage stage;
//Table mainTable;
private ShapeRenderer shapeRenderer;
private MyGdxGame game;
public LoadingScreen(MyGdxGame game){
this.game = game;
assetManager = game.getAssetManager();
shapeRenderer = new ShapeRenderer();
stage = new Stage(new StretchViewport(MyGdxGame.WIDTH, MyGdxGame.HEIGHT));
}
#Override
public void show() {
assetManager.loadGeneral();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderProgressBar();
if (assetManager.getManager().update()) {
game.setScreen(new LoginScreen(game));
}
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
private void renderProgressBar() {
float progress = assetManager.getManager().getProgress();
System.out.println(PROGRESS_BAR_WIDTH * progress);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.RED);
shapeRenderer.rect(
(MyGdxGame.WIDTH - PROGRESS_BAR_WIDTH) / 2f,
(MyGdxGame.HEIGHT - PROGRESS_BAR_HEIGHT) / 2f,
PROGRESS_BAR_WIDTH * progress,
PROGRESS_BAR_HEIGHT
);
shapeRenderer.end();
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
dispose();
}
#Override
public void dispose() {
stage.dispose();
shapeRenderer.dispose();
}
}
I guess methods of interest are render and renderProgressBar. Like I said, all I get is a white background until the loading is finished, but the print inside renderProgressBar prints the correct values.
Set projectionMatrix of ShapeRenderer using stage camera.
shapeRenderer.setProjectionMatrix(stage.getCamera().combined);

Can't center image scene2d

I'am trying to center my image, but I can't. I tryied everything with viewports and nothing. I won't want to use magic numbers to make the trick! it's possible that the default viewport is messing up?
here is my code:
public class LoadingScreen extends AbstractScreen{
private Stage stage;
private Texture texture;
private Image loading_image;
public LoadingScreen(XBallGame game ) {
super(game);
texture = TextureManager.SPLASH;
loading_image = new Image();
stage = new Stage();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1); //sets clear color to black
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); //clear the batch
stage.act(delta);
stage.draw();
}
#Override
public void show() {
loading_image.addAction(Actions.sequence(Actions.alpha(0)
,Actions.fadeIn(0.5f),Actions.delay(2),Actions.run(new Runnable() {
#Override
public void run() {
System.out.println("DONE");
}
})));
stage.addActor(loading_image);
loading_image.setPosition(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
}
UPDATE (Fixed the problem) :
Just added this line:
loading_image.setPosition((Gdx.graphics.getWidth()/2) - loading_image.getWidth()/2,
(Gdx.graphics.getHeight()/2) - loading_image.getHeight()/2);
and works fine!

libgdx rendering srite over another causes background sprite to disapear

image of problem http://imgur.com/ZNpHiiu so I succesfully rendered a background for my game , now i tried to render some sprites on top of it and screen gets all messed up big Blue L.
code main
public class WizardGame extends Game {
public static final String TITLE = "Are you a bad enough wizard to save the princess?!", VERISION = "0.0.0.0.PREALPHA";
PlayTest pt = new PlayTest();
private FPSLogger fps;
public void create() {
Gdx.gl20.glClearColor(0, 0, 0, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
fps = new FPSLogger();
fps.log();
pt.create();
}
public void dispose() {
super.dispose();
}
public void render() {
super.render();
pt.render();
}
public void resize(int width, int height) {
super.resize(width, height);
}
public void pause() {
super.pause();
}
public void resume() {
super.resume();
}
}
playtest code
public class PlayTest implements Screen {
private TextureAtlas atlas;
private AtlasRegion floor;
private SpriteBatch batch;
private OrthographicCamera camera;
private Sprite background;
private BitmapFont font;
private String fps;
Blocks block = new Blocks();
public void render() {
Gdx.gl20.glClearColor(0, 0, 05, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
background.setBounds(0, 0, 720, 1280);
background.draw(batch);
block.renderBlocks();
batch.end();
}
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
public void show() {
}
public void hide() {
}
public void pause() {
}
public void resume() {
}
public void dispose() {
atlas.dispose();
batch.dispose();
}
public void create() {
atlas = new TextureAtlas(Gdx.files.internal("data/wizardpack.pack"));
floor = atlas.findRegion("Backfloor");
background = new Sprite(floor);
batch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("data/magicdebug48.fnt"));
block.create();
}
#Override
public void render(float delta) {
// TODO Auto-generated method stub
}
}
block code
public class Blocks extends Game {
private TextureAtlas atlas;
private SpriteBatch batch;
private AtlasRegion sword,shield,staff,fireball,iceball;
private Sprite sprSword,sprShield,sprStaff,sprFireball,sprIceball;
#Override
public void create() {
atlas = new TextureAtlas(Gdx.files.internal("data/wizardpack.pack"));
batch = new SpriteBatch();
sword = atlas.findRegion("Sword");
shield = atlas.findRegion("Shield");
staff = atlas.findRegion("Staff");
fireball = atlas.findRegion("flame");
iceball = atlas.findRegion("icespell");
sprSword = new Sprite(sword);
sprShield = new Sprite(shield);
sprStaff = new Sprite(staff);
sprFireball = new Sprite(fireball);
sprIceball = new Sprite(iceball);
}
public void renderBlocks(){
Gdx.gl20.glClearColor(0, 0, 05, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(sword, 0, 0, 0, 0, 32, 32, 1, 1, 0);
//sprShield.setBounds(15, 15, 32, 32);
// sprShield.draw(batch);
batch.end();
}
public void dispose(){
atlas.dispose();
batch.dispose();
}
}
I fixed it :D the call to renderBlocks has to be after the end of batch.

Categories