I have just finished creating the next step of the Android game project, which is to render objects, text on the Android phone screen. Now let us begin coding, first I would like to change the game scene orientation mode from landscape to portrait because this game is all about shooting enemy planes, therefore we need to make the phone scene stands in the portrait mode which is the best orientation for this type of game.

Go to the manifest file in the Android Studio to change the screen orientation to the portrait mode.

android:screenOrientation="portrait"

Next thing we need to do is to get the screen size of the mobile device within the onCreate method in the AndroidLauncher class and put in the MainActivity class.

@Override
	protected void onCreate (Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
		DisplayMetrics displayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		int height = displayMetrics.heightPixels; // viewport height and width
		int width = displayMetrics.widthPixels;
		initialize(new MainActivity(width, height), config);
	}

Next, all we need is to create the texture region object and the bitmap font object as well as the texture atlas object that will be used to render the text and object on the screen.

package com.gamingdirectional.lonemoon;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;

public class MainActivity extends ApplicationAdapter {

	SpriteBatch batch;
	OrthographicCamera camera;
	float viewportWidth;
	float viewportHeight;
	TextureRegion bgRegion;
	TextureRegion planeRegion;
	Vector2 planePosition;
	Vector2 planeDefaultPosition;
	BitmapFont font;
	StringBuilder score = new StringBuilder();
	Integer point;
	TextureAtlas atlas;

	public MainActivity(int width, int height) {
		viewportWidth = width;
		viewportHeight = height;
	}
	
	@Override
	public void create () {
		resetGame();
	}

	@Override
	public void render () {
		Gdx.gl.glClearColor(1, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		updateGame();
		drawGame();
	}

	private void drawGame() {

		batch.setProjectionMatrix(camera.combined); // setting the batch projection matrix
		batch.begin();
		batch.draw(bgRegion, 0, 0);
		batch.draw(planeRegion, planePosition.x, planePosition.y);
		font.draw(batch, score, viewportWidth/6-font.getScaleX()/2, viewportHeight - font.getScaleY()/4);
		batch.end();
	}

	private void updateGame() {

		camera.update();
		updateScore();

	}

	private void updateScore() {
		point++;
		score.replace(0, 1, point.toString());
	}


	/* resetting game objects, background, font, camera, and create sprite batch object */

	private void resetGame() {
		batch = new SpriteBatch();
		atlas = new TextureAtlas(Gdx.files.internal("lonemoon.txt"));
		resetScore();
		resetFont();
		resetCamera();
		resetBackground();
		resetPlane();
	}

	private void resetScore() {
		point = 0;
	}

	private void resetFont() {

		font = new BitmapFont(Gdx.files.internal("freefont.fnt"),
				Gdx.files.internal("freefont.png"), false);
	}

	private void resetCamera() {
		camera = new OrthographicCamera();
		camera.setToOrtho(false, viewportWidth, viewportHeight);
	}

	private void resetPlane() {
		planePosition= new Vector2();
		planeDefaultPosition= new Vector2();
		//plane = new Texture("plane.png");
		planeRegion = atlas.findRegion("plane");//new TextureRegion(plane, plane.getWidth(), plane.getHeight());

		planeDefaultPosition.set(viewportWidth/2-planeRegion.getRegionWidth()/2, viewportHeight/2-planeRegion.getRegionHeight()/2);
		planePosition.set(planeDefaultPosition.x, planeDefaultPosition.y);
	}

	private void resetBackground() {
		//background = new Texture("background0.png");
		bgRegion = atlas.findRegion("background0");//new TextureRegion(background, background.getWidth(), background.getHeight());
	}


	@Override
	public void dispose () {
		batch.dispose();
		font.dispose();
		atlas.dispose();
	}
}

The final outcome is as followed.

Rendering background, plane and text

What is happening?

In the above code, we are using the texture atlas to seek the image within the huge png file which contains all the images we need for this game up until this moment, background, and plane. SpriteBatch class which will improve the performance of the game will draw these regions into the game scene. We also create the BitmapFont object which is used to render the text on the scene. Besides that, we also create a game camera that is used to show the game scene. As you can see in the program we are also increasing the score point through each game loop iteration.

These are the assets we need up until this moment.

The game assets

The freefont files are needed to create the bitmap font object. The other two files are used to create the texture atlas object.

OK, hope you do enjoy reading the code, see you all in the next chapter!

Click to rate this post!
[Total: 0 Average: 0]
Please follow and like us:

1 Comment

  1. Your style is really unique in comparison to other people I’ve
    read stuff from. I appreciate you for posting when you have the opportunity, Guess I will just book mark this web site.

Leave a Reply

Your email address will not be published. Required fields are marked *