<<

LibGDX Notes

LibGDX Notes These notes assume that you have a familiarity with programming. If you do program in Java you probably use an integrated development environment (IDE) already. There are many possible choices for IDE’s, for beginners:

 BlueJ (www.bluej.org)  DrJava (www.drjava.org)

For more advanced there is:

(eclipse.org)  NetBeans (netbeans.org)  IntelliJ IDEA (www.jetbrains.com/idea/)

The first book I am tacking “Beginning Java Game Development with LibGDX” will use BlueJ. I will detail the steps to get the same “hello world” program with LibGDX up and running on BlueJ, Eclipse and NetBeans.

Using BlueJ 1. Obtain a copy of BlueJ at www.bluej.org

Figure 1 - www.bluej.org

BlueJ has a download installer for Windows, Mac OS X, Ubuntu/Debian, Raspbian Linux.

1

LibGDX Notes

Since I already have Java JDK installed I will select “BlueJ Installer”

How to tell if you have Java JDK installed.  Open a command prompt window  Enter “java –version”

2. The Window version for BlueJ is a *.msi file that you can install by double-clicking on it. You should see a desktop icon for BlueJ – what does it look like?

Figure 2 - Desktop Icon

3. Double-click the desktop icon

The application window is rather and simple – especially for all you developers that use Eclipse and NetBeans!

Figure 3 illustrates what the screen looks like when first started.

2

LibGDX Notes

Figure 3 - BlueJ Start Screen

4. From the menu select Project  New Project… 5. A “New Project” file dialog opens up. Navigate to the location of your BlueJ projects. I decided to place all my BlueJ projects under a directory BLUEJ and for this specific project to create a directory named HELLO_WORLD.

Figure 4 - Folder organization for my BlueJ projects

The screen will now appear as:

3

LibGDX Notes

Figure 5 - IDE after creating HELLO_WORLD project

6. Create a new class by clicking on the “New Class…” button.

Figure 6 - Enter name of the new class "HelloWorld"

7. Enter the class name “HelloWorld” and click on “Ok”

4

LibGDX Notes

Figure 7 - Updated BlueJ IDE with new class

8. Double-click on the “HelloWorld” rectangle or right-click over the rectangle and select “Open Editor”

You will see that the class has a lot of template code already inserted for you!

Figure 8 - HelloWorld with template code

9. Delete all the code and enter the following code:

Table 1 - HelloWorld.java code

/** * Write a description of class HelloWorld here. *

5

LibGDX Notes

* @author Lorraine Figueroa * @version May 28 2016 */ public class HelloWorld { public static void main() { System.out.println("Hello, World!"); }

}

The IDE should look like this:

Figure 9 - Our HelloWorld program

10. Click on the “Compile” button

You should see the following message at the bottom of the IDE in the status screen.

Figure 10 - Status message after compilation

11. Return to the main screen with the “HelloWorld” rectangle. Right-click and select “void main()”

6

LibGDX Notes

Figure 11 - Running the class main()

A terminal window will open up with the results of running the program:

Figure 12 - Result of running our HelloWorld program

To the left of your HelloWorld class as illustrated in Figure 13 is a README.TXT file that automatically got created when we created our BlueJ project.

12. Double-click on the README.TXT file in order to open up the contents of the file in a text editor.

7

LibGDX Notes

Figure 13 - Our Hello World Project

The original contents of the README.TXT file is shown below:

Table 2 - Original README.TXT file

------This is the project README file. Here, you should describe your project. Tell the reader (someone who does not know anything about this project) all he/she needs to know. The comments should usually include at least: ------

PROJECT TITLE: PURPOSE OF PROJECT: VERSION or DATE: HOW TO START THIS PROJECT: AUTHORS: USER INSTRUCTIONS:

A good will make sure that the README.TXT is updated and accurate so that years from now others will be able to find out the purpose of the project and know how to execute it.

8

LibGDX Notes

Table 3 - Update README.TXT file

PROJECT TITLE: HelloWorld PURPOSE OF PROJECT: This is a test project to get acquainted with BlueJ VERSION or DATE: 05/28/2016 HOW TO START THIS PROJECT: Execute the main in the class HelloWorld AUTHORS: L. Figueroa USER INSTRUCTIONS: N

