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
This commit is contained in:
parent
70f142c2fa
commit
53feedf407
@ -10,9 +10,11 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
|||||||
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
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.FitViewport;
|
||||||
import com.badlogic.gdx.utils.viewport.Viewport;
|
import com.badlogic.gdx.utils.viewport.Viewport;
|
||||||
import com.me.brickbuster.state.MenuState;
|
import com.me.brickbuster.state.MenuState;
|
||||||
|
import com.me.brickbuster.state.PlayState;
|
||||||
|
|
||||||
public class BrickBuster extends Game {
|
public class BrickBuster extends Game {
|
||||||
|
|
||||||
@ -26,8 +28,8 @@ public class BrickBuster extends Game {
|
|||||||
|
|
||||||
public BitmapFont font;
|
public BitmapFont font;
|
||||||
public SpriteBatch sb;
|
public SpriteBatch sb;
|
||||||
|
public SpriteBatch fb;
|
||||||
public ShapeRenderer sr;
|
public ShapeRenderer sr;
|
||||||
public PolygonSpriteBatch pb;
|
|
||||||
|
|
||||||
public AssetManager assets;
|
public AssetManager assets;
|
||||||
|
|
||||||
@ -42,8 +44,8 @@ public class BrickBuster extends Game {
|
|||||||
font.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
|
font.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
|
||||||
|
|
||||||
sb = new SpriteBatch();
|
sb = new SpriteBatch();
|
||||||
|
fb = new SpriteBatch();
|
||||||
sr = new ShapeRenderer();
|
sr = new ShapeRenderer();
|
||||||
pb = new PolygonSpriteBatch();
|
|
||||||
|
|
||||||
assets = new AssetManager();
|
assets = new AssetManager();
|
||||||
|
|
||||||
@ -62,9 +64,10 @@ public class BrickBuster extends Game {
|
|||||||
public void resize(int width, int height) {
|
public void resize(int width, int height) {
|
||||||
viewport.update(width, height);
|
viewport.update(width, height);
|
||||||
|
|
||||||
sb.setProjectionMatrix(cam.combined);
|
Matrix4 projection = cam.combined.cpy().scl(PlayState.PIXEL_PER_METER);
|
||||||
sr.setProjectionMatrix(cam.combined);
|
sb.setProjectionMatrix(projection);
|
||||||
pb.setProjectionMatrix(cam.combined);
|
fb.setProjectionMatrix(cam.combined);
|
||||||
|
sr.setProjectionMatrix(projection);
|
||||||
|
|
||||||
super.resize(width, height);
|
super.resize(width, height);
|
||||||
}
|
}
|
||||||
|
@ -43,15 +43,12 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(ShapeRenderer sr) {
|
public void render(SpriteBatch sb, ShapeRenderer sr) {
|
||||||
SpriteBatch sb = state.getGame().sb;
|
|
||||||
sb.begin();
|
|
||||||
sb.setColor(Color.WHITE);
|
sb.setColor(Color.WHITE);
|
||||||
sb.draw(texture, (pos.x - RADIUS) * PlayState.PIXEL_PER_METER,
|
sb.draw(texture, pos.x - RADIUS,
|
||||||
(pos.y - RADIUS) * PlayState.PIXEL_PER_METER,
|
pos.y - RADIUS,
|
||||||
RADIUS*2 * PlayState.PIXEL_PER_METER,
|
RADIUS*2,
|
||||||
RADIUS*2 * PlayState.PIXEL_PER_METER);
|
RADIUS*2);
|
||||||
sb.end();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -40,15 +40,15 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(ShapeRenderer sr) {
|
public void render(SpriteBatch sb, ShapeRenderer sr) {
|
||||||
SpriteBatch sb = state.getGame().sb;
|
|
||||||
sb.setColor(type.getColor());
|
sb.setColor(type.getColor());
|
||||||
float x = (getX() - BRICK_WIDTH/2) * PlayState.PIXEL_PER_METER;
|
float x = pos.x - BRICK_WIDTH/2;
|
||||||
float y = (getY() - BRICK_HEIGHT/2) * PlayState.PIXEL_PER_METER;
|
float y = pos.y - BRICK_HEIGHT/2;
|
||||||
sb.draw(region, x, y,
|
sb.draw(region, x, y,
|
||||||
BRICK_WIDTH/2 * PlayState.PIXEL_PER_METER, BRICK_HEIGHT/2 * PlayState.PIXEL_PER_METER,
|
BRICK_WIDTH/2, BRICK_HEIGHT/2,
|
||||||
BRICK_WIDTH * PlayState.PIXEL_PER_METER, BRICK_HEIGHT * PlayState.PIXEL_PER_METER,
|
BRICK_WIDTH, BRICK_HEIGHT,
|
||||||
1f, 1f, MathUtils.radiansToDegrees * body.getAngle());
|
1f, 1f,
|
||||||
|
MathUtils.radiansToDegrees * body.getAngle());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.me.brickbuster.entity;
|
package com.me.brickbuster.entity;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.me.brickbuster.state.FieldState;
|
import com.me.brickbuster.state.FieldState;
|
||||||
@ -19,7 +20,7 @@ public abstract class Entity {
|
|||||||
this.pos = new Vector2(x, y);
|
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);
|
public abstract void update(float dt);
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package com.me.brickbuster.entity;
|
|||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.Input;
|
import com.badlogic.gdx.Input;
|
||||||
import com.badlogic.gdx.graphics.Color;
|
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;
|
||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
|
||||||
import com.badlogic.gdx.math.MathUtils;
|
import com.badlogic.gdx.math.MathUtils;
|
||||||
@ -38,13 +39,10 @@ public class Paddle extends Entity implements PhysicsBody {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(ShapeRenderer sr) {
|
public void render(SpriteBatch sb, ShapeRenderer sr) {
|
||||||
sr.begin(ShapeType.Filled);
|
sr.begin(ShapeType.Filled);
|
||||||
sr.setColor(sticky? STICKY_COLOR : PADDLE_COLOR);
|
sr.setColor(sticky? STICKY_COLOR : PADDLE_COLOR);
|
||||||
sr.rect((getX() - width/2) * PlayState.PIXEL_PER_METER,
|
sr.rect(pos.x - width/2, pos.y, width, PADDLE_HEIGHT);
|
||||||
getY() * PlayState.PIXEL_PER_METER,
|
|
||||||
width * PlayState.PIXEL_PER_METER,
|
|
||||||
PADDLE_HEIGHT * PlayState.PIXEL_PER_METER);
|
|
||||||
sr.end();
|
sr.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.me.brickbuster.entity;
|
package com.me.brickbuster.entity;
|
||||||
|
|
||||||
import com.badlogic.gdx.graphics.Color;
|
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;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.badlogic.gdx.physics.box2d.Body;
|
import com.badlogic.gdx.physics.box2d.Body;
|
||||||
@ -24,12 +25,10 @@ public class Shield extends Entity implements PhysicsBody {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(ShapeRenderer sr) {
|
public void render(SpriteBatch sb, ShapeRenderer sr) {
|
||||||
sr.begin(ShapeRenderer.ShapeType.Filled);
|
sr.begin(ShapeRenderer.ShapeType.Filled);
|
||||||
sr.setColor(PowerUpType.SHIELD.getColor());
|
sr.setColor(PowerUpType.SHIELD.getColor());
|
||||||
sr.rect(0, pos.y * PlayState.PIXEL_PER_METER,
|
sr.rect(0, pos.y, PlayState.BOARD_WIDTH, SHIELD_HEIGHT);
|
||||||
PlayState.BOARD_WIDTH * PlayState.PIXEL_PER_METER,
|
|
||||||
SHIELD_HEIGHT * PlayState.PIXEL_PER_METER);
|
|
||||||
sr.end();
|
sr.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.me.brickbuster.entity.powerup;
|
package com.me.brickbuster.entity.powerup;
|
||||||
|
|
||||||
import com.badlogic.gdx.graphics.Color;
|
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;
|
||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
@ -35,12 +36,10 @@ public abstract class PowerUp extends Entity implements PhysicsBody, CollisionLi
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(ShapeRenderer sr) {
|
public void render(SpriteBatch sb, ShapeRenderer sr) {
|
||||||
sr.begin(ShapeType.Filled);
|
sr.begin(ShapeType.Filled);
|
||||||
sr.setColor(color);
|
sr.setColor(color);
|
||||||
sr.circle(getX() * PlayState.PIXEL_PER_METER,
|
sr.circle(pos.x, pos.y, RADIUS);
|
||||||
getY() * PlayState.PIXEL_PER_METER,
|
|
||||||
RADIUS * PlayState.PIXEL_PER_METER);
|
|
||||||
sr.end();
|
sr.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ import java.util.ArrayList;
|
|||||||
public class EditorState extends FieldState {
|
public class EditorState extends FieldState {
|
||||||
|
|
||||||
public static final long BLINK_INTERVAL = 250l;
|
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;
|
public static final int ROWS = 33;
|
||||||
|
|
||||||
private Brick placeHolder;
|
private Brick placeHolder;
|
||||||
@ -72,25 +72,25 @@ public class EditorState extends FieldState {
|
|||||||
}
|
}
|
||||||
Brick brick = bricks[col][row];
|
Brick brick = bricks[col][row];
|
||||||
if (brick != null) {
|
if (brick != null) {
|
||||||
brick.render(game.sr);
|
brick.render(game.sb, game.sr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!blink) {
|
if (!blink) {
|
||||||
placeHolder.render(game.sr);
|
placeHolder.render(game.sb, game.sr);
|
||||||
}
|
}
|
||||||
game.sb.end();
|
game.sb.end();
|
||||||
|
|
||||||
debugRenderer.render(world, game.cam.combined.cpy().scl(PlayState.PIXEL_PER_METER));
|
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.setColor(Color.GRAY);
|
||||||
game.font.draw(game.sb, String.format("Current powerup: %s", curPowerUpType), 10, 230);
|
game.font.draw(game.fb, String.format("Current powerup: %s", curPowerUpType), 10, 230);
|
||||||
game.font.draw(game.sb, "WASD: Move brick", 10, 170);
|
game.font.draw(game.fb, "WASD: Move brick", 10, 170);
|
||||||
game.font.draw(game.sb, "ENTER: Set brick BACKSPACE: Delete brick F12: Save", 10, 110);
|
game.font.draw(game.fb, "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.font.draw(game.fb, "UP/DOWN: type LEFT/RIGHT: shape SPACE: powerup", 10, 55);
|
||||||
game.sb.end();
|
game.fb.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -21,4 +21,9 @@ public abstract class FieldState extends State {
|
|||||||
textures = game.assets.get("textures.atlas", TextureAtlas.class);
|
textures = game.assets.get("textures.atlas", TextureAtlas.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose() {
|
||||||
|
textures.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,11 +21,13 @@ public class MenuState extends State {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render() {
|
public void render() {
|
||||||
|
game.sb.setProjectionMatrix(game.cam.combined);
|
||||||
game.sb.begin();
|
game.sb.begin();
|
||||||
game.sb.draw(playButton,
|
game.sb.draw(playButton,
|
||||||
BrickBuster.BOARD_WIDTH/2-playButton.getWidth()/2,
|
BrickBuster.BOARD_WIDTH/2-playButton.getWidth()/2,
|
||||||
BrickBuster.BOARD_HEIGHT/2-playButton.getHeight()/2);
|
BrickBuster.BOARD_HEIGHT/2-playButton.getHeight()/2);
|
||||||
game.sb.end();
|
game.sb.end();
|
||||||
|
game.sb.setProjectionMatrix(game.cam.combined.cpy().scl(PlayState.PIXEL_PER_METER));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -80,30 +80,32 @@ public class PlayState extends FieldState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
long start = System.nanoTime();
|
long start = System.nanoTime();
|
||||||
|
|
||||||
game.sb.begin();
|
game.sb.begin();
|
||||||
for (Brick block : bricks) {
|
for (Brick block : bricks) {
|
||||||
block.render(game.sr);
|
block.render(game.sb, game.sr);
|
||||||
}
|
|
||||||
game.sb.end();
|
|
||||||
for (PowerUp powerUp : powerUps) {
|
|
||||||
powerUp.render(game.sr);
|
|
||||||
}
|
}
|
||||||
for (Ball ball : balls) {
|
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) {
|
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));
|
//debugRenderer.render(world, game.cam.combined.cpy().scl(PIXEL_PER_METER));
|
||||||
long renderTime = System.nanoTime() - start;
|
long renderTime = System.nanoTime() - start;
|
||||||
|
|
||||||
game.sb.begin();
|
game.fb.begin();
|
||||||
game.font.setColor(Color.GRAY);
|
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);
|
Gdx.graphics.getFramesPerSecond(), updateTime/1000000f, renderTime/1000000f), 10, BrickBuster.BOARD_HEIGHT-10);
|
||||||
game.sb.end();
|
game.fb.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user