Author Topic: Help: code not blending to complete black color  (Read 4007 times)

0 Members and 1 Guest are viewing this topic.

Offline thesolarcode

  • Newbie
  • Posts: 8
    • View Profile
Help: code not blending to complete black color
« on: May 13, 2021, 03:53:00 pm »
I have this code and thought that with the _RGBA alpha setting (line 67) drawing over the whole screen the image should get darker and darker, but somehow the lines are stuck at some very dark grey color. Any idea how I can fix this?

Code: QB64: [Select]
  1.  
  2. dim shared screenx%, screeny%, tx1%, ty1%, tx2%, ty2%, x1%, y1%, x2%, y2%, d%
  3. screenx%=800
  4. screeny%=600
  5.        
  6. ' open new (full)screen
  7. SCREEN _NEWIMAGE(screenx%,screeny%,32)
  8.  
  9. tx1%=screenx%*rnd
  10. ty1%=screeny%*rnd
  11. tx2%=screenx%*rnd
  12. ty2%=screeny%*rnd
  13. x1%=screenx%*rnd
  14. y1%=screeny%*rnd
  15. x2%=screenx%*rnd
  16. y2%=screeny%*rnd
  17. d%=3
  18.  
  19.         color _RGB(256*rnd, 256*rnd, 256*rnd)
  20.         smearfx
  21.        
  22.         if x1% < tx1% then
  23.                 x1% = x1% + d%
  24.         ELSE
  25.                 x1% = x1% - d%
  26.         end if
  27.         if ABS(x1%-tx1%) < d% then
  28.                 tx1%=screenx%*rnd
  29.         end if
  30.  
  31.         if y1% < ty1% then
  32.                 y1% = y1% + d%
  33.         ELSE
  34.                 y1% = y1% - d%
  35.         end if
  36.         if abs(y1%-ty1%) < d% then
  37.                 ty1%=screeny%*rnd
  38.         end if
  39.        
  40.         if x2% < tx2% then
  41.                 x2% = x2% + d%
  42.         ELSE
  43.                 x2% = x2% - d%
  44.         end if
  45.         if abs(x2%-tx2%) < d% then
  46.                 tx2%=screenx%*rnd
  47.         end if
  48.  
  49.         if y2% < ty2% then
  50.                 y2% = y2% + d%
  51.         ELSE
  52.                 y2% = y2% - d%
  53.         end if
  54.         if abs(y2%-ty2%) < d% then
  55.                 ty2%=screeny%*rnd
  56.         end if
  57.  
  58. sub smearfx()
  59.     _LIMIT 50
  60.     LINE (0, 0)-(screenx%, screeny%), _RGBA(0, 0, 0, 25), BF
  61.         line (x1%, y1%)-(x2%, y2%)
  62.     _DISPLAY
  63.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help: code not blending to complete black color
« Reply #1 on: May 13, 2021, 04:36:43 pm »
The screen is actually transparent until you cls, just cls will give default black background. That might help?

Hey nice effect!

Offline thesolarcode

  • Newbie
  • Posts: 8
    • View Profile
Re: Help: code not blending to complete black color
« Reply #2 on: May 13, 2021, 05:04:10 pm »
Thanks,

Not sure how to use CLS without destroying the line effect - I only draw one additional line per screen update, all the other lines are just made darker with one command :)
I'm probably doing the wrong thing - am I effectively just setting transparency to 25% and thus lines are still visible?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help: code not blending to complete black color
« Reply #3 on: May 13, 2021, 05:08:41 pm »
Oh just do it first thing before you start drawing, that sets the background to Black instead of transparent which looks black with no drawings on it.

I think you've got it right just CLS first thing, once, before drawing.
« Last Edit: May 13, 2021, 05:10:29 pm by bplus »

FellippeHeitor

  • Guest
Re: Help: code not blending to complete black color
« Reply #4 on: May 13, 2021, 05:38:39 pm »
thesolarcode: I've tried to understand the effect you're seeing with programs I'd written before, like fireworks and others. I never managed to understand why it doesn't eventually end up blacking the whole screen - those trails will remain there until you actually clear the screen with full alpha. I managed to observe the same outcome while playing with other programming languages too, it's still a mystery to me.

