Quote: "the command ik paste image allows you to crop the image to whatever dimensions you want."
It doesn't crop. It stretches
Quote: "where you can use a2SetClip x1, y1, x2, y2, to clip the pasting of images, dots, lines, triangles and boxes filled with single colours or gradients."
Your right... minus images. a2SetClip only works with drawing operations.
The reason I wanted to know was if you could do it directly to images rather than a work around like the method I came up with. Though the sprite based method is actually quite fast. The code needs refining, especially the math for the UV's because there seems to be some float point errors with the division. But I made a safe guard to handle it.
As of uses. GUI components like windows that can be scrolled or scaled to reveal more content. I'm thinking the current method works very well. If you'd like to take a look at it, here it is:
[Edit: Updated v3]
#constant MAX_ITEMS 100
#constant SW screen width()
#constant SH screen width()
randomize timer()
type rect
x as float
y as float
width as float
height as float
endtype
global _rect as rect
global _clip as rect
global dim _image(MAX_ITEMS) as rect
sync on : sync rate 0
for n = 0 to MAX_ITEMS
_image(n).x = rnd(SW-300)
_image(n).y = rnd(SH-300)
_image(n).width = 20+rnd(280)
_image(n).height = 20+rnd(280)
cls rgb(rnd(255),rnd(255),rnd(255))
get image n+1,0,0,_image(n).width,_image(n).height,1
next
do : cls rgb(64,64,64)
x = mousex()
y = mousey()
_clip.x = x - 150
_clip.y = y - 150
_clip.width = 300
_clip.height = 300
// I use Advanced2d for 2d, but because many don't, did it like this
ink rgb(255,0,0),0
box _clip.x-1,_clip.y-1,_clip.x+_clip.width+1,_clip.y+_clip.height+1
ink rgb(64,64,64),0
box _clip.x,_clip.y,_clip.x+_clip.width,_clip.y+_clip.height
ink rgb(255,255,255),0
for n = 0 to MAX_ITEMS
_rect = _image(n)
clipPasteImage( n+1, _rect, _clip )
next
ink rgb(255,255,255),0
text 0,0,"Move Mouse to Reveal"
sync
loop
function clipPasteImage( id as integer, imageRegion as rect , clipRegion as rect )
local x as integer
local y as integer
local width as integer
local height as integer
local u1 as float
local u2 as float
local v1 as float
local v2 as float
local imageRight as float
local imageBottom as float
local clipRight as float
local clipBottom as float
imageRight = imageRegion.x + imageRegion.width
imageBottom = imageRegion.y + imageRegion.height
clipRight = clipRegion.x + clipRegion.width
clipBottom = clipRegion.y + clipRegion.height
// check if the image is overlapping the clipping region
x = (( imageRegion.x >= clipRegion.x ) && ( imageRegion.x <= clipRight ))
x = x || (( clipRegion.x >= imageRegion.x ) && ( clipRegion.x <= imageRight ))
y = (( imageRegion.y >= clipRegion.y ) && ( imageRegion.y <= clipBottom ))
y = y || (( clipRegion.y >= imageRegion.y ) && ( clipRegion.y <= imageBottom ))
if x && y
// check if the image is fully inside the clipping region else it needs to be clipped
if (imageRegion.x > clipRegion.x) && (imageRegion.y > clipRegion.y)
if (imageRight < clipRight) && (imageBottom < clipBottom)
paste image id, imageRegion.x, imageRegion.y
exitfunction
endif
endif
// work out the UV coords for the clip
u1 = 0.0 : u2 = 1.0
v1 = 0.0 : v2 = 1.0
`negative x
if imageRegion.x < clipRegion.x
u1 = (clipRegion.x-imageRegion.x) / imageRegion.width
endif
`positive x
if imageRight > clipRight
u2 = 1.0+(clipRight-imageRight) / imageRegion.width
endif
`negative y
if imageRegion.y < clipRegion.y
v1 = (clipRegion.y-imageRegion.y) / imageRegion.height
endif
`positive y
if imageBottom > clipBottom
v2 = 1.0+(clipBottom-imageBottom) / imageRegion.height
endif
// adjust sprite and paste
sprite 1,0,0,id
hide sprite 1
set sprite texture coord 1, 0, u1, v1
set sprite texture coord 1, 1, u2, v1
set sprite texture coord 1, 2, u1, v2
set sprite texture coord 1, 3, u2, v2
width = imageRegion.width*(u2-u1)
height = imageRegion.height*(v2-v1)
x = imageRegion.x + u1*imageRegion.width
y = imageRegion.y + v1*imageRegion.height
// safety - float point error (i think - this was done quick so all of this needs refinement)
if (u1 > 0) && ((x+width) < imageRight) then inc width, 1
if (v1 > 0) && ((y+height) < imageBottom) then inc height, 1
if (u2 < 1) && ((x+width) < clipRight) then inc width, 1
if (v2 < 1) && ((y+height) < clipBottom) then inc height, 1
size sprite 1, width, height
paste sprite 1, x, y
endif
endfunction
Note: As I said, needs refinement. And seems to be one bug... on it - [Fixed bug]