Beginning Game Development with libGDX

Contents

Introduction

Game development is a complex yet fascinating topic and there are numerous approaches to game development from a beginner’s perspective. Years ago a game programmer had to master certain knowledge and skills to develop a game from start to finish but today it’s a different story. Many game frameworks with amazing features have been developed and are now freely available for game developers such as Unity, Unreal, libGDX, Construct, Phaser, Godot.

These frameworks both have a same goal: to make the process of creating game easier, cheaper and less time-consuming for everyone. Still, there are differences in the goals, scope, and philosophy of these frameworks. This article will discuss these differences and ultimately explain the choice of Java and libGDX as the language and tool for game development.

Framework scopes

Aside from their common goal of making game development more approachable, frameworks vary in features and scope. Widely known frameworks include Unity and Unreal Engine. These frameworks strive to provide all the tools and libraries users need to develop various types of games, from web and mobile games to the most advanced AAA games.

Unity

Advanced frameworks provide several abstraction layers over core game facilities such as 3D support out of the box, asset management, sound and audio handling, networking and math utilities, texture rendering, and more. Unity and Unreal also offer fully developed software development kits (SDKs), which are user-friendly software interfaces that enable users to manage various game development tasks.

However, the downsides of these frameworks include loading overhead and complexity, which can be excessive for small web or mobile games. Additionally, developers often need a deep understanding and fluency in the framework’s API to effectively utilize its features and optimize game performance, addressing hidden quirks and shortcomings.

On the other hand, there are lighter frameworks that implement similar abstractions but are limited to a smaller set of features. These frameworks are more attractive to casual developers who don’t need all the extras that larger frameworks offer. Notable examples include Godot, Phaserjs and libGDX.

Begin libGDX

Especially, the libGDX abstraction layer is remarkably thin and barebones, making it suitable for a wide range of games, from puzzles and card games to platformers or even RPGs. Some developers may prefer this abstraction because they have more controls over all aspects of the code base. By using only a thin layer to support cross-platform texture rendering with hardware acceleration, game developers are free to implement their own solution for designing and structure game objects.

Java and libGDX

Aside from its small footprint and low overhead, libGDX uses Java as its primary language, which can be both a pro and a con for developers. Java is notorious for its verbosity; however, its simplicity, stability, and low learning curve are attractive features that may be suitable for both beginners and seasoned developers.

Java can also take advantage of its strong support from all major IDEs and text editors, such as Eclipse, IntelliJ IDEA, NetBeans, and VSCode. These tools make testing, running, and debugging Java applications and games much easier compared to other languages.

libGDX at a glance

The core usage of libGDX lies in its SpriteBatch class, which is primarily used within the render loop:


    public void render() {

        // Clear the screen with white color
        ScreenUtils.clear(1, 1, 1, 1);

        // Retrieve delta time used to animate element
        float delta = Gdx.graphic.getDeltaTime();

        // Increase x and y position over time, 20 is a sample speed
        x += delta * 20;
        y += delta * 20;

        // Start drawing the ball texture with the size 100x100px in x, y positions
        batch.begin();
        batch.draw(ballTexture, x, y, 100, 100);
        batch.end();
    }

The code above draws a ball initially at position (0, 0) and animate it to the top right corner by increasing the ball spatial values (x, y). Each time the render method get called the screen is clear and the ball position are updated.

The code may be verbose and seems excessive for a simple task like moving a ball across the screen but you actually have a lot of controls over many aspects of the rendering process.

There are no dedicated objects like Sprite, Entity or Node in libGDX. Everything are Texture or TextureRegion, to make something show up on screen you draw its texture at desired position along with its width and height. More transformations such as scaling and rotation can be used with other batch.draw method signatures. You’re freely to build your own abstraction over the ball texture like BallSprite or BallEntity which all have custom properties that unique to the ball and the game you want to build.

Ball Move

This significantly reduces the memory footprint, making the framework very lightweight. Other features and advantages of libGDX will be discussed in later articles and tutorials.

Getting started: Hello libGDX

Install Java

To get started, install Java JDK if you haven’t done so. If you’re on MacOS or Linux it is recommended to use SDKMAN. If you’re on Windows, try scoop. These package managers will install and manage software versions faster and easier. If you’d prefer simpler approach, download JDK from Oracle is sufficient.

Install libGDX

The source code of libGDX is available on github but you don’t need to download the source to develop libGDX game. Instead, download gdx-setup.jar which is small UI application that generates and scaffolds template code to help you get started.

Run gdx-setup.jar

Open the terminal, create a new folder for your “Hello libGDX” game and run gdx-setup.jar in that folder:


    cd ~/data/ookigame-code
    mkdir hello-libgdx
    cd hello-libgdx
    java -jar path/to/gdx-setup.jar

This will run the setup application, fill out all fields Project name, Package name, Game Class, Output folder and disable Android and iOS platformers as you only use Desktop and HTML targets. Hit Generate button to generate project code templates:

gdx-setup

This will generate the project files:

hello-libgdx

Open the project in IDE

Follow the instruction to open the project files in your IDE. If you use IntelliJ IDEA, go to File - Open - build.gradle to open the file as project

idea-project

Double hit Ctrl key to open the Run Anything dialog, type gradle desktop:run to build and run the sample application

gradle-run

This will run our sample application and draw a sample badlogic.webp image in the lower left corner of the screen

run-hello

That’s it! Now you’re ready to develop a new game with libGDX!

Download the source hello-libgdx on github

To sum up

Game developers now have a wide variety of choices when selecting the best frameworks to accelerate development. While full-fledged frameworks like Unity or Unreal offer a plethora of advanced features, some developers may prefer more lightweight solutions with smaller footprints and lower overhead. In such cases, libGDX can be an ideal choice.