Having trouble getting started..(Noob libGDX question)

Hey guys. I’m new to LibGDX and fairly new to Java. I do however have experience in other languages such as C#. I’ve read through a basic java book and am now moving on to libgdx…It seems a lot of the tutorials are outdated…I’m working on a basic brick breaker game and using this tutorial: http://code.google.com/p/libgdx/wiki/SimpleApp

I’m just using the camera and spritebatch code and such from it…but it doesn’t seem to be working correctly. I’m getting an exception on the batch.draw method. When I run it, the application shows briefly, and it looks like the paddle is stretched across the screen, but then it shuts down with the exception. I’ve been searching around to no avail. I appreciate any help. I’ll post my code below as it isn’t much.

{EDIT} Actually, it doesn’t throw an exception with the code below…but the paddle disappears and it’s just a white screen.


package com.psillicoder.brickbreaker;

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;
import com.badlogic.gdx.math.Rectangle;


public class Game implements ApplicationListener {
   private OrthographicCamera camera;
   private SpriteBatch batch;
   //private Texture ballImage;
   private Texture paddleImage;
   
   //rectangles
   //Rectangle ballRect;
   Rectangle paddleRect;
   
   @Override
   public void create() {      
      float w = Gdx.graphics.getWidth();
      float h = Gdx.graphics.getHeight();
      
      paddleImage = new Texture(Gdx.files.internal("data/paddle.PNG"));
      //ballImage = new Texture(Gdx.files.internal("data/ball.png"));
      
      camera = new OrthographicCamera(1, h/w);
      batch = new SpriteBatch();
      
      //paddle
      paddleRect = new Rectangle();
      paddleRect.x = 400/2 - 64/2;
      paddleRect.y = 20;
      paddleRect.width = 64;
      paddleRect.height = 16;
      
      
      
      
      
      
      }
      

   @Override
   public void dispose() {
      batch.dispose();
      //ballImage.dispose();
      paddleImage.dispose();
   }

   @Override
   public void render() {      
      Gdx.gl.glClearColor(1, 1, 1, 1);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      camera.update();

      
      batch.setProjectionMatrix(camera.combined);
      batch.begin();
      
      batch.draw(paddleImage, paddleRect.x, paddleRect.y);
      batch.end();
   }

   @Override
   public void resize(int width, int height) {
   }

   @Override
   public void pause() {
   }

   @Override
   public void resume() {
   }

   

}

are you using opengl1.0 when loading the desktop version? Because if your images are not to the power of two the will not work. So make sure in your desktop file that this is in there:

cfg.useGL20 = true;

If that is not the case I am not sure…Maybe share the .java file of your desktop project and ill take a look, and also what is the resolution of your image?

usegl20 was set to false. I set it to true but got the same result. The size of the paddle image is 64x16. I’m not sure honestly what resolution…I just opened up paint and made a paddle image and saved as PNG. I’m guessing that is 24 bit?

Here is my new desktop file.


package com.psillicoder.brickbreaker;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

public class Main {
	public static void main(String[] args) {
		LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
		cfg.title = "BrickBreaker";
		cfg.useGL20 = true;
		cfg.width = 400;
		cfg.height = 600;
		
		new LwjglApplication(new Game(), cfg);
	}
}

Just went and tried your code in one of my games and commenting out this line of code seemed to solve it:

 // batch.setProjectionMatrix(camera.combined);

thats in the render method btw

Your exactly right…Thanks. I’ve been searching everywhere trying to figure it out. I’ve got a lot to learn.
I see from that tutorial that it sets the coordinate system specified by the camera…I guess the camera didn’t have a coordinate system specified. I should probably check into that next. Lol.

Sorry for such a dumb question, but again, thanks!

No problem, remember Things Take Time to learn If you liked my help would you be kind enough to hit the appreciation button on that post?:slight_smile:

No problem, but I don’t see an appreciate button?

its in the top right corner of that particular post

All I see is Reply and Quote. This is literally my first post here. Are there any post count restrictions? If so, i’ll do it as soon as i’m allowed.

[EDIT] I guess there is a post count restriction, but this post put me over it. The button is there now. Again, thanks!

Its usually right next to those two so I suppose that may be the case.

Got ya figured out.

Now onto separating the ball and paddle into separate classes…

do I need to pass the spritebatch to a render method in my ball and paddle class? or do I do it in the main render class?

Typically you would use OrthographicCamera like so:

public void create() {
    //sets up a camera to the viewport width/height
    cam = new OrthographicCamera(); 
}

public void resize(int width, int height) {
    //update the camera with new viewport
    cam.setToOrtho(false, width, height);
}

public void render() {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    //before rendering, you need to update the batch with the new camera matrix
    batch.setProjectionMatrix(cam.combined);

    batch.begin();
    // ... render sprites ... //
    batch.end();
}

The projection matrix now expects coordinates in “screen-space” from the lower-left origin. So rendering a 25x25 sprite at (0, 0) will render it in the lower left corner, and rendering it at (5, 10) will render it 5 pixels to the right, and 10 pixels up.

Typically you would pass SpriteBatch to other classes (like your Ball or Paddle class’s render method).

Another solution with LibGDX is to use the Stage and scene2D package. It has some added benefits that might come in handy down the road like hit-testing, integration with UI elements (tables, layouts, skins, etc), easing/tweening, etc.

If you choose to use Stage, your code will look a little different:

Stage stage;
Image sprite;

public void create() {
    stage = new Stage();

    //load the PNG texture
    Texture tex = new Texture("data/paddle.png");

    //create an Image which wraps the texture with position/size/etc
    image = new Image(tex);

    //set screen coordinates from lower-left origin
    image.setPosition(25, 25);

    //add the image to the screen
    stage.addActor(image);
}

public void resize(int width, int height) {
    stage.setViewport(width, height, false);
}

public void render() {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
}