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]
[com.badlogic.gdx.audio Sound]
[com.badlogic.gdx.assets AssetManager]
[com.badlogic.gdx.files FileHandle]
[com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera
PerspectiveCamera Pixmap Pixmap$Format PixmapIO Texture VertexAttributes$Usage]
[com.badlogic.gdx.graphics.g2d SpriteBatch]

View File

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