Java single Array best choice for accessing pixels for manipulation?
Posted
by
Petrol
on Game Development
See other posts from Game Development
or by Petrol
Published on 2014-05-26T12:31:12Z
Indexed on
2014/05/26
22:06 UTC
Read the original article
Hit count: 145
I am just watching this tutorial https://www.youtube.com/watch?v=HwUnMy_pR6A and the guy (who seems to be pretty competent) is using a single array to store and access the pixels of his to-be-rendered image.
I was wondering if this really is the best way to do this. The alternative of Multi-Array does have one pointer more, but Arrays do have an O(1) for accessing each index and calculating the index in a single array seems to take one addition and one multiplication operation per pixel.
And if Multi-Arrays really are bad, can't you use something with Hashing to avoid those addition and multiplication operations?
EDIT: here is his code...
public class Screen {
private int width, height;
public int[] pixels;
public Screen(int width, int height) {
this.width = width;
this.height = height;
// creating array the size of one index/int for every pixel
// single array has better performance than multi-array
pixels = new int[width * height];
}
public void render() {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixels[x + y * width] = 0xff00ff;
}
}
}
}
© Game Development or respective owner