Here, try this one: https://openprocessing.org/sketch/1191793 (same technique of semi-transparent black rectangle to cover previous frame - same trail effect (defect)).
« Last Edit: May 13, 2021, 06:08:33 pm by FellippeHeitor »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Help: code not blending to complete black color
« Reply #5 on: May 14, 2021, 03:41:27 am »
I had a little problem when I ran this.... Cut and paste into IDE then F5. A small black window view appeared in the top left corner of the screen just for a moment. Pressed F5 again. The black window view appeared again and the whole display locked up. Rebooted. Re-ran. F5 black window again for a moment. F5 and the program ran normally. Escape to end. Tried again and the black window appeared again and locked up the system. Each time it locked up, the only two thing that worked was mouse movement and the system reset button....

I checked the listing. There doesn't seem to be anything there tat I have not used before (except for checking:off).

The time that it ran, it ran very well... I see what you mean about the 'dark grey' lines. Looks a lot like the qbasic screen saver, Mystify... Cool...

As the listing looks 'normal', I'm going to assume that the problem may be with my machine, and pass on running again until I try to figure out the problem.

I am running on Linux Mint 20, 64bit using QB64 1.5 stable from git 3043116
Logic is the beginning of wisdom.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Help: code not blending to complete black color
« Reply #6 on: May 14, 2021, 03:48:22 am »
Ok. First test... Removal of 'checking:off'. Successfully ran the program 7 times in succession... Locked up on the 8th try.... Oh well.... Worth a shot...
Logic is the beginning of wisdom.

Offline thesolarcode

  • Newbie
  • Posts: 8
    • View Profile
Re: Help: code not blending to complete black color
« Reply #7 on: May 14, 2021, 04:53:42 am »
I changed the code a bit - this time I copy an image over and over to achieve the fading effect, but I have the same problem with the faded lines being still visible.
I guess there is no other way in QB64 as to darken/fade each pixel individually, right?

Code: QB64: [Select]
  1.  
  2. dim shared screenx%, screeny%, tx1%, ty1%, tx2%, ty2%, x1%, y1%, x2%, y2%, d%, handle2&
  3. screenx%=800
  4. screeny%=600
  5.        
  6. SCREEN _NEWIMAGE(screenx%,screeny%,32)
  7.  
  8. ' create new black image with alpha channel
  9. handle2& = _NEWIMAGE(screenx%,screeny%,32)
  10. _DEST handle2&
  11. LINE (0, 0)-(screenx%, screeny%), _RGBA(0, 0, 0, 3), BF
  12.  
  13. tx1%=screenx%*rnd
  14. ty1%=screeny%*rnd
  15. tx2%=screenx%*rnd
  16. ty2%=screeny%*rnd
  17. x1%=screenx%*rnd
  18. y1%=screeny%*rnd
  19. x2%=screenx%*rnd
  20. y2%=screeny%*rnd
  21. d%=3
  22.  
  23.         color _RGB(256*rnd, 256*rnd, 256*rnd)
  24.         smearfx
  25.        
  26.         if x1% < tx1% then
  27.                 x1% = x1% + d%
  28.         ELSE
  29.                 x1% = x1% - d%
  30.         end if
  31.         if ABS(x1%-tx1%) < d% then
  32.                 tx1%=screenx%*rnd
  33.         end if
  34.  
  35.         if y1% < ty1% then
  36.                 y1% = y1% + d%
  37.         ELSE
  38.                 y1% = y1% - d%
  39.         end if
  40.         if abs(y1%-ty1%) < d% then
  41.                 ty1%=screeny%*rnd
  42.         end if
  43.        
  44.         if x2% < tx2% then
  45.                 x2% = x2% + d%
  46.         ELSE
  47.                 x2% = x2% - d%
  48.         end if
  49.         if abs(x2%-tx2%) < d% then
  50.                 tx2%=screenx%*rnd
  51.         end if
  52.  
  53.         if y2% < ty2% then
  54.                 y2% = y2% + d%
  55.         ELSE
  56.                 y2% = y2% - d%
  57.         end if
  58.         if abs(y2%-ty2%) < d% then
  59.                 ty2%=screeny%*rnd
  60.         end if
  61.  
  62. sub smearfx()
  63.     _LIMIT 50
  64.     'LINE (0, 0)-(screenx%, screeny%), _RGBA(0, 0, 0, 25), BF
  65.     _PUTIMAGE (0,0), handle2&, 0
  66.     line (x1%, y1%)-(x2%, y2%)
  67.     _DISPLAY
  68.  

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Help: code not blending to complete black color
« Reply #8 on: May 14, 2021, 06:28:32 am »
It's not just QB64. Because of my "screen lock" issue, I tried to replicate the problem, using RCBasic. No screen lockup but the "dark line" issue was still there. Reducing the "alpha" to "30" reduced it a little... Too low and the display looks like only a few lines or too high and the "grey line" is more obvious. I will continue to research...
Logic is the beginning of wisdom.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Help: code not blending to complete black color
« Reply #9 on: May 14, 2021, 09:17:28 am »
Here is a rough conversion of a javascript example (
)

