Skip to content

Commit

Permalink
optimize image loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Barkmin committed May 3, 2022
1 parent 0c40b41 commit 0594f22
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 2 additions & 2 deletions resources/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ source.repository=https://github.com/mikebarkmin/processing-library-scratch.git
# This is used to compare different versions of the same Library, and check if
# an update is available.

library.version=15
library.version=16


# The version as the user will see it.

library.prettyVersion=1.13.0
library.prettyVersion=1.13.1


# The min and max revision of Processing compatible with your Library.
Expand Down
23 changes: 18 additions & 5 deletions src/eu/barkmin/processing/scratch/ScratchImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import processing.core.PImage;
import processing.core.PApplet;

import java.util.HashMap;

/**
* The base class for representing scratch costumes and backdrops.
*/
public class ScratchImage {
private String name;
private PImage image;
private PImage originalImage;
private final PImage originalImage;
private ScratchColor tint = new ScratchColor();
private float transparency = 255;

private static final HashMap<String, PImage> originalImages = new HashMap<>();

/**
* Construct a ScratchImage object by a name and a path to an image.
*
Expand All @@ -21,8 +25,8 @@ public class ScratchImage {
*/
public ScratchImage(String name, String imagePath) {
this.name = name;
this.originalImage = ScratchStage.parent.loadImage(imagePath);
this.image = this.originalImage.copy();
this.originalImage = ScratchImage.loadImage(imagePath);
this.image = this.originalImage;
}

/**
Expand All @@ -32,12 +36,21 @@ public ScratchImage(String name, String imagePath) {
*/
public ScratchImage(ScratchImage i) {
this.name = i.name;
this.image = i.image.copy();
this.originalImage = i.originalImage.copy();
this.image = i.image;
this.originalImage = i.originalImage;
this.tint = new ScratchColor(i.tint);
this.transparency = i.transparency;
}

private static PImage loadImage(String path) {
PImage image = originalImages.get(path);
if (image == null) {
image = ScratchStage.parent.loadImage(path);
originalImages.put(path, image);
}
return image;
}

/**
* Returns the name
*
Expand Down
1 change: 1 addition & 0 deletions src/eu/barkmin/processing/scratch/ScratchStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PGraphics;
import processing.core.PImage;
import processing.event.KeyEvent;
import processing.event.MouseEvent;

Expand Down

0 comments on commit 0594f22

Please sign in to comment.