Libgdx Notes
Total Page:16
File Type:pdf, Size:1020Kb
LibGDX Notes LibGDX Notes These notes assume that you have a familiarity with Java 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 programmers there is: Eclipse (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 basic 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 programmer 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 C:\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.