Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
LibGDX Game Development By Example

You're reading from   LibGDX Game Development By Example Learn how to create your very own game using the libGDX cross-platform framework

Arrow left icon
Product type Paperback
Published in Aug 2015
Publisher
ISBN-13 9781785281440
Length 280 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
James Cook James Cook
Author Profile Icon James Cook
James Cook
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting to Know LibGDX FREE CHAPTER 2. Let's Get These Snakes Out of This Book! 3. Making That Snake Slick 4. What the Flap Is the Hype About? 5. Making Your Bird More Flightworthy 6. Onto the Next Platform...Game 7. Extending the Platform 8. Why Are All the Birds Angry? 9. Even Angrier Birds! 10. Exporting Our Games to the Platforms 11. Third-party Services Index

Importing a project

So far, we have launched the "Hello World" game via the command line, and haven't seen a single line of code so far. Let's change that.

To do this, I will use IntelliJ IDEA. If you are using Android Studio, the screenshots will look familiar. If you are using Eclipse, I am sure you will be able to see the common concepts.

To begin with, we need to generate the appropriate IDE project files. Again, this is using Gradle to do the heavy lifting for us.

Once again, on the command line, run the following (pick the one that applies):

  • On Windows: gradlew idea or gradlew eclipse
  • On Linux and Mac OS X: ./gradlew idea or ./gradlew eclipse

Now, Gradle will have generated some project files. Open your IDE of choice and open the project.

Once the project is open, have a poke around and look at some of the files. I think our first port of call should be the build.gradle file in the root of the project. Here, you will see that the layout of our project is defined and the dependencies we require are on display.

It is a good time to mention that going forward, there will be new releases of LibGDX, and to update our project to the latest version, all we need to do is update the following property:

gdxVersion = '1.6.4'

Now, run your game and Gradle will kick in and download everything for you!

Next, we should look for our game class, remember the one we specified in the setup application—MyGdxGame.java? Find it, open it, and be in awe of how simple it is to display that red screen and Bad Logic Games logo. In fact, I am going to paste the code here for you to see how simple it is:

public class MyGdxGame extends ApplicationAdapter {
  SpriteBatch batch;
  Texture img;
  @Override
  public void create () {
    batch = new SpriteBatch();
    img = new Texture("badlogic.jpg");
  }

  @Override
  public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(img, 0, 0);
    batch.end();
  }
}

We will cover what all this means in the next chapter, but, essentially, we can see that when the create() method is called, it sets up a SpriteBatch batch and creates a texture from a given JPEG file. Then, on the render() method, this is called on every iteration of the game loop; it covers the screen with the color red, then it draws the texture at the (0, 0) coordinate location.

Finally, we will look at the DesktopLauncher class, which is responsible for running the game in the desktop environment. Let's take a look at the following code snippet:

public class DesktopLauncher {
  public static void main (String[] arg) {
    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    new LwjglApplication(new MyGdxGame(), config);
  }
}

The preceding code shows how simple it is. We have a configuration object that will define how our desktop application runs, setting things like screen resolution and framerate, amongst others. In fact, this is an excellent time to utilize the open source aspect of LibGDX. In your IDE, click through to the LwjglApplicationConfiguration class. You will see all the properties that can be tweaked and notes on what they mean.

The instance of the LwjglApplicationConfiguration class is then passed to the constructor of another class LwjglApplication, along with an instance of our MyGdxGame class.

Finally, those who have worked with Java a lot in the past will recognize that it is wrapped in a main method—a traditional entry point for a Java application.

That is all that is needed to create and launch a desktop-only LibGDX game.

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You have been reading a chapter from
LibGDX Game Development By Example
Published in: Aug 2015
Publisher:
ISBN-13: 9781785281440
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime