7

So, I'm creating a 2d top-down game in Java.

I'm following instructions from Java 2D: Hardware Accelerating - Part 2 - Buffer Strategies to take advantage of hardware acceleration.

Basically, what I'm thinking is this:
I'd like to be able to easily add more sections to the map. So I'd rather not go the route suggested in a few of the tutorials I've seen (each map tile has an adjacency list of surrounding tiles; beginning with a center tile, populate the screen with a breadth-first search).

Instead, my idea would be to have screen-sized collections of tiles (say 32x32 for simplicity), and each of these screen "chunks" would have an list referencing each adjacent collection. Then, I would create a buffer for the current screen and the 8 adjacent screens and draw the visible portion in the VRAM buffer.

My question is, would this be a correct way to go about this, or is there a better option? I've looked through quite a few tutorials, but they all seem to offer the same (seemingly high maintenance) options.

It would seem this would be a better choice, as doing things at the tile level would require 1024 times as many adjacency lists. Also, the reason I was considering putting only the visible portion in VRAM, while leaving the "current" screen and its adjacent screens in standard buffers was because I'm new to hardware acceleration and am not entirely sure how much space is acceptable to assume to be available. Because Java attempts to accelerate standard buffers anyways, it should theoretically be as fast as putting each in VRAM?

Any and all suggestions are welcome!

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
bendicott
  • 369
  • 2
  • 17

1 Answers1

3

I haven't looked at any of the popular tile-based game engines, but I'd consider using the fly-weight pattern to render only the tiles that are visible in the viewport of a JScrollPane. JTable is both an example and a usable implementation.

Addendum: One advantage of the JTable approach is view-model separation, which allows one to relegate the acquisition of tile-related resources to the model. This makes it easier to optimize without having to change the view.

Even without scroll bars, one can leverage scrollRectToVisible() by extending JComponent or an appropriate subclass. The setDoubleBuffered() method may be helpful, too.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I've not dealt with patterns so far, but after reading the article [here](http://www.javaworld.com/javaworld/jw-07-2003/jw-0725-designpatterns.html?page=1) I think I've got a decent grasp on its use, at least. The JScrollPane seems almost tailored for this (once the scroll bars are hidden). Still need to look into JTable, but this is a great start. Thanks! – bendicott Aug 13 '11 at 23:09
  • After looking up JTables, what I'm getting is: I would create a JTable and insert it into a JScrollPane. Then, using the flyweight pattern (limiting tile objects to one of each tile type) and some representation of map tiles (char array or bitmapdata, maybe), insert their graphics into JTable cells? Or should I not be populating the entire JTable? Mostly I'm not sure if I'm using flyweights properly. Also, if this is the correct way to go about this, are there any good references for hardware-acceleration concerning JTables? I'm having trouble even finding references for using images in them. – bendicott Aug 14 '11 at 00:57
  • Actually, `JTable` already uses the flyweight pattern; you'd just implement the data model and desired renderer(s). More above. – trashgod Aug 14 '11 at 01:06