Sign in to follow this  
Clueless Programmer

[java] Drawing a Tile Based Map in Java

Recommended Posts

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[i][j], j * 32, i * 32, null); g.drawImage(tile2[i][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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this