Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (428)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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  
  Storing Images for a Tile Map  (Read 1767 times)
0 Members and 1 Guest are viewing this topic.
Offline 8ballj

Senior Newbie





« Posted 2008-02-28 03:08:29 »

Right now I have a class that draws a tile map.  It has about 10 different BufferedImage objects that are defined in the class.  All the images are stored in a folder on my computer.

BufferedImage Img1 = ...C:/path/img1.bmp
BufferedImage Img2= ...C:/path/img2.bmp
etc...
....

The thing is, I"m going to have over 100 images by the time the game is complete.  Is it normal/standard to have 100+ BufferedImages defined in the field above the constructor in a given class?  Once the images are initialized as the program is loaded, will it have any hinderence on performance during map scrolling later on?

Thanks!
Offline fletchergames

Senior Member





« Reply #1 - Posted 2008-02-28 05:03:50 »

It would be more normal to list the image filenames in an external file of some kind.  You can have a text file with lines like "treasure_chest1 = some filename".  It's always good to move the data out of the code.

You can then store the images in a HashMap, hashed by the name of each tile (e.g. treasure_chest1).  This at least saves you from typing all those "BufferedImage whatever = ..." lines.

If the tiles are the same for every map, you will only need to load the images once.  If they are different on different maps, having it set up this way allows you to load new images when you change maps.

I'm sure this can be improved upon, but it's a start.

Having over 100 BufferedImages shouldn't be too bad unless your tiles are really big.  If you don't have much video RAM, you don't really need for all the tiles to be hardware accelerated, so long as the most common are.

If drawing the screen takes longer than scrolling the screen, you won't be able to scroll the screen as fast as you like.  When you scroll the screen, you basically have to redraw the whole thing.  If you have a sufficiently large area in video RAM (which might not even be possible), you could scroll by just changing what part of the area you're displaying.  That might resolve any problems.
Offline 8ballj

Senior Newbie





« Reply #2 - Posted 2008-02-28 06:53:48 »

I don't plan on having more than one type of terrain, and the tiles are 25x25 pixel png's.  I'm also wondering how to get around my paintComponent method looking like:

if(map
  • [y]==1) {
    g.drawImage(trees, x, y...)
}
if(map
  • [y]==2) {
   g.draw Image (rock, x,y...
}

.
.
.if (map
  • [y]==100) {
g.drawImage(plant)
}


Is this the only way to do it really?
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 2008-02-28 13:17:07 »

>Is this the only way to do it really?

Nah... usually you just use an array.

g.drawImage(images[map[x][y]],...

And loading... well, you can for example call em tile0001.png, tile0002.png etc and load em in a loop or put several of those tiles into one image. Or name mapping in the editor and packaging/map-remapping during the build process, but that's somewhat complicated and not really required for simple games with rather little media.

弾幕 ☆ @mahonnaiseblog
Offline 8ballj

Senior Newbie





« Reply #4 - Posted 2008-02-28 17:16:46 »

Awesome... never though to of the array thing.  so each idex in the array corresponds to the index of map
  • [y].  I'm just using a BufferedImage[ ] array at this point.  Is that the simplest array to use (as opposed to a Vector or ArrayList?).  Thanks everyone again for the fast help!
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #5 - Posted 2008-02-28 17:30:16 »

Ye, arrays are faster and far simpler than any of the collection classes. If you can use arrays without having to jump through hoops, you should really use em. For this kind of thing the number of elements is known beforehand and the size remains as it is for the whole run.

弾幕 ☆ @mahonnaiseblog
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.

xsi3rr4x (79 views)
2014-04-15 18:08:23

BurntPizza (71 views)
2014-04-15 03:46:01

UprightPath (82 views)
2014-04-14 17:39:50

UprightPath (66 views)
2014-04-14 17:35:47

Porlus (82 views)
2014-04-14 15:48:38

tom_mai78101 (106 views)
2014-04-10 04:04:31

BurntPizza (166 views)
2014-04-08 23:06:04

tom_mai78101 (262 views)
2014-04-05 13:34:39

trollwarrior1 (212 views)
2014-04-04 12:06:45

CJLetsGame (221 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!