So, I’ve been working on my map editor for a little while now, in os x 10.7, but yesterday while I was waiting to play the next game here at the lan I’m at, I installed eclipse on my windows partition so I could code a little more on it. But when I started it, the tiles were all 1 off. As in 32 pixels off on the y-axis. :s Anyone know why there’s that weird difference? (It’s also rendering less tiles than in os x)
My render code is:
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
// Tiles on the screen
int tilesAcross = (int) Math.ceil(this.getWidth() / 32) + 2;
int tilesDown = (int) Math.ceil(this.getHeight() / 32) + 2;
// Tile the player is on
int ptx = this.player.x / 32;
int pty = this.player.y / 32;
// The offset of the tile
int xOffset = this.player.x % 32;
int yOffset = this.player.y % 32;
int tilesRendered = 0;
for (int i = 0; i < tilesAcross; i++) {
for (int j = 0; j < tilesDown; j++) {
tilesRendered++;
int tx = ptx + i - tilesAcross / 2;
int ty = pty + j - tilesDown / 2;
int rx = i * 32 - (32 + xOffset);
int ry = j * 32 - (32 + yOffset) + 32; //These last 32 is for the tiles to be placed correctly in windows, isn't needed in osx
if (tx < 0 || tx >= MAP_WIDTH || ty < 0 || ty >= MAP_HEIGHT) {
g.setColor(Color.BLACK);
g.fillRect(rx + 16, ry, 32, 32);
continue;
}
g.drawImage(map[tx][ty].sprite.image, rx + 16, ry, null);
if (map[tx][ty].getClass() == Water.class) {
int corner = 0;
int side = 0;
// top
if (ty - 1 >= 0) {
// left corner
if (tx - 1 >= 0) {
if (map[tx - 1][ty - 1].getClass() != Water.class) corner = corner | 1;
}
// center
if (map[tx][ty - 1].getClass() != Water.class) side = side | 2;
// right corner
if (tx + 1 < MAP_HEIGHT) {
if (map[tx + 1][ty - 1].getClass() != Water.class) corner = corner | 2;
}
}
// middle left
if (tx - 1 >= 0) {
if (map[tx - 1][ty].getClass() != Water.class) side = side | 1;
}
// middle right
if (tx + 1 < MAP_HEIGHT) {
if (map[tx + 1][ty].getClass() != Water.class) side = side | 4;
}
// bottom
if (ty + 1 < MAP_HEIGHT) {
// left corner
if (tx - 1 >= 0) {
if (map[tx - 1][ty + 1].getClass() != Water.class) corner = corner | 8;
}
// center
if (map[tx][ty + 1].getClass() != Water.class) side = side | 8;
// right corner
if (tx + 1 < MAP_HEIGHT) {
if (map[tx + 1][ty + 1].getClass() != Water.class) corner = corner | 4;
}
}
// Filters out corners that are covered by sides
// Foo magic!
corner &= ~((side << 3) | (side >> 1) | side);
// Something's overlapping!
if (corner > 0) {
Sprite sprite = getCornerSprite(corner);
if (sprite != null) {
g.drawImage(sprite.image, rx + 16, ry, null);
}
}
if (side > 0) {
Sprite sprite = getSideSprite(side);
if (sprite != null) {
g.drawImage(sprite.image, rx + 16, ry, null);
}
}
}
if (this.debug) {
g.setColor(Color.gray);
g.drawRect(rx + 16, ry, 32, 32);
}
}
}
// Paint the player
g.setColor(Color.red);
g.fillRect(this.getWidth() / 2, this.getHeight() / 2, this.player.size, this.player.size);
if (this.debug) {
g.setFont(new Font("serif", Font.PLAIN, 12));
g.setColor(Color.gray);
g.fillRect(15, 5, 270, 60);
g.setColor(Color.black);
g.drawRect(15, 5, 270, 60);
g.drawString("Player position: " + this.player.x + "px/" + this.player.y + "px (" + ptx + "/" + pty + ")", 20, 20);
g.drawString("Frames per second: " + fps, 20, 35);
g.drawString("Tiles rendered: " + tilesRendered, 20, 50);
}
g.dispose();
bs.show();
frames++;
}
Anyone got some pro tips on how to not need those extra 32 pixels(At line 31)? (And maybe tell me why I need to add 16px to the x-axis in both windows and os x, for the tiles to be placed right)
(Also, so far I’ve only made water and grass tiles, so that’s why it’s checking for Water.class )