DrawImageRect !

BlitzMax Forums/BlitzMax Beginners Area/DrawImageRect !

hub(Posted 2006) [#1]
It seems not easy to do this with bmax !

' 200x200 image
myimage = loadimage ("img.png")

... uhm now how to copy a portion for example
x = 10, y=10, width=100, height='100' to the backbuffer ? How to do this with bmax ! Could you post an easy example ! Many Thanks !!!

b+ documentation :
DrawImageRect image,x,y,rect_x,rect_y,rect_width,rect_height
Parameters
image = variable holding the image handle 
x = x location on the screen to draw the image 
y = y location on the screen to draw the image 
rect_x = starting x location within the image to draw 
rect_y = starting y location within the image to draw 
rect_width = the height of the area to draw 
rect_height = the width of the area to draw 


Description
This command will let you draw a rectangular PORTION of an image to the designated location on the screen. 



Dreamora(Posted 2006) [#2]
With Images (don't forget the image:TImage), you must draw the image to backbuffer and grab the wished portion from it. You can't read it straight from the texture.

The alternative is going over Pixmaps using lockimage


tonyg(Posted 2006) [#3]
You can use the
setviewport method


CS_TBL(Posted 2006) [#4]
Is it possible to apply the scale also to the x/y coordinates in a canvas? Uh, as in:

at scale 1,1 I draw a 8x8 tile at coord 8,8

at scale 2,2 I draw the 8x8 tile -which looks like 16x16- at 8,8 (which is automatically transformed to 16,16


ImaginaryHuman(Posted 2006) [#5]
According to the OpenGl documentation, you cannot use a viewport to always guarantee that something will not be drawn outside of the viewport area. It isn't consistent across graphics cards or implementations of OpenGL. You CAN define a clipping window, which is different. The viewport is to do with `how you look at the scene`, not so much a restriction on its perimeter.


tonyg(Posted 2006) [#6]
... but I thought a scissor box limited the rendering and any pixels outside the box were ignored.
<edit>
i.e is the same restriction for glscissor as glviewport?


hub(Posted 2006) [#7]
Could you test it. Here i can't display correctly the second character ! Maybe an error into my code, but i can't find it !!! Thanks for your help !



Png file :



Haramanai(Posted 2006) [#8]
Here only the first is ok... the others looks like random ones.
I have a question.
Why you are not using an animatedImage for this task?


tonyg(Posted 2006) [#9]
It must be something wrong with your drawimagerect code. This works using the drawimagepart function...

<edited> for copy/paste error.


hub(Posted 2006) [#10]
Many thanks for your help !




CS_TBL(Posted 2006) [#11]
Is there a bug with the DrawImage command when using a custom drawimagerect (with the viewport thingy) ?
Stuff like CLS works as it should, but DrawImage actually draws a column of the whole image! The *rect width is perfect, but the height doesn't work at all!

Strict

Local window:TGadget=CreateWindow("o_O",0,0,640,480)
Local canvas:Tgadget=CreateCanvas(4,4,512,256,window)

AutoImageFlags(0)
Local img:TImage=LoadImage( pick some image, like a 640x480 one orso )


Local t:Int
Local scale:Int=1

SetGraphics CanvasGraphics(canvas)
	SetScale scale,scale
	For t=1 To 1 Step 1
		DrawImageRect2 img,t*8*scale,16*scale,t*8*scale,0,8*scale,8*scale
	Next
Flip


OnEnd quit
Repeat
	WaitEvent()
	If EventID()=EVENT_WINDOWCLOSE End
Forever

Function quit()
	GCCollect()
	End
End Function

Function DrawImageRect2(img:TImage,x:Int,y:Int,pickx:Int,picky:Int,w:Int,h:Int)
	Local tempx:Int
	Local tempy:Int
	Local tempw:Int
	Local temph:Int
	
	GetViewport tempx,tempy,tempw,temph
	
	SetViewport x,y,w,h
	DrawImage img,x-pickx,y-picky
	SetClsColor 255,255,255;Cls
	SetViewport tempx,tempy,tempw,temph
End Function