If you plan on using BlueJ I highly recommend that you obtain the reference manual at https://www.bluej.org/doc/bluej-ref-manual.pdf and spend 15 to 20 minutes going over it in order to get an idea what features the IDE has and for future reference.

13. Head over to https://libgdx.badlogicgames.com/ to download the jar file for the LibGDX library.

Figure 14 - LibGDX banner

The Java framework library can be used to create programs for desktops, Android devices, iOS and HTML5.

14. Click the “Download” icon and head over to the “Releases” download folder and find the latest file in the format libgdx-x.x.x.zip.

Figure 15 - Downloading the latest LibGDX zip file

15. Unzip the contents of the zip file into a location we will use to add LibGDX to all our projects (and across IDEs). I will place it at :\LibGDX

You will find several jar files in the folder you extracted the zip file into:

9

LibGDX Notes

Figure 16 - Jar files in LibGDX

The files we want to use are:

 gdx.jar  gdx-natives.jar  gdx-backend-lwjgl.jar  gdx-backend-lwjgl-natives.jar

Since we want to use these files across many BlueJ projects let’s put the files in a location that will be available to all our BlueJ projects

16. Find the location of where BlueJ was installed. On my Windows system it was C:\Program Files (x86)\BlueJ.

Figure 17 - Location of BlueJ installation

10

LibGDX Notes

17. Copy the jar file noted above into the BlueJ \lib\userlib folder.

Figure 18 - Moving LibGDX jar file into BlueJ

18. Start or re-start BlueJ IDE. Close the old HelloWorld project if it automatically opened up. 19. Create a new project called ViewImage 20. Create a new class ViewImage with the contents as shown below:

Table 4 - ViewImage.java

/** * Write a description of class ViewImage here. * * @author Lorraine Figueroa * @version 5/29/2016 */

import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.Texture;

public class ViewImage extends Game { private Texture texture; private SpriteBatch batch;

public void create() { FileHandle worldFile = Gdx.files.internal("pong.png");

11

LibGDX Notes

texture = new Texture(worldFile); batch = new SpriteBatch(); }

public void render() { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

batch.begin(); batch.draw(texture, 0, 120); batch.end(); } }

21. Create another new class named GameLauncher with the contents:

Table 5 - GameLauncher.java

/** * Write a description of class GameLauncher here. * * @author Lorraine Figueroa * @version 5/29/2016 */ import com.badlogic.gdx.backends.lwjgl.LwjglApplication; public class GameLauncher { public static void main(String[] args) { ViewImage myProgram = new ViewImage(); LwjglApplication launcher = new LwjglApplication(myProgram); } }

22. Compile both programs and Run GameLauncher.

12

LibGDX Notes

Figure 19 - Executing GameLauncher

23. Just click “OK”

Figure 20 - Result of running GameLauncher

The nice feature in BlueJ is that you can “see” the relationship between files in the IDE.

13

LibGDX Notes

Figure 21 - BlueJ class relationships

The image above makes it clear that the GameLauncher invokes or includes a reference to the ViewImage object.

None of the methods in myProgram are actually invoked by ViewImage. It passes the myProgram object to another class LwjglApplication that manages to get the methods in ViewImage invoked so we see our image.

There are several advantages to using LibGDX as your Java game framework:

 It has methods for handling user input from the keyboard, mouse, game pad or touch screens  It can be used to render 2D graphics  It has methods to play sound effects or play music  It allows you to easily integrate other third party tools  It has methods to render 3D graphics and even load 3D models.

14

LibGDX Notes

Figure 22 - Soldier 3D model

At this time I will admit that using BlueJ for my IDE Java Development does not feel like a good idea. I will now detail how to get the ViewImage project constructed using Eclipse.

Using Eclipse I will have to admit that BlueJ seems simple and I could see how it can be quite appealing for students who are starting to learn how to program in Java but as a Java program I am used to having to do less work with respect to getting my imports in and also NOT having to compile every file. So I will step through how to recreate the LibGDX ViewImage using Eclipse.

1. Open Eclipse and create a new workspace to hold all our LibGDX project.

Figure 23 - Creating a workspace

Note: I am using a 64-bit OS (Windows 7), with 64-bit Java SDK and 64-bit version of Eclipse.

15

LibGDX Notes

2. Enter the workspace and create a new Java Project – ViewImage by right-clicking in the Project Explorer and selecting New  Project …  Java Project and click on Next>.

