Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] How come CLS clears more than the _DEST image here?
#1
I was playing with an image drawing program tonight, drawing over a moving plasma background.  To do this I'm combining two image screens (back& and drawing&) onto the main screen (_DEST 0).  That works fine, but when I try to CLS the drawing& image screen, it also clears the plasma back& image too.  How come?  I must be misunderstanding something about _DEST and CLS use.

Here's the program.  Use the mouse to draw on the screen.  You can press SPACE to clear the screen.  Afterwards you will see the plasma background is cleared also, although I _DEST to the drawing& image.

Line #93 is where the CLS thing happens.

- Dav

Code: (Select All)
'==============
'plasmadraw.bas
'==============
'Draw on a moving plasma background
'Coded by Dav, OCT/2024

Screen _NewImage(800, 800, 32)

'make screens
back& = _NewImage(800, 800, 32) 'plasma background
drawing& = _NewImage(800, 800, 32) 'drawing screen

points = 8 'number of drawing points on drawing screen
Dim pointa(points), pointx(points), pointy(points), lastmx(points), lastmy(points)
pointsize = 3 'size of points

cx = _Width \ 2: cy = _Height \ 2

Do

    t = Timer * 1

    '=== update plasma background image
    _Dest back&
    For y = 0 To _Height Step 3
        For x = 0 To _Width Step 3
            r = Int(128 + 127 * Sin(x / 15 + t) + 127 * Cos(y / 15 + t))
            g = Int(128 + 127 * Sin(x / 25 + t * 1.5) + 127 * Cos(y / 25 + t * 1.5))
            b = Int(128 + 127 * Sin(x / 35 + t * 2) + 127 * Cos(y / 35 + t * 2))
            Line (x, y)-Step(2, 2), _RGBA(r / 3, g / 3, b / 3, Rnd * 30), BF
        Next
    Next

    '=== do/update drawing screen
    _Dest drawing&
    While _MouseInput: Wend
    mx = _MouseX: my = _MouseY

    If _MouseButton(1) Then
        If stilldown = 1 Then
            dx = mx - cx: dy = my - cy
            a = _Atan2(dy, dx)
            dis = Sqr(dx * dx + dy * dy)
            For i = 1 To points
                pointa(i) = (360 / points) * i * (3.14159 / 180)
                pointx(i) = cx + dis * Cos(pointa(i) + a)
                pointy(i) = cy + dis * Sin(pointa(i) + a)
                stepx = lastmx(i) - pointx(i)
                stepy = lastmy(i) - pointy(i)
                length = Int((stepx ^ 2 + stepy ^ 2) ^ .5)
                dx2 = stepx / length
                dy2 = stepy / length
                r = Int(128 + 127 * Sin(pointx(i) / 25 + t) + 127 * Cos(pointy(i) / 25 + t))
                g = Int(128 + 127 * Sin(pointx(i) / 25 + t * 1.5) + 127 * Cos(pointy(i) / 25 + t * 1.5))
                b = Int(128 + 127 * Sin(pointx(i) / 25 + t * 2) + 127 * Cos(pointy(i) / 25 + t * 2))
                For i2 = 0 To length
                    newx = pointx(i) + dx2 * i2
                    newy = pointy(i) + dy2 * i2
                    fc newx, newy, pointsize, _RGB(r, g, b), 0
                Next
                lastmx(i) = pointx(i)
                lastmy(i) = pointy(i)
            Next
        Else
            dx = mx - cx
            dy = my - cy
            a = _Atan2(dy, dx)
            dis = Sqr(dx * dx + dy * dy)
            For i = 1 To points
                pointa(i) = (360 / points) * i * (3.14159 / 180)
                pointx(i) = cx + dis * Cos(pointa(i) + a)
                pointy(i) = cy + dis * Sin(pointa(i) + a)
                lastmx(i) = pointx(i)
                lastmy(i) = pointy(i)
                r = Int(128 + 127 * Sin(pointx(i) / 25 + t) + 127 * Cos(pointy(i) / 25 + t))
                g = Int(128 + 127 * Sin(pointx(i) / 25 + t * 1.5) + 127 * Cos(pointy(i) / 25 + t * 1.5))
                b = Int(128 + 127 * Sin(pointx(i) / 25 + t * 2) + 127 * Cos(pointy(i) / 25 + t * 2))
                fc pointx(i), pointy(i), pointsize, _RGB(r, g, b), 0
            Next
            stilldown = 1
        End If
    Else
        stilldown = 0
    End If

    '=== combine both images to the main screen image
    _Dest 0
    _PutImage (0, 0), back&
    _PutImage (0, 0), drawing&

    If InKey$ = " " Then
        _Dest drawing& 'point to drawing& image
        Cls, 0: _Display '<<< this clears back& image too.  How come?
        _Dest 0
    End If

    _Display
    _Limit 30

Loop Until _KeyDown(27)


Sub fc (cx As Integer, cy As Integer, radius As Integer, clr~&, grad)
    If radius < 1 Then Exit Sub ' safety bail
    If grad = 1 Then
        red = _Red32(clr~&)
        grn = _Green32(clr~&)
        blu = _Blue32(clr~&)
        alpha = _Alpha32(clr~&)
    End If
    r2 = radius * radius
    For y = -radius To radius
        x = Abs(Sqr(r2 - y * y))
        ' If doing gradient
        If grad = 1 Then
            For i = -x To x
                dis = Sqr(i * i + y * y) / radius
                red2 = red * (1 - dis) + (red / 2) * dis
                grn2 = grn * (1 - dis) + (grn / 2) * dis
                blu2 = blu * (1 - dis) + (blu / 2) * dis
                clr2~& = _RGBA(red2, grn2, blu2, alpha)
                Line (cx + i, cy + y)-(cx + i, cy + y), clr2~&, BF
            Next
        Else
            Line (cx - x, cy + y)-(cx + x, cy + y), clr~&, BF
        End If
    Next
End Sub

Find my programs here in Dav's QB64 Corner
Reply
#2
Try:
Code: (Select All)
CLS, 0

The issue may be that you're clearing the screen with BLACK (with a full alpha background), and what you need is to clear it with a TRANSPARENT 0-alpha background.   If these images are supposed to overlay one on top of the other, there's a huge difference between Black and Transparent.

I haven't actually taken time to look at the code yet, but from what you describe, it'd be my guess that the problem could likely be something that simple for you.  Wink
Reply
#3
Ah, that's exactly it, Steve!  Yes, doing CLS, 0 works perfectly.

Thank you, thank you, thank you!

- Dav

Find my programs here in Dav's QB64 Corner
Reply




Users browsing this thread: 3 Guest(s)