How To Make Android Games. Part 4.

This time we will take a look at the texture creation - the main graphic aspect of any game. The LibGDX engine we have chosen to work with uses OpenGL standard.


How To Make Android Games Part 4


Let's start by opening "MyTestGame.java" file located in the "test-game" project. Let's clean this code up and we should get this result (or just simply replace your old code with this one):

package com.testgame; import com.badlogic.gdx.ApplicationListener; public class MyTestGame implements ApplicationListener { @Override public void create() { } @Override public void dispose() { } @Override public void render() { } @Override public void resize(int width, int height) { } @Override public void pause() { } @Override public void resume() { } }
Now if we start the game we will see a blank (or black screen). Your next step should involve creating a simple texture. You can do it in any graphic editor (eg. GIMP): create a square (64x64 pixels) divided in 4 squares (32x32 pixels each) and fill them with red, green, blue (RGB) and the remaining one leave as blank or fill it with white color and save it as "quads.png" inside the "assets/data" directory within your "test-game-android" project.
Then open "MyTestGame.java" file and use "create()" method in which you need to add this line of code:

@Override public void create() { quadTexture = new Texture("data/quads.png"); }
In order to display this texture we will use SpriteBatch class, all you got to do is to add a line of code into the "create()" method we've used before. It shuld look like this:

@Override public void create() { quadTexture = new Texture("data/quads.png"); batch = new SpriteBatch(); }
Now call up the "render()" method and add this line of code:

@Override public void render() { batch.begin(); batch.draw(quadTexture, 100, 100); batch.end(); }
If we decide to launch our project we will see the 4-color square displayed on a black background. We can manipulate the "render()" method as much as we need, for example we can add two more squares and make them bigger just by adding more "batch.draw" lines with different sizes of the same texture.

@Override public void render() { batch.begin(); batch.draw(quadTexture, 30, 30, 50, 50); batch.draw(quadTexture, 100, 50, 150, 150); batch.draw(quadTexture, 270, 100, 200, 200); batch.end(); }
But what if we want to use only a part of the texture, then we will need the "TextureRegion" class applied in the "create()" method (in this example we choose to display only one of the 4 colors):

@Override public void create() { quadTexture = new Texture("data/quads.png"); batch = new SpriteBatch(); redRegion = new TextureRegion(quadTexture, 0, 0, 32, 32); }
We also need to display it and we use "render()" method again:

@Override public void render() { batch.begin(); batch.draw(redRegion, 30, 30, 50, 50); batch.end(); }
Our next article will be devoted to the "SpriteBatch" class only and in more detail.
You can find Part 3 of this tutorial here.




What do you think about How to Make Android Games. Part 4.? Please, leave your comments in the section below.

Share this article with your friends:
 

No comments:

Post a Comment