[java] Drawing a Tile Based Map in Java

Started by
7 comments, last by OrangyTang 15 years, 9 months ago
I am developing an rpg game in Java with a 2 layer tile based map. I have a set of possible tiles stored in a 2d Image array, and my map data is stored in 2 text files. I only draw the tiles needed to fill the screen (which is 800 x 600). The code I am using looks something like this: for (int i = a; i < b; i++) { for (int j = c; j < d; j++) { g.drawImage(tile[j], j * 32, i * 32, null); g.drawImage(tile2[j], j * 32, i * 32, null); } } This method works well, and it displays correctly, however the time it is taking to draw is unacceptable (around 100ms to draw just the map on each frame). Does anyone have any suggestions on how to draw a tile based map to the screen more efficiently?
Advertisement
does the second layer always have tiles? Maybe you can skip the draws for empty tiles
The second layer doesn't always have tiles, however it has a lot of them. The first layer just holds grass/water/etc, while the second layer holds roads/trees/signs/etc.
Hi,
Are you drawing to a buffer then copying to the screen? Also, you could consider adjusting the loop a bit, the i*32 could be taken out of the loop.

Have you put any counters in to find out how many tiles are being drawn during each loop?

[Edited by - ssjx on July 3, 2008 3:19:06 AM]

---
Visit ssjx.co.uk for Java, Windows and Cybiko games, programs and source code!

Well, I use a BufferStrategy (named bufferStrategy), and every time I draw to the screen, i have Graphics g = bufferStrategy.getDrawGraphics(), then I draw to g, and I call bufferStrategy.show(). The i * 32 is just the y position it is being drawn at, since my tiles are 32 x 32. With my current set up, everything is drawing to the screen correctly, it just takes way too long. As for setting up a count variable, I did. It is drawing about about 500 - 600 tiles (which, is about expected since 800 / 32 = 25 tiles wide and 600 / 32 = 20 tiles tall, 25 * 20 is 500 plus tiles on second layer)

[Edited by - Clueless Programmer on July 5, 2008 1:13:06 AM]
Which Java version, which OS, which graphics card?
Java 1.6, Ubuntu 8.04 (Hardy Heron), ATI X1400 (I think) graphics card (I am running an IBM Thinkpad T60)
Quote:Original post by Clueless Programmer
Java 1.6, Ubuntu 8.04 (Hardy Heron), ATI X1400 (I think) graphics card (I am running an IBM Thinkpad T60)


Do you have OGL acceleration enabled and working?

Unless you get Java to use OpenGL via Java2D, you'll be stuck with these rates. It's not Java acceleration per se that's the problem.

The mobile versions of graphics cards in my experience often have problems supporting GL properly.
For best drawing performance in the non-GL pipeline, make sure your images are only single-bit alpha (ie. solid or fully transparent pixels, no inbetween) and create your images via GraphicsConfiguration.createCompatibleImage(w, h, Transparency.BITMASK). Also you'll want to trim your loop so you're only drawing tiles which are visible.

With the above and proper use of BufferStrategy I found you could get four layers of 32x32 tiles at 800x600 and still hit 60fps.

This topic is closed to new replies.

Advertisement