Heyho,
it’s December 1st and the “bitowl learns libgdx” – advent calendar starts now 
I hope, that you have installed Eclipse with the Android-SDK as I told you in the first post. If you did that, we can install libgdx.
Download the latest release-build (atm 0.9.9) at http://libgdx.badlogicgames.com/download.html.
Now you have a zip file, that you save on your hard drive and extract it into a folder called “libgdx” (somewhere where you will find it later).

I hope, that you have installed Eclipse with the Android-SDK as I told you in the first post. If you did that, we can install libgdx.
Download the latest release-build (atm 0.9.9) at http://libgdx.badlogicgames.com/download.html.
Now you have a zip file, that you save on your hard drive and extract it into a folder called “libgdx” (somewhere where you will find it later).
Open this folder and start the gdx-setup-ui.jar inside it (on windows you can do a double-click, on other systems you may have to use the terminal :P).
Today libgdx has this awesome program, that creates the different projects (desktop, android) for you and links everything so that it works.

Fill everything in the left column (we only want to make a game for android and desktop, so we uncheck “html5″). Then you have to click the folder-symbol in the center column next to “LibGDX” and select the zip-File you downloaded.
If “LibGDX”‘s color switched to green you can generate your project in the fourth column.

Fill everything in the left column (we only want to make a game for android and desktop, so we uncheck “html5″). Then you have to click the folder-symbol in the center column next to “LibGDX” and select the zip-File you downloaded.
If “LibGDX”‘s color switched to green you can generate your project in the fourth column.
This program generated the projects for us and linked all the librarys, but we still have to import it into Eclipse.
Therefore we start Eclipse and go to the menu “File” >> “Import”. In the opening dialog we choose “General” >> “Existing Projects into Workspace”. In the next window we choose the folder which we generated our project in as the “root directory”. Now there should be three projects which are selected and we click on “Finish”.

There are now the different projects. “testgame” ist the main project, which contains all our code for the game”. in “testgame-android” and “testgame-desktop” are only starter-classes, which start our game on the different systems.
Therefore we start Eclipse and go to the menu “File” >> “Import”. In the opening dialog we choose “General” >> “Existing Projects into Workspace”. In the next window we choose the folder which we generated our project in as the “root directory”. Now there should be three projects which are selected and we click on “Finish”.

There are now the different projects. “testgame” ist the main project, which contains all our code for the game”. in “testgame-android” and “testgame-desktop” are only starter-classes, which start our game on the different systems.
Important to us are two directories: in testgame the folder “src”, which contains all our classes and in testgame-android the folder “assets”, which contains all our files (bilder, fonts, sounds) which should be later packed with the game.
Let us take a look on the most important file. The “TestGame” (or however you called it)-class in the testgame-project:
package de.bitowl.libgdxtest;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class TestGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}

At the moment this class makes surce, that a test-image is displayed, but all this code seems way to complicated for us. Hence we just throw away everything we don’t need:
package de.bitowl.libgdxtest;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
public class TestGame implements ApplicationListener {
@Override
public void create() {
}
@Override
public void render() {
Gdx.gl.glClearColor((float) Math.random(),(float) Math.random(), (float) Math.random(), 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
}
This code seems pretty easy. Let me explain the methods to you.
If our game is started, the method create() on our game-class is called. The method resize() is called, if the screensize has changed (on Desktop, if you are in window-modus or on android if you change the orientation of the device).
On android additionally the methods pause() and resume() are called, if you send the app to background.
dispose() is called, if the game is finished and all the ressources can be destroyed.
On android additionally the methods pause() and resume() are called, if you send the app to background.
dispose() is called, if the game is finished and all the ressources can be destroyed.
And most important in every frame (60 times per second) render() is called.
Gdx.gl.glClearColor((float) Math.random(),(float) Math.random(), (float) Math.random(), 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
These are OpenGL-commands that clean the screen with a color (random, so we see something happening).
We can start our “game” by choosing testgame-desktop in the “Project Explorer” (left column in eclipse) and press “Run” (the green arrow).

Well, it does not look really good, but that shall be enough for today.
We can start our “game” by choosing testgame-desktop in the “Project Explorer” (left column in eclipse) and press “Run” (the green arrow).

Well, it does not look really good, but that shall be enough for today.
Tomorrow we will try to get a real image on the screen.
You can find the full source code at GitHub.
Please write sugestions, complaints, spelling- , grammar- and translationmistactes as well as ideas, what kind of game I shall make here, in the comments.
No comments:
Post a Comment