Page 1 of 1

Stencils with fixed content

Posted: Wed Sep 26, 2012 5:40 pm
by micha
Dear Community,

I am currently working on an isometric game. As I am working on an older machine, I am concerned about performance. I figured that, if I draw the whole map into a canvas in the beginning, the game is much faster, compared to the solution, where I draw all the tiles separately. However now it is much more difficult to hide objects, that are behind map-features. (What I mean exactly becomes clear from the .love file).
To (partly) hide objects, I use stencils, which means that I only draw those parts, that are visible. Now the problem is, that each time I call love.graphics.setStencil(...), the whole stencil drawing function is called (which slows down my performance). In my case, the shape of the stencil is constant over the whole time. I would prefer to have the stencil drawn once and then use it without implicitly calling the stencil drawing function every frame.
I tried solving this by using canvases inside the stencil drawing function, however, even the transparent parts in the canvas block the view in the stencil. Same for using images with transparent parts in the stencil drawing function: I get rectangles then.

Any ideas how to switch stencils fast?

For demonstration, see attached .love-file.
Controls:
arrows: move player
a: jump
w,s: raise/lower current tile
e,d: change current tile type
i: spawn red ball
o: delete all balls
esc: quit
Please ignore slight bugs in player-physics. Also the hiding of the objects only works correctly for the flat landscape pieces.

Re: Stencils with fixed content

Posted: Thu Sep 27, 2012 6:35 pm
by tv_user
Well I didn't get any dramatic framerate drop while experimenting with your demo (pretty nice btw), although my computer is not old either. I also didn't look deeply into your code but...
1. If your stencils are constant throughout the game, then I believe you just need to set the stencils once, at the beginning
2. Are you sure canvases are the best option for your project? Tiles can be quite useful, and you certainly don't have to draw EVERY quad EVERY frame - just the ones that are visible!

Re: Stencils with fixed content

Posted: Thu Sep 27, 2012 8:11 pm
by micha
tv_user wrote:Well I didn't get any dramatic framerate drop while experimenting with your demo (pretty nice btw), although my computer is not old either. I also didn't look deeply into your code but...
1. If your stencils are constant throughout the game, then I believe you just need to set the stencils once, at the beginning
2. Are you sure canvases are the best option for your project? Tiles can be quite useful, and you certainly don't have to draw EVERY quad EVERY frame - just the ones that are visible!
Thanks for your reply.
Can you try out spawning many balls (press "I" about 20 times)? When I have many objects, I get a really low framerate (around 15)

1. My stencils are constant throughout the game, but I have several of them and I need to switch stencils for each object.
2. The canvas itself is not the problem. It is fast. I tried drawing the visible tiles each frame, but that again was to slow on my computer (in my game there are around 300 tiles in one picture)

Re: Stencils with fixed content

Posted: Wed Oct 03, 2012 10:08 pm
by micha
Hey guys,

I tried around a bit and one possible solution is without stencils. This solution has a framerate for 30 FPS on my computer.

I'd appreciate it, if some of you could run the attached code and tell me what framerate you get there.
As before your can walk around with the arrow keys and jump with 'a'.

And could you then run the first code I posted, press 'i' a couple of times (until some balls roll around in the game) and tell me the framerate you get there?
An answer like this would be enough: "Old code: x FPS, new code y FPS"

Party graffiti

Posted: Tue Nov 06, 2012 7:46 am
by thiruppathy
I tried solving this by using canvases inside the stencil drawing function, however, even the transparent parts in the canvas block the view in the stencil. Same for using images with transparent parts in the stencil drawing function: I get rectangles then.

Re: Stencils with fixed content

Posted: Tue Nov 06, 2012 12:11 pm
by kikito
I have had similar discussions with someone (I have forgotten his name) who wanted to optimize the display of an isometric game.

The conclusion that we arrived at was that the best compromise between speed, simplicity and machine support was to use one image/spritebatch/canvas per row, instead of one for the whole ground. Then your drawing process becomes the following:

Code: Select all

-- y0 is the "upmost visible row", yn is the "downmost visible row"
local y0,yn = getVisibleRowIndices()
for y=y0, yn do
  drawTilesInRow(rows[y]) --> this can be an image, a spritebatch, a canvas ...
  drawEnemiesInRow(row[y])
end
This is actually pretty fast. If you have 100 tiles on the screen, you need ~15 draw operations to draw the whole screen instead of just 1, but you save all the stencil calculations. And you can use spritebatches to draw them, and they work in all graphic cards AFAIK - you can use them instead of canvases if they are not supported.

Also keep in mind that if you plan to have more than one enemy/item per row, you will have to sort them out by z component before you draw them.

Digital graffiti stencils

Posted: Wed Nov 14, 2012 7:13 am
by thiruppathyttt
tried solving this by using canvases inside the stencil drawing function, however, even the transparent parts in the canvas block the view in the stencil. Same for using images with transparent parts in the stencil drawing function: I get rectangles then.