Page 1 of 3

Somebody helps me out with the performance problem?

Posted: Mon Mar 30, 2015 9:32 am
by liyonglove2d
THE .LOVE FILE IS HERE http://pan.baidu.com/s/1qWsQiRU

Em, it's my first post here :nyu: . I've been making a game named 'miner' or something in English. I encounter with performance problem with my game, and do not know how to solve it.

Here're game screenshots to tell what kind of game it is:
screen snap1
screen snap1
game1.png (770.06 KiB) Viewed 6542 times
screensnap2
screensnap2
game2.png (335.7 KiB) Viewed 6542 times
The .love is a little big, I'll upload it if necessary. :3
I got only 20 or even lower FPS sometimes, and I think the key problem is that I render too many entities in one frame, about 1000~3000 drawing calls I guess. Do I have to cut down the count of entities I draw? I'm wondering if there're other solutions to my problem. I really appriciate your help! ^^

oh I forgot there's the whole project on git. https://code.csdn.net/iamagoodguy254/lo ... ree/master
tell me if you cannot visit the page.

Re: Somebody helps me out with the performance problem?

Posted: Mon Mar 30, 2015 9:55 am
by arampl
Hi, liyonglove2d!

Maybe you at least show code of your love.draw() function here in the message (you can use "Code" tags for readability).

Without any information people can only "tell fortune" about your situation. I can only imagine that you use some type of loop in which all tiles of the map are drawn, even if they don't visible on the screen. If so, you can look at kikito's excellent solution in the topic "Faster way to draw rectangles?" in the "Support and Development" section.

And maybe you should learn more about using canvases, sprite batches, quads and meshes in the docs.

Re: Somebody helps me out with the performance problem?

Posted: Mon Mar 30, 2015 10:11 am
by liyonglove2d
arampl wrote:Hi, liyonglove2d!

Maybe you at least show code of your love.draw() function here in the message (you can use "Code" tags for readability).

Without any information people can only "tell fortune" about your situation. I can only imagine that you use some type of loop in which all tiles of the map are drawn, even if they don't visible on the screen. If so, you can look at kikito's excellent solution in the topic "Faster way to draw rectangles?" in the "Support and Development" section.

And maybe you should learn more about using canvases, sprite batches, quads and meshes in the docs.
Oh thanks a lot. Every entity has its own 'ondraw' function, so the love.draw() function is just very simple.
Indeed the lights're drawn with sprite batches, tiles're drawn with canvases, but trees/animals/ui items and all other things are drawn seperately. so i'm wondering if it is these seperately-drawn objects that caused the problem.
Would you help me with these problems :death: :
Is the drawing speed relative with the size of the image to be drawn?
How many images can be drawn to the screen in one frame if I want 60 FPS for my game?
Are there convenient ways to find out the key problem of my code?
thanks again :nyu: .

Re: Somebody helps me out with the performance problem?

Posted: Mon Mar 30, 2015 10:17 am
by arampl
liyonglove2d wrote:How many images can be drawn to the screen in one frame if I want 60 FPS for my game?
No one can tell you that. It depends on the system specs.

I think you can prepare all you static sprites/tiles on one (or few) big image/canvas and draw it only once.
You can also try to use texture atlases somehow: http://en.wikipedia.org/wiki/Texture_atlas.

P.S. Are your trees animated?

Tip for HUD: you can draw UI elements only once (and redraw only when they're changed), then use inverted stencil (see in the docs) to protect HUD from other drawings. Not sure, but this can involve using custom love.run() function (the default one clears screen in each frame).

Re: Somebody helps me out with the performance problem?

Posted: Mon Mar 30, 2015 12:58 pm
by liyonglove2d
I intend to animate all the plants, though not yet... I believe there's a way to do this with high performance...
As for UI, inverted stencil is a wanderful idea, I'll give it a try!
Thanks a lot for your response :)

Re: Somebody helps me out with the performance problem?

Posted: Mon Mar 30, 2015 1:09 pm
by rmcode
For finding out what really is the bottleneck in your code I suggest you run some "timed" tests.

Code: Select all

function love.draw()
        local start = love.timer.getTime()
        -- code...
        local tend = love.timer.getTime();
        print("Time passed " .. tend - start);
end
This will give you an idea of where your game spends the most time. There also are some love profilers around, but I never used them.

Re: Somebody helps me out with the performance problem?

Posted: Mon Mar 30, 2015 1:42 pm
by T-Bone
If I had to take a shot in the dark and make a random guess, I think what you need is to use Canvases and/or Spritebatches to bulk multiple draw calls into a single draw call. For many kinds of games, this does wonders for performance.

For your game, I can guess that rendering the background to a Canvas, instead of rendering each tile individually, will save a lot of performance.

Re: Somebody helps me out with the performance problem?

Posted: Mon Mar 30, 2015 3:33 pm
by liyonglove2d
thanks a lot, guys.
I found most of the time are spent to draw the animals and plants, there're over 200 animals and plants to be drawn in one frame. So I reduced the count by modifying the map generator. The performance is getting much better, though not good enough :)

Here's the code, but I dont think it will help a lot to find out the reason coz it is not documented at all and anyway tedious...
https://code.csdn.net/iamagoodguy254/lo ... ree/master

Re: Somebody helps me out with the performance problem?

Posted: Mon Mar 30, 2015 3:37 pm
by liyonglove2d
Shall I upload the .love file?

Re: Somebody helps me out with the performance problem?

Posted: Mon Mar 30, 2015 8:14 pm
by s-ol
Uploading a .love is probably a good idea.

I didn't take an actual look yet, but are you drawing the whole map every frame? You should try and determine only the visible entities and map parts so you don't spend 90% of your drawing calls on offscreen targets that aren't even visible; implement some kind of spatial partition for entities and just draw the visible part of the map (plus some margin).