From 3de4943f674309150af9ce4616c5e028405acb90 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Wed, 16 Sep 2015 09:11:39 -0700 Subject: [PATCH] Sped up loading on ipad by a lot. --- desktop/src-common/advent/pathfind.clj | 21 +++++++++++++-------- desktop/src-common/advent/utils.clj | 21 +++++++++++++++++---- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/desktop/src-common/advent/pathfind.clj b/desktop/src-common/advent/pathfind.clj index 0888ebf9..a8e17ac3 100644 --- a/desktop/src-common/advent/pathfind.clj +++ b/desktop/src-common/advent/pathfind.clj @@ -122,11 +122,16 @@ (defn map-from-resource [filename] (let [pm (pixmap filename) black (color 0 0 0 255) - painful (color 255 0 0 255)] - (vec (take-nth scale (for [x (range (pixmap! pm :get-width))] - (vec (take-nth scale (for [y (reverse (range (pixmap! pm :get-height))) - :let [current-color (color (pixmap! pm :get-pixel x y))]] - (cond - (color! current-color :equals black) 0 - (color! current-color :equals painful) 2 - :else 1))))))))) + painful (color 255 0 0 255) + result (transient []) + scale (long scale) + height (long (pixmap! pm :get-height))] + (doseq [^long x (range (/ (pixmap! pm :get-width) scale)) + ^long y (range (/ height scale)) + :let [ + current-color (color (pixmap! pm :get-pixel (unchecked-multiply scale x) (unchecked-subtract height (unchecked-multiply scale y))))]] + (conj! result (cond + (color! current-color :equals black) 0 + (color! current-color :equals painful) 2 + :else 1))) + (partition (/ (pixmap! pm :get-height) scale) (persistent! result) ))) diff --git a/desktop/src-common/advent/utils.clj b/desktop/src-common/advent/utils.clj index ab515929..de1e3caa 100644 --- a/desktop/src-common/advent/utils.clj +++ b/desktop/src-common/advent/utils.clj @@ -5,6 +5,7 @@ [play-clj.utils :refer :all] [play-clj.math :refer :all] [play-clj.g2d :refer :all] + [play-clj.entities :refer [->TextureEntity]] [clojure.java.io :as io] [clojure.edn :as edn] [clojure.string :as str]) @@ -152,10 +153,22 @@ (texture! frame :flip true false) frame)))) -(defn split-texture [file [w h] frames] - (let [sheet (texture! (get-texture file) :split w h)] - (for [i frames] - (aget sheet 0 i)))) +(defn split-texture [file [tile-width tile-height] frames] + (let [sheet (get-texture file) + sheet-obj ^TextureRegion (:object sheet) + width ^int (int (.getRegionWidth sheet-obj)) + x ^int (int (.getRegionX sheet-obj)) + y ^int (int (.getRegionY sheet-obj))] + (for [frame frames + :let [new-tex ^TextureRegion (TextureRegion. sheet-obj)]] + (do + (.setRegion new-tex + (unchecked-add x (unchecked-multiply tile-width frame)) + y + tile-width + tile-height) + (->TextureEntity new-tex)))) + ) (defn make-anim [file [w h] speed frames] (animation speed (split-texture file [w h] frames)))