LibGdx Screen: render and lifecycle - java

I have a problem, I think its related to Screen render and its lifecycle.
Basically I have two screens (Menu and Game). In GameScreen render method i call World.update and after that my Render. In hide method (of GameScreen) i dispose of the SpriteBatch from Redner class.
So when I change the screen from Game to Menu (within World.update) Java crashes. As far as I can tell, the dispose is making the crash.
So my question is, when i set a new screen in the middle of the render cycle, is that render cycle still going to finish with its old screen? Meaning, am I calling batch.dispose before the rendering was finished, and that is why i get the problem?
Thank you for all the help
public class GameScreen extends AbstractGameScreen {
private static final String TAG = GameScreen.class.getName();
private WorldController worldController;
private WorldRenderer worldRenderer;
private boolean paused;
public GameScreen(Game 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 render time
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);
Gdx.gl.glClear(GL20.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() { // Similar as create method
worldController = new WorldController(game);
worldRenderer = new WorldRenderer(worldController);
Gdx.input.setCatchBackKey(true);
}
#Override
public void hide() { // Similar to dispose method
worldRenderer.dispose();
Gdx.input.setCatchBackKey(false);
}
#Override
public void pause() {
paused = true;
}
#Override
public void resume() {
super.resume();
// Only called on Android
paused = false;
}
}

That's basically correct. The screen that calls setScreen from within its render method will have hide called on itself and then will continue through the rest of its code in its render method. So you are killing your sprite batch right before trying to draw with it.
So don't call dispose from within your hide method. In fact, it is probably bad practice for a Screen to ever call dispose on itself. You can reserve that for the Game class that owns it. For example, you could do something like this in your game class:
#Override
public void render() {
super.render();
if (getScreen() != gameScreen && gameScreen != null) {
gameScreen.dispose();
gameScreen = null;
}
}
By the way, you should probably put the SpriteBatch in your Game subclass and let all the different screens share it. It's a fairly big object to be allocating and deallocating for no reason.

Related

Disposing Asset Manager in LibGDX

Here is my OOP setup:
Class MainGame extends Game:
...
public void create () {
assets = new Assets(); // my custom Assets object (constructor creates manager and AssetDescriptors
assets.load(); // loads Asset Descriptors into manager
assets.getManager().finishLoading();
batch = new SpriteBatch();
setScreen(new PlayScreen(this, assets));
}
...
public void dispose () {
assets.dispose(); // disposes asset manager
super.dispose();
batch.dispose();
}
Class PlayScreen implements Screen:
public PlayScreen(MainGame game, Assets assets) {
this.assets = assets;
this.game = game;
background = assets.getManager().get(assets.getBackground());
hero = new HeroSprite(290, 1100, this, assets);
// Create Game camera and Hub also
}
public void render(float delta) {
// Clear screen
// Use game batch to draw to screen
}
public void dispose() {
assets.dispose();
}
Class HeroSprite:
public HeroSprite(int x, int y, PlayScreen screen, Assets assets){
this.assets = assets;
this.screen = screen;
// Animation Textures
heroWalking = assets.getManager().get(assets.getWalking()); // This is an Asset Descriptor
walkAnime = new Animation(heroWalking) // my own custom animation class which takes the spritesheet and returns the appropriate frames
...
}
// This does not contain a dispose method
Sorr if there's a lot of code there I tried to minimize as much as possible. My main question is if I am disposing correctly?
It seems ok, my game runs fine. But when I quit the game and re-enter a black screen shows. This happens both on Desktop and Android.
A solution that works is to clear the memory on Android using Super Cleaner App. Since it's a memory issue I figures it has something to do with me disposing incorrectly.
EDIT : Revealing my Assets Class
public class Assets {
private AssetManager manager;
private AssetDescriptor<Texture> background;
private AssetDescriptor<Texture> wood;
public AssetManager getManager() {
return manager;
}
public AssetDescriptor<Texture> getBackground() {
return background;
}
public AssetDescriptor<Texture> getWood() {
return hero;
}
public Assets(){
manager = new AssetManager();
background = new AssetDescriptor<Texture>("textures/static/bg.png", Texture.class);
hero = new AssetDescriptor<Texture>("textures/static/hero.png", Texture.class);
...
}
public void load(){
manager.load(background);
manager.load(hero);
...
}
public void dispose(){
manager.dispose();
}
}
Screen.dispose() does not get called automatically, you should manually call dispose when you exit a screen and not needing the assets anymore. But obviously when you need them again you need to load them again and you need a new AssetManager for that. So if your assets won't take up that much you should just keep it in memory.
On exit however you need to dispose. Game.dispose() does get called automatically. Here you should call getScreen().dispose() and have everything needing disposing in the dispose() method of the effective Screen.
Since currently it does not look like you are disposing between screens it might be because you have a static AssetManager, you should show your Assets() class.

Assets wont get rendered after disposing and reloading them

In my game I have currently two screens. The MenuScreen and the GameScreen. Through the Play option in the menu the player can switch to the GameScreen and start the Gameplay and with Escape he can get back to the MenuScreen. I dispose the used Assets when I switch to the other Screen in the hide() method and load the needed Assets for the new Screen in the constructor of the Screen I switch to. The problem is that the Textures and Sound Effects aren't rendered/played when I switch back.
For example when I start the game in the MenuScreen, then switch to the GameScreen everything is fine. But when I switch back to the MenuScreen the MenuScreen is just a black window. When I then switch to the GameScreen again it's black too except for the BitmapFont.
Maybe there is a fundemental flaw in the way I handle this. I tried to leave out as much unnecessary things as I can from the code I post here, but I fear that it's still too much.
RessourceLoader Class:
public class RessourceLoader {
public static AssetManager manager;
public static void create() {
manager = new AssetManager();
}
public static void loadMenuScreen() {
manager.load("gfx/menuBackground.png", Texture.class);
}
public static void getMenuScreen() {
menuBackground = manager.get("gfx/menuBackground.png", Texture.class);
}
public static void disposeMenuScreen() {
menuBackground.dispose();
}
public static void loadGameScreen() {
// load GameScreen Assets through AssetManager
}
public static void getGameScreen() {
// get GameScreen Assets through AssetManager
}
public static void disposeGameScreen() {
// dispose all GameScreen Assets
}
public static void dispose() {
manager.dispose();
}
}
MenuScreen Class:
public class MenuScreen implements Screen {
// Game starts in the MenuScreen
// Instance of game
private PHGame game;
// Orthographic camera
private OrthographicCamera cam;
public MenuScreen(PHGame phGame) {
game = phGame;
RessourceLoader.loadMenuScreen();
RessourceLoader.manager.finishLoading();
RessourceLoader.getMenuScreen();
cam = new OrthographicCamera();
cam.setToOrtho(true, 640, 480);
game.batcher.setProjectionMatrix(cam.combined);
}
#Override
public void render(float delta) {
// Fills background with black to avoid flickering
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Begin Drawing
game.batcher.begin();
// Draw Menu
// Stop drawing
game.batcher.end();
// Pressing Space confirms currently selected menu item
if (GameKeys.isPressed(GameKeys.SPACE)) {
game.setScreen(new GameScreen(game));
}
// Update Key Presses
GameKeys.update();
}
#Override
public void hide() {
dispose();
}
#Override
public void dispose() {
RessourceLoader.disposeMenuScreen();
}
}
GameScreen Class:
public class GameScreen implements Screen {
// Instance of game
private PHGame game;
private GameWorld world;
private GameRenderer renderer;
private float runTime;
public GameScreen(PHGame phGame) {
game = phGame;
RessourceLoader.loadGameScreen();
RessourceLoader.manager.finishLoading();
RessourceLoader.getGameScreen();
world = new GameWorld(game, this);
renderer = new GameRenderer(world, game);
}
#Override
public void render(float delta) {
// runTime is the amount of time the game is running
runTime += delta;
// Updates the Game World
world.update(delta);
// Renders everything
renderer.render(runTime);
// Update Key Presses
GameKeys.update();
}
#Override
public void hide() {
dispose();
}
#Override
public void dispose() {
RessourceLoader.disposeGameScreen();
}
}
GameRenderer Class:
public class GameRenderer {
// Instance of PHGame
PHGame game;
// Instance of Game World
private GameWorld world;
// Orthographic Camera
private OrthographicCamera cam;
// If true hitbox's will be shown
private boolean showHitbox;
// Game Objects
private Player player;
public GameRenderer(GameWorld world, PHGame game) {
this.game = game;
this.world = world;
player = world.getPlayer();
cam = new OrthographicCamera();
cam.setToOrtho(true, 640, 480);
showHitbox = false;
game.batcher.setProjectionMatrix(cam.combined);
}
public void render(float runTime) {
// draw objects and hud
}
}
If there are any questions regarding my problem I'll try to answer then as good as I can.
Refer to the github article 'managing your assets'. AssetManagers should not be static. 'This typically would cause black/missing textures or incorrect assets.'
After you dispose your asset manager it can no londer be used. Instead use manager.unload to unload assets. manager.unload("gfx/menuBackground.png");
EDIT:
I also didn't see any overriden show() methods. If you want your assets back you will need to load your assets in the screen's show method every time.

Control sprite rotation (it's spinning like crazy)

So, I have created a texture, and then a sprite.
On my render() method, I am check for user input. If the user has touched/clicked, then I want my sprite to rotate 90 degrees ONCE.
Right now the rotation works. However, it rotates multiple times per click!
How can I make it rotate only once per touch? I have a feeling that I might have to use delta time, and that occurs because the render method is being called frequently, but I don't know how to fix it... Thanks!
public class MyGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
Sprite sprite;
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
sprite = new Sprite(img);
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
batch.end();
if (Gdx.input.isTouched()) {
rotateRight();
}
}
private void rotateRight() {
sprite.setRotation(sprite.getRotation() - 90);
}
}
Right now you are polling input inside of your render method. Polling simply checks the status of the input (is it touched or not) and does not care for any actual "event" occurred.
Instead of this you need to look for input events via event handling as this will give you access to the actual event of the screen being touched or untouched. You can do this by implementing InputProcessor which will give you access to override a bunch of touch event methods so you can execute your rotate method only on the event of the touch.
Example:
public void update()
{
Gdx.input.setInputProcessor(new InputProcessor() {
#Override
public boolean TouchDown(int x, int y, int pointer, int button) {
if (button == Input.Buttons.LEFT) {
rotateRight();
return true;
}
return false
}
});
}
Don't forget to call your update() method from your render method. I put it in a separate function just for readability and not to clog up your rendering code.
You can also have your class implement InputProcessor and override the methods in your class so you do not have to do it inline as I did above.
if (Gdx.input.justTouched() && Gdx.input.isTouched()) {
rotateRight();
}

