Skip to content

Commit

Permalink
Merge branch 'main' into gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
damn committed Dec 13, 2023
2 parents 334f5a2 + c809640 commit 0cee7b2
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 204 deletions.
11 changes: 11 additions & 0 deletions deploy_docs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set -e

git checkout gh-pages
git merge main --commit
lein codox
rm -rf docs
mv target/doc docs
git add docs
git commit -m 'Update docs'
git push
git checkout main
155 changes: 130 additions & 25 deletions src/gdl/app.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,141 @@
(:require [clojure.string :as str]
[x.x :refer [defcomponent update-map]]
[gdl.lc :as lc]
[gdl.draw :as draw]
[gdl.graphics.viewport :as viewport]
[gdl.graphics.shape-drawer :as shape-drawer]
[gdl.scene2d.ui :as ui])
(:import (com.badlogic.gdx Gdx ApplicationAdapter)
com.badlogic.gdx.audio.Sound
com.badlogic.gdx.assets.AssetManager
com.badlogic.gdx.files.FileHandle
com.badlogic.gdx.utils.ScreenUtils
(com.badlogic.gdx.graphics Color Texture OrthographicCamera)
com.badlogic.gdx.graphics.g2d.SpriteBatch
(com.badlogic.gdx.utils Align ScreenUtils)
(com.badlogic.gdx.graphics Color Texture OrthographicCamera Pixmap Pixmap$Format)
(com.badlogic.gdx.graphics.g2d Batch SpriteBatch BitmapFont TextureRegion)
(com.badlogic.gdx.backends.lwjgl3 Lwjgl3Application Lwjgl3ApplicationConfiguration)
com.badlogic.gdx.utils.SharedLibraryLoader
(com.badlogic.gdx.utils.viewport Viewport FitViewport)))
(com.badlogic.gdx.utils.viewport Viewport FitViewport)
space.earlygrey.shapedrawer.ShapeDrawer))

