Anyone got a nice little CopyRect type of function

BlitzMax Forums/BlitzMax Programming/Anyone got a nice little CopyRect type of function

Grey Alien(Posted 2006) [#1]
? I searched the forum and found a couple of ideas. 1) Use CreateImage (smaller), GrabImage, then drawimage 2) set a view port 3) Indiepaths Render to Texture (or whatever it's called).

Anyone willing to say which is the best method, and has anyone got any code? I'm gonna try the viewport method first.


Kuron(Posted 2006) [#2]
I would have sworn Tim posted a function for this some time ago that worked very well. I could be mistaken though. Its hard to remember who wrote what :c/


TartanTangerine (was Indiepath)(Posted 2006) [#3]
What are to wanting the function to do grey? is it to draw part of an image?


ImaginaryHuman(Posted 2006) [#4]
If you want to copy a rect FROM the backbuffer TO the backbuffer, in OpenGL at least you can use glCopyPixels()

You could also use glCopyTexSubImage2D() to copy into a texture then draw the texture.

Otherwise in non-OpenGL specific, use GrabPixmap, then create a second pixmap the size you want, then use Pixmap.paste, then DrawPixmap.


tonyg(Posted 2006) [#5]
Can't you use SetUv or pixmapwindow?
Graphics 640,480
mypixmap:TPixmap=LoadPixmap("max.png")
mypixmaprect:TPixmap=PixmapWindow(mypixmap,20,20,100,100)
DrawPixmap mypixmap,0,0
DrawPixmap mypixmaprect,200,0

Flip
WaitKey()



Yan(Posted 2006) [#6]
If you just want to draw part of an image (Heavily based on Dreamora's code from another thread...it's a long story)...



Grey Alien(Posted 2006) [#7]
Blimey lots of responses, thanks a lot! In the end I did this, which works fine:

' -----------------------------------------------------------------------------
' ccCopyRect
' -----------------------------------------------------------------------------
Function ccCopyRect(image:TImage,x,y,w,h,frame=0)
	'Draws a part of an image by using a viewport.
	SetViewport x,y,w,h
	DrawImage(image,x,y,frame)
	SetViewport 0,0,screenwidth, screenheight
End Function


Anyway see anything wrong with this? It should be cross platform too right?


TartanTangerine (was Indiepath)(Posted 2006) [#8]
Yeah the problem with your code is that you are assuming that the starting co-ords for the image are always 0,0. What if I wanted to draw only the bottom-right of the image or a 32x32 square from the center of the image?


ImaginaryHuman(Posted 2006) [#9]
You might want to do a little more research Mr Alien .... I remember reading in the OpenGL manual, I think, that you cannot be gauranteed that a viewport will crop everything outside of the viewport area. A viewport is more for defining the characteristics of the area that you ARE looking at, which might be extended beyond the edges of that area. To be sure that an area definitely will be clipped you have to define a Scissor window and switch on the scissor test. At least for OpenGL. They say that otherwise, on some systems they will actually not clip at all and just draw over the edges, trashing everything outside of the `viewport area`.


Diablo(Posted 2006) [#10]
Method SetViewport( x,y,w,h )
If x=0 And y=0 And w=GraphicsWidth() And h=GraphicsHeight()
glDisable GL_SCISSOR_TEST
Else
glEnable GL_SCISSOR_TEST
glScissor x,GraphicsHeight()-y-h,w,h
EndIf
End Method



Grey Alien(Posted 2006) [#11]
Indiepath: Thanks I can fix that easy enough.

AngelDaniel: Yeah I read that when you posted it a few months back. Hmm..

Diable. Is that a *new* replacement method or the one in the modules, I'm not understanding this time of night ;-)


Diablo(Posted 2006) [#12]
Diable. Is that a *new* replacement method or the one in the modules, I'm not understanding this time of night ;-)

Its taken out of glMax2D.bmx.


tonyg(Posted 2006) [#13]
Is there something wrong with doing this...
Function tg_drawimagerect(image:TImage,x:Int,y:Int,xs:Int,ys:Int,width:Int,height:Int)
    DrawImage LoadImage(PixmapWindow(LockImage(image),xs,xy,width,height)),x,y
End Function

?
It seems quick enough, is native Bmax so OK for GL/DX and takes all rotates, blends etc