January 26, 2021, 06:10:15 AM

Author Topic: [bmx] DrawImageRect like function by Dreamora [ 1+ years ago ]  (Read 522 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : DrawImageRect like function
Author : Dreamora
Posted : 1+ years ago

Description : This source is meant for an own file to import and not as stand alone code (will result in an error) that will compile this way (it would fail because of import but no framework)

Code :
Code: BlitzMax
  1. Rem
  2.         image: handle of the image you want to draw
  3.         x , y: Position at which you want to draw the imagerect
  4.         dw,dh: Drawing height and width of the image part you want do draw.
  5.         l,t: top left position on the image you want to start drawing (in pixels)
  6.         w,h: width and height of the imagerect you want to draw
  7. End Rem
  8.  
  9. Function DrawImageArea(image:TImage, x#, y#, dw#, dh#, l#, t#, w#, h#, frame:Int=0)
  10.         Local origin_x#,origin_y#
  11.         GetOrigin (origin_x , origin_y)
  12.         Local tw = Pow2Size(image.width)
  13.         Local th = Pow2Size(image.height)
  14.         Local l1#       = l + w
  15.         Local h1#       = t + h
  16.         Local x0#=-image.handle_x
  17.         Local y0# = - image.handle_y
  18.        
  19.         If l1 > image.width
  20.                 'x1 = x0 + rw + image.width - l1
  21.                 l1 = image.width
  22.         EndIf
  23.          
  24.         If h1 > image.height
  25.                 'y1 = y0 + rh + image.height - h1
  26.                 h1 = image.height
  27.         EndIf
  28.  
  29.         If TGLImageFrame (image.frame(frame))
  30.                 Local frame:TGLImageFrame       = TGLImageFrame (image.frame(frame))
  31.                                                                
  32.                 frame.u0 = l / tw
  33.                 frame.v0 = t / th
  34.                 frame.u1 = l1 / tw
  35.                 frame.v1 = h1 / th
  36.                 frame.Draw x0,y0,x0+dw,y0+dh,x+origin_x,y+origin_y
  37.                
  38.                 frame.u0        = 0
  39.                 frame.v0        = 0
  40.                 frame.u1        = 1
  41.                 frame.v1        = 1
  42.         Else
  43.                 Local frame:TD3D7ImageFrame     = TD3D7ImageFrame(image.frame(frame))
  44.                 frame.setUV (l/tw, t/th, l1 / tw, h1 / th)
  45.                 frame.Draw x0,y0,x0+dw,y0+dh,x+origin_x,y+origin_y
  46.                 frame.setUV (0,0,1,1)
  47.         EndIf
  48.        
  49.         Function Pow2Size(n)
  50.                 Local ry = 1
  51.                
  52.                 While ry < n
  53.                         ry :* 2
  54.                 Wend
  55.                
  56.                 Return ry
  57.         End Function
  58. End Function


Comments :


hub(Posted 1+ years ago)

 .


Dreamora(Posted 1+ years ago)

 Extended the code a little by adding the possibility to define the drawn imagearea width through dw and dh.By using negative h and w you can even flip your image rect.Corrected a bug with non-power of 2 textures. was spoted and corrected by GeoffTheGyratingGiraffe (BM internaly is not saving the real image size in VRAM but the size of the originally loaded, which is not the same for non-power of 2)


Yan(Posted 1+ years ago)

 I've just noticed that you're not resetting the UVs correctly.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal