I am working on a sprite coloring mechanism for my CS project game, and perhaps as a continuity to my last question, I again encountered the wrath of for-loop overdose in all its fury.
In short, I created a three-dimensional BufferedImage array to represent a sprite with three aspects defined by its location on the array space. This is how it works:
for (int i = 0; i < 50; i++) { //BTABLE[][][] initializer loop
BTABLE[0][0][i] = GRAYSCALE.get(i); //assigns sprite to sprite type axis
for (int j = 0; j < 360; j++) {
if ((i < 8) || ((13 < i) && (i < 17)) || ((18 < i) && (i < 24)) || (36 < i)) {
BTABLE[0][j][i] = rotateImage(BTABLE[0][0][i], 360 - j);
}
for (int l = 0; l < 10; l++) {
if (BTABLE[0][j][i] != null) {
BTABLE[l][j][i] = recolorImage(BTABLE[0][j][i], l * 36);
}
}
}
}
[0][0][x] as sprite type axis: represents different sprite for all integers from 0 to 49, inclusive
[0][y][0] as angular rotation axis: represents different angular orientations for sprites defined by sprite type axis, containing 360 orientations of sprite from 0 deg to 360 degs
[z][0][0] as color axis: (is supposed to)represents different HSB hue for all types and angular rotations of sprites, 45 variations of color for each sprite on [0][y][x] plane
So for example, a call of BTABLE[10][20][30] will get BufferedImage sprite of type 30, angular orientation of 20 * 2 = 40 deg, and color with 10 * 8 = 80 hue deg.
The problem is, since the recolorImage has to be called at minimum 180,000 times, however lightweight I make it, the heap space is all used up even before the array is initialized.
Is there a way to build the sprite sheet more efficiently so that a single method does not have to run 180,000 times, or if that is not possible, can the method be made lightweight enough to not blow up in the initialization process? The only viable method that I have is repainting each pixel of input picture to the desired color.
Polypeptide is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.