Skip to content

carlwilk32/sdl-jna-shooter

Repository files navigation

Long time ago in a galaxy far, far away...

This is an example of what happens when Enterprise Java meets Game development.

SDL2 | JNA | DI | Java 2D Shooter

Prerequisites

You will need any JRE and SDL2 libs at minimum.

Installing SDL2

This project uses Simple DirectMedia Layer, which is written in C and doesn't have direct Java mappings. We will use JNA calls to access it from Java. In order to do so the following SDL2 libs should be available in your system:

  • SDL2
  • SDL2_image
  • SDL2_ttf
  • SDL2_mixer

There are different ways obtaining and installing the above. One can download it directly from libsdl-org GitHub releases for their specific system or use your system's package manager.

MacOS

using homebrew

    brew install sdl2 sdl2_image sdl2_ttf sdl2_mixer
Arch Linux

using yay

    yay -S sdl2 sdl2_image sdl2_ttf sdl2_mixer

You can also build them manually from sources, but this process will not be covered here. To do so please refer to SDL documentation here.

Build

./gradlew clean build

Run

./gradlew run

Known issues

JNA can't find SDL

JNA tries to load libraries from default system locations, but sometimes it might fail.

If game didn't launch for you with similar exceptions:

UnsatisfiedLinkError: Unable to load library 'SDL2'
... 
java.io.IOException: Native library (darwin-aarch64/libSDL2.dylib) not found in resource pat

double check that you have installed SDL libs and they're available in your system.

For example, in my system

pkg-config --libs sdl2

results in -L/usr/local/lib -lSDL2

Then provide above path explicitly to JNA by specifying it in JvmArgs using -Djna.library.path=/usr/local/lib/. It should work now.