LibGDX - My splash screen isn't showing - java

Here is my splash screen:
package com.badlogic.gdx.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Splash implements Screen {
private SpriteBatch batch;
private Sprite splash;
#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
batch.begin();
splash.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
batch = new SpriteBatch();
Texture texture = new Texture(Gdx.files.internal("badlogic.jpg"));
splash = new Sprite(texture);
splash.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
When I run the application, the splash screen doesn't show, it just jumps straight to the game itself. I'm knew at this so I'm not completely sure what the problem is. My best guess is that the splash screen isn't linked to the game screen but I'm not sure.
Heres the Main Game screen, I don't have a menu yet.
public class SlingshotSteve extends Game {
private OrthographicCamera camera;
// Creates our 2D images
private SpriteBatch batch;
private TextureRegion backgroundTexture;
private Texture texture;
#Override
public void create() {
setScreen(new Splash());
camera = new OrthographicCamera(1280, 720);
batch = new SpriteBatch();
Texture texture = new Texture(Gdx.files.internal("background.jpg"));
backgroundTexture = new TextureRegion(texture, 0, 0, 500, 500);
Music mp3Sound = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
mp3Sound.setLooping(true);
mp3Sound.play();
}
#Override
public void render() {
super.render();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(backgroundTexture, 0, 0);
batch.end();
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
#Override
public void pause() {
super.pause();
}
#Override
public void resume() {
super.resume();
}
#Override
public void dispose() {
batch.dispose();
texture.dispose();
super.dispose();
}
}

I believe your problem lies here
#Override
public void render() {
super.render();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(backgroundTexture, 0, 0);
batch.end();
}
I believe that super.render() calls your current screen's render method, which means that your splash screen is indeed drawing itself. However, since right afterwards you draw your background, the actual splash screen is not visible.
Try commenting out everything but super.render(); in your render method, and see if that doesn't fix things.

It's because you provide an implementation for render() in your Game class, rather than use a separate Screen. If you use Screens, you mustn't provide an implementation for render() in your Game class.
Also, you should allocate the Texture for the SplashScreen in a constructor, rather than in the show() method. But that's just a minor caveat.

Related

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 - How to clear the Screen

I'm trying to display 2 different screens, changing when the user touches the screen. So far with the code below the screens change but the text just keeps overlapping and piling up. I need to dispose of EVERYTHING on the screen before switching.
One of the 2 similar pages(only the text is different on the 2)
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
public class MainMenu implements Screen {
OrthographicCamera camera;
SpriteBatch batch;
Screens game;
BitmapFont font;
public MainMenu(Screens game) {
this.game = game;
}
#Override
public void dispose() {
batch.dispose();
font.dispose();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void render(float delta) {
CharSequence str = "Main Menu";
batch = new SpriteBatch();
font = new BitmapFont();
batch.begin();
font.draw(batch, str, 200, 200);
batch.end();
if (Gdx.input.justTouched()) // use your own criterion here
game.setScreen(game.anotherScreen);
}
#Override
public void show() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
}
Screens.java
package com.me.mygdxgame;
import com.badlogic.gdx.Game;
public class Screens extends Game {
MainMenu mainMenuScreen;
AnotherScreen anotherScreen;
#Override
public void create() {
mainMenuScreen = new MainMenu(this);
anotherScreen = new AnotherScreen(this);
setScreen(mainMenuScreen);
}
}
Change your render function to:
#Override
public void render(float delta) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); //clears the buffer
CharSequence str = "Main Menu";
batch = new SpriteBatch();
font = new BitmapFont();
batch.begin();
font.draw(batch, str, 200, 200);
batch.end();
if (Gdx.input.justTouched()) // use your own criterion here
game.setScreen(game.anotherScreen);
}

libgdx - ShapeRenderer in Group.draw renders in wrong colour

MyGroup:
public class GShape extends Group{
private ShapeRenderer shape;
public GShape() {
super();
shape = new ShapeRenderer();
}
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
shape.begin(ShapeType.Line);
Gdx.gl10.glLineWidth(5);
shape.setColor(1, 1f, 1f, 1f);
shape.line(0, 0, 200, 100);
shape.end();
}
}
Main:
public class GameControl implements ApplicationListener {
private Stage stage;
private GShape gShape;
#Override
public void create() {
stage = new Stage(480,320,false);
Texture t = new Texture(Gdx.files.internal("data/the200.png"));
Image i = new Image(t);
stage.addActor(i);
gShape = new GShape();
stage.addActor(gShape);
}
#Override
public void dispose() {
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.draw();
// gShape.render();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
Colour of shape is not white? Why?
http://nw7.upanh.com/b3.s38.d3/352dd792eb77ce6df204a7af47ae1ac6_55348087.cos.jpg?rand=0.19125773780979216
You are probably getting inconsistent results because you're mixing SpriteBatch and ShapeRenderer contexts. Both of these expect state they "store" in OpenGL to be maintained between begin() and end() calls.
The Actor draw() method is called in a context where the SpriteBatch begin() has already been called, so you need to end it before beginning your ShapeRenderer. (And you need to restart the SpriteBatch before returning.
Like this:
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
batch.end(); // ** End the batch context
shape.begin(ShapeType.Line);
// .. draw lines ...
shape.end()
batch.begin(); // ** Restart SpriteBatch context that caller assumes is active
}

LIBGDX how to add sprite from a texture atlas

I need to know how I can add a sprite from a texture atlas so I can implement it as a sprite for my main menu.
Here is my code for the main menu:
public class MainMenu implements Screen {
CrazyZombies game;
Stage stage;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
TextureRegion firstLayer;
TextureRegion secondLayer;
TextureRegion thirdLayer;
TextureRegion fourthLayer;
TextureRegion fifthLayer;
TextureRegion sixthLayer;
TextureRegion seventhLayer;
TextureRegion eighthLayer;
TextureRegion ninthLayer;
TextureRegion tenthLayer;
TextureRegion eleventhLayer;
public MainMenu(CrazyZombies game){
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1);
batch.begin();
batch.end();
}
#Override
public void resize(int width, int height) {
Gdx.input.setInputProcessor(stage);
}
#Override
public void show() {
batch = new SpriteBatch();
skin = new Skin();
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
batch.dispose();
skin.dispose();
atlas.dispose();
stage.dispose();
}
public void loadLayers() {
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("data/mainmenu/MainMenu.pack"));
firstLayer = atlas.findRegion("1layer");
}
}
So I need to go about getting firstLayer onto the screen and every way I have tried ended with a crash.
You are not doing it right. Your render method doesn't have any draw. Take a look at the SuperJumper demo in libgdx.
To answer on how to create a sprite-
Sprite mySprite;
...
...
mySprite = new Sprite(someTextureRegion);
mySprite.setSize(SOME_WIDTH, SOME_HEIGHT);
mySprite.setOrigin(SOME_WIDTH/2f, SOME_HEIGHT/2f);
mySprite.setPosition(world.sprite.bounds.x, world.sprite.bounds.y);
...
...
mySprite.draw(batch);

Categories