My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering

So the following is my code for the Terrian Generation of my game, however the way i have it rendering all the tile rectangles as opposed to just rendering whats visable on my JFrame causes lots of lag. All suggestions welcomed! :)

Re: My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering

Have you used a profiler to determine that the code you've posted is the source of the lag, or are you guessing?

January 19th, 2014, 04:00 AM

Cekeh

Re: My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering

When I remove the render chunk of the code, the lag disappears. So yeah I suppose you could say its a guess. I can post all my code if it would be easier.

Also I'm fairly new to java, only learning off of tutorials and some java books I have lying around the house. So if you have any tips, please state them as I would love to learn more.

January 19th, 2014, 04:53 AM

GregBrannon

Re: My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering

So you have some evidence that's the source of the lag. Didn't want to waste time looking at code that wasn't the problem. The key areas to investigate are: large nested for loops, in the tens of thousands, the images are being loaded each time from a slow resource, and/or the thread that does the drawing is being slept somewhere else.

Do you have a loop that controls the pace of the game and/or "sleeps the thread" somewhere? If so post that. What I'm asking for might be your game loop. Either way, post that.

If one reduces the code you posted to code that actually does something, there's a nested for loop and a switch statement. How large can that nested for loop be? What's the maximum (chunkx - 32) * (chunky - 32)? Then in the switch statement, what does _LoadImages do? Are the images being loaded already in memory or are they being obtained from a laggy resource like a hard drive or the internet? Why do you even need the switch() statement? It seems the only difference in the statement in the case blocks is the index which is the same as the switch parameter. Why not use that parameter to indicate the desired image and replace the entire switch() logic with a single line of code. That code will be more elegant, but it won't change performance.

Re: My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering

_LoadImages, just simply stores a buffered image that was inside the init() method. Yes my game has a loop timer of 60fps.

January 20th, 2014, 03:41 AM

GregBrannon

Re: My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering

It's interesting that I asked several questions, suggested a couple places to look, and asked for code and you gave such a short answer that covers about 20% of what I asked. How are we supposed to help with so little information?

January 20th, 2014, 06:05 AM

Cekeh

Re: My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering

Im sorry I'm very busy and not at my computer much lately, I've been posting from my phone. I will answer more as soon as I get a good look at the code myself.