Hi! I've been running into an odd issue when using resize() on tiling images, and I was wondering if anyone could help me out?
I'm writing a simple 2D map editor, using square tiles. I'm trying to convert it from colored squares to tiled images, but the more copies of a particular image I use, the darker all of that image becomes, creating a strong gradient across all instances of that image on the entire map. If I disable the call to the resize function, the gradient issue disappears, but then, the tiles are the wrong size at most of the zoom levels.
The code is below - there're some bits that might look a bit odd. I've stripped out a lot of stuff for question to make it easier to see what's going on in my code, which is why there's a lot of switch statements with only one case. The resize function is being called in drawGrid. The tiles are simple 25 x 25 pixel .bmps. They don't need to be anything special: a flat colored square achieves the same effect.
//Map variables int tall, wide; int[][] myMap; int mapSizeX; int mapSizeY; String[] newMap; int penColor=7; int toolSetting = 1; boolean editMode = true; PVector viewPort; float tileSize; int startX, startY;
Hey, all. I'm relatively new to writing code, and am having some trouble with my sketch crashing without giving me an error message. My apologies for the massive code dump here - I'm not sure where (or why) the crash is happening, so I can't just show the problematic part.
What I'm trying to do here is create a random map generator. It works by randomly selecting pre-generated rooms and sticking them together. The pre-generated rooms are made with a level editor I wrote, and consist of a text file with the name of the room, the size of the room across the x axis, the size across y, and then a string of numbers indicating what color each square should be. This string of numbers is assembled into a two dimensional array.
int tall = 55; int wide = 90; int[][] masterMap; int[][] loadRoom;
void setup() { size(wide*10, tall*10); masterMap = new int[wide][tall]; initializeMap();
exits = new ArrayList(); northRooms = new ArrayList(); southRooms = new ArrayList(); eastRooms = new ArrayList(); westRooms = new ArrayList(); roomIndex = loadStrings("maps/roomIndex.txt"); sortRoomsByEntrances();
startPoint = new PVector(int(random(20, wide-20)), int(random(0, tall/2)), 3); //startPoint = new PVector(20, 20, 1); startRoom(); }
color getColor(int theColor) { int fillColor = color(0, 0, 0); switch (theColor) { case 0: fillColor = color(255, 255, 255); break;
case 1: fillColor = color(0, 255, 0); break;
case 2: fillColor = color(0, 255, 0); break;
case 3: fillColor = color(0, 255, 0); break;
case 4: fillColor = color(0, 255, 0); break;
case 5: fillColor = color(0, 0, 0); break;
case 6: fillColor = color(100,20,20); break;
case 7: fillColor = color(120); break;
case 8: fillColor = color(0,0,255); break;
case 9: fillColor = color(255,0,0); break; } return fillColor; }
void initializeMap() { for (int i = 0; i <= wide-1; i++) { for (int j = 0; j <= tall-1; j++) { if (i==0 && (j>=0 && j <=tall)) { masterMap[i][j] = 5; } else if (j==0 && (i>=0 && i <=wide)) { masterMap[i][j] = 5; } else if (j==(tall)-1 && (i>=0 && i <=wide)) { masterMap[i][j] = 5; } else if (i==(wide)-1 && (j>=0 && j <=tall)) { masterMap[i][j] = 5; } else { masterMap[i][j] = 0; } } } }
void drawGrid() { stroke(1); for (int i = 0; i < wide; i++) { for (int j = 0; j < tall; j++) { fill(getColor(masterMap[i][j])); rect(i*10, j*10, 10, 10); } } }
void checkExits() { exits = new ArrayList(); for (int i = 0; i < wide; i++) { for (int j = 0; j < tall; j++) { if (masterMap[i][j] > 0 && masterMap[i][j] < 5) { try { if (masterMap[i+1][j] != 0 && masterMap[i-1][j] != 0 && masterMap[i][j+1] != 0 && masterMap[i][j-1] != 0) { println("Closed exit at " + int(startPoint.x) + ", " + int(startPoint.y)); masterMap[i][j] = 9; } }
catch (Exception e) { println("Caught exception in checkExits: " + e); masterMap[i][j] = 8; } if (masterMap[i][j] == 1) { PVector tempVec = new PVector(i, j, 1); exits.add(tempVec); } if (masterMap[i][j] == 2) { PVector tempVec = new PVector(i, j, 2); exits.add(tempVec); } if (masterMap[i][j] == 3) { PVector tempVec = new PVector(i, j, 3); exits.add(tempVec); } if (masterMap[i][j] == 4) { PVector tempVec = new PVector(i, j, 4); exits.add(tempVec); } } } } }
//Places the first room void startRoom() { String[] newRoom = loadStrings("maps/StartRoom.txt");
int roomWidth = int(newRoom[1]); int roomHeight = int(newRoom[2]); int count = 3; for (int i = int(startPoint.x); i < int(startPoint.x) + roomWidth; i++) { for (int j = int(startPoint.y); j < int(startPoint.y) + roomHeight; j++) { masterMap[i][j] = int(newRoom[count]); count++; } } }
//Sorts pre-generate rooms by which direction the doors are in void sortRoomsByEntrances() { String[] newRoom; for (int i = 0; i <=roomIndex.length-1; i++) { loadPath = "maps/" + roomIndex[i]; newRoom = loadStrings(loadPath); for (int j = 0; j < newRoom.length-1; j++) { if (int(newRoom[j]) == 1) { southRooms.add(newRoom); } if (int(newRoom[j]) == 2) { westRooms.add(newRoom); } if (int(newRoom[j]) == 3) { northRooms.add(newRoom); } if (int(newRoom[j]) == 4) { eastRooms.add(newRoom); } } } }
//Places rooms, checks to make sure there's no overlap, draws to masterMap array void placeRooms() { ArrayList roomList = new ArrayList(); boolean roomPlaced = false; boolean roomBlocked; int roomWidth; int roomHeight; int count = 3; int xZero = 0; int yZero = 0; int[][] tempRoom; int[][] tempMap = new int[wide][tall]; PVector NRoomEntrance = new PVector(0, 0); PVector SRoomEntrance = new PVector(0, 0); PVector ERoomEntrance = new PVector(0, 0); PVector WRoomEntrance = new PVector(0, 0);
//Building tempMap to check room placement for (int i = 0; i <= wide-1; i++) { for (int j = 0; j <= tall-1; j++) { tempMap[i][j] = masterMap[i][j]; } } println("built temp map");
//Get proper room array and transfer into roomList switch (int(startPoint.z)) { case 1: for (int i = 0; i < (northRooms.size()); i++) { roomList.add(northRooms.get(i)); } break;
case 2: for (int i = 0; i < (eastRooms.size()); i++) { roomList.add(eastRooms.get(i)); }
break;
case 3: for (int i = 0; i < (southRooms.size()); i++) { roomList.add(southRooms.get(i)); } break;
case 4: for (int i = 0; i < (westRooms.size()); i++) { roomList.add(westRooms.get(i)); } break; }
println("Got room array.");
while (roomPlaced == false) { int roomPick = int(random(0, roomList.size())); if (roomList.size()>0) { count = 3; String[] testRoom = (String[])roomList.get(roomPick); println("Picked random room"); roomWidth = int(testRoom[1]); roomHeight = int(testRoom[2]); println("Got room width and height"); tempRoom = new int[roomWidth][roomHeight]; roomBlocked = false; //builds tempRoom 2D array for placing new room for (int i = 0; i < roomWidth; i++) { println(i); for (int j = 0; j < roomHeight; j++) { println(j); tempRoom[i][j] = int(testRoom[count]); if (tempRoom[i][j] == 1) { NRoomEntrance = new PVector(i, j); } if (tempRoom[i][j] == 2) { ERoomEntrance = new PVector(i, j); } if (tempRoom[i][j] == 3) { SRoomEntrance = new PVector(i, j); } if (tempRoom[i][j] == 4) { WRoomEntrance = new PVector(i, j); } count++; // println("count = " + count); } } println("found room exits");
//finds co-ordinates in masterMap for upperleft corner of the new room switch (int(startPoint.z)) { case 1: //fit and draw north rooms xZero = int(startPoint.x) - int(SRoomEntrance.x); yZero = int(startPoint.y) - int(SRoomEntrance.y) - 1; break;
case 2: //Fit and draw East Rooms xZero = int(startPoint.x) +1; yZero = int(startPoint.y) - int(WRoomEntrance.y); break;
case 3: //fit and draw south rooms xZero = int(startPoint.x) - int(NRoomEntrance.x); yZero = int(startPoint.y) +1; break;
case 4: //fit and draw west rooms xZero = int(startPoint.x) - roomWidth; yZero = int(startPoint.y) - int(ERoomEntrance.y); break; }