Sunday, June 22, 2014

[libgdx] day 19 – pixmaps

A Pixmap contains image data in your main memory. You can change the data of the pixmap and then create a texture of it that you can use.
You can either create a pixmap from an existing file or by defining its size.
Pixmap pixmap = new Pixmap( 32, 32, Format.RGBA8888 );

Pixmap pixmap = new Pixmap(Gdx.files.internal("graphics/test.png"));
There are some methods to draw on pixmaps. (more in the API as allways :P)
pixmap.setColor( 0, 1, 0, 1);
pixmap.fillCircle( 16,16,16 );
pixmap.setColor( 1, 0, 0, 1);
pixmap.drawRectangle( 5,5, 10,10);
After you created a Texture from the Pixmap you probably will not need it anymore. So dispose it right now.

[libgdx] day 18 – gestures

If you’Ve got a touchscreen you probably like to use gestures like pinch to zoom to view for example details on a map.
Of couse libgdx makes this really simple for us.

Maybe you remember the InputProcessors from day 3. We first wrote our own and later took the one from the stage. But you can as well combine two InputProcessors. The second one only gets the event if the first let’s it through (return false for example if you do not press a button in a menu).
InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(stage);
multiplexer.addProcessor(new GameInputProcessor());
Gdx.input.setInputProcessor(multiplexer);
libgdx has a interface called GestureListener that you have to implement:

[libgdx] day 17 – true type fonts

If you want to use a basic font (without effects) in different sizes, it is annoying to have to create a different bitmap font with Hiero every time.
Then you can use the libgdx-extension gdx-freetype.
To link this extension to your project, you have to go to the libgdx/extensions folder and copy the following files:
armeabi/libgdx-freetype.so -> testgame-android/libs/ameabi/ 
armeabi-v7a/libgdx-freetype.so -> testgame-android/libs/ameabi-v7a/ 
gdx-freetype.jar -> testgame/libs/   -> testgame-android/libgdx
gdx-freetype-natives.jar -> testgame-desktop/libs/ 
In Properties -> Java Build Path -> Libraries -> Add JARs… you add both jar-files to the desktop project and the gdx-freetype.jar to the android project.
After that you simply put the ttf-files of the fonts you want to create “on-the-fly” into a subfolder of assets. (I’m using a free font, that I found at dafont.com)
WIth a FreeTypeFontGenerator you can now generate the BitmapFonts:

[libdgx] day 16 – parallax scrolling

Today I react to the proposal from derSchotte to tell you something about parallax scrolling.
parallax scrolling means, that the different layers of the screen are scrolled at different speeds. The more distance the layer has to the camera the slower it does move.

With this technique you can fake a certain three-dimensionality in 2D.
parallax
As I’m still not 100% healthy, a few days too late with my tutorials and have much other stuff to do, the next days the tutorials will only be rough written and then a few days later written more precisely.
Inspired by this forum post I as well wrote these ParallaxLayer and ParallaxBackground classes (just a little bit simpler).

[libgdx]day 15 – letterbox

Sometimes you want that your game keeps the aspect ratio and that black borders appear. These borders are called letterbox.

I’m folliwing the idea of blog.arcamara.es.
First we have to define the screensize our game has:
Rectangle viewport;
float virtualWidth=800;
float virtualHeight=480;
float virtualAspectRatio=virtualWidth/virtualHeight;
In create() we create the camera and an rectangle for the part of the real window that is filled with the game.

[libgdx] day 14 – simple collisions

Today we want to test for collision very simple by seing our gameobjects as rectangles.
Then I created a subclass of Image (an Actor) for our ship:
package de.bitowl.libgdxtest;

import com.badlogic.gdx.math.Rectangle;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
public class Ship extends Image{
 int SPEED=70;
 Rectangle bounds;
 public Ship(AtlasRegion findRegion) {
  super(findRegion);
  bounds=new Rectangle((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight());
 }

 public Rectangle getBounds(){
  return bounds;
 }

 private void setXY(float pX,float pY){
  setPosition(pX, pY);
  bounds.setX((int)pX);
  bounds.setY((int)pY);
 }
 @Override
 public void act(float delta) {
  // move ship
  if(Gdx.input.isKeyPressed(Keys.RIGHT)){
   setXY(getX()+delta*SPEED,getY());
  }else if(Gdx.input.isKeyPressed(Keys.LEFT)){
   setXY(getX()-delta*SPEED,getY());
  }

  if(Gdx.input.isKeyPressed(Keys.UP)){
   setXY(getX(),getY()+delta*SPEED);
  }else if(Gdx.input.isKeyPressed(Keys.DOWN)){
   setXY(getX(),getY()-delta*SPEED);
  }
 }
}
Most of the code is known and lets the ship react to the arrow-keys.

[libgdx]day 13 – settings

We already have a cool options menu (with more or less sencefull options), but the settings we change there aren’t saved yet.
In libgdx therefore there is the class Preferences. We give our TestGame an object of this class, so that we can access it from everywhere.
In our LoadingScreen we then create the object:
// load options
game.preferences=Gdx.app.getPreferences("settings");
“settings” is the name of our preferences. We could store different bundles of preferences, if we want for example have one bundle for our settings and one for the highscores.
Every preference consists of a key and a value. If we ask for a specific value via it’s key (e.g. “volume”) we can as well pass a default value for the case that this preference is not set yet.

[libgdx] day 12 – half time

And now half of the time until christmas is over.
Today we will deal with the hardware, that is just avaiable for android devices, but still easily accessable via libgdx.

hardware-keys

Most android-smartphones have hardware-keys like menu,back or the volume-keys. By default the back-keys is causing the quit of the app and if you press the menu-key for a longer time a on-screen keyboard is called. If you want to define your own actions for these keys, you have to suppress the default reaction:
Gdx.input.setCatchBackKey(true);
Gdx.input.setCatchMenuKey(true);
// [...]
if (Gdx.input.isKeyPressed(Keys.BACK) || Gdx.input.isKeyPressed(Keys.MENU)){

accelerometer

[libgdx] day 11 – loading screen

I think it looks somehow kind of professional if the app shows a little loading-screen at start. Of course this only makes sence if you really have things to load there.

If we put all our image into one big texture, we’ll need that all the time. Such things we can preload at the beginning.
In Libgdx there is a class called AssetManager for this. We create an object of this in our Game-class so that we can access it from all screens.
As well I added a new Screen called LoadingScreen.
public class TestGame extends Game{
 AssetManager assets;

 @Override
 public void create() {
  setScreen(new LoadingScreen(this));
 }
}
In the constructor of the LoadingScreen we tell the Assetmanger with

[libgdx] day 10 – tilemaps

To create tilemaps we first need to install the Tiled Map Editor: http://www.mapeditor.org/
(when you have linux you may have to compile it yourself. everything you need to know is in the README file)
After we start the program, we create a new map via File > New. Here we have to specify the size of the map and the size of the tiles.
Now we need tiles, that our map consists of. I downloaded this tileset and put it into a new subfolder maps of my stuff folder.

Popular Posts