Hello.
I'm having a program that in the end will be a map viewer. (Like Google maps but with he ability to interact with the map).
I'm loading the map images into an array and somehow, although they are very small, they take up a lot of memory.
The images are around 1 KB each. Still I had to release some 200 MB memory for Processing.
The images are in .png format.
And it takes almost a quarter of a second to load the 16 images.
Can someone see what I'm doing wrong?
Code://textBoxOne variables
PFont fontA;
int textBoxOneX = 600;
//Window settings
int windowWidth = 1920;
int windowHeight = 1200;
//Image settings
PImage a; //Temporary holder
PImage[][] mapHolder; //Array holder
int mapImageWidth = 256;
int mapImageHeight = 256;
String mapImagePath;
//Map settings
String mapType = "Map"; //Currently two modes (Map/Sat)
//Starting position
int xPos = 71195;
int yPos = 39185;
//This is the allowed space to move in
int xPosMin = 70195;
int xPosMax = 72213;
int yPosMin = 38185;
int yPosMax = 40211;
/*This is the space which contains images
int xPosMin = 71195;
int xPosMax = 71213;
int yPosMin = 39185;
int yPosMax = 39211;
*/
//The xSpan and ySpan is the number of images which will be loaded simultaneously
//((number of images to cover up the window) times three so that one can look around with mouse without the need to load more images)
int xSpan = (windowWidth/mapImageWidth) + 1;
int ySpan = (windowHeight/mapImageHeight) + 1;
void setup(){
size(windowWidth,windowHeight);
background(200);
noLoop();
//Text
fontA = loadFont("CourierNew36.vlw");
textFont(fontA, 20);
textMode(MODEL);
println(xSpan*ySpan);
}
void keyPressed(){
if (key != CODED) {
switch (key) {
case 'x': mapType = "Map"; redraw(); break;
case 'c': mapType = "Sat"; redraw(); break;
}
} else {
switch (keyCode) {
case UP: moveMap(1,2); break;
case DOWN: moveMap(1,3); break;
case LEFT: moveMap(1,1); break;
case RIGHT: moveMap(1,4); break;
}
}
}
void draw(){
mapHolder = new PImage[xSpan + 1][ySpan + 1];
getMaps();
showTextBox();
}
PImage getMapImage(int xPos, int yPos, int zoom, String type){
mapImagePath = "map/" + type + "_x=" + xPos + "y=" + yPos + "zoom=" + zoom + ".png";
return loadImage(mapImagePath);
}
void getMaps(){
for (int x = 0; x <= xSpan; x++){
for (int y = 0; y <= ySpan; y++){
mapHolder[x][y] = getMapImage (xPos + x, yPos + y, 17, mapType);
try {
image(mapHolder[x][y], x*256, y*256);
} catch (Exception e) {
image(loadImage("map/unavailable.png"), x*256, y*256);
}
}
}
}
void showTextBox(){
fill(0);
text("xPos: " + xPos, textBoxOneX, 60);
text("yPos: " + yPos, textBoxOneX, 90);
}
void moveMap(int step, int direction){
if (direction == 1 && xPos - step >= xPosMin){
xPos -= step;
}
if (direction == 2 && yPos - step >= yPosMin){
yPos -= step;
}
if (direction == 3 && yPos + step + ySpan <= yPosMax){
yPos += step;
}
if (direction == 4 && xPos + step + xSpan <= xPosMax){
xPos += step;
}
redraw();
}