Map Background Questions

Ok, writing a 2d game. I’m writing it like you’re seeing your character from a bird’s eye view, so you can walk right, left, “up”, and “down”. I’m planning on having map tiles about 40 titles per screen (not sure exactly yet). So every time you take a step, the map background will scroll so that your character is always at the center of the screen.

I’m trying to figure out how I’m going to draw the map background on to each frame. I was thinking about doing the common string thing, where you have a set # of strings of a set length for the map consisting of identifiers like “GGGGGGGGGGGGGGGGGGGG” or whatnot. However, it seems like drawing all 40 tiles every frame will cause a huge slow down. Am I going about this wrong or will this work? Does the complexity of the tiles matter? Any light on this and related things would be appreciated!

Ok, I wrote out how I was thinking to do it, basically, here’s my code:

public class TestMap extends Map {
	
	public TestMap() throws java.io.IOException{populate();}
	
	public void populate() throws java.io.IOException{
		for(int i=0;i<yMax;i++){
			ArrayList<Image> list = new ArrayList<Image>(xMax);
			for(int k=0;k<xMax;k++){
				if((i+k)%2 == 0) list.add(javax.imageio.ImageIO.read(new File("brown.gif")));
				else list.add(javax.imageio.ImageIO.read(new File("green.gif")));
			}
			tree.put(i, list);
		}
	}
	
	public void drawMap(Graphics g, FSEM screen, int x, int y){
		int mapX = x-12, mapY = y-5;
		int bitX=0,bitY=0;
		for(int i=0;i<12;i++){
			ArrayList<Image> list = (ArrayList<Image>)tree.get(mapY);
			for(int k=0;k<26;k++){
				g.drawImage(list.get(mapX), bitX,bitY,screen);
				bitX+=50; mapX++;
			}
			mapX=x-12; bitX=0;
			bitY+=50; mapY++;
		}
	}
}

Basically, I call drawMap before I draw the characters on the map. This isn’t working out too well though. When I used a mapx/y max of 50/50, it seemed to work ok, took about 20 seconds to load the map at the beginning though. But when I increased the map to 100x100 or more, I get an out of memory exception. Is there a better way to do this??

uh yes there absolutely is a better way

I didn’t ever program a tile map based game, or tried to learn how to, but one thing is clear: You load an image only once. If you have to draw the image multiple times you do that, draw same image multiple times, you don’t need new identical image for each draw. No wonder you get OutOfMemoryException.

I would use a simple char array, pseudo code:

switch (map[x][y]) {
    case 'G': g.drawImage(green.jpg ...); break;
    case 'B': g.drawImage(brown.jpg ...); break;
}

Edit: check out some tutorials about tile games, maybe kev has written some… search :wink:

EDIT**
Wow, I didn’t realize how long it took to fetch the images. I added in 2 image members, initialized them to the brown/green images in the constructor, and changed it in the loops from fetching the images to just drawing those members, and wa-la, sped up the program like 5000x
/EDIT****

Yea, that was pretty dumb. I always do something really stupid right off. So I’ve changed it to this:

public class TestMap extends Map {
	
	public TestMap(){populate();}
	
	public void populate(){
		for(int i=0;i<yMax;i++){
			ArrayList<Integer> list = new ArrayList<Integer>(xMax);
			for(int k=0;k<xMax;k++){
				if((i+k)%2 == 0) list.add(new Integer(0));
				else list.add(new Integer(1));
			}
			tree.put(i, list);
		}
	}
	
	public void drawMap(Graphics g, FSEM screen, int x, int y) throws java.io.IOException{
		int mapX = x-12, mapY = y-5;
		int bitX=0,bitY=0;
		for(int i=0;i<12;i++){
			ArrayList<Integer> list = (ArrayList<Integer>)tree.get(mapY);
			for(int k=0;k<26;k++){
				if(list.get(mapX).equals(0)) g.drawImage(javax.imageio.ImageIO.read(new File("brown.gif")), bitX,bitY,screen);
				else g.drawImage(javax.imageio.ImageIO.read(new File("green.gif")),bitX,bitY,screen);
				bitX+=50; mapX++;
			}
			mapX=x-12; bitX=0;
			bitY+=50; mapY++;
		}
	}
}

So this one actually runs correctly, but now it’s really really slow. I’m pretty sure it’s still poorly written, haven’t figured out just what it is, any ideas anybody? Also, I tried looking up a tutorial for this sort of mapping, but I couldn’t find any. I found this one, http://javacooperation.gmxhome.de/TutorialStartEng.html, the platform game tutorial, but what’s done there is different from what I’m trying to do, the background there doesn’t really change, it’s just the obsticals that are changing. If anybody knows of any good tutorials or examples, point me their way!

More problems with it. I’ve got it set up so that it scrolls right left up and down, but it doesn’t do it smoothly, it kind of pops at the end, and the edges of the screen stretch look screwy.

public void drawMap(Graphics g, FSEM screen, int x, int y,int offX,int offY) throws java.io.IOException{
		int mapX = x-12, mapY = y-5;
		int bitX=0,bitY=0;
		for(int i=0;i<13;i++){
			ArrayList<Integer> list = (ArrayList<Integer>)tree.get(mapY);
			for(int k=0;k<26;k++){
				if(list.get(mapX).equals(0)) g.drawImage(brown, bitX+offX,bitY+offY,screen);
				else g.drawImage(green,bitX+offX,bitY+offY,screen);
				bitX+=50; mapX++;
			}
			mapX=x-12; bitX=0;
			bitY+=50; mapY++;
		}
	}

anybody know what I’m doing wrong? Or rather, how to make it right, I’m pretty sure its the whole offset thing I’m doing that’s wrong…

Edit: Figured out why it was popping, got that fixed, but the edges are still stretching. I’m not sure how to fix this, any ideas?

Here, I found a small tile-game scroll example way before and I saved it. Forgot who wrote it. Rename to .java.