Figure 24 - Creating new Java Project

3. Enter the project name ViewImage and click on Finish.

Figure 25 - Our Eclipse Project Explorer

4. Highlight the src folder (as shown above) and right-click on New  Package and enter the name: com.att.brainycode.libgdx.examples. Click on Finish.

16

LibGDX Notes

Figure 26 - Project with new package

Before we add our first class let’s add the same four jars files we identified to the project.

5. Right-click the ViewImage project icon. Select “Properties” from the menu and the “Java Build Path” option

Figure 27 - Java Build Path dialog

6. Click on “Add External JARs…” button 7. Navigate to the location of the LibGDX jars and select (hold CTRL) the four jar files we want to add to the project.

17

LibGDX Notes

Figure 28 - Selecting the four LibGDX JAR files

8. Click Open.

Figure 29 - Adding the JAR to the build

9. Click on “OK”

18

LibGDX Notes

10. Highlight the package and create the Java class by right-clicking and selecting New  Class.

Figure 30 - Creating ViewImage class

11. Enter the Name and DO NOT forget to add Game as the superclass but clicking Browse… and entering the name “Game”

The file contexts for ViewImage will be the same as the version in BlueJ but with the package line. In addition, Eclipse will assist you when finding and adding the right imports to the file.

Table 6 - Eclipse version of ViewImage.java package com.att.brainycode.libgdx.examples; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class ViewImage extends Game {

19

LibGDX Notes

private Texture texture; private SpriteBatch batch;

@Override public void create() { FileHandle worldFile = Gdx.files.internal("pong.png"); texture = new Texture(worldFile); batch = new SpriteBatch(); }

public void render() { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

batch.begin(); batch.draw(texture, 0, 120); batch.end(); }

}

12. Create a new class named GameLauncher.

Table 7 - Eclipse version of GameLauncher.java

package com.att.brainycode.libgdx.examples;

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

public class GameLauncher {

public static void main(String[] args) { ViewImage myProgram = new ViewImage(); LwjglApplication launcher = new LwjglApplication(myProgram); }

}

20

LibGDX Notes

13. Try to run the program (I know it will not work since we have not placed the pong.png image in a location that it can be found). To run either click on the “Run” icon.

Figure 31- Eclipse run icon

Figure 32 - Run As dialog

14. Select Run As “Java Application”

You will see the following expected error message:

Table 8- Eclipse console error message

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: pong.png at com.badlogic.gdx.graphics.Pixmap.(Pixmap.java:140) at com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98) at com.badlogic.gdx.graphics.Texture.(Texture.java:100) at com.badlogic.gdx.graphics.Texture.(Texture.java:92)

21

LibGDX Notes

at com.att.brainycode.libgdx.examples.ViewImage.create(ViewImage.java:18) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120) Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: pong.png (Internal) at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136) at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:222) at com.badlogic.gdx.graphics.Pixmap.(Pixmap.java:137) ... 6 more

15. Highlight the project and create new folder New  Folder. Name it res (for resources) 16. Copy the pong.png image to the res folder.

Figure 33 - New "res" folder

We will need to add this directory to the class path so that all our resources are located.

17. Again right-click the project and select “Properties” and Java Build Path. Click on “Add External Class Folder…” button on the Libraries tab. 18. Navigate to the res folder in your project and click “OK”

22

LibGDX Notes

Figure 34 - Navigating to the res project

You will now see res added to the classpath:

Figure 35 - Adding res to the classpath

19. Run GameLauncher again you will now see the pong image.

23

LibGDX Notes

Figure 36 - Pong image from Eclipse.

The eclipse IDE is more complicated that BlueJ but it provides too many advantages that I require when doing development.

24

LibGDX Notes

25

LibGDX Notes

References

1. Curry, Michael. Java Game Programming. Self-published. 2. Stemkoski, Lee. Beginning Java Game Development with LibGDX. 2015. APRESS. 3. Nair, Suryakumar Balakrishan. Learning LibGDX Game Development. 2015. Packt Publishing. 4. Marquez, David Saltares. LibGDX Cross-platform Game Development Cookbook. 2014. Packt Publishing. 5. Cook, James. LibGDX Game Development By Example. 2015. Packt Publishing. 6. Bose, Juwal. LibGDX Game Development Essentials. 2014. Packt Publishing.

26