Image does not immediately get replaced after calling repaint()+revalidate()

I'm making a game/simulator and I'm using the following method to load an Image into my current Java project:
public static Image getImage(String name) {
URL url = ImageLoader.class.getClassLoader().getResource("resources/" + name);
Image img = Toolkit.getDefaultToolkit().createImage(url);
ImageLoader.tracker.addImage(img, 1);
try {
ImageLoader.tracker.waitForID(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return img;
}
This loads the images into my class that is drawing all items including the player (who has 4 different images for each direction he can face: north, south, east, west):
private Image player = ImageLoader.getImage("playerEast.png");
private Image playerEast = ImageLoader.getImage("playerEast.png");
private Image playerWest = ImageLoader.getImage("playerWest.png");
private Image playerSouth = ImageLoader.getImage("playerSouth.png");
private Image playerNorth = ImageLoader.getImage("playerNorth.png");
The class that loads above images paints them in the area like such:
public class TerritoryPanel extends JPanel implements Observer {
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
//Draw player
g.drawImage(player, (x, y, this);
}
I'm trying to update the pics with this method in the same class:
public void rotateEast(){
player = playerEast;
repaint();
revalidate();
}
..but just calling this rotateEast() does not update the picture immediately, it only gets updated afterwards when my regular Observer update() cycle calls repaint(), this happens every second so the missing repaint of rotateEast is visible:
#Override
public void update(Observable o, Object arg) {
if (EventQueue.isDispatchThread()) {
this.repaint();
} else { // Simulation-Thread
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
repaint();
}
});
}
}
Why does the repaint() from rotateEast seem to have no effect? Thank you very much in forward
but just calling this rotateEast() does not update the picture immediately, it only gets updated afterwards when my regular Observer update() cycle calls repaint(),
Don't know if it will make a difference but the normal logic is:
revalidate(); // to invoke the layout manager
repaint(); // to repaint all the components
Even if it doesn't make a difference in this case this order you should always use when dynamically changing the property of a component including adding/remove components from a panel.

