Kotlin/Native bindings for raylib 6.2.
Supported platforms:
- Linux x86-64 (
linuxX64) - Windows x86-64 (
mingwX64) - macOS Apple Silicon (
macosArm64) - Web (Kotlin/Wasm in the browser, see Web)
Add the dependency to your native source set:
kotlin {
sourceSets {
commonMain.dependencies {
implementation("io.github.enekocm7:raylib-kt:0.5.0")
}
}
}Add the dependency to your kargo.toml:
[dependencies]
"io.github.enekocm7:raylib-kt" = "0.5.0"The API is available from the raylib package:
import raylib.*fun main() {
InitWindow(600, 400, "raylib-kt")
SetTargetFPS(60)
while (!windowShouldClose) {
drawing {
ClearBackground(RAYWHITE)
DrawText("Hello from kotlin", 200, 200, 20, BLACK)
}
}
CloseWindow()
}raylib-kt can run in the browser, like C raylib's PLATFORM_WEB. Kotlin/Native has no Emscripten target, so the web target is Kotlin/Wasm ([target.wasm] environment = "browser") talking to raylib compiled with Emscripten (PLATFORM_WEB, OpenGL ES 2.0 / WebGL).
Build the library for the web with:
kargo script scripts/build-raylib-web.ktThis downloads and activates a pinned emsdk under build/ (it needs git, plus python3 on Linux and macOS), generates src-wasm/RaylibBindings.kt and a C shim from raylib's raylib_api.json, and writes raylib.mjs and raylib.wasm to build/web/dist/. The task is also available as kargo task raylib-web.
examples/web is a complete project. Its resources/index.html is the HTML shell to copy: it loads raylib.mjs, exposes it as the global raylib, and only then imports the Kotlin module (which runs main as soon as it loads). Put raylib.mjs and raylib.wasm next to it (the build script copies them into examples/web/resources/) and run kargo run there.
- Main loop. A browser tab cannot run a blocking
while (!windowShouldClose)loop. UsemainLoop { ... }instead: on native it is that loop, on the web it schedules frames withrequestAnimationFrameand returns immediately, so code after it does not run when the window closes. - Frame rate.
SetTargetFPSis accepted and ignored on the web; the browser paces frames to the display.windowShouldCloseandWaitTimeare not available. - Assets. raylib reads files from Emscripten's virtual file system. Call
preloadAssets("assets/ship.png", "assets/hit.wav") { ... }to download files (paths relative to the page) into it, then load them as usual and startmainLoopfrom the callback. - Shipping
raylib.mjs/raylib.wasm. The Kotlin/Wasm library only contains the bindings; it does not bundle the Emscripten build. Copy the two files into your page next to the generated module, as in the example.
The web bindings are generated, so function and constant names match raylib, but the types differ because there is no cinterop:
- Structs are plain Kotlin data classes (
Color,Vector2,Rectangle,Texture, ...).ColorisColor(r, g, b, a = 255)withIntcomponents, not aCValue<Color>. - Only functions whose arguments are scalars, strings and structs are bound (about 440 of raylib's 620). Anything taking or returning a pointer, array, callback or varargs (for example
DrawTriangleFan,LoadFileData,SetTraceLogCallback,TextFormat,SetShaderValue) is not available yet. raymath.handrlgl.hare not exposed.- Native-only raylib features do not apply on the web: threads, file dialogs, and most window management (monitors, positions, minimizing). Audio starts only after a user gesture, as browsers require.
The published package includes the raylib static libraries, so you do not need to install raylib separately.
Building from source needs Kargo, which downloads the JDK, Kotlin/Native compiler and native toolchains automatically.
sudo apt update
sudo apt install git curl tar unzip \
libasound2-dev libgl1-mesa-dev libglu1-mesa-dev \
libwayland-dev libwayland-bin libxkbcommon-dev \
libx11-dev libxrandr-dev libxi-dev libxcursor-dev libxinerama-devsudo dnf install git curl tar unzip \
alsa-lib-devel mesa-libGL-devel mesa-libGLU-devel \
wayland-devel libxkbcommon-devel \
libX11-devel libXrandr-devel libXi-devel libXcursor-devel libXinerama-develsudo pacman -S --needed base-devel git curl tar unzip \
alsa-lib mesa glu wayland libxkbcommon \
libx11 libxrandr libxi libxcursor libxineramasudo zypper install git-core curl tar unzip \
alsa-devel Mesa-libGL-devel Mesa-libGLU-devel \
wayland-devel libxkbcommon-devel \
libX11-devel libXrandr-devel libXi-devel libXcursor-devel libXinerama-develFor other Linux distributions, install the equivalent X11, Wayland, xkbcommon, OpenGL, and ALSA development packages. The wayland-scanner command must be available on PATH.
Install the Xcode command-line tools so xcrun and the macOS SDK are available:
xcode-select --installClone the repository:
git clone https://github.com/enekocm7/raylib-kt.git
cd raylib-ktkargo buildkargo build first runs the raylib task (scripts/build-raylib.kt), which downloads and builds raylib and bundles its headers and static libraries under cinterop/, and then builds the library. Apple targets can only be built on macOS, so on Linux and Windows remove "macos_arm64" from [target.native] platforms in kargo.toml first.
If mise is installed, the same build can be run with:
mise run buildTo publish the package to your local Maven repository:
mise run publishLocalraylib-kt is distributed under the zlib/libpng license.