(defn render-with [{:keys [^SpriteBatch batch
(defn- degree->radians [degree]
(* degree (/ (Math/PI) 180)))

(defn- text-height [^BitmapFont font text]
(-> text
(str/split #"\n")
count
(* (.getLineHeight font))))

(defn- draw-texture [^Batch batch texture [x y] [w h] rotation color]
(if color (.setColor batch color))
(.draw batch texture
x
y
(/ w 2) ; rotation origin
(/ h 2)
w ; width height
h
1 ; scaling factor
1
rotation)
(if color (.setColor batch Color/WHITE)))

(defn- unit-dimensions [unit-scale image]
(if (= unit-scale 1)
(:pixel-dimensions image)
(:world-unit-dimensions image)))

(defrecord Drawer [batch unit-scale default-font ^ShapeDrawer shape-drawer]
draw/Drawer
(text [_ {:keys [font text x y h-align up?]}]
(let [^BitmapFont font (or font default-font)
data (.getData font)
old-scale (.scaleX data)]
(.setScale data (float (* old-scale unit-scale)))
(.draw font
batch
(str text)
(float x)
(float (+ y (if up? (text-height font text) 0)))
(float 0) ; target-width
(case (or h-align :center)
:center Align/center
:left Align/left
:right Align/right)
false) ; wrap false, no need target-width
(.setScale data (float old-scale))))
(image [_ {:keys [texture color] :as image} position]
(draw-texture batch texture position (unit-dimensions unit-scale image) 0 color))
(image [this image x y]
(image this image [x y]))
(rotated-centered-image [_ {:keys [texture color] :as image} rotation [x y]]
(let [[w h] (unit-dimensions unit-scale image)]
(draw-texture batch
texture
[(- x (/ w 2))
(- y (/ h 2))]
[w h]
rotation
color)))
(centered-image [this image position]
(draw/rotated-centered-image this image 0 position))
(ellipse [_ [x y] radius-x radius-y color]
(.setColor shape-drawer ^Color color)
(.ellipse shape-drawer (float x) (float y) (float radius-x) (float radius-y)) )
(filled-ellipse [_ [x y] radius-x radius-y color]
(.setColor shape-drawer ^Color color)
(.filledEllipse shape-drawer (float x) (float y) (float radius-x) (float radius-y)))
(circle [_ [x y] radius color]
(.setColor shape-drawer ^Color color)
(.circle shape-drawer (float x) (float y) (float radius)))
(filled-circle [_ [x y] radius color]
(.setColor shape-drawer ^Color color)
(.filledCircle shape-drawer (float x) (float y) (float radius)))
(arc [_ [centre-x centre-y] radius start-angle degree color]
(.setColor shape-drawer ^Color color)
(.arc shape-drawer centre-x centre-y radius start-angle (degree->radians degree)))
(sector [_ [centre-x centre-y] radius start-angle degree color]
(.setColor shape-drawer ^Color color)
(.sector shape-drawer centre-x centre-y radius start-angle (degree->radians degree)))
(rectangle [_ x y w h color]
(.setColor shape-drawer ^Color color)
(.rectangle shape-drawer x y w h) )
(filled-rectangle [_ x y w h color]
(.setColor shape-drawer ^Color color)
(.filledRectangle shape-drawer (float x) (float y) (float w) (float h)) )
(line [_ [x y] [ex ey] color]
(draw/line shape-drawer x y ex ey color))
(line [_ x y ex ey color]
(.setColor shape-drawer ^Color color)
(.line shape-drawer (float x) (float y) (float ex) (float ey)))
(with-line-width [this width draw-fn]
(let [old-line-width (.getDefaultLineWidth shape-drawer)]
(.setDefaultLineWidth shape-drawer (float (* width old-line-width)))
(draw-fn this)
(.setDefaultLineWidth shape-drawer (float old-line-width)))))

(defn render-with [{:keys [^Batch batch
shape-drawer
gui-camera
world-camera
world-unit-scale]
:as context}
gui-or-world
renderfn]
draw-fn]
(let [^OrthographicCamera camera (case gui-or-world
:gui gui-camera
:world world-camera)
unit-scale (case gui-or-world
:gui 1
:world world-unit-scale)]
(shape-drawer/set-line-width unit-scale)
:world world-unit-scale)
drawer (-> context
(select-keys [:batch :default-font :shape-drawer])
(assoc :unit-scale unit-scale)
map->Drawer)]
(.setColor batch Color/WHITE) ; fix scene2d.ui.tooltip flickering
(.setProjectionMatrix batch (.combined camera))
(.begin batch)
(renderfn (assoc context :unit-scale unit-scale))
(.end batch)
(shape-drawer/set-line-width 1)))
(draw/with-line-width drawer unit-scale draw-fn)
(.end batch)))

(defn- update-viewports [{:keys [gui-viewport world-viewport]} w h]
(let [center-camera? true]
Expand All @@ -55,17 +156,10 @@
(defn- recursively-search-files [folder extensions]
(loop [[^FileHandle file & remaining] (.list (.internal Gdx/files folder))
result []]
(cond (nil? file)
result

(.isDirectory file)
(recur (concat remaining (.list file)) result)

(extensions (.extension file))
(recur remaining (conj result (str/replace-first (.path file) folder "")))

:else
(recur remaining result))))
(cond (nil? file) result
(.isDirectory file) (recur (concat remaining (.list file)) result)
(extensions (.extension file)) (recur remaining (conj result (str/replace-first (.path file) folder "")))
:else (recur remaining result))))

(defn- load-assets [^AssetManager manager folder file-extensions ^Class klass log-load-assets?]
(doseq [file (recursively-search-files folder file-extensions)]
Expand Down Expand Up @@ -94,22 +188,33 @@

(defcomponent :batch batch
(lc/dispose [_]
(.dispose ^SpriteBatch batch)))
(.dispose ^Batch batch)))

(defcomponent :assets manager
(lc/dispose [_]
(.dispose ^AssetManager manager)))

(defcomponent :shape-drawer-texture texture
(lc/dispose [_]
(.dispose ^Texture texture)))