How to pass canvas reference to class

I'm doing a simple game and I have a class that I call GameLoop and inside the run() method the code is passing the reference of the canvas to a method that draws a circle. This code is just some testcode.
My question is how do I pass the reference of the canvas if I want to create several sprite objects(classes) with a circle and put them in a list when the game starts? I have done this before, but then I used bitmap in each sprite object and just passed a reference of the image, but in this case I'm not sure how I would do since the canvas is inside the run() method, and I want to create my sprite objects in a method like initializeGameObjects() once in the beginning. I hope my question isn't unclear!? Preciate some help! Thanks!
// Game loop ---------------------------------------
#Override
public void run() {
// TODO Auto-generated method stub
while (gameRunning) {
if (!surfaceHolder.getSurface().isValid())
continue;
canvas = surfaceHolder.lockCanvas();
// Call method to draw objects on screen
drawObjects(canvas);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
// End game loop ---------------------------------------
// Method that draw everything on canvas
private void drawObjects(Canvas canvas) {
// Test
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
// Clear screen with black color
canvas.drawRGB(0, 0, 0);
// Draw a circle
canvas.drawCircle(100, 100, 100, paint);
}
I would implement Runnable and construct a Thread. This way you can have a "setup" method which ultimately calls a "start" method. The "start" method would draw to the canvas before starting the thread.
This is actually better practice for this kind of stuff. You may want to init stuff before starting a thread.
public class Game implements Runnable {
private Thread thisThread = new Thread(this);
private boolean gameRunning = false;
public void startGame() {
gameRunning = true;
//Draw to canvas
thisThread.start();
}
#Override
public void run() {
while(gameRunning) {
//Draw other objects
}
}
}
Define an interface:
public interface Renderable{
public void renderToCanvas(Canvas c);
}
Define a List of Renderables:
private List<Renderable> sprites = new ArrayList<Renderable>();
create methods to add/remove sprites, make sure they are synchronized:
public synchronized void addSprite(Renderable r){
sprites.add(r);
}
public synchronized void removeSprite(Renderable r){
sprites.remove(r);
}
This is drawing method, also synchronized, so you don't end up drawing and modifying sprite list at same time. This is either called in a loop by other thread, or a view's onDraw() method:
public synchronized void draw(Canvas c){
// lock canvas if needed
// common drawing code
for(Renderable r :sprites){
r.renderToCanvas(c);
}
// release canvas if locked
}
Now, you are free to define any type of sprite, Circle, Square or whatever,
public class Circle implements Renderable{
private Paint mPaint;
private float mRadius;
public Circle(Paint p, float r){
mPaint = p;
mRadius = r;
}
#Override
public void renderToCanvas(Canvas c){
c.drawCircle(100, 100, mRadius, mPaint);
}
}
and add it to sprite list:
addSprite(new Circle(new Paint(),20f));
Note: I used synchronized for simplicity, but there are better alternatives to it like atomic flags.These are required only if you want to add/remove sprites while game loop is running. If you setup all sprites before starting game loop, this is not a problem.

Categories