Author Topic: A couple cheap-o effects in small code (spotlight, movable message box)  (Read 5009 times)

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Here's a couple of cheap effects with small code.  I like playing around with simple small bits of code and see what can be done quickly.  Here's what came up this morning while enjoying my coffee. This fakes a spotlight effect and draws a movable message box with some transparency.  Works ok for text background, but not good for graphics.

- Dav

Code: QB64: [Select]
  1.  
  2. SCREEN _NEWIMAGE(640, 640, 32)
  3.  
  4. FOR p = 1 TO 1000
  5.     PRINT RND * 100;
  6.  
  7. 'fake a spotlight...
  8. FOR x = 0 TO _WIDTH + (_WIDTH / 3) STEP .3
  9.     CIRCLE (_WIDTH / 2, _HEIGHT / 2), x, _RGBA(0, 0, 0, x / 2)
  10.  
  11.  
  12.     m = _MOUSEINPUT: mx = _MOUSEX: my = _MOUSEY
  13.     _PUTIMAGE (0, 0), back&
  14.     LINE (mx - 100, my - 100)-(mx + 100, my + 100), _RGBA(0, 0, 0, 128), BF
  15.     LINE (mx - 100, my - 100)-(mx + 100, my + 100), _RGBA(255, 255, 255, 200), B
  16.     _PRINTSTRING (mx - 80, my - 80), "Use mouse - Move me"
  17.     _DISPLAY
  18.     _LIMIT 500
  19.  
  20.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A couple cheap-o effects in small code (spotlight, movable message box)
