Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Map Background Questions  (Read 1976 times)
0 Members and 1 Guest are viewing this topic.
Offline TLE

Senior Newbie





« Posted 2007-01-16 20:05:32 »

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!
Offline TLE

Senior Newbie





« Reply #1 - Posted 2007-01-16 22:50:42 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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??
Offline Kova

Senior Devvie





« Reply #2 - Posted 2007-01-17 00:26:49 »

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:

1  
2  
3  
4  
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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline TLE

Senior Newbie





« Reply #3 - Posted 2007-01-17 02:24:29 »

*************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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
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!
Offline TLE

Senior Newbie





« Reply #4 - Posted 2007-01-17 03:10:58 »

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. 

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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?
Offline Kova

Senior Devvie





« Reply #5 - Posted 2007-01-17 15:46:30 »

Here, I found a small tile-game scroll example way before and I saved it. Forgot who wrote it. Rename to .java.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (145 views)
2017-09-30 02:57:34

theagentd (213 views)
2017-09-26 18:23:31

cybrmynd (292 views)
2017-08-02 12:28:51

cybrmynd (284 views)
2017-08-02 12:19:43

cybrmynd (294 views)
2017-08-02 12:18:09

Sralse (287 views)
2017-07-25 17:13:48

Archive (966 views)
2017-04-27 17:45:51

buddyBro (1092 views)
2017-04-05 03:38:00

CopyableCougar4 (1663 views)
2017-03-24 15:39:42

theagentd (1425 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!