From 53feedf407702687cdf28bddca9f8e2837eaf3d4 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Thu, 22 Nov 2018 15:44:13 +0400 Subject: [PATCH] Pass spritebatch instance to all entity's render methods Scale spritebatch and shaperenderer projections to PIXEL_PER_METER, no longer have to scale in each entity's render method Use a separate spritebatch for font rendering Dispose the texture atlas --- core/src/com/me/brickbuster/BrickBuster.java | 13 ++++++---- core/src/com/me/brickbuster/entity/Ball.java | 13 ++++------ core/src/com/me/brickbuster/entity/Brick.java | 14 +++++------ .../src/com/me/brickbuster/entity/Entity.java | 3 ++- .../src/com/me/brickbuster/entity/Paddle.java | 8 +++---- .../src/com/me/brickbuster/entity/Shield.java | 7 +++--- .../brickbuster/entity/powerup/PowerUp.java | 7 +++--- .../com/me/brickbuster/state/EditorState.java | 18 +++++++------- .../com/me/brickbuster/state/FieldState.java | 5 ++++ .../com/me/brickbuster/state/MenuState.java | 2 ++ .../com/me/brickbuster/state/PlayState.java | 24 ++++++++++--------- 11 files changed, 60 insertions(+), 54 deletions(-) diff --git a/core/src/com/me/brickbuster/BrickBuster.java b/core/src/com/me/brickbuster/BrickBuster.java index df4d0ac..8585215 100644 --- a/core/src/com/me/brickbuster/BrickBuster.java +++ b/core/src/com/me/brickbuster/BrickBuster.java @@ -10,9 +10,11 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Matrix4; import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.Viewport; import com.me.brickbuster.state.MenuState; +import com.me.brickbuster.state.PlayState; public class BrickBuster extends Game { @@ -26,8 +28,8 @@ public class BrickBuster extends Game { public BitmapFont font; public SpriteBatch sb; + public SpriteBatch fb; public ShapeRenderer sr; - public PolygonSpriteBatch pb; public AssetManager assets; @@ -42,8 +44,8 @@ public class BrickBuster extends Game { font.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); sb = new SpriteBatch(); + fb = new SpriteBatch(); sr = new ShapeRenderer(); - pb = new PolygonSpriteBatch(); assets = new AssetManager(); @@ -62,9 +64,10 @@ public class BrickBuster extends Game { public void resize(int width, int height) { viewport.update(width, height); - sb.setProjectionMatrix(cam.combined); - sr.setProjectionMatrix(cam.combined); - pb.setProjectionMatrix(cam.combined); + Matrix4 projection = cam.combined.cpy().scl(PlayState.PIXEL_PER_METER); + sb.setProjectionMatrix(projection); + fb.setProjectionMatrix(cam.combined); + sr.setProjectionMatrix(projection); super.resize(width, height); } diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 8490258..359edd7 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -43,15 +43,12 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener { } @Override - public void render(ShapeRenderer sr) { - SpriteBatch sb = state.getGame().sb; - sb.begin(); + public void render(SpriteBatch sb, ShapeRenderer sr) { sb.setColor(Color.WHITE); - sb.draw(texture, (pos.x - RADIUS) * PlayState.PIXEL_PER_METER, - (pos.y - RADIUS) * PlayState.PIXEL_PER_METER, - RADIUS*2 * PlayState.PIXEL_PER_METER, - RADIUS*2 * PlayState.PIXEL_PER_METER); - sb.end(); + sb.draw(texture, pos.x - RADIUS, + pos.y - RADIUS, + RADIUS*2, + RADIUS*2); } @Override diff --git a/core/src/com/me/brickbuster/entity/Brick.java b/core/src/com/me/brickbuster/entity/Brick.java index 4db3a8d..6211ede 100644 --- a/core/src/com/me/brickbuster/entity/Brick.java +++ b/core/src/com/me/brickbuster/entity/Brick.java @@ -40,15 +40,15 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener { } @Override - public void render(ShapeRenderer sr) { - SpriteBatch sb = state.getGame().sb; + public void render(SpriteBatch sb, ShapeRenderer sr) { sb.setColor(type.getColor()); - float x = (getX() - BRICK_WIDTH/2) * PlayState.PIXEL_PER_METER; - float y = (getY() - BRICK_HEIGHT/2) * PlayState.PIXEL_PER_METER; + float x = pos.x - BRICK_WIDTH/2; + float y = pos.y - BRICK_HEIGHT/2; sb.draw(region, x, y, - BRICK_WIDTH/2 * PlayState.PIXEL_PER_METER, BRICK_HEIGHT/2 * PlayState.PIXEL_PER_METER, - BRICK_WIDTH * PlayState.PIXEL_PER_METER, BRICK_HEIGHT * PlayState.PIXEL_PER_METER, - 1f, 1f, MathUtils.radiansToDegrees * body.getAngle()); + BRICK_WIDTH/2, BRICK_HEIGHT/2, + BRICK_WIDTH, BRICK_HEIGHT, + 1f, 1f, + MathUtils.radiansToDegrees * body.getAngle()); } @Override diff --git a/core/src/com/me/brickbuster/entity/Entity.java b/core/src/com/me/brickbuster/entity/Entity.java index 52e297b..635157b 100644 --- a/core/src/com/me/brickbuster/entity/Entity.java +++ b/core/src/com/me/brickbuster/entity/Entity.java @@ -1,5 +1,6 @@ package com.me.brickbuster.entity; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.Vector2; import com.me.brickbuster.state.FieldState; @@ -19,7 +20,7 @@ public abstract class Entity { this.pos = new Vector2(x, y); } - public abstract void render(ShapeRenderer sr); + public abstract void render(SpriteBatch sb, ShapeRenderer sr); public abstract void update(float dt); diff --git a/core/src/com/me/brickbuster/entity/Paddle.java b/core/src/com/me/brickbuster/entity/Paddle.java index d3266fe..2328b22 100644 --- a/core/src/com/me/brickbuster/entity/Paddle.java +++ b/core/src/com/me/brickbuster/entity/Paddle.java @@ -3,6 +3,7 @@ package com.me.brickbuster.entity; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.MathUtils; @@ -38,13 +39,10 @@ public class Paddle extends Entity implements PhysicsBody { } @Override - public void render(ShapeRenderer sr) { + public void render(SpriteBatch sb, ShapeRenderer sr) { sr.begin(ShapeType.Filled); sr.setColor(sticky? STICKY_COLOR : PADDLE_COLOR); - sr.rect((getX() - width/2) * PlayState.PIXEL_PER_METER, - getY() * PlayState.PIXEL_PER_METER, - width * PlayState.PIXEL_PER_METER, - PADDLE_HEIGHT * PlayState.PIXEL_PER_METER); + sr.rect(pos.x - width/2, pos.y, width, PADDLE_HEIGHT); sr.end(); } diff --git a/core/src/com/me/brickbuster/entity/Shield.java b/core/src/com/me/brickbuster/entity/Shield.java index 753bdd4..5c9a291 100644 --- a/core/src/com/me/brickbuster/entity/Shield.java +++ b/core/src/com/me/brickbuster/entity/Shield.java @@ -1,6 +1,7 @@ package com.me.brickbuster.entity; import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Body; @@ -24,12 +25,10 @@ public class Shield extends Entity implements PhysicsBody { } @Override - public void render(ShapeRenderer sr) { + public void render(SpriteBatch sb, ShapeRenderer sr) { sr.begin(ShapeRenderer.ShapeType.Filled); sr.setColor(PowerUpType.SHIELD.getColor()); - sr.rect(0, pos.y * PlayState.PIXEL_PER_METER, - PlayState.BOARD_WIDTH * PlayState.PIXEL_PER_METER, - SHIELD_HEIGHT * PlayState.PIXEL_PER_METER); + sr.rect(0, pos.y, PlayState.BOARD_WIDTH, SHIELD_HEIGHT); sr.end(); } diff --git a/core/src/com/me/brickbuster/entity/powerup/PowerUp.java b/core/src/com/me/brickbuster/entity/powerup/PowerUp.java index a23052b..a10cb86 100644 --- a/core/src/com/me/brickbuster/entity/powerup/PowerUp.java +++ b/core/src/com/me/brickbuster/entity/powerup/PowerUp.java @@ -1,6 +1,7 @@ package com.me.brickbuster.entity.powerup; import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.Vector2; @@ -35,12 +36,10 @@ public abstract class PowerUp extends Entity implements PhysicsBody, CollisionLi } @Override - public void render(ShapeRenderer sr) { + public void render(SpriteBatch sb, ShapeRenderer sr) { sr.begin(ShapeType.Filled); sr.setColor(color); - sr.circle(getX() * PlayState.PIXEL_PER_METER, - getY() * PlayState.PIXEL_PER_METER, - RADIUS * PlayState.PIXEL_PER_METER); + sr.circle(pos.x, pos.y, RADIUS); sr.end(); } diff --git a/core/src/com/me/brickbuster/state/EditorState.java b/core/src/com/me/brickbuster/state/EditorState.java index a56babf..c164289 100644 --- a/core/src/com/me/brickbuster/state/EditorState.java +++ b/core/src/com/me/brickbuster/state/EditorState.java @@ -22,7 +22,7 @@ import java.util.ArrayList; public class EditorState extends FieldState { public static final long BLINK_INTERVAL = 250l; - public static final int COLUMNS = 11; + public static final int COLUMNS = 10; public static final int ROWS = 33; private Brick placeHolder; @@ -72,25 +72,25 @@ public class EditorState extends FieldState { } Brick brick = bricks[col][row]; if (brick != null) { - brick.render(game.sr); + brick.render(game.sb, game.sr); } } } if (!blink) { - placeHolder.render(game.sr); + placeHolder.render(game.sb, game.sr); } game.sb.end(); debugRenderer.render(world, game.cam.combined.cpy().scl(PlayState.PIXEL_PER_METER)); - game.sb.begin(); + game.fb.begin(); game.font.setColor(Color.GRAY); - game.font.draw(game.sb, String.format("Current powerup: %s", curPowerUpType), 10, 230); - game.font.draw(game.sb, "WASD: Move brick", 10, 170); - game.font.draw(game.sb, "ENTER: Set brick BACKSPACE: Delete brick F12: Save", 10, 110); - game.font.draw(game.sb, "UP/DOWN: type LEFT/RIGHT: shape SPACE: powerup", 10, 55); - game.sb.end(); + game.font.draw(game.fb, String.format("Current powerup: %s", curPowerUpType), 10, 230); + game.font.draw(game.fb, "WASD: Move brick", 10, 170); + game.font.draw(game.fb, "ENTER: Set brick BACKSPACE: Delete brick F12: Save", 10, 110); + game.font.draw(game.fb, "UP/DOWN: type LEFT/RIGHT: shape SPACE: powerup", 10, 55); + game.fb.end(); } @Override diff --git a/core/src/com/me/brickbuster/state/FieldState.java b/core/src/com/me/brickbuster/state/FieldState.java index f7ff6fd..d735a16 100644 --- a/core/src/com/me/brickbuster/state/FieldState.java +++ b/core/src/com/me/brickbuster/state/FieldState.java @@ -21,4 +21,9 @@ public abstract class FieldState extends State { textures = game.assets.get("textures.atlas", TextureAtlas.class); } + @Override + public void dispose() { + textures.dispose(); + } + } diff --git a/core/src/com/me/brickbuster/state/MenuState.java b/core/src/com/me/brickbuster/state/MenuState.java index ec79ba1..3e55591 100644 --- a/core/src/com/me/brickbuster/state/MenuState.java +++ b/core/src/com/me/brickbuster/state/MenuState.java @@ -21,11 +21,13 @@ public class MenuState extends State { @Override public void render() { + game.sb.setProjectionMatrix(game.cam.combined); game.sb.begin(); game.sb.draw(playButton, BrickBuster.BOARD_WIDTH/2-playButton.getWidth()/2, BrickBuster.BOARD_HEIGHT/2-playButton.getHeight()/2); game.sb.end(); + game.sb.setProjectionMatrix(game.cam.combined.cpy().scl(PlayState.PIXEL_PER_METER)); } @Override diff --git a/core/src/com/me/brickbuster/state/PlayState.java b/core/src/com/me/brickbuster/state/PlayState.java index 0e873ba..0012fec 100644 --- a/core/src/com/me/brickbuster/state/PlayState.java +++ b/core/src/com/me/brickbuster/state/PlayState.java @@ -80,30 +80,32 @@ public class PlayState extends FieldState { } long start = System.nanoTime(); + game.sb.begin(); for (Brick block : bricks) { - block.render(game.sr); - } - game.sb.end(); - for (PowerUp powerUp : powerUps) { - powerUp.render(game.sr); + block.render(game.sb, game.sr); } for (Ball ball : balls) { - ball.render(game.sr); + ball.render(game.sb, game.sr); + } + game.sb.end(); + + for (PowerUp powerUp : powerUps) { + powerUp.render(game.sb, game.sr); } for (Shield shield : shields) { - shield.render(game.sr); + shield.render(game.sb, game.sr); } - paddle.render(game.sr); + paddle.render(game.sb, game.sr); //debugRenderer.render(world, game.cam.combined.cpy().scl(PIXEL_PER_METER)); long renderTime = System.nanoTime() - start; - game.sb.begin(); + game.fb.begin(); game.font.setColor(Color.GRAY); - game.font.draw(game.sb, String.format("FPS: %d Update: %.2f ms Render: %.2f ms", + game.font.draw(game.fb, String.format("FPS: %d Update: %.2f ms Render: %.2f ms", Gdx.graphics.getFramesPerSecond(), updateTime/1000000f, renderTime/1000000f), 10, BrickBuster.BOARD_HEIGHT-10); - game.sb.end(); + game.fb.end(); } @Override