Hello I have a question regarding the way i deal with drawing a scene.
TL;DR What technique do you use to draw Tiles in your 2D game?
I first populate my tile map (essentially a 2D array of Images) once.
Once the 2D array is populated, i populate a buffered image, i set it to be the size of the 2D array (number of elements x size of image, etc)
- Once the dimensions of said buffered image are done, i draw each image of the 2D array in the correct x and y location of the buffered image (start at 0, 0, then 0, 32, etc…)
So now i have this big buffered image, which is the size of the entire map (about 200x32px wide and 200x32px tall for the most part)
however, the issue comes when i draw the buffered image:
my draw function’s signature is as follows:
public void draw( final Graphics2D g,
final int xOffset,
final int yOffset,
final int width,
final int height) { … }
It basically takes a point and how wide and tall to draw (monitor size for example), once i have this information, I call the bufferedimage’s subImage function, which takes in
an x and y location as well as the width and height, this returns the desired part of the big buffered image, that is what I draw.
However, I believe this causes memory leaks, I ran the profiler and generations usually go up at random intervals, all the way up to 8, probably more but I got tired of running the game.
My question is, what would be a better way of drawing a particular scene in the game?