   Faster tile loading!
  (Java)I'm having trouble with my scrolling not being entirely smooth. This is not a frame rate or buffering issue; rather it is a data issue.My game loads the entire map at the start of the level into memory, using a user defined level object which contains map layers as arrays in an arraylist. Every time it scrolls one full tile, it gets a new snapshot from the level, but this lags ever so slightly when scrolling across many tiles.This isn't a key listening problem either.How do I go about getting it smooth as butter? Is it Javas fault?   
  Can you elaborate on exactly how you store and render the map? In particular which API and platform are you targetting (J2ME?)At any rate your best bet is probably to treat the rendered map window as a circular buffer and only fill in new rows of tiles at the edges when scrolling, rather than redrawing the whole thing. This is, more or less, how scrolling used to be done in old days of 8/16-bit consoles.(I'll dig out an article or describe the scheme in detail if you want to, but your best bet is probably to dig out a NES/SNES emulator and turn on the debug tilemap viewer.)Though with a bit of luck there might be some other way to optimize the rendering and avoid the hassle of partial updates.   ;Quote:Original post by implicitCan you elaborate on exactly how you store and render the map? In particular which API and platform are you targetting (J2ME?)At any rate your best bet is probably to treat the rendered map window as a circular buffer and only fill in new rows of tiles at the edges when scrolling, rather than redrawing the whole thing. This is, more or less, how scrolling used to be done in old days of 8/16-bit consoles.(I'll dig out an article or describe the scheme in detail if you want to, but your best bet is probably to dig out a NES/SNES emulator and turn on the debug tilemap viewer.)Though with a bit of luck there might be some other way to optimize the rendering and avoid the hassle of partial updates.I think my problem is that each cell is storing an image, rather than a pointer. i think its loading the same image from file for every cell. i'll update you on how it goes. Is there a clean way to do this? I'm thinking some sort of image storing class that keeps all the images loaded and at disposal.   ;public class ImageLoader{public static ArrayList image_paths = new ArrayList();public static ArrayList images = new ArrayList();public static Image getImage(String image_path){if(image_path==null){return null;}else if(image_paths.contains(image_path)){return (Image)images.get(image_paths.indexOf(image_path));}else{image_paths.add(image_path);Image image = (new ImageIcon(image_path)).getImage();images.add(image);return image;}}public static void unloadImage(String image_path){if(image_paths.contains(image_path)){int index = image_paths.indexOf(image_path);images.remove(index);image_paths.remove(index);}}}Surprisingly this didn't speed anything up. Still ever so slightly stuttering on scrolling.   ;  I know little about Java and its graphics graphics libraries but if you're getting poor performance from basic the Swing functions then perhaps you should try an API aimed more towards animation instead? Java 2D perhaps?At any rate you might want to consider pre-loading all of the tiles up-front instead of fetching them on-demand if you want to minimize stuttering (or at least swap that ArrayList for a Hashtable or something.)Quote:Original post by overeasySurprisingly this didn't speed anything up. Still ever so slightly stuttering on scrolling.At the end of the day you're doing more work on some frames than others. This is a big no-no in real-time games, and you should look into ways of spreading out the load over multiple frames.Instead of redrawing every tile on the whole map screen when scrolling perhaps you could shift the previous contents around and fill in the edges which have been scrolled out?If this isn't sufficient you can avoid the shifting altogether through a circular back-buffer arrangement as I mentioned earlier. And if even that isn't enough then you can give the buffer an extra column/row of off-screen tiles and gradually fill it in over multiple frames instead of all at once when shifting to a new tile position.I can't help thinking that you shouldn't need to bother with any of this unless you're targeting a cellphone or something. Personally I'd look into using a more gaming-oriented graphics API and try redrawing everything every frame.   ;Quote:Original post by implicitI know little about Java and its graphics graphics libraries but if you're getting poor performance from basic the Swing functions then perhaps you should try an API aimed more towards animation instead? Java 2D perhaps?At any rate you might want to consider pre-loading all of the tiles up-front instead of fetching them on-demand if you want to minimize stuttering (or at least swap that ArrayList for a Hashtable or something.)Quote:Original post by overeasySurprisingly this didn't speed anything up. Still ever so slightly stuttering on scrolling.At the end of the day you're doing more work on some frames than others. This is a big no-no in real-time games, and you should look into ways of spreading out the load over multiple frames.Instead of redrawing every tile on the whole map screen when scrolling perhaps you could shift the previous contents around and fill in the edges which have been scrolled out?If this isn't sufficient you can avoid the shifting altogether through a circular back-buffer arrangement as I mentioned earlier. And if even that isn't enough then you can give the buffer an extra column/row of off-screen tiles and gradually fill it in over multiple frames instead of all at once when shifting to a new tile position.I can't help thinking that you shouldn't need to bother with any of this unless you're targeting a cellphone or something. Personally I'd look into using a more gaming-oriented graphics API and try redrawing everything every frame.I've finished separating drawing and simulating, and its the simulating that is lagging. This is 99% likely to be the loading of the current "frame", or set of visible tiles plus a border offscreen. Graphics are good, data manipulation is slow. :(;  1) Don't load images on demand. Load them all ahead of time. The ImageIcon constructor itself is slow compared to everything else, because it has to read data from the hard drive (which is much, much slower than reading from RAM). This is why games have loading screens.2)Quote:Original post by overeasypublic class ImageLoader{public static ArrayList image_paths = new ArrayList();public static ArrayList images = new ArrayList();public static Image getImage(String image_path){if(image_path==null){return null;}else if(image_paths.contains(image_path)){return (Image)images.get(image_paths.indexOf(image_path));Don't use parallel lists for this. Use an associative container, such as java.util.HashMap. They're designed for exactly this kind of lookup.public class ImageLoader {  // Assuming you're using Java 5 or 6; for old Java versions, just leave off  // the bit between the angle brackets  public static HashMap<String, Image> mapping;  public static Image getImage(String image_path) {    if (image_path == null) { return null; }    if (!mapping.containsKey(image_path)) { mapping.put(image_path, (new ImageIcon(image_path)).getImage());    }    return mapping.get(image_path);  }  public static void unloadImage(String image_path){    mapping.remove(image_path);  }}