Skip to content

Using libgdx with Clojure

jvnn edited this page Oct 13, 2013 · 8 revisions

Clojure is a dialect of the Lisp programming language, written for the JVM and with functional programming in mind. Clojure comes with native Java interoperability, making it able to leverage the powerful existing libraries in the Java ecosystem. There are a few existing sources for use of Clojure game development using Libgdx.

This article assumes you have Leiningen installed, along with moderate knowledge of Clojure, or a Lisp. ClojureTV on YouTube has a lot of good videos, specifically Clojure for Java Programmers (Part 2).

project.clj

Because Libgdx is not hosted on any of Leiningen's default maven repos, we need to add our own, in the Sonatype repos.

(defproject cljdx "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :license {:name "Apache License, Version 2.0"
          :url "http://www.apache.org/licenses/LICENSE-2.0.html"
          :distribution :repo
          :comments "Same as LibGDX"}
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [com.badlogicgames.gdx/gdx "0.9.9-SNAPSHOT"]
                 [com.badlogicgames.gdx/gdx-backend-lwjgl "0.9.9-SNAPSHOT"]
                 [com.badlogicgames.gdx/gdx-platform "0.9.9-SNAPSHOT"
                  :classifier "natives-desktop"]]
  :repositories [["sonatype"
                  "https://oss.sonatype.org/content/repositories/snapshots/"]]
  :source-paths ["src/clojure"]
  :java-source-paths ["src/java"]
  :aot [cljdx.desktop-launcher]
  :main cljdx.desktop-launcher)

The code can be translated in a fairly straightforward manner:

here is the equivalent to Main.java, which we are naming cljdx.desktop-launcher:

(ns cljdx.desktop-launcher
  (:require [cljdx.core])
  (:import [com.badlogic.gdx.backends.lwjgl LwjglApplication])
  (:gen-class))

(defn -main
  []
  (LwjglApplication. (cljdx.core.Game.) "cljdx" 800 600 true))

and here is the Screen implementation (as cljdx.core)

(ns cljdx.core
  (:import [com.badlogic.gdx Game Gdx Graphics Screen]
           [com.badlogic.gdx.graphics Color GL20]
           [com.badlogic.gdx.graphics.g2d BitmapFont]
           [com.badlogic.gdx.scenes.scene2d Stage]
           [com.badlogic.gdx.scenes.scene2d.ui Label Label$LabelStyle]))

(gen-class
  :name cljdx.core.Game
  :extends com.badlogic.gdx.Game)

(def main-screen
  (let [stage (atom nil)]
    (proxy [Screen] []
      (show []
        (reset! stage (Stage.))
        (let [style (Label$LabelStyle. (BitmapFont.) (Color. 1 1 1 1))
              label (Label. "Hello world!" style)]
          (.addActor @stage label)))
      (render [delta]
        (.glClearColor (Gdx/gl) 0 0 0 0)
        (.glClear (Gdx/gl) GL20/GL_COLOR_BUFFER_BIT)
        (doto @stage
          (.act delta)
          (.draw)))
      (dispose[])
      (hide [])
      (pause [])
      (resize [w h])
      (resume []))))

(defn -create [^Game this]
  (.setScreen this main-screen))

and thats all the code necessary! a lein uberjar will generate the required runnable jar!

Games written in Clojure using Libgdx

Misc. info

Table of Contents

a note from the translation

Wiki Style Guide

Clone this wiki locally