Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fading trails
#1
For @bplus

Code: (Select All)
Const fade = 5 'speed by which our trail disappears
Randomize Timer
Screen _NewImage(640, 480, 32)
background = _NewImage(640, 480, 32)
movescreen = _NewImage(640, 480, 32)

'let's draw a nice background of junk
_Dest background
Cls , &HFFF000F0&&
For i = 1 To 20
Line (Rnd * 100, Rnd * 100)-(Rnd * 700, Rnd * 500), _RGB32(Rnd * 256, Rnd * 256, Rnd * 256), BF
Next

'then let's swap to the movescreen and work
_Dest movescreen
Do
Cls , 0, 0
While _MouseInput: Wend
mx = _MouseX: my = _MouseY
'fade the movescreen over time
FadeScreen movescreen, fade
'update the red box position that chases the mouse slowly
x = x + Sgn(mx - x): y = y + Sgn(my - y)
'draw the solid box which chases after the mouse
Line (x, y)-Step(50, 50), &HFFFF0000, BF
'put the parts on the screen
_PutImage , background, 0
_PutImage , movescreen, 0
_Limit 60
_Display
Loop

Sub FadeScreen (s, faderate)
Dim m As _MEM, o As _Offset
m = _MemImage(s)
Dim p As _Unsigned Long
Do Until o >= m.SIZE
_MemGet m, m.OFFSET + o, p
If p <> 0 Then 'fade the point down to 0 (transparent)
r = _Red32(p) - faderate: If r < 0 Then r = 0
g = _Green32(p) - faderate: If g < 0 Then g = 0
b = _Blue32(p) - faderate: If b < 0 Then b = 0
a = _Alpha32(p) - faderate: If a < 0 Then a = 0
_MemPut m, m.OFFSET + o, _RGBA32(r, g, b, a) As _UNSIGNED LONG
End If
o = o + 4
Loop
_MemFree m
End Sub

A demo of how to keep a background from being altered, while fading out a trail behind an image. Nothing fancy here; just a simple red box that chases after your mouse and leaves a nice trail behind itself as it does so, without fading or altering the background image any whatsoever.
Reply
#2
Sandworms! I hates 'em!

 - Sam
Shoot first and shoot people who ask questions, later.
Reply
#3
+1 Oh man! great Steve, thanks!

Cool and cute and brief, I will study, I see you are using Mem's.
b = b + ...
Reply
#4
(01-01-2025, 06:20 AM)bplus Wrote: +1 Oh man! great Steve, thanks!

Cool and cute and brief, I will study, I see you are using Mem's.

You *may* be able to do the same with PSET, but you'd probably need to _DONTBLEND first.

PSET tends to do blending automatically, so if you go to PSET (x.y), _RGBA32(0,0,0,0), it doesn't do much of anything at all.  The goal isn't to get down to BLACK (which is RGBA(0,0,0,255)), but to get down to TRANSPARENT (RGBA(0,0,0,0)), and you certainly can't do that with _BLEND enabled.

I just chose to use _MEM as this is an inner loop type process and something which we're calling over and over repeatedly, so it's one of those spots where you want to minimize impact on how long it takes to run, as much as possible.
Reply
#5
+1 thanks again! that is definitely interesting demo though I've yet time to really study it.

Yeah I do want to get into using mem stuff for that time savings.

Pset tip is tempting for firework we were working yesterday.
b = b + ...
Reply




Users browsing this thread: 1 Guest(s)