GAME DESIGN IN LIBGDX

GUIDE – STILL IMAGES

separator-blank.png

DESCRIPTION

Here is the basic code for images.

IMAGE SIZES

In LibGDX, image sizes need to be a power of two.  So the width and the height need to be one of the following sizes in pixels: 2, 4, 8, 16, 32, 64, 128, 256, 512, …

Yes this means that you might have to open up your images in a graphic editor such as Paint, Paint.NET or Photoshop.

FILE LOCATIONS

You must place your image(s) inside the assets folder of the core project.  Remember to make sure you can see the file from inside Eclipse.  If you can’t, simply right-click on the folder and hit Refresh.

CODE

public class StillImageDemo extends ApplicationAdapter

{

   private SpriteBatch batch;

   private Texture texture;

   private Sprite sprite;

 

   @Override

   public void create ()

   {

      batch = new SpriteBatch();

      texture = new Texture(Gdx.files.internal("smiley256.png"));

      sprite = new Sprite(texture);

   }

 

   @Override

   public void dispose()

   {

       batch.dispose();

       texture.dispose();

   }  

  

   @Override

   public void render()

   { 

         //UPDATE - none

        

       //Clear screen

         Gdx.gl.glClearColor(0, 0, 0, 1);

         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        

       //Render

       batch.begin();

       sprite.setPosition(200, 110);

       sprite.draw(batch);

       batch.end();

   }

}

JAR FILE

Click here.


separator-campeau.png