Even in the JS version, the remnants of the fading, was still present.

Code: QB64: [Select]
  1. DIM SHARED canvWidth
  2. DIM SHARED canvHeight
  3.  
  4. canvWidth = 800
  5. canvHeight = 600
  6. SCREEN _NEWIMAGE(canvWidth, canvHeight, 32)
  7.  
  8.  
  9. DIM SHARED x1, y1, x2, y2
  10. DIM SHARED xv1, yv1, xv2, yv2
  11.  
  12. DIM SHARED x1b, y1b, x2b, y2b
  13. DIM SHARED xv1b, yv1b, xv2b, yv2b
  14.  
  15. DIM SHARED alpha
  16.  
  17. cyan = _RGB32(0, 255, 255)
  18. yellow = _RGB32(255, 255, 0)
  19.  
  20. '   Init
  21. x1 = INT(RND * canvWidth)
  22. y1 = INT(RND * canvHeight)
  23. x2 = INT(RND * canvWidth)
  24. y2 = INT(RND * canvHeight)
  25.  
  26. x1b = INT(RND * canvWidth)
  27. y1b = INT(RND * canvHeight)
  28. x2b = INT(RND * canvWidth)
  29. y2b = INT(RND * canvHeight)
  30.  
  31. xv1 = 5 - INT(RND * 10)
  32. yv1 = 5 - INT(RND * 10)
  33. xv2 = 5 - INT(RND * 10)
  34. yv2 = 5 - INT(RND * 10)
  35.  
  36. xv1b = 5 - INT(RND * 10)
  37. yv1b = 5 - INT(RND * 10)
  38. xv2b = 5 - INT(RND * 10)
  39. yv2b = 5 - INT(RND * 10)
  40.  
  41. alpha = 30
  42.  
  43.     update
  44.  
  45. SUB update
  46.     _LIMIT 60
  47.     LINE (0, 0)-(canvWidth - 1, canvHeight - 1), _RGBA(0, 0, 64, alpha), BF
  48.     alpha = 15
  49.  
  50.     x1 = x1 + xv1
  51.     y1 = y1 + yv1
  52.     x2 = x2 + xv2
  53.     y2 = y2 + yv2
  54.     x1b = x1b + xv1b
  55.     y1b = y1b + yv1b
  56.     x2b = x2b + xv2b
  57.     y2b = y2b + yv2b
  58.  
  59.     IF x1 < 0 OR x1 > canvWidth THEN
  60.         xv1 = xv1 * -1
  61.     END IF
  62.     IF y1 < 0 OR y1 > canvHeight THEN
  63.         yv1 = yv1 * -1
  64.     END IF
  65.     IF x2 < 0 OR x2 > canvWidth THEN
  66.         xv2 = xv2 * -1
  67.     END IF
  68.     IF y2 < 0 OR y2 > canvHeight THEN
  69.         yv2 = yv2 * -1
  70.     END IF
  71.  
  72.     IF x1b < 0 OR x1b > canvWidth THEN
  73.         xv1b = xv1b * -1
  74.     END IF
  75.     IF y1b < 0 OR y1b > canvHeight THEN
  76.         yv1b = yv1b * -1
  77.     END IF
  78.     IF x2b < 0 OR x2b > canvWidth THEN
  79.         xv2b = xv2b * -1
  80.     END IF
  81.     IF y2b < 0 OR y2b > canvHeight THEN
  82.         yv2b = yv2b * -1
  83.     END IF
  84.  
  85.     LINE (x1, y1)-(x2, y2), _RGB32(0, 255, 255)
  86.     LINE (x1b, y1b)-(x2b, y2b), _RGB32(255, 255, 0)
  87.     _DISPLAY
  88.  
Logic is the beginning of wisdom.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Help: code not blending to complete black color
« Reply #10 on: May 15, 2021, 01:44:52 am »
Quote
I guess there is no other way in QB64 as to darken/fade each pixel individually, right?

MEMGET and MEMPUT to the rescue


