Skip to content

Commit 0594f22

Browse files
author
Mike Barkmin
committed
optimize image loading
1 parent 0c40b41 commit 0594f22

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

resources/build.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ source.repository=https://github.com/mikebarkmin/processing-library-scratch.git
131131
# This is used to compare different versions of the same Library, and check if
132132
# an update is available.
133133

134-
library.version=15
134+
library.version=16
135135

136136

137137
# The version as the user will see it.
138138

139-
library.prettyVersion=1.13.0
139+
library.prettyVersion=1.13.1
140140

141141

142142
# The min and max revision of Processing compatible with your Library.

src/eu/barkmin/processing/scratch/ScratchImage.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
import processing.core.PImage;
44
import processing.core.PApplet;
55

6+
import java.util.HashMap;
7+
68
/**
79
* The base class for representing scratch costumes and backdrops.
810
*/
911
public class ScratchImage {
1012
private String name;
1113
private PImage image;
12-
private PImage originalImage;
14+
private final PImage originalImage;
1315
private ScratchColor tint = new ScratchColor();
1416
private float transparency = 255;
1517

18+
private static final HashMap<String, PImage> originalImages = new HashMap<>();
19+
1620
/**
1721
* Construct a ScratchImage object by a name and a path to an image.
1822
*
@@ -21,8 +25,8 @@ public class ScratchImage {
2125
*/
2226
public ScratchImage(String name, String imagePath) {
2327
this.name = name;
24-
this.originalImage = ScratchStage.parent.loadImage(imagePath);
25-
this.image = this.originalImage.copy();
28+
this.originalImage = ScratchImage.loadImage(imagePath);
29+
this.image = this.originalImage;
2630
}
2731

2832
/**
@@ -32,12 +36,21 @@ public ScratchImage(String name, String imagePath) {
3236
*/
3337
public ScratchImage(ScratchImage i) {
3438
this.name = i.name;
35-
this.image = i.image.copy();
36-
this.originalImage = i.originalImage.copy();
39+
this.image = i.image;
40+
this.originalImage = i.originalImage;
3741
this.tint = new ScratchColor(i.tint);
3842
this.transparency = i.transparency;
3943
}
4044

45+
private static PImage loadImage(String path) {
46+
PImage image = originalImages.get(path);
47+
if (image == null) {
48+
image = ScratchStage.parent.loadImage(path);
49+
originalImages.put(path, image);
50+
}
51+
return image;
52+
}
53+
4154
/**
4255
* Returns the name
4356
*

src/eu/barkmin/processing/scratch/ScratchStage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import processing.core.PApplet;
44
import processing.core.PConstants;
55
import processing.core.PGraphics;
6+
import processing.core.PImage;
67
import processing.event.KeyEvent;
78
import processing.event.MouseEvent;
89

0 commit comments

Comments
 (0)