How to draw just a part of a sprite?

BlitzMax Forums/BlitzMax Beginners Area/How to draw just a part of a sprite?

Josepho(Posted 2007) [#1]
Hi to all, i have another question, anyone knows a speed way to draw only a part of a loaded image?

Thank you!


GfK(Posted 2007) [#2]
Use LoadImage in conjunction with LoadPixmap and PixmapWindow. There's a couple of examples in the code archives.


Grey Alien(Posted 2007) [#3]
You can use viewport but it doesn't work on all graphics cards!


Josepho(Posted 2007) [#4]
I am using grabimage, is it a good way to do it?


Grey Alien(Posted 2007) [#5]
this is the best method I have found so far:

' -----------------------------------------------------------------------------
' ccDrawImageArea by Ian Duff (faster?)
' -----------------------------------------------------------------------------
' Note that this code works fine in DirectX or OpenGL on PCs - it autodetects (JB).
Function ccDrawImageArea(image:TImage, x#, y#, rx#, ry#, rw#, rh#, theframe=0)
  Local origin_x#, origin_y# ; GetOrigin (origin_x, origin_y)
  Local tw = Pow2Size(image.width)
  Local th = Pow2Size(image.height)
  Local rw1#  = rx + rw
  Local rh1#  = ry + rh
  Local x0# = -image.handle_x, x1# = x0 + rw
  Local y0# = -image.handle_y, y1# = y0 + rh
  
  If rw1 > image.width
    x1 = x0 + rw + image.width - rw1
    rw1 = image.width
  EndIf
   
  If rh1 > image.height
    y1 = y0 + rh + image.height - rh1
    rh1 = image.height
  EndIf
?Win32
  If TD3D7ImageFrame(image.frame(theframe))
    Local frame:TD3D7ImageFrame = TD3D7ImageFrame(image.frame(theframe))
    
    frame.setUV(rx / tw, ry / th, rw1 / tw, rh1 / th)
    frame.Draw x0, y0, x1, y1, x + origin_x, y + origin_y
    frame.setUV(0, 0, image.width / Float(tw), image.height / Float(th))
  Else
?
    Local frameA:TGLImageFrame = TGLImageFrame (image.frame(theframe))
                
    frameA.u0 = rx / tw
    frameA.v0 = ry / th
    frameA.u1 = rw1 / tw
    frameA.v1 = rh1 / th
    
    frameA.Draw x0, y0, x1, y1, x + origin_x, y + origin_y
    
    frameA.u0 = 0
    frameA.v0 = 0
    frameA.u1 = image.width / Float(tw)
    frameA.v1 = image.height / Float(th)
?Win32
  EndIf
?
  
  Function Pow2Size(n)
    Local ry = 1
    
    While ry < n
      ry :* 2
    Wend
    
    Return ry
  End Function
End Function