Code: QB64: [Select]
  1.  
  2. DIM SHARED screenx%, screeny%, tx1%, ty1%, tx2%, ty2%, x1%, y1%, x2%, y2%, d%, handle2&
  3.  
  4. DIM handle AS LONG
  5. screenx% = 800
  6. screeny% = 600
  7.  
  8. handle = _NEWIMAGE(screenx%, screeny%, 32)
  9.  
  10. m = _MEMIMAGE(handle)
  11.  
  12. SCREEN handle
  13.  
  14.  
  15. ' create new black image with alpha channel
  16. 'handle2& = _NEWIMAGE(screenx%, screeny%, 32)
  17. '_DEST handle2&
  18. 'LINE (0, 0)-(screenx%, screeny%), _RGBA(0, 0, 0, 255), BF
  19. '_DEST 0
  20.  
  21. tx1% = screenx% * RND
  22. ty1% = screeny% * RND
  23. tx2% = screenx% * RND
  24. ty2% = screeny% * RND
  25. x1% = screenx% * RND
  26. y1% = screeny% * RND
  27. x2% = screenx% * RND
  28. y2% = screeny% * RND
  29. d% = 3
  30.  
  31.     COLOR _RGB(256 * RND, 256 * RND, 256 * RND)
  32.     ' smearfx
  33.  
  34.  
  35.     FOR T = 0 TO (800 * 600 - 1) * 4 STEP 4
  36.         B = _MEMGET(m, m.OFFSET + T, _UNSIGNED _BYTE)
  37.         G = _MEMGET(m, m.OFFSET + T + 1, _UNSIGNED _BYTE)
  38.         R = _MEMGET(m, m.OFFSET + T + 2, _UNSIGNED _BYTE)
  39.         IF B > 1 THEN B = B - 2
  40.         IF B = 1 THEN B = 0
  41.         IF G > 1 THEN G = G - 2
  42.         IF G = 1 THEN G = 0
  43.         IF R > 1 THEN R = R - 2
  44.         IF R = 1 THEN R = 0
  45.         _MEMPUT m, m.OFFSET + T, B
  46.         _MEMPUT m, m.OFFSET + T + 1, G
  47.         _MEMPUT m, m.OFFSET + T + 2, R
  48.     NEXT T
  49.  
  50.     IF x1% < tx1% THEN
  51.         x1% = x1% + d%
  52.     ELSE
  53.         x1% = x1% - d%
  54.     END IF
  55.     IF ABS(x1% - tx1%) < d% THEN
  56.         tx1% = screenx% * RND
  57.     END IF
  58.  
  59.     IF y1% < ty1% THEN
  60.         y1% = y1% + d%
  61.     ELSE
  62.         y1% = y1% - d%
  63.     END IF
  64.     IF ABS(y1% - ty1%) < d% THEN
  65.         ty1% = screeny% * RND
  66.     END IF
  67.  
  68.     IF x2% < tx2% THEN
  69.         x2% = x2% + d%
  70.     ELSE
  71.         x2% = x2% - d%
  72.     END IF
  73.     IF ABS(x2% - tx2%) < d% THEN
  74.         tx2% = screenx% * RND
  75.     END IF
  76.  
  77.     IF y2% < ty2% THEN
  78.         y2% = y2% + d%
  79.     ELSE
  80.         y2% = y2% - d%
  81.     END IF
  82.     IF ABS(y2% - ty2%) < d% THEN
  83.         ty2% = screeny% * RND
  84.     END IF
  85.  
  86.     LINE (x1%, y1%)-(x2%, y2%)
  87.  
  88. SUB smearfx ()
  89.     _LIMIT 50
  90.     LINE (0, 0)-(screenx%, screeny%), _RGBA(0, 0, 0, 10), BF
  91.     '_PUTIMAGE (0, 0), handle2&, 0
  92.     LINE (x1%, y1%)-(x2%, y2%)
  93.     _DISPLAY

« Last Edit: May 15, 2021, 02:24:50 am by NOVARSEG »

Offline thesolarcode

  • Newbie
  • Posts: 8
    • View Profile
Re: Help: code not blending to complete black color
« Reply #11 on: May 15, 2021, 02:32:34 am »
Yes, that is it - the effect looks really great now (even better if you decrease by 1 - then it fades really slowly towards black color).
Today I learned something new (how to use MEMGET/PUT for gfx).
Thanks :)
« Last Edit: May 20, 2021, 10:09:35 am by thesolarcode »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Help: code not blending to complete black color
« Reply #12 on: May 15, 2021, 03:09:54 am »
Decrease by 1 does look better.


Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Help: code not blending to complete black color
« Reply #13 on: May 15, 2021, 03:51:29 am »
small bug . changed
 FOR T = 0 TO (800 * 600 - 1) * 4 STEP 4
