Hello World - libGDX Batch Questions

I just learn about libGdx and find it’s good. However I have several questions regarding their example of Hello World,

  1. To change OpenGL between LWJGL and JOGL, is it really simple enough by just do this
new LwjglApplication(new HelloWorld(), "Hello World", 480, 320, false);

without change anything in backend (HelloWorld class)?

  1. When I did above, it works. However the window with LwjglApplication didn’t allow you to resize. How to fix it?

  2. When I have a class implementing ApplicationListener with custom constructor, which one called first, the constructor or create()?

  3. In moving the text, why need to multiply 60 instead declare “new Vector2(60, 60)”? does it to create smooth move?

textPosition.x += textDirection.x * Gdx.graphics.getDeltaTime() * 60;
  1. How the “Gdx.Graphics.getDeltaTime()” calculated? If it’s calculated from SpriteBatch.begin() to SpriteBatch.end() then the time taken for update game logic isn’t counted right?

  2. There is a commented line. I uncommented it but only see the text moved faster. What exactly it did?

textPosition.add(textDirection.tmp().mul(Gdx.graphics.getDeltaTime()).mul(60));
  1. Is there advantage to use Vector2 instead Point, and awt.Color instead gdx.graphics.Color?

  2. Is it right to reuse BitmapFont to draw multiply Strings?

  3. When I cropping texture with SpriteBatch.draw, the 0,0 position of texture relative to screen is left bottom, while cropping area (x, y, width, height or constructing TextureRegion) using left up as 0,0. Is it true? just to be sure.

  4. Whatever game it is, I should implement ApplicationListener and declare SpriteBatch right? Is Gdx.graphics static?

Thanks. ;D

  1. Yup

  2. You can’t resize yet.

  3. If you don’t know the answer to this one, I don’t think you are ready to start OpenGL. You need read up a bit more on Java and objects. The constructor is always called first before anything in every single class that ever exists.

  4. Who says you have to?

  5. getDeltaTime() returns the amount of time since the last frame, it has nothing to do with SpriteBatch.

  6. That does the same thing as line 4 except it also applies it to the Y variable.

  7. You cannot use java.awt.Color in libGDX. In fact, it is best to completely avoid AWT.

  8. Of course! BitmapFont describes a font and you use it draw all your Strings of that specific font. You only create new BitmapFonts when creating a different font.

  9. According to the JavaDocs, the (x,y) position is always the bottom left for everything.

  10. O_o What happened to #10? :stuck_out_tongue:

  11. You will need SpriteBatch to render textures, fonts, etc… so yes. All the public instance variables in Gdx are static.

I should now it’ll be you who answer first. thanks ;D

3, 7 and 11 are just to be sure, how do you think I can come to this “far” anyway 8) Honestly I want to argue about number 9, but need time first to prove it one more time. I took that bottom left in mind cropping an image and it resulted wrong.

And number 10 is still on vacation.

This is what I mean from no.9

I wan to get left up clip of an image (1)
| 1 | 2 |
| 3 | 4 |

yet the code become into


spriteBatch.draw(texture, 
				centerX - texture.getWidth() / 4, centerY - texture.getHeight() / 4, 
				texture.getWidth()/2, texture.getHeight()/2, 
				0,0,
				texture.getWidth()/2, texture.getHeight()/2, 
				false, false);

The coordinates and sizes are in texels so that might be why.

texels? actually it’s not really problem, just odd when I have to have different point of view when imagining crop area LoL.

Coorndinate and sizes are just in arbitary units. Sprite batch projection matrix will tell how those map to screen. By default it’s pixel perfect so then those size are 1:1 pixels but never they are texels.

Careful with absolutes. :slight_smile: If a super class constructor calls a method that has been overridden by a subclass, the subclass’ method will be called before the subclass’ constructor.

The javadocs specify the origin for things. Sometimes it is lower left, sometimes it is upper left. This is part of the fun! :wink: Kidding, lower left origin is typically used for coordinates, since y up makes mathematical sense. If you don’t like it (I hate it personally), you can change the SpriteBatch projection matrix. the easy way to do this is to use OrthographicCamera. See the examples for this. Texture coordinates use upper left origin and y down, since that is typical for bitmaps.

Thanks for replies (and for not let me alone with ra4king :P)

@Nate: When I read ra4king’s post, I go back to code and see it’s implementing (not extending) so I can know the answer :wink: I’m still on first step so changing projection matrix is a bit too much for me now. I’m just curious with people shouting this lib over threads so I want to test out.

You like it and you know it!

  1. Utils package has many class, what is the best choice to replace ArrayList and HashMap?
  2. FPS, without any line code, is it true that lib set it to 60fps?
  3. To turn an app with lwjgl backend into applet, what should I do? change LwjglApplication with LwjglApplet?

public static void main (String[] argv) {
		new LwjglApplication(new Game(), "blah", 750, 500, false);
}

  1. Why would you want to replace those? They each do their own respective jobs quite fast already.

  2. Yes.

  3. Extend LwjglApplet and in the constructor call “super(new Game(),false);” as per the javadocs. :wink:

Thanks! time to rock! (and come again with more questions)

Hehe question again

  1. because I still have java2d concept in mind, is it bad to treat libgdx’s TextureRegion as j2d’s Image/BufferedImage? currently I store sprite using TextureRegion

  2. Apart from above question, why I got

http://dl.dropbox.com/u/54138920/img/po.jpg
with this code


Texture sprites = new Texture(Gdx.files.internal("res/sprites.png"));
textures.put("ship", new TextureRegion(sprites, 0, 0, 80, 42)); //ship correct
textures.put("missile", new TextureRegion(sprites, 0, 50, 32, 16)); //missile draw ship's part?!

looks like on second TextureRegion’s constructor, the params X and Y don’t work ??? the sprites were drawn correctly in place and rechecked using image editor.

  1. Do you think it’s killing a fly with RPG by using this lib to just create simple 2D game, just because I want nice particle effect?
  1. I don’t know, I’ve never used TextureRegion. I always just use Texture :slight_smile:

  2. What’s “textures”? A HashMap? Double check the original image to make sure you’re not doing anything wrong.

  3. No, libGDX is good enough for anything, even the simplest projects :slight_smile:

For more detailed questions, you should join the #badlogic channel on APIStudios.

IRC not allowed here 8)
I did want to just use texture but I create sprite by cropping around from one large file so I use TextureRegion. And textureRegion.getTexture() give weird result.

  1. Really, does libgdx only support fnt font, not ttf? many cool free fonts on ttf format. I converted one but it said “invalid font”.
  1. Array and ObjectMap
  2. TextureRegions work as expect. What you ran into is called texture filtering. This happens if you don’t render your image at pixel perfect positions, with pixel perfect sizes.
  3. ttf support is in soon via stb-truetype, it has some limitations though (chinese/arabic might be problematic, very large font sizes, e.g. 130pt, might be to large).

Thanks for replying! how about number 16? can’t solve it until now…