LibGDX
lastmod 2025-02-14created 2024-03-259 edits
libGDX is a free and open-source game development application framework written in Java, with some C and C++ components for performance-dependent code. It allows desktop and mobile game development using the same code base. It is cross-platform, supporting Windows, Linux, Mac OS X, Android, iOS, BlackBerry OS, and web browsers with WebGL support.
libGDX is a cross-platform Java game development framework based on OpenGL (ES), designed for Windows, Linux, macOS, Android, web browsers, and iOS. It provides a robust and well-established environment for rapid prototyping and iterative development. Unlike other frameworks, libGDX does not impose a specific design or coding style, allowing you the freedom to create games according to your preferences.
Categories
Ecosystem
Project setup
A few notes:
- Currently (2024-03-26) Gradle does not yet support JDK 19, so neither does libGDX. Use JDK versions 11 to 18. See that page for details.
- Also note that HTML Export is not supported when using Kotlin.
- In the second step, download the gdx-setup.jar file and run it with
java -jar gdx-setup.jarto generate a project. - Note that Android projects currently (2024-03-26) require downloading SDK API Level 33.
- Generate downloads a lot of things. Note that it takes quite a while.
- Bullet: 3D Collision Detection and Rigid Body Dynamics Library.
- FreeType: Scalable font. Great to manipulate font size dynamically. However be aware that it does not work with HTML target if you cross compile for that target.
- Tools: Set of tools including: particle editor (2d/3d), bitmap font and image texture packers.
- Controller Library to handle controllers (e.g.: XBox 360 controller).
- Box2d: Box2D is a 2D physics library.
- Box2dlights: 2D lighting framework that uses box2d for raycasting and OpenGL ES 2.0 for rendering.
- Ashley: A tiny entity framework.
- Ai: An artificial intelligence framework.
The output log is as follows:
Generating app in /home/user/Project/cw
Executing '/home/user/Project/cw/gradlew clean --no-daemon'
Downloading https://services.gradle.org/distributions/gradle-8.5-bin.zip
............10%.............20%............30%.............40%.............50%............60%.............70%.............80%............90%.............100%
Welcome to Gradle 8.5!
Here are the highlights of this release:
- Support for running on Java 21
- Faster first use with Kotlin DSL
- Improved error and warning messages
For more details see https://docs.gradle.org/8.5/release-notes.html
To honour the JVM settings for this build a single-use Daemon process will be forked. For more on this, please refer to https://docs.gradle.org/8.5/userguide/gradle_daemon.html#sec:disabling_the_daemon in the Gradle documentation.
Daemon will be stopped at the end of the build
> Task :android:clean UP-TO-DATE
> Task :core:clean UP-TO-DATE
> Task :desktop:clean UP-TO-DATE
> Task :html:clean
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.5/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 32m 28s
4 actionable tasks: 1 executed, 3 up-to-date
Done!
To import in Eclipse: File -> Import -> Gradle -> Existing Gradle Project
To import to Intellij IDEA: File -> Open -> build.gradle
To import to NetBeans: File -> Open Project...
Project Layout
This will create a directory called mygame with the following layout:
settings.gradle <- definition of sub-modules. By default core, desktop, android, html, ios
build.gradle <- main Gradle build file, defines dependencies and plugins
gradlew <- local Gradle wrapper
gradlew.bat <- script that will run Gradle on Windows
gradle <- script that will run Gradle on Unix systems
local.properties <- IntelliJ only file, defines Android SDK location
assets/ <- contains your graphics, audio, etc.
core/
build.gradle <- Gradle build file for core project
src/ <- Source folder for all your game's code
desktop/
build.gradle <- Gradle build file for desktop project
src/ <- Source folder for your desktop project, contains LWJGL launcher class
android/
build.gradle <- Gradle build file for android project
AndroidManifest.xml <- Android specific config
res/ <- contains icons for your app and other resources
src/ <- Source folder for your Android project, contains android launcher class
html/
build.gradle <- Gradle build file for the html project
src/ <- Source folder for your html project, contains launcher and html definition
webapp/ <- War template, on generation the contents are copied to war. Contains startup url index page and web.xml
ios/
build.gradle <- Gradle build file for the iOS project
src/ <- Source folder for your iOS project, contains launcher
Jython Setting
Basic
@Override
public void create() { }
@Override
public void resize(int width, int height) { }
@Override
public void dispose() { }
@Override
public void render() { }
@Override
public void pause() { }
@Override
public void resume() { }
-
create()- As the name suggests, this function is called by default when the game is created. It is the same as Android's onCreate(). It is used to initialize the class's variables. -
resize(int width, int height)- Runs when the game's size changes; the arguments width and height are the new size of the game screen. People who have never developed a game may have no idea what to do when the size changes. In Libgdx, this function mainly redefines a variable called viewpoint, which I'll cover in the next post. -
dispose()- Called when memory runs low. If some variable is holding a lot of memory unnecessarily while the game is running, handle it inside the dispose function. -
render()- The most important function in Libgdx. It is called 60 times per second and is responsible for actually drawing the screen. It seems to aim to guarantee at least 60fps. Developers draw their graphics in this function. -
pause()- Like Android's onPause(), called when the game is paused or terminated midway. -
resume()- Also like Android's onResume(), called when the game resumes from the Pause state.
Projects used
- Simply Piano
- Slay the Spire by Mega Crit Games
See also
Favorite site
- libGDX - Wikipedia
- libgdx web site
- Github - libgdx project site
- An In-Depth Analysis of LibGDX vs Pygame | Aircada Pro (LibGDX vs Pygame)