6

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>
Adinia
  • 3,722
  • 5
  • 40
  • 58
KLD
  • 75
  • 1
  • 8

1 Answers1

23

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:

  1. create(): called when your app is started
  2. dispose(): called when your game is closed (not crashed :P)
  3. pause(): called on android, when call is incoming/home button is pressed. On desktop when you minimize window/when the game lost the focus.
  4. resume(): called when you come back after pause state.
  5. resize(): called when you resize the window and on android, when screen rotates (you have to allow it in the manifest file)
  6. 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 Screens are parts of your game with different logic and view, so that they have to be in sepparate classes. Screens implement the Screen interface, which again gives you some useful methods:

  1. show(): called, when you set this Screen as the Games Screen with MyGdxGame.setScreen().
  2. hide(): called, when another Screen is set as the Games 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:

  1. ShapeRenderer, most times used for debug only. It can render simple shapes like circles, squares...
  2. SpriteBatch, used to render Textures Sprites...

So most times you will render using the SpriteBatch. You render in a block made of:

  1. SpriteBatch.begin(), starts the SpriteBatch. Set his viewPort, Matrices... before this call
  2. SpriteBatch.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.
  3. SpriteBatch.end(), flush() the buffer to the GPU, so everything is drawn on the screen.

Notes:

  1. Never have 2 SpriteBatches in their begin state at once. So before begin()ing a SpriteBatch call end() on the other running SpriteBatch
  2. If possible use only 1 SpriteBatch and try do call end() only once per render loop, as it cost some performance.
  3. Don't call 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:

  1. StarAssault
  2. Steigert's blog
William Price
  • 4,033
  • 1
  • 35
  • 54
Robert P
  • 9,398
  • 10
  • 58
  • 100
  • This answer was not at all too complicated, i followed you perfectly. Thanks a lot, i enjoyed your simple explanation, my motto is K.I.S.S.(Keep it Stupidly Simple). I also appreciate your links to the tutorials as well, that helps a lot. I'll go over everything, and let you know if i have any more questions. I would up vote you but i don't have enough reputation yet. Anyway, thanks a lot, keep up the good karma! – KLD Mar 10 '14 at 11:12
  • 2
    `resize()` is also called on Android when the screen rotates, if you set the Activity to allow it in the manifest. – Tenfour04 Mar 10 '14 at 12:28
  • @cortelldavis i am happy that i could help. I also enjoyed those tutorials when i was begining. So i wanted to share my knowledge to, even if i am still new and did not complete one game xD I would like to hear something about your progress, would be worth more then an upvote (in my opinion). If you have any more questions feel free to ask. SO is a great community and most people are here to help (: – Robert P Mar 10 '14 at 13:02
  • @Springrbua Ok i went to download StarAssault as a zip file. I extracted it into a separate folder on my desktop, then imported the folder into my eclipse workspace. When i go to run the project i get the following error code: Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: images/bob_01.png I have not modified the project in anyway, why isn't it finding the files included with the game project's asset directory? – KLD Mar 11 '14 at 05:54
  • @cortelldavis Did you look at the first part of the tutorial? There they show how to setup a project. In Eclipse you have to make sure, that the Assets folre is a source folder and is in the "order and export" list. Also the "DesktopProject" has the "AndroidProject" as dependency, because the Assets are in the Android project. Make sure all those things are done. Take a look at the first part of the tutorial there they show how to do – Robert P Mar 11 '14 at 06:57
  • @cortelldavis a verry important notice: In the StarAssault project, for some reason, they work with `ppuX` and `ppuY`. This is what the camera should do for you, so this is a bit missleading. Search for some camera tutorials, would be better. – Robert P Mar 11 '14 at 06:58
  • @Springrbua alright thanks, i read that, but for some reason i figured if i just downloaded the zip, it would already be handled. Guess i'll have to try it out and see, thanks a ton. – KLD Mar 11 '14 at 08:54
  • @cortelldavis does the StarAssault project work now? – Robert P Apr 07 '14 at 14:05
  • Pause is also called when the game loses focus, and resume gets called when the focus is back on the game. – thetheodor Jun 06 '14 at 16:06
  • @thetheodor yes, thats why minimizing the window on the desktop causes the game to pause. Thanks – Robert P Jun 11 '14 at 05:58