resize() creating a gradient?
in
Programming Questions
•
11 months ago
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.
Any help I can get will be greatly appreciated.
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;
void setup() {
size(1000, 740);
frameRate(30);
//setup map
mapSizeX = 51;
mapSizeY = 51;
myMap = new int[mapSizeX][mapSizeY];
initializeMap();
tileSize = 10;
wide = int(450/tileSize);
tall = int(450/tileSize);
viewPort = new PVector (0, 0);
}
void draw()
{
// background(255);
if (mousePressed)
{
int findX = (int)((mouseX-265)/tileSize) ;
int findY = (int)((mouseY-41)/tileSize) ;
if ((mouseX > 265 && mouseX < width - 255) && (mouseY > 41 && mouseY < 442))
{
println("Mouse coords: " + (mouseX - 265) + ", " + (mouseY - 41));
println("Map coords: " + findX + ", " + findY);
}
}
fill(255);
rect(206, 18, 40, 20); //Turn counter
fill(0);
drawGrid();
}
void mousePressed()
{
int findX = (int)(mouseX-265)/(int)tileSize +(int)viewPort.x;
int findY = (int)(mouseY-41)/(int)tileSize +(int)viewPort.y;
if (mouseButton == LEFT)
{
myMap[findX][findY] = penColor;
}
}
void mouseDragged()
{
int findX = (int)(mouseX-265)/(int)tileSize +(int)viewPort.x;
int findY = (int)(mouseY-41)/(int)tileSize +(int)viewPort.y;
if (mouseButton==LEFT)
{
if (editMode == true && findX < wide+viewPort.x && findX >= 0 && findY < tall+viewPort.y && findY >= 0)
{
switch(toolSetting)
{
case 1:
myMap[findX][findY] = penColor;
break;
}
}
}
}
void initializeMap()
{
for (int i = 0; i <= 50; i++)
{
for (int j = 0; j <= 50; j++)
{
myMap[i][j] = 9;
}
}
}
void drawGrid()
{
stroke(1);
PImage stoneTile = loadImage("tiles/stonefloor.bmp");
for (int i = 0; i < wide; i++)
{
for (int j = 0; j < tall; j++)
{
if (i+viewPort.x < mapSizeX && i+viewPort.x >= 0 && j+viewPort.y < mapSizeY && j+viewPort.y >= 0)
{
if (myMap[(i+(int)viewPort.x)][(j+(int)viewPort.y)] == 7)
{
stoneTile.resize((int)tileSize,(int)tileSize);
image(stoneTile, 265+i*tileSize, 41+j*tileSize);
}
else
{
fill(getColor(myMap[(i+(int)viewPort.x)][(j+(int)viewPort.y)]));
rect(265+i*tileSize, 41+j*tileSize, tileSize, tileSize);
}
}
}
}
}
void zoomLevel(int zoom) {
println(zoom);
switch (zoom)
{
case 0:
tileSize = 10;
break;
case 1:
tileSize = 15;
break;
case 2:
tileSize = 18;
break;
case 3:
tileSize = 25;
break;
}
wide = int(450/tileSize);
tall = int(450/tileSize);
if (viewPort.x + wide > mapSizeX)
{
viewPort.x = 0;
}
if (viewPort.y + tall > mapSizeY)
{
viewPort.y=0;
}
}
color getColor(int theColor)
{
int fillColor = color(0, 0, 0);
switch (theColor)
{
case 9:
fillColor = color(255);
break;
}
return fillColor;
}
Any help I can get will be greatly appreciated.
1