(defn- default-components [{:keys [tile-size]}]
(let [batch (SpriteBatch.)]
(merge {:batch batch
:gdl.graphics.shape-drawer batch
:assets (load-all-assets {:folder "resources/" ; TODO these are classpath settings ?
:sound-files-extensions #{"wav"}
:image-files-extensions #{"png" "bmp"}
:log-load-assets? false})
; this is the gdx default skin - copied from libgdx project, check not included in libgdx jar somewhere?
:gdl.scene2d.ui (ui/skin (.internal Gdx/files "scene2d.ui.skin/uiskin.json"))}
(let [texture (let [pixmap (doto (Pixmap. 1 1 Pixmap$Format/RGBA8888)
(.setColor Color/WHITE)
(.drawPixel 0 0))
texture (Texture. pixmap)]
(.dispose pixmap)
texture)]
{:shape-drawer (ShapeDrawer. batch (TextureRegion. texture 0 0 1 1))
:shape-drawer-texture texture})
(let [gui-camera (OrthographicCamera.)
gui-viewport (FitViewport. (.getWidth Gdx/graphics)
(.getHeight Gdx/graphics)
Expand Down
31 changes: 31 additions & 0 deletions src/gdl/draw.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns gdl.draw)

(defprotocol Drawer
(text [_ {:keys [font text x y h-align up?]}])
(image [_ image position]
[_ image x y])
(centered-image [_ image position])
(rotated-centered-image [_ image rotation position])
(ellipse [_ position radius-x radius-y color])
(filled-ellipse [_ position radius-x radius-y color])
(circle [_ position radius color])
(filled-circle [_ position radius color])
(arc [_ center-position radius start-angle degree color])
(sector [_ center-position radius start-angle degree color])
(rectangle [_ x y w h color])
(filled-rectangle [_ x y w h color])
(line [_ start-position end-position color]
[_ x y ex ey color])
(with-line-width [_ width draw-fn]))

(defn grid [drawer leftx bottomy gridw gridh cellw cellh color]
(let [w (* gridw cellw)
h (* gridh cellh)
topy (+ bottomy h)
rightx (+ leftx w)]
(doseq [idx (range (inc gridw))
:let [linex (+ leftx (* idx cellw))]]
(line drawer linex topy linex bottomy color))
(doseq [idx (range (inc gridh))
:let [liney (+ bottomy (* idx cellh))]]
(line drawer leftx liney rightx liney color))))
28 changes: 0 additions & 28 deletions src/gdl/graphics/font.clj

This file was deleted.

41 changes: 1 addition & 40 deletions src/gdl/graphics/image.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns gdl.graphics.image
(:import (com.badlogic.gdx.graphics Color Texture)
(:import (com.badlogic.gdx.graphics Texture)
(com.badlogic.gdx.graphics.g2d Batch TextureRegion)))

; TODO
Expand All @@ -19,45 +19,6 @@
; * -> I cache only dimensions & scale for my texture-regions
; * color & rotation applied on rendering

(defn- draw-texture [^Batch batch texture [x y] [w h] rotation color]
(if color (.setColor batch color))
(.draw batch texture
x
y
(/ w 2) ; rotation origin
(/ h 2)
w ; width height
h
1 ; scaling factor
1
rotation)
(if color (.setColor batch Color/WHITE)))

(defn- unit-dimensions [{:keys [unit-scale world-unit-scale] :as context} image]
{:pre [(number? unit-scale)]}
(cond
(= unit-scale world-unit-scale) (:world-unit-dimensions image)
(= unit-scale 1) (:pixel-dimensions image)))

(defn draw
([{:keys [batch] :as context} {:keys [texture color] :as image} position]
(draw-texture batch texture position (unit-dimensions context image) 0 color))
([context image x y]
(draw context image [x y])))

(defn draw-rotated-centered
[{:keys [batch] :as context} {:keys [texture color] :as image} rotation [x y]]
(let [[w h] (unit-dimensions context image)]
(draw-texture batch
texture
[(- x (/ w 2))
(- y (/ h 2))]
[w h]
rotation
color)))

(defn draw-centered [context image position]
(draw-rotated-centered context image 0 position))

(defn- texture-dimensions [^TextureRegion texture]
[(.getRegionWidth texture)
Expand Down
Loading

0 comments on commit 0cee7b2

Please sign in to comment.