09-25-2024, 12:39 PM
(This post was last modified: 09-25-2024, 12:42 PM by ahenry3068.)
(09-25-2024, 12:26 PM)Dav Wrote: Has anyone made a routine to copy a paste a non-rectangular area of the screen? Or does QB64PE have a built-in way? I'm working on a new drawing program and trying to add a feature where you can draw an enclosed area with the mouse, copy it, and paste it. Making a circle area routine was easy, but having some difficulty with the any shape one. I'm doing a polygon approach for the any shaped one, capture all the pixels inside the drawn polygon. Wondered if someone here has already made such a routine that is working.
Here's the circle copy/paste that is working ok.
- Dav
Code: (Select All)
Screen _NewImage(1000, 700, 32)
For t = 1 To 7000
Color _RGB(Rnd * 255, Rnd * 255, Rnd * 255)
Print _Trim$(Str$(Int(Rnd * 10)));
Next
Circle (100, 100), 51, _RGB(255, 255, 255)
x& = GetCircle&(100, 100, 50)
Locate 2, 24:: Color _RGB(255, 255, 255)
Print " Copied this area of the screen "
_Delay 2
Cls
Do
PutCircle Rnd * _Width, Rnd * _Height, x&, Rnd * 255
_Limit 30
Loop Until InKey$ <> ""
Function GetCircle& (cx, cy, radius)
If circleimg& <> 0 Then _FreeImage circleimg&
circleimg& = _NewImage(radius * 2, radius * 2) '
_Dest circleimg&
For x = 0 To radius * 2
For y = 0 To radius * 2
If Sqr((x - radius) ^ 2 + (y - radius) ^ 2) <= radius Then
clr& = Point(cx - radius + x, cy - radius + y)
PSet (x, y), clr&
End If
Next
Next
GetCircle& = _CopyImage(circleimg&)
_FreeImage circleimg&
_Dest 0
End Function
Sub PutCircle (cx, cy, img&, alpha)
If img& = 0 Then Exit Sub
radius = _Width(img&) / 2
_Source img&
_Dest 0
For x = 0 To radius * 2
For y = 0 To radius * 2
If Sqr((x - radius) ^ 2 + (y - radius) ^ 2) <= radius Then
clr~& = Point(x, y)
r = _Red32(clr~&)
g = _Green32(clr~&)
b = _Blue32(clr~&)
PSet (cx + x, cy + y), _RGBA(r, g, b, alpha)
End If
Next
Next
_Source 0
End Sub
I don't have any code handy but I have a good idea how you must go about it. Because of how Video Ram is layed out _PutImage & _GetImage only operate on rectangular images. I believe you MUST be in 32 bit color mode to do this but what you have to do is do a _GetImage of what you want. Then Mask out the shape with pixels that have an _Alpha (opacity) value of 0. Then when you do the _PutImage the ALPHA=0 pixels will not be visible. Depending on the shape the masking code will be different. Your code is clever but I have the feeling that it might be to slow for animation. (Maybe not though, It is a bit amazing how fast modern computers are. But my code mind set is stuck on slower systems)