Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Realtime "Fake" 2D-Lighting

Author
Message
Mr Kohlenstoff
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 6th Dec 2008 20:23 Edited at: 7th Dec 2008 11:14
Hi!

I just wrote a "prototype" for a pseudo-light-effect for a 2D-game I'm working on. As expected the FPS arent to high because I'm using memblock-editing in realtime instead of shaders, which are probably usually used for realtime lights. However, my computer reaches ~25-30 FPS at average which is far to much to be used in a game. However, I'm sure there is much to optimize which I am going to do later.
Still I'd like to know how much FPS you all get and if anyone has an idea of how to speed it up. Furthermore I'd like to know if anyone knows how to use shaders in 2D-DBP-Applications (using paste sprite etc.).

So, here's the code:



I'm probably going to update it as soon as it runs any
Thanks.


- - - - - - - - -

Just a quick screenshot:



- - - - - - - - -


Edit:
I optimised the code a bit and now get 30-35 instead of 27 FPS. I'll see if I can get it any further..

Edit2:
Using a division where I used multiplications before increased my FPS to ~42.

Edit3:
A friend of mine just worked on some 2D spritebased flashlights and got them running ~10 times faster. However, I'd still be interested how much FPS you get.

Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 7th Dec 2008 18:18
Pretty cool . I get 51-52 FPS.

Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 8th Dec 2008 02:05 Edited at: 8th Dec 2008 02:05
This is cool. I managed to get a little more speed out of it by doing



Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 8th Dec 2008 02:12 Edited at: 8th Dec 2008 11:01
Here's a bit more speed.



32FPS original. 56FPS now. Smug mode activated!

BTW i have some ideas how to make this better. Will post after work.

Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 8th Dec 2008 18:52
OK i got it up to about 80fps. Basically, instead of looping over the whole area, it just loops over a smaller square for each light. To reset the whole memblock each time i'm copying from another memblock, which seems to be quick.
I also changed looping over y within x to x within y, so the positions inside the memblock consecutively read/written are closer, but this didn't really have much of an effect.
There are some other methods that could be tried but nothing simple.

Sorry for messy code. I left in some commented bits because they might be of interest. Hold space to use original code and see the speed difference.


Mr Kohlenstoff
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 8th Dec 2008 20:26 Edited at: 8th Dec 2008 20:27
Not bad - I'm now getting 130 instead of 50 FPS.

But it is still way slower than the version of my friend (Joel Schumacher known as Arctic) though, but he allowed me to post the code here. His code is not just much faster but also more flexible: You can use own (even if just unrotated) lightsprites and vary the quality easily (by defining the LIGHTMAP_FACTOR constant which defines the quotient of lightmap-size to screen-resolution (where 0.5 seems to be the perfect value because it is in most cases fast enough and looks quite perfect due to texture filtering )).
His example-code can not really be compared to mine because it uses another resolution of screen and lights and apart from that 4 instead of 3 lights. Still it runs at constant 200 FPS on my computer.
Of course everyone is allowed to use the code however he wants, as long as he mentions my friends name in the credits (and does not sell the code or proclaims it as his' etc. etc.).




Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 9th Dec 2008 00:12 Edited at: 9th Dec 2008 01:13
That's awesome! Neatly coded too. Going to read it through and see how it works. Cheers.

OK i've had a skim through. It seems the new code is essentially the same kind of thing, but with precalculated light-maps, and a lower resolution light image, right? I'd previously imagined it was something like using sprites for each light.

You could still do something like this. Here's the idea:
Take a new camera, with black background, and light ghosted objects for each "light". Render the camera view at whatever lightmap resolution you require to an image. Then use this image as a sprite lightmap as you are doing already.

This method would allow you to rotate lights, turn them up and down with object ambient, and you can have coloured lights too. Not sure how fast would be, but in DB(P) 3D stuff seems to happen hella quicker than 2D stuff.

Anyway your mate's code is plenty fast enough and your game looks sweet.

Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 9th Dec 2008 02:35 Edited at: 9th Dec 2008 03:21
EDIT - in attatched code, paste

somewhere before the do loop - otherwise looks dim!


OK here's what i mean. For 15 lights (set in attatched project) I get around 75fps, (since there are two cameras screen fps() returns a value of twice this). For large numbers of lights, n, the product n*FPS is ~1500. For low numbers of lights it doesn't get that much faster, but for reasonable numbers of lights i expect it to be competitive with the memblock/sprites system. You also get rotating and coloured lights, plus you can easily scale the "ambient" brightness using the backdrop colour, or could even use a base lightmap. Sorry for messy code. Here's a pic:

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-05-02 02:50:35
Your offset time is: 2024-05-02 02:50:35