Stencils with fixed content

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Stencils with fixed content

Post 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.
Attachments
isogame.love
(207.59 KiB) Downloaded 269 times
User avatar
tv_user
Citizen
Posts: 57
Joined: Sun Aug 12, 2012 4:39 pm
Location: Portugal

Re: Stencils with fixed content

Post 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!
Did my comment help/offended you? Use the Karma button!!
LÖVEing each day...
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Stencils with fixed content

Post 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)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Stencils with fixed content

Post 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"
Attachments
isogame.love
(208.37 KiB) Downloaded 189 times
thiruppathy
Prole
Posts: 1
Joined: Tue Nov 06, 2012 7:33 am
Location: INDIA
Contact:

Party graffiti

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Stencils with fixed content

Post 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.
When I write def I mean function.
thiruppathyttt
Prole
Posts: 1
Joined: Wed Nov 14, 2012 7:07 am
Contact:

Digital graffiti stencils

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests