I am having an issue when switching screens in libgdx. I am building a asteroids game clone. So first my MainMenuScreen class (which uses a Fitviewport) is rendered and then I call setScreen() to GameScreen (GameScreen doesn't use a Fitviewport) and that works except that the second screen renders as if its using a Fitviewport. If I resize the second screen then the whole window is used for rendering. Why is this happening? Here are some pictures:
MainMenuScreen class:
GameScreen class after switching screens, the screen has black bars on the side (I colored the boundary in red for you) which I don't want:
I want the GameScreen to use all the window area for rendering, and not have black bars like the MainMenu. I am not using any Fitviewport in GameScreen also.
Here are relevant parts of my MainMenuScreen class:
public class MainMenuScreen implements Screen
{
private static final String TAG = "MainMenu";
private static final int VIRTUAL_WIDTH = 400;
private static final int VIRTUAL_HEIGHT = 400;
MyGdxGame game;
...
public MainMenuScreen(MyGdxGame game)
{
this.game = game;
viewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
stage = new Stage(viewport);
// Play button listener
btnPlay.addListener( new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.log(TAG, "PLAY");
MainMenuScreen.this.game.setScreen(MainMenuScreen.this.game.gameScreen);
};
});
....
}
Here is my GameScreen class:
MyGdxGame game;
OrthographicCamera guiCam;
World world;
WorldRenderer renderer;
public GameScreen(MyGdxGame game, SpriteBatch batch)
{
this.game = game;
guiCam = new OrthographicCamera(400, 400);
guiCam.position.set(400 / 2, 400 / 2, 0);
world = new World();
renderer = new WorldRenderer(game, batch, world);
}
public void draw()
{
GL20 gl = Gdx.gl;
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
}
#Override
public void show()
{
world.createLevel();
}
#Override
public void render(float delta)
{
draw();
}
#Override
public void resize(int width, int height)
{
}
#Override
public void pause()
{
}
#Override
public void resume()
{
}
#Override
public void hide()
{
}
#Override
public void dispose()
{
}
The problem is this line of code:
public void resize(int width, int height)
{
viewport.update(width, height, true);
}
Whenever you update the viewport, you are also updating the underlying opengl viewport and this causes the black bars to persist to the second screen (https://github.com/libgdx/libgdx/wiki/Viewports). Thus if you want to reset it back to normal you must use the following line of code in your second screen show() method (https://github.com/libgdx/libgdx/wiki/Scene2d):
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Related
I have a screen with a Stage and a TiledMap. The map renders fine, but trying to move the camera won't do anything to the map. In the code below the camera is being moved to the right, and as a result the image goes left on the screen, but the map stays "glued" to the screen.
Before
About Half A Second Later
public class TestScreen implements Screen{
private Stage stage;
private TiledMap testMap;
private OrthogonalTiledMapRenderer mapRenderer;
private Image image;
public TestScreen() {
stage = new Stage(new StretchViewport(GameBase.WIDTH, GameBase.HEIGHT));
testMap = GameBase.assets.getLevel("level25");
image = new Image(GameBase.assets.getTexture("sleuth"));
stage.addActor(image);
mapRenderer = new OrthogonalTiledMapRenderer(testMap, 1f);
mapRenderer.setView(stage.getCamera().combined, 0, 0, GameBase.WIDTH, GameBase.HEIGHT);
image.setOrigin(Align.center);
}
#Override
public void show() {
// TODO Auto-generated method stub
}
#Override
public void render(float delta) {
image.rotateBy(2);
stage.getCamera().position.x += 5;
stage.getCamera().update();
mapRenderer.render();
stage.act(delta);
stage.draw();
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
You have to call mapRenderer.setView(stage.getCamera()); on every frame to update the view on the tile map.
What I want to do is have a virtual size for my world and scale that world on the screen as much as possible without changing the aspect ratio, the FitViewport seemed like the best candidate. Thats how I intialised my viewport on the stage.
public class PlayStage extends Stage{
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
private final Vector2 gravity = new Vector2(0.f, -9.8f);
private World physWorld;
public PlayStage(){
super();
OrthographicCamera camera = new OrthographicCamera();
camera.setToOrtho(false, WIDTH, HEIGHT);
setViewport(new FitViewport(WIDTH, HEIGHT, camera));
physWorld = new World(gravity, false);
Gdx.input.setInputProcessor(this);
Ball ball = new Ball(physWorld);
setKeyboardFocus(ball);
addActor(ball);
}
#Override
public void draw() {
super.draw();
}
#Override
public void act(float delta) {
super.act(delta);
physWorld.step(delta, 10, 5);
}
#Override
public void dispose() {
super.dispose();
physWorld.dispose();
}}
This is how the sprite looks when rendered (scaled too much on the x coordinate). Also I get no touch down events for my actors.
You need to update stage viewport. It's better to resize your viewport from resize method.
#Override
public void resize(int width, int height) {
stage.getViewport().update(width,height);
//stage.getViewport().update(width,height,true); // -> If you want camera to be at centre.
}
I solved it on my own, all I had to do was to update the viewport after creation like this getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
I have a problem with LibGDX. I am trying to create an application, and for ease of use I want to draw everything at a fixed resolution (1920x1080).
To do this, I am trying to create a FitViewport and an OrthographicCamera so that the viewport always fits nicely to the screen size, and everything automatically scales.
In my Screen class I create such a viewport and a camera:
AbstractScreen() {
camera = new OrthographicCamera(Constants.VIRTUAL_WIDTH, Constants.VIRTUAL_HEIGHT);
viewport = new FitViewport(Constants.VIRTUAL_WIDTH, Constants.VIRTUAL_HEIGHT, camera);
}
(where VIRTUAL_WIDTH is 1920 and VIRTUAL_HEIGHT is 1080)
Now, when I try to render an image that is 1920x1080px, only one third of that image is drawn!
Here is a screenshot of what should be drawn: screenshot
Here is the image that should be fully drawn: image
public abstract class AbstractScreen implements Screen {
private Viewport viewport;
private OrthographicCamera camera;
protected SpriteBatch spriteBatch;
AbstractScreen() {
camera = new OrthographicCamera(Constants.VIRTUAL_WIDTH, Constants.VIRTUAL_HEIGHT);
viewport = new FitViewport(Constants.VIRTUAL_WIDTH, Constants.VIRTUAL_HEIGHT, camera);
spriteBatch = new SpriteBatch();
}
#Override
public void show() {
viewport.apply();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
}
#Override
public void resize(int width, int height) {
viewport.update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
And here is the actual screen implementation:
class LoadingScreen extends AbstractScreen {
private SpriteBatch spriteBatch;
private Texture texture;
#Override
public void show() {
super.show();
spriteBatch = new SpriteBatch();
texture = new Texture("textures/test.png");
}
#Override
public void render(float delta) {
super.render(delta);
spriteBatch.begin();
spriteBatch.draw(texture, 0, 0);
spriteBatch.end();
}
}
Thank you!
Your LoadingScreen is creating its own SpriteBatch named spriteBatch, which hides the spriteBatch in the AbstractScreen superclass.
So your LoadingScreen.render() calls super.render(), which sets up the appropriate projection matrix in AbstractScreen.spriteBatch, but then you are using LoadingScreen.spriteBatch to draw with.
So remove the spriteBatch field in LoadingScreen, and don't instantiate it there. Make the spriteBatch in AbstractScreen protected so you can access it from the subclass.
By the way, SpriteBatches must be disposed in dispose() or you will leak memory. Better yet, don't instantiate SpriteBatch inside your Screen. Instantiate a single SpriteBatch in your Game and pass it to your Screen's constructor so everything can share the same instance, rather that wasting time disposing and instantiating new ones every time you switch screens. SpriteBatch is a heavy object.
I have a Stage with OrthographicCamera and also I have a Actor with InputListener setter by addListener (from actor class). The problem is that the actor doesn't proccess input, but if in my screen I delete OrthographicCamera the Actor proccess the input, so, with OrthographicCamera Actor doesn't proccess input but it works if I remove OrthographicCamera.
Any advice?
I have the following code
public class Test implements Screen {
private Game game;
private Stage stage;
private MemoryActor actor;
private AssetManager manager;
private boolean loaded = false;
float width, height;
private OrthographicCamera camera;
public Test(Game game){
this.game = game;
stage = new Stage();
manager = new AssetManager();
manager.load("img.png",Texture.class);
manager.load("img1.png",Texture.class);
InputMultiplexer im = new InputMultiplexer();
im.addProcessor(stage);
Gdx.input.setInputProcessor(im);
height = Gdx.graphics.getHeight();
width = Gdx.graphics.getWidth();
camera = new OrthographicCamera(width, height);
camera.position.set(((width / 2)), ((height / 2)), 0);
camera.update();
stage.setViewport(new ExtendViewport(300,300, camera));
}
public void createActor(){
Texture back = manager.get("img.png", Texture.class);
actor = new MemoryActor(manager.get("img1.png", Texture.class), back,0,0,50,50);
actor.setInputListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("down");
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("up");
}
});
stage.addActor(actor);
}
#Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (manager.update()){
if (!loaded){
createActor();
loaded = true;
}
}
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
and MemoryActor:
public class MemoryActor extends Actor {
...
public MemoryActor(){}
public MemoryActor(Texture texture, Texture texBack, float x, float y, float width, float height){
...
}
public void setInputListener(InputListener il){
addListener(il);
}
#Override
public void draw(Batch batch, float alpha){
...
}
}
Just registering the stage as an InputProcessor isn't enough. You also need to trigger the event processing of Stage via stage.act() in every frame.
Furthermore you need to properly update the stage's Viewport when a resize event occurs. This can be done via stage.getViewport().update(width, height, true). Otherwise the stage will process the events based on incorrect assumptions about the screen size and might also render your stage not the way you want it. The true is important because it will also center the camera on the new screen size, which is necessary in case of UIs.
I just started learning libGDX, and was following the example from http://steigert.blogspot.in/2012/02/2-libgdx-tutorial-game-screens.html (the second link on libGdx help page).
As of now, I just display a logo of 512x512. There is nothing else happening in the application but when I run the application in desktop mode,I get a FPS of 15-16. When I remove the image, I get 60fps for the blank screen. For android its even worse, I get 3-4 fps in Galaxy SL - GT-i9003 (Temple Run runs on playable speed on the device).
My laptop plays World of Warcraft without any hiccups in high quality so its baffling that such a small app would only achieve 15fps.
public class SplashScreen extends AbstractScreen {
private Texture splashTexture;
private TextureRegion splashTextureRegion;
public SplashScreen(MyGDXGame game){
super(game);
}
#Override
public void show()
{
super.show();
// load the splash image and create the texture region
splashTexture = new Texture("splash.png");
// we set the linear texture filter to improve the stretching
splashTexture.setFilter( TextureFilter.Linear, TextureFilter.Linear );
// in the image atlas, our splash image begins at (0,0) at the
// upper-left corner and has a dimension of 512x301
splashTextureRegion = new TextureRegion( splashTexture, 0, 0, 512, 382 );
}
#Override
public void render(float delta ) {
super.render( delta );
// we use the SpriteBatch to draw 2D textures (it is defined in our base
// class: AbstractScreen)
batch.begin();
// we tell the batch to draw the region starting at (0,0) of the
// lower-left corner with the size of the screen
batch.draw( splashTextureRegion, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );
// the end method does the drawing
batch.end();
}
#Override
public void dispose()
{
super.dispose();
splashTexture.dispose();
}
}
Here is the relevant section of AbstractScreen class:
public class AbstractScreen implements Screen {
protected final MyGDXGame game;
protected final BitmapFont font;
protected final SpriteBatch batch;
public AbstractScreen(MyGDXGame game ){
this.game = game;
this.font = new BitmapFont();
this.batch = new SpriteBatch();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor( 0f, 0f, 0f, 1f );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
}
...
}
And the desktop app:
public class Main {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "First Game";
cfg.useGL20 = false;
cfg.width = 800;
cfg.height = 480;
new LwjglApplication(new MyGDXGame(), cfg);
}
MyGDXGame is as follows:
public class MyGDXGame extends Game {
private FPSLogger fpsLogger;
public SplashScreen getSplashScreen() {
return new SplashScreen( this );
}
#Override
public void create() {
fpsLogger = new FPSLogger();
}
#Override
public void dispose() {
super.dispose();
}
#Override
public void render() {
super.render();
setScreen(getSplashScreen());
fpsLogger.log();
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
#Override
public void pause() {
super.pause();
}
#Override
public void resume() {
super.resume();
}
I have read so many good things about libGdx, so this issue seems baffling to me. What am I doing wrong to get such a low frame rate?
I think setScreen(getSplashScreen()); in your render() method might be the problem. Move it to the create() method of MyGDXGame.
Right now you are switching the screen in every single frame and recreate a SpriteBatch every single time which is a very heavy object (see my comment to your question).