From 16ba12315c1b73470296b265d5c100fae1b28d6e Mon Sep 17 00:00:00 2001 From: Anthony Urena Date: Mon, 7 Apr 2014 23:26:41 -0400 Subject: [PATCH] added rough implementation of screen shot capturing --- src/play_clj/core.clj | 5 +++-- src/play_clj/core_utils.clj | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/play_clj/core.clj b/src/play_clj/core.clj index ec3caa4..5a84b9a 100644 --- a/src/play_clj/core.clj +++ b/src/play_clj/core.clj @@ -6,8 +6,9 @@ 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 Texture VertexAttributes$Usage] + PerspectiveCamera Pixmap Pixmap$Format PixmapIO Texture VertexAttributes$Usage] [com.badlogic.gdx.graphics.g2d SpriteBatch] [com.badlogic.gdx.graphics.g3d ModelBatch] [com.badlogic.gdx.graphics.glutils ShapeRenderer] @@ -26,7 +27,7 @@ [com.badlogic.gdx.scenes.scene2d Actor Stage] [com.badlogic.gdx.scenes.scene2d.utils ActorGestureListener Align ChangeListener ClickListener DragListener FocusListener] - [com.badlogic.gdx.utils Timer$Task] + [com.badlogic.gdx.utils ScreenUtils Timer$Task] [play_clj.entities ShapeEntity])) (load "core_basics") diff --git a/src/play_clj/core_utils.clj b/src/play_clj/core_utils.clj index 73383ec..fec49e1 100644 --- a/src/play_clj/core_utils.clj +++ b/src/play_clj/core_utils.clj @@ -7,6 +7,35 @@ [& 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)))) + ; static fields (defmacro scaling