I understand that it is a framework; even more an open-source cross-platform game development library. I went to the libgdx homepage and followed the instruction on the video tutorial. After properly setting up my project I was able to run the default my-gdx-game project on the multiple supported platforms. Great, fine and dandy...now what?
I have been scouring the forums, wikis, javadocs, and many many more sites looking for decent straightforward how-to's. Unfortunately I couldn't find any, most of the help out there assumes you have some basic knowledge of this library.
I feel like the video tutorial showed me how to set up the project correctly, effectively getting my feet wet, then just assumed I know how to swim, and left me 300 miles out into the sea or something. I'm having trouble digesting the library, because I only started using it yesterday, so I'm a complete newcomer when it comes to libgdx.
I want to move my existing projects over to libgdx, but I'm use to BufferedImages, JFrames and things like that. Any help from the experienced veterans would be nice.
By the way I posted my core project below, so you guys can walk me through what is exactly going on here...
<code>
package com.me.mygdxgame;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
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 MyGdxGame 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(GL20.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() {
}
}
</code>
If you want to port your javax.swing
game to libgdx you have to know how you made your game. Did you separate the logic and the view? If yes, fine you only have to rewrite the view. If not, well, its maybe better to start from scratch.
First basics to know: The entrypoint to your game is the ApplicationListener
Interface. If you don't need every method inside it, extend Game
, which gives you some deffault behaivor. So i would suggest to use that. The Game
or ApplicationListener
now gives you possibility to react on some application events:
create()
: called when your app is starteddispose()
: called when your game is closed (not crashed :P)pause()
: called on android, when call is incoming/home button is pressed. On desktop when you minimize window/when the game lost the focus.resume()
: called when you come back after pause state.resize()
: called when you resize the window and on android, when screen rotates (you have to allow it in the manifest file)render()
: called every gameloop (if enabled), used to update the objects in game and then draw them to the screen.The default render() implementation in the Game
class calles render for your current Screen
.
Screen? Whats that?
Well think about a normal game. You start it and it showes a menu, with some buttons like Start Game
, Settings
... This is the Menu screen. So Screen
s are parts of your game with different logic and view, so that they have to be in sepparate classes. Screen
s implement the Screen
interface, which again gives you some useful methods:
show()
: called, when you set this Screen
as the Game
s Screen
with MyGdxGame.setScreen()
.hide()
: called, when another Screen
is set as the Game
s Screen
.So if the current Screen
is the MenuScreen
and i press the Play Game
button, show()
for the PlayScreen
is called and hide()
for the MenuScreen
is called.
The Screen
also has the methods pause()
, resume()
and resize()
, which are called if the same methods in Game
are called (always only for the current Screen). If you implement ApplicationListener
you have to write this default behaivor yourself.
Note, that dispose()
for the Screen
is not called automatically.
Next thing is the drawing. For drawing in 2d there are the:
ShapeRenderer
, most times used for debug only. It can render simple shapes like circles, squares...SpriteBatch
, used to render Texture
s Sprite
s...So most times you will render using the SpriteBatch
. You render in a block made of:
SpriteBatch.begin()
, starts the SpriteBatch. Set his viewPort, Matrices... before this callSpriteBatch.draw()
, draws the thing you give it as parameter (there are many different draw() methods, look at the API) to a Buffer, and flush()
them to the GPU as soon as it is full or SpriteBatch.end()
is called.SpriteBatch.end()
, flush()
the buffer to the GPU, so everything is drawn on the screen.Notes:
begin
state at once. So before begin()
ing a SpriteBatch call end()
on the other running SpriteBatchend()
only once per render loop, as it cost some performance.draw()
outside the begin-end block.Last but not least for this "Tutorial":
The camera
. It helps you calculating your movements... in your own worldunits instead of pixels. Also you can use it to show different parts of your gameworld by moving it arround.
First: set your camerasviewport in the construcotr:
camera = new OrthographicCamera(16, 9);
This sets a viewport of 16 width and 9 height. So your physical screen/monitor is now 16 units width and 9 units heigh. To apply this camera to your SpriteBatch call:
`SpriteBatch.setProjectionMatrix(cam.combined)`
By doing this the SpriteBatch
renders the things your camera is looking at.
The cameras 0,0 point by deffault is in its middle. So to draw something in the middle now use:
batch.draw(yourTexture, 0, 0);
To draw in the upper right corner use:
batch.draw(yourTexture, 16 / 2, 9 / 2);
To move your camera use:
cam.translate(16/2, 9/2);
Make sure to call cam.update()
after you change its position!
Now the right upper corner of your camera is at 16,9 instead of 16/2, 9/2. And the 0,0 point is the left lower corner.
I hope this was not to complicated for the begining. Some tutorials, which helped me to learn:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With