« Reply #1 on: September 30, 2021, 12:38:08 pm »
Spotlight reminds me of Colored Search Lights bouncing off each other:
Code: QB64: [Select]
  1. _Title "Bouncing Search Lights" 'started 2019-04-28 B+, continued by Petr to REAL TEXT SCREEN :-D
  2. 'continued by B+ with normal letter size :-))
  3. ' 2019-04-29 another variation
  4. ' 2019-04-29 now same as graphics mode!!! Thanks Petr!!!
  5.  
  6.  
  7. 'this really text mode??? ;-))
  8.  
  9. Const xmax = 640
  10. Const ymax = 400
  11.  
  12. Dim txt(1 To 25) As String, v(1 To 25)
  13. For i = 1 To 25
  14.     b$ = ""
  15.     For j = 1 To 100
  16.         b$ = b$ + Chr$(Int(Rnd * 96) + 32)
  17.     Next
  18.     txt(i) = b$: v(i) = Int(Rnd * 3) + 1
  19. balls = 15
  20. Dim x(balls), y(balls), r(balls), c(balls), dx(balls), dy(balls), a(balls), rr(balls), gg(balls), bb(balls)
  21. For i = 1 To balls
  22.     r(i) = rand(10, 65)
  23.     x(i) = rand(r(i), xmax - r(i))
  24.     y(i) = rand(r(i), ymax - r(i))
  25.     c(i) = rand(1, 15)
  26.     dx(i) = rand(1, 5) * rdir
  27.     dy(i) = rand(1, 5) * rdir
  28.     rr(i) = rand(150, 255)
  29.     gg(i) = rand(150, 255)
  30.     bb(i) = rand(150, 255)
  31. Bplus = _NewImage(xmax, ymax, 32)
  32. While _KeyDown(27) = 0
  33.     _Dest Bplus
  34.     Cls
  35.     For i = 1 To balls
  36.         'ready for collision
  37.         a(i) = _Atan2(dy(i), dx(i))
  38.         power1 = (dx(i) ^ 2 + dy(i) ^ 2) ^ .5
  39.         imoved = 0
  40.         For j = i + 1 To balls
  41.             If Sqr((x(i) - x(j)) ^ 2 + (y(i) - y(j)) ^ 2) < r(i) + r(j) Then
  42.                 imoved = 1
  43.                 a(i) = _Atan2(y(i) - y(j), x(i) - x(j))
  44.                 a(j) = _Atan2(y(j) - y(i), x(j) - x(i))
  45.                 'update new dx, dy for i and j balls
  46.                 power2 = (dx(j) ^ 2 + dy(j) ^ 2) ^ .5
  47.                 power = (power1 + power2) / 2
  48.                 dx(i) = power * Cos(a(i))
  49.                 dy(i) = power * Sin(a(i))
  50.                 dx(j) = power * Cos(a(j))
  51.                 dy(j) = power * Sin(a(j))
  52.                 x(i) = x(i) + dx(i)
  53.                 y(i) = y(i) + dy(i)
  54.                 x(j) = x(j) + dx(j)
  55.                 y(j) = y(j) + dy(j)
  56.                 Exit For
  57.             End If
  58.         Next
  59.         If imoved = 0 Then
  60.             x(i) = x(i) + dx(i)
  61.             y(i) = y(i) + dy(i)
  62.         End If
  63.         If x(i) < r(i) Then dx(i) = -dx(i): x(i) = r(i)
  64.         If x(i) > xmax - r(i) Then dx(i) = -dx(i): x(i) = xmax - r(i)
  65.         If y(i) < r(i) Then dy(i) = -dy(i): y(i) = r(i)
  66.         If y(i) > ymax - r(i) Then dy(i) = -dy(i): y(i) = ymax - r(i)
  67.         For rad = r(i) To 0 Step -1
  68.             fcirc x(i), y(i), rad, _RGBA32(rr(i), gg(i), bb(i), 20)
  69.         Next
  70.     Next
  71.  
  72.     Color , _RGBA(0, 0, 0, 0)
  73.     For i = 1 To 25
  74.         txt(i) = Mid$(txt(i), v(i)) + Mid$(txt(i), 1, v(i) - 1)
  75.         For l = 1 To 80
  76.             If i Mod 2 Then midInk 60, 0, 0, 0, 0, 60, l / 80 Else midInk 0, 0, 60, 60, 0, 0, l / 80
  77.             Locate i, l: Print Mid$(txt(i), l, 1);
  78.         Next
  79.     Next
  80.  
  81.     _Dest 0 'redirecting to text screen
  82.  
  83.     Himage = _CopyImage(Bplus, 33)
  84.     _PutImage (0, 0)-(xmax, ymax), Himage, 0
  85.     _Display
  86.     _Limit 10
  87.     _FreeImage Himage
  88.  
  89. Function rand (lo, hi)
  90.     rand = (Rnd * (hi - lo + 1)) \ 1 + lo
  91.  
  92. Function rdir ()
  93.     If Rnd < .5 Then rdir = -1 Else rdir = 1
  94.  
  95. Sub fcirc (CX As Integer, CY As Integer, R As Integer, C As _Unsigned Long)
  96.     Dim Radius As Integer, RadiusError As Integer
  97.     Dim X As Integer, Y As Integer
  98.     Radius = Abs(R): RadiusError = -Radius: X = Radius: Y = 0
  99.     If Radius = 0 Then PSet (CX, CY), C: Exit Sub
  100.     Line (CX - X, CY)-(CX + X, CY), C, BF
  101.     While X > Y
  102.         RadiusError = RadiusError + Y * 2 + 1
  103.         If RadiusError >= 0 Then
  104.             If X <> Y + 1 Then
  105.                 Line (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  106.                 Line (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  107.             End If
  108.             X = X - 1
  109.             RadiusError = RadiusError - X * 2
  110.         End If
  111.         Y = Y + 1
  112.         Line (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  113.         Line (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  114.     Wend
  115.  
  116. Sub midInk (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  117.     Color _RGB32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
  118.  
  119.  
  120.  
  121.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: A couple cheap-o effects in small code (spotlight, movable message box)
« Reply #2 on: September 30, 2021, 12:54:36 pm »
On my SCREEN ZERO HERO laptop, there is considerable lag between the mouse and spotlight effect. My hunch is on that on faster machines like the one Mark has, the lag I experienced  may not be as noticeable. Since I don't do gaming, I have a CPU that moves at the speed of shite. It's so slow,even math calculation show some lag, but hey, that's fine for me, as I'm slow at math, anyway. What I noticed from a few test modifications it was the LINE statements being responsible for the lag. I'm not sure if a MEM routine could be substituted to speed things up.

Neat effect. It reminded of MS Magnifier.

Edited: This post is in reference to lag noticed in Dav's code and not the code bplus posted.

Pete
« Last Edit: September 30, 2021, 01:16:38 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A couple cheap-o effects in small code (spotlight, movable message box)
« Reply #3 on: September 30, 2021, 12:57:25 pm »
Pete try decreasing the number of balls=search lights there is a lot of calculations deciding ball collisions.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: A couple cheap-o effects in small code (spotlight, movable message box)
« Reply #4 on: September 30, 2021, 01:09:48 pm »
Oops, sorry Mark, I was referring to the lag in Dav's code. You and I were posting at the same time. I'll have a peek at your routine, now...

Okay, just tried it. I think I remember seeing that some time ago. Anyway, no noticeable lag in your rendition. Now I'm curious why the speed difference exists between your effect and Dav's.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: A couple cheap-o effects in small code (spotlight, movable message box)
« Reply #5 on: September 30, 2021, 01:21:27 pm »
Cool effect @bplus.  I don't remember seeing that one before, but I remember the fcirc function.  Your fcirc is so much faster than the method I used.  I just compared it using the replacement below and mine is so much slower. 

Edit: I wonder why that is?  There is so much more going on in the fcirc function...

- Dav

Code: QB64: [Select]
  1.  
  2.             'fcirc x(i), y(i), rad, _RGBA32(rr(i), gg(i), bb(i), 20)
  3.  
  4.             FOR rrr = 1 TO rad STEP .4
  5.                 CIRCLE (x(i), y(i)), rrr, _RGBA32(rr(i), gg(i), bb(i), 20)
  6.             NEXT
  7.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A couple cheap-o effects in small code (spotlight, movable message box)
« Reply #6 on: September 30, 2021, 01:23:43 pm »
Oops, sorry Mark, I was referring to the lag in Dav's code. You and I were posting at the same time. I'll have a peek at your routine, now...

Okay, just tried it. I think I remember seeing that some time ago. Anyway, no noticeable lag in your rendition. Now I'm curious why the speed difference exists between your effect and Dav's.

Pete

That is curious! If I recall Dav is using a slower machine or older version of Windows or ... some difference with my system. My machine is going on 5 years old I think the bootup has gone from nearly instant to about 5 minutes now!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A couple cheap-o effects in small code (spotlight, movable message box)
« Reply #7 on: September 30, 2021, 01:27:55 pm »
Cool effect @bplus.  I don't remember seeing that one before, but I remember the fcirc function.  Your fcirc is so much faster than the method I used.  I just compared it using the replacement below and mine is so much slower. 

Edit: I wonder why that is?  There is so much more going on in the fcirc function...

- Dav

Code: QB64: [Select]
  1.  
  2.             'fcirc x(i), y(i), rad, _RGBA32(rr(i), gg(i), bb(i), 20)
  3.  
  4.             FOR rrr = 1 TO rad STEP .4
  5.                 CIRCLE (x(i), y(i)), rrr, _RGBA32(rr(i), gg(i), bb(i), 20)
  6.             NEXT
  7.  

That's the Gold Standard Circle Fill for QB64 (it's in the Utility's section of Samples in Library along with Ellipse work as well), Steve first introduced it to us some time ago. It has been compared to filling with Circle Loop and Paint and comes out ahead in speed hands down!

Plus if you use alpha coloring there is 0 overlap unlike using a circle in a loop (use get a light line cross sort of like my current avatar).

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A couple cheap-o effects in small code (spotlight, movable message box)
« Reply #8 on: September 30, 2021, 01:41:16 pm »
Is this faster for Dav's code?
Code: QB64: [Select]
  1.  
  2. Screen _NewImage(640, 640, 32)
  3. For p = 1 To 1000
  4.     Print Rnd * 100;
  5.  
  6. 'fake a spotlight...
  7. For x = .75 * _Width To 0 Step -.4
  8.     Circle (_Width / 2, _Height / 2), x, _RGB32(0, 0, 0, x)
  9.  
  10.     m = _MouseInput: mx = _MouseX: my = _MouseY
  11.     _PutImage (0, 0), back&
  12.     Line (mx - 100, my - 100)-(mx + 100, my + 100), _RGBA(0, 0, 0, 128), BF
  13.     Line (mx - 100, my - 100)-(mx + 100, my + 100), _RGBA(255, 255, 255, 200), B
  14.     _PrintString (mx - 80, my - 80), "Use mouse - Move me"
  15.     _Display
  16.     _Limit 500
  17.  
  18.  
  19.  
  20.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A couple cheap-o effects in small code (spotlight, movable message box)
« Reply #9 on: September 30, 2021, 01:54:47 pm »
Here is a Circle Fill using Circle in a loop for an Alpha color:
Code: QB64: [Select]
  1. Screen _NewImage(640, 640, 32)
  2.  
  3. For x = .35 * _Width To 0 Step -.25
  4.     Circle (_Width / 2, _Height / 2), x, _RGB32(255, 255, 255, 80)
  5.  
End up with all sorts of shades, nice pattern though.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: A couple cheap-o effects in small code (spotlight, movable message box)
« Reply #10 on: September 30, 2021, 02:26:13 pm »
Mark, the modifications to Dav's code made no appreciable speed difference on my laptop. I figured out the difference is in the mouse routine. 

This code fixes the lag issue...

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 640, 32)
  2.  
  3. FOR p = 1 TO 1000
  4.     PRINT RND * 100;
  5.  
  6. 'fake a spotlight...
  7. FOR x = 0 TO _WIDTH + (_WIDTH / 3) STEP .3
  8.     CIRCLE (_WIDTH / 2, _HEIGHT / 2), x, _RGBA(0, 0, 0, x / 2)
  9.  
  10.  
  11.     m = _MOUSEINPUT: mx = _MOUSEX: my = _MOUSEY
  12.     IF mx <> oldmx OR my <> oldmy THEN
  13.         oldmx = mx: oldmy = my
  14.         _PUTIMAGE (0, 0), back&
  15.         LINE (mx - 100, my - 100)-(mx + 100, my + 100), _RGBA(0, 0, 0, 128), BF
  16.         LINE (mx - 100, my - 100)-(mx + 100, my + 100), _RGBA(255, 255, 255, 200), B
  17.         _PRINTSTRING (mx - 80, my - 80), "Use mouse - Move me"
  18.         _DISPLAY
  19.     END IF
  20.     _LIMIT 500
  21.  
  22.  

It only calls _DISPLAY if the mouse is moved.

BTW: Working on that CIRCLE / ELLIPSE routine was a kick. Glad to see it's still considered the "Gold Standard."

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: A couple cheap-o effects in small code (spotlight, movable message box)
« Reply #11 on: September 30, 2021, 04:56:02 pm »
I do not know which book you guys are reading, but in my book, I do not consider these effects as 'cheep-o'. They are brilliant! Well done!
Logic is the beginning of wisdom.