Tile rendering FPS lag

I use tiles entity in my game they are set in ArrayList. The more tiles I have on my map the lower fps I have. I think I found the problem when I don’t render the fps its 10x bigger.


for (int i = 0; i < tile.size(); i++) {
			tile.get(i).render(screen);
		}

this is how I render the tiles and I think this is the problem because the more tiles I have the slower it renders.
I want to render the tiles only that I will see on the screen.
Something like this


for (int i = 0; i < TheNumberOfTilesICanSeeOnTheScreen(); i++) {
			tile.get(i).render(screen);
		}

Help me please I cant continue making my game because of this serious problem.

What I do is have each tile have a rectangle that’s it’s region and then I see if that region intersects the current screen region (which I calculate), using [icode].intersects(Rectangle r)[/icode]. Each tiles region would just be [icode]new Rectangle(Math.round(x), Math.round(y), TILE_WIDTH, TILE_HEIGHT)[/icode].

CopyableCougar4

You should make sure to only render tiles that are onscreen.

Also your rendering algorithm could be slow. What are you using? OpenGL? Swing? Drawing stuff in Swing sucks horrendously. You should transition to LWJGL and you’ll get literally 10000x multiplier in terms of the number of sprites you can render at once.

Can change every pixel like I can with Graphics in java with LWJGL?

LWJGL is pretty much better than Java2D in all circumstances. It’s more complicated, but faster.

I wouldn’t even bother writing a game in Java2D these days, since you can use something like libGdx and get better performance for about the same amount of code as regular Java2D.

I recommend looking into a graphics library and brushing up on your basic Java skills before venturing further into game development.

Also, for tile based games, why not use a regular array? Then you know what’s in the bounds of the screen for much cheaper (it’s 1 if call and 1 multiplication calculation, versus at least 4 if calls per bounding box) [you can actually get this to be less by just saying for(int i = playerPositionTileX + tilesToRightEdge; i > playerPositionTileX - tilesToLeftEdge; i–) and doing another loop for the Y, and just rendering out those like, 50 objects, and never having to check anything for collision, no if statements of any sort, so it’s even cheaper, since on larger maps you can save thousands of calls]

I assume geometry is static, so it’s not like you need to make your map bigger suddenly. Even if this was the case, you could always bump your array up a size by recasting it, it seems like a big waste of resources to use an ArrayList for your map. Entities, surely, but geometry, no.

What I did is draw the tiles into an image on startup and then just draw that image. Drawing the tiles individually will always be extremely slow.


int playerx, int playery;

int xt = playerx/tile.width,yt = playery/tile.height // xt is x tile

int x0 = xt - (screen.width / 2) / tile.width;
int y0 = yt - (screen.height / 2) / tile.height;

int x1 = xt + (screen.width / 2) / tile.width;
int y1 = yt + (screen.height / 2) / tile.height;

for(int y=y0;y<y1;y++)
{
     for(int y=y0;y<y1;y++)
     {
          tiles[x+y*level.width].render();
     }     
}