Code archives/Graphics/DrawImageRect like function

This code has been declared by its author to be Public Domain code.

Download source code

DrawImageRect like function by Dreamora2005
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)
Rem
	image: handle of the image you want to draw
	x , y: Position at which you want to draw the imagerect
	dw,dh: Drawing height and width of the image part you want do draw.
	l,t: top left position on the image you want to start drawing (in pixels)
	w,h: width and height of the imagerect you want to draw
End Rem

Function DrawImageArea(image:TImage, x#, y#, dw#, dh#, l#, t#, w#, h#, frame:Int=0)
	Local origin_x#,origin_y#
	GetOrigin (origin_x , origin_y)
	Local tw = Pow2Size(image.width)
	Local th = Pow2Size(image.height)
	Local l1#	= l + w
	Local h1#	= t + h
	Local x0#=-image.handle_x
	Local y0# = - image.handle_y
	
	If l1 > image.width
		'x1 = x0 + rw + image.width - l1
		l1 = image.width
	EndIf
	 
	If h1 > image.height
		'y1 = y0 + rh + image.height - h1
		h1 = image.height
	EndIf

	If TGLImageFrame (image.frame(frame))
		Local frame:TGLImageFrame 	= TGLImageFrame (image.frame(frame))
								
		frame.u0 = l / tw
		frame.v0 = t / th
		frame.u1 = l1 / tw
		frame.v1 = h1 / th
		frame.Draw x0,y0,x0+dw,y0+dh,x+origin_x,y+origin_y
		
		frame.u0	= 0
		frame.v0	= 0
		frame.u1	= 1
		frame.v1	= 1
	Else
		Local frame:TD3D7ImageFrame	= TD3D7ImageFrame(image.frame(frame))
		frame.setUV (l/tw, t/th, l1 / tw, h1 / th)
		frame.Draw x0,y0,x0+dw,y0+dh,x+origin_x,y+origin_y
		frame.setUV (0,0,1,1)
	EndIf
	
	Function Pow2Size(n)
		Local ry = 1
		
		While ry < n
			ry :* 2
		Wend
		
		Return ry
	End Function
End Function

Comments

hub2006
.


Dreamora2006
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)


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


Code Archives Forum