Making stuff work between windows and osx?

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 :stuck_out_tongue: )

Are you sizing the window during initialization, or are you sizing the canvas/panel/whatever inside the window? If you’re sizing the window, the size parameters will include decorations such as the border and title. Since windows and os x have different sized decorations, that could cause your ceil() computations to get off.

Just tried to remove all the “+16” and “+32” in the render method, and changed my windowframe to:

package gui;

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MainWindow extends JFrame {

	public MainWindow(Canvas content) {
		this.setTitle("Map Testing");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Set the size
		Dimension size = new Dimension(800, 600);
//		this.setPreferredSize(size);
		content.setPreferredSize(size);
		
		this.getContentPane().add(content);
		
		this.pack();
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
}

But I’m still getting the weird out of alignment errors in windows. (Haven’t tested it in os x, since i’m here to game, rather than to code :P)


Anyone…? :confused:

No one has got a solution for this? :confused:

Using setPreferredSize(Dimension) and then packing should work. What problems are you still having?

The same problems as before. It doesn’t align up in both windows and osx. Plus it doesn’t align up when I resize the window either. Which would be awesome if it did. I tried with setPreferredSize(), but that gives the same issues. :confused:

Try printing out the size of the ‘content’ component after you call setVisible(true) by doing “content.getSize()” and see what it tells you.

They are what I except them to be. ^^ 800x600. :stuck_out_tongue:

Then your problem is in your code, not the window sizing, because the window is the correct size :stuck_out_tongue:

Yeah, I figured as much xD I just can’t figure out what the heck I’m doing wrong! :stuck_out_tongue: (Which is why I pasted the render code here too ^_^)

Looking through the render code, I see nothing that should causes differences between OS’s. Try using a debugger in the problem areas on both OS’s and notice what differences you see. Good luck! :slight_smile:

Haha, I guess I’ll have to do something eventually. But it’s fricking odd that it’s not doing as expected. It even happens when I resize the window. :confused: