Merge screenshot functions and allow specifying file name

This commit is contained in:
oakes
2014-04-08 01:49:27 -04:00
parent 16ba12315c
commit aa0ad6df4c
2 changed files with 22 additions and 28 deletions

View File

@@ -6,7 +6,6 @@
InputMultiplexer InputProcessor Net Screen] InputMultiplexer InputProcessor Net Screen]
[com.badlogic.gdx.audio Sound] [com.badlogic.gdx.audio Sound]
[com.badlogic.gdx.assets AssetManager] [com.badlogic.gdx.assets AssetManager]
[com.badlogic.gdx.files FileHandle]
[com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera [com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera
PerspectiveCamera Pixmap Pixmap$Format PixmapIO Texture VertexAttributes$Usage] PerspectiveCamera Pixmap Pixmap$Format PixmapIO Texture VertexAttributes$Usage]
[com.badlogic.gdx.graphics.g2d SpriteBatch] [com.badlogic.gdx.graphics.g2d SpriteBatch]

View File

@@ -7,34 +7,29 @@
[& body] [& body]
`(app! :post-runnable (fn [] ~@body))) `(app! :post-runnable (fn [] ~@body)))
(defn new-file! (defn screenshot!
"Creates a new [File](http://docs.oracle.com/javase/7/docs/api/java/io/File.html)" "Captures a screenshot and either returns it as a `pixmap` or saves it to the
([name extension] specified path.
(new-file! "./" name extension))
([path name extension]
(java.io.File. (str path name extension))))
(defn capture-screen! (screenshot!)
"Returns a [Pixmap](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Pixmap.html) that contains a capture of the current screen. (screenshot! \"out.png\")
Note that you must call [dispose](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Pixmap.html#dispose) on the Pixmap or you will cause a memory leak on the native heap. (screenshot! (files! :external \"out.png\"))"
screenshot! captures and disposes the Pixmap automatically." ([]
[] (let [pic (Pixmap. (game :width) (game :height) (Pixmap$Format/RGBA8888))
(let [pixmap (Pixmap. (game :width) (game :height) (Pixmap$Format/RGBA8888))
pixel-data (ScreenUtils/getFrameBufferPixels true) pixel-data (ScreenUtils/getFrameBufferPixels true)
pixels (.getPixels pixmap)] pixels (.getPixels pic)]
(doto pixels (doto pixels
(.clear) (.clear)
(.put pixel-data) (.put pixel-data)
(.position)) (.position))
pixmap)) pic))
([path]
(defn screenshot! (let [pic (screenshot!)
"Captures a screenshot and writes it to disk." handle (if (string? path)
[] (files! :local path)
(let [pixmap (capture-screen!) path)]
handle (FileHandle. (new-file! (str (System/currentTimeMillis)) ".png"))] (PixmapIO/writePNG handle pic)
(do (PixmapIO/writePNG handle pixmap) (.dispose pic))))
(.dispose pixmap))))
; static fields ; static fields