to
FOR T = 0 TO 800 * 600 * 4 - 1 STEP 4


Code: QB64: [Select]
  1.  
  2. DIM SHARED screenx%, screeny%, tx1%, ty1%, tx2%, ty2%, x1%, y1%, x2%, y2%, d%, handle2&
  3.  
  4. DIM handle AS LONG
  5. screenx% = 800
  6. screeny% = 600
  7.  
  8. handle = _NEWIMAGE(screenx%, screeny%, 32)
  9.  
  10. m = _MEMIMAGE(handle)
  11.  
  12. SCREEN handle
  13.  
  14.  
  15. ' create new black image with alpha channel
  16. 'handle2& = _NEWIMAGE(screenx%, screeny%, 32)
  17. '_DEST handle2&
  18. 'LINE (0, 0)-(screenx%, screeny%), _RGBA(0, 0, 0, 255), BF
  19. '_DEST 0
  20.  
  21. tx1% = screenx% * RND
  22. ty1% = screeny% * RND
  23. tx2% = screenx% * RND
  24. ty2% = screeny% * RND
  25. x1% = screenx% * RND
  26. y1% = screeny% * RND
  27. x2% = screenx% * RND
  28. y2% = screeny% * RND
  29. d% = 3
  30.  
  31.     COLOR _RGB(256 * RND, 256 * RND, 256 * RND)
  32.     ' smearfx
  33.  
  34.     FOR T = 0 TO 800 * 600 * 4 - 1 STEP 4
  35.         B = _MEMGET(m, m.OFFSET + T, _UNSIGNED _BYTE)
  36.         G = _MEMGET(m, m.OFFSET + T + 1, _UNSIGNED _BYTE)
  37.         R = _MEMGET(m, m.OFFSET + T + 2, _UNSIGNED _BYTE)
  38.         IF B > 1 THEN B = B - 1
  39.         'IF B = 1 THEN B = 0
  40.         IF G > 1 THEN G = G - 1
  41.         ' IF G = 1 THEN G = 0
  42.         IF R > 1 THEN R = R - 1
  43.         'IF R = 1 THEN R = 0
  44.         _MEMPUT m, m.OFFSET + T, B
  45.         _MEMPUT m, m.OFFSET + T + 1, G
  46.         _MEMPUT m, m.OFFSET + T + 2, R
  47.     NEXT T
  48.  
  49.     IF x1% < tx1% THEN
  50.         x1% = x1% + d%
  51.     ELSE
  52.         x1% = x1% - d%
  53.     END IF
  54.     IF ABS(x1% - tx1%) < d% THEN
  55.         tx1% = screenx% * RND
  56.     END IF
  57.  
  58.     IF y1% < ty1% THEN
  59.         y1% = y1% + d%
  60.     ELSE
  61.         y1% = y1% - d%
  62.     END IF
  63.     IF ABS(y1% - ty1%) < d% THEN
  64.         ty1% = screeny% * RND
  65.     END IF
  66.  
  67.     IF x2% < tx2% THEN
  68.         x2% = x2% + d%
  69.     ELSE
  70.         x2% = x2% - d%
  71.     END IF
  72.     IF ABS(x2% - tx2%) < d% THEN
  73.         tx2% = screenx% * RND
  74.     END IF
  75.  
  76.     IF y2% < ty2% THEN
  77.         y2% = y2% + d%
  78.     ELSE
  79.         y2% = y2% - d%
  80.     END IF
  81.     IF ABS(y2% - ty2%) < d% THEN
  82.         ty2% = screeny% * RND
  83.     END IF
  84.  
  85.     LINE (x1%, y1%)-(x2%, y2%)
  86.  
  87. SUB smearfx ()
  88.     _LIMIT 50
  89.     LINE (0, 0)-(screenx%, screeny%), _RGBA(0, 0, 0, 1), BF
  90.     _PUTIMAGE (0, 0), handle2&, 0
  91.     LINE (x1%, y1%)-(x2%, y2%)
  92.     _DISPLAY
  93.  
« Last Edit: May 15, 2021, 03:53:19 am by NOVARSEG »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Help: code not blending to complete black color
« Reply #14 on: May 15, 2021, 07:16:33 am »
I still have difficulty executing. Eventually it started and the fade effect is much better... Well done!
Logic is the beginning of wisdom.