Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  Tile map speed problem  (Read 1313 times)
0 Members and 1 Guest are viewing this topic.
Offline TLE

Senior Newbie





« Posted 2007-07-12 23:42:19 »

Ok, I have a tile map type game with about 24x13 tiles per screen.  To draw each frame, I would first go through each tile on screen and draw the background tiles.  Then I'd go through again and draw the players/monster.  This was working fine.  However, I just added in item drops on the ground, which are drawn immediately after each background tile, when applicable.  It only draws 1 item per tile max, 0 if no items are on the tile.  However, when I put a lot of items on the tiles on screen, the frame rate slows down a whole lot.  When I tested with lots of monsters on screen I didn't have this problem.

Here's the code where the items are drawn:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
   public void drawMap(Graphics g, int x, int y,int offX,int offY) throws java.io.IOException{

      int startLocX = x - C.TILE_WIDTH_PER_SCREEN/2 - 2;
      int startLocY = y - C.TILE_HEIGHT_PER_SCREEN/2 - 2;

      for(int mapY= startLocY; mapY<y+C.TILE_HEIGHT_PER_SCREEN/2 +2; mapY++){

         for(int mapX= startLocX; mapX<x+C.TILE_WIDTH_PER_SCREEN/2+2 ;mapX++){

            int effectiveX=C.centerX + (mapX-x)*C.tileW+offX-30;
            int effectiveY = C.centerY + (mapY-y)*C.tileH+offY-26;

            g.drawImage(getTile(mapX,mapY),effectiveX,effectiveY,null);
           
            ArrayList<Item> items=getItems(C.TEST_MAP,mapX,mapY);
            if(items != null && items.size()>0)
               g.drawImage(items.get(items.size()-1).getPicture(),effectiveX,effectiveY,null);
         }
      }
                      }

The getPicture() method just returns a pointer to a pre-loaded buffered image, right now it's just returning a default picture, regardless of the item.
Any ideas?
Offline broumbroum

Junior Member





« Reply #1 - Posted 2007-07-12 23:56:04 »

You missed to handle an nullpointer :
(...)
Here's the code where the items are drawn:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   public void drawMap(Graphics g, int x, int y,int offX,int offY) throws java.io.IOException{
//(...)
           ArrayList<Item> items=getItems(C.TEST_MAP,mapX,mapY);
            /* if(items != null && items.size()>0) // you may get a NullPointerException over here because of the mix you made with init conditions.
               g.drawImage(items.get(items.size()-1).getPicture(),effectiveX,effectiveY,null); */

                                if(items != null) {
                                              if(items.size()>0)
                           g.drawImage(items.get(items.size()-1).getPicture(),effectiveX,effectiveY,null);
                               }
         }
      }
                      }
(...)
  Cool

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline TLE

Senior Newbie





« Reply #2 - Posted 2007-07-13 15:00:20 »

No, what I have written doesn't miss any null pointers.  I put the check for null first in an 'and' statement, the way java compiles, it checks the first test in the and, if it fails, then it doesn't check the rest of the and statement, because FALSE and anything is false, so it's optimizes it automatically by not checking the rest.  So if != null is ever false (hence it is null), then it will never check the size any way, so there's no exception thrown. 

Besides, the problem I'm having isn't that the program doesn't run, or that it crashes, the problem is that it slows down.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #3 - Posted 2007-07-13 15:17:27 »

Are those item images translucent perhaps?

弾幕 ☆ @mahonnaiseblog
Offline 2playgames

Junior Member





« Reply #4 - Posted 2007-07-13 15:34:42 »

and how were they created? new BufferedImage(), createCompatibleImage, Tookit?

Offline TLE

Senior Newbie





« Reply #5 - Posted 2007-07-13 16:31:08 »

This is how they're created originally:
1  
2  
itemPallet = javax.imageio.ImageIO.read(new File(C.PICTURE_PREFIX+"items.gif"));
defaultPic = itemPallet.getSubimage(0,0,50,50);


Where itemPallet is a big gif image with all the items side by side, and each item pic is a subimage of that pallet, the defautPic is the only 1 being used right now, so getPicture()'s code is:
1  
2  
3  
getPicture(){
    return defaultPic;
}


And the itemPallet.gif has a transparent background.  It's total size is about 3kB (not sure what parts of this matters :\ )

Also, I'm using the same methods for monster pictures, but I haven't drawn those yet, so just using gif images I have laying around.  The monster pics are about 2.4kB, so size isn't much different.  Although the monsters aren't using pallets and subimages, and I plan to do something similar as this itemPallet in the future, so definitely need to work out what's going wrong here.
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #6 - Posted 2007-07-13 17:11:34 »

First of all... the compressed size doesn't matter. Eg you can create some 0.1kb png which is 30k x 30k pixels in size.

getSubimage is one of the slower methods... especially if the spritesheet exceeds 2^16 pixels. Try to copy em over as shown here:
http://www.java-gaming.org/forums/index.php?topic=16801.msg131902#msg131902

弾幕 ☆ @mahonnaiseblog
Offline TLE

Senior Newbie





« Reply #7 - Posted 2007-07-14 18:05:23 »

Wow, that helped TREMENDOUSLY oNyx!  Thanks a ton! 

Would you recommend I make create all my images in this fashion?  I mean the ones that aren't spliced, should I Still create them like this?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (11 views)
2014-07-24 01:59:36

Riven (9 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (49 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!