DrawImageRect !

BlitzMax Forums/BlitzMax Beginners Area/DrawImageRect !

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. 


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

You can use the
setviewport method

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

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.

... 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?

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 !

Graphics 800,600

Type TFont

	Field x : Int[255]
	Field y : Int[255]
	Field w : Int[255]
	Field hImage:Timage
	
	Const CTE_NBRE_CARACTERES = 50	

	Function Charger_police:TFont(FileName$)
	
		Local i:Int
		Local ValeurAscii:Int
		Local Caractere$
		
		Local f:TFont = New TFONT
		
		For i=0 To CTE_NBRE_CARACTERES - 1
			
			ReadData Caractere$
			Valeur_ascii = Asc (Caractere$)
						
			ReadData f.x[Valeur_ascii]
			ReadData f.y[Valeur_ascii]
			ReadData f.w[Valeur_ascii]

		Next
		
		f.hImage = LoadImage (FileName$)
		
		Return f
		
	End Function

	Function DrawImageRect(img:TImage, x#, y#, rx#, ry#, rw#, rh#)
		SetViewport x, y, rw, rh
		DrawImage img, x - rx, x - ry
		
		SetViewport 0, 0, GraphicsWidth(), GraphicsHeight()
	End Function
		
	Method Afficher_texte (Texte$, PosX, PosY)
	
		Local i:Int
		Local Caractere : String
		Local aw = 0
		For i=0 To Len(Texte$)- 1
			
			CodeAscii = Texte$[i]
			lx = x [CodeAscii]
			ly = y [CodeAscii]
			lw = w [CodeAscii]
			
			DrawImageRect (hImage, PosX+aw,PosY,lx,ly,64,64)

			aw = aw + lw + 5
		
		Next
	
	End Method
		
		
End Type

Local f:TFONT

f = TFont.Charger_police ("media/bada.png")
While Not KeyDown(KEY_ESCAPE)
	f.Afficher_texte ("HELLO", 100, 100)
	Flip
Wend

	
' Symbo, x, y, width			
DefData "!",0,0,22
DefData "-",64,0,25
DefData "#",128,0,32
DefData "$",192,0,23
DefData "%",256,0,34
DefData "&",320,0,29
DefData "'",384,0,15
DefData "(",448,0,21
DefData ")",0,64,21
DefData "*",64,64,21
DefData "+",128,64,17
DefData ",",192,64,17
DefData "-",256,64,17
DefData ".",320,64,13
DefData "/",384,64,32
DefData "0",448,64,26
DefData "1",0,128,18
DefData "2",64,128,26
DefData "3",128,128,24
DefData "4",192,128,23
DefData "5",256,128,23
DefData "6",320,128,24
DefData "7",384,128,23
DefData "8",448,128,27
DefData "9",0,192,26
DefData "A",64,192,25
DefData "B",128,192,24
DefData "C",192,192,24
DefData "D",256,192,25
DefData "E",320,192,22
DefData "F",384,192,22
DefData "G",448,192,25
DefData "H",0,256,26
DefData "I",64,256,16
DefData "J",128,256,22
DefData "K",192,256,26
DefData "L",256,256,18
DefData "M",320,256,33
DefData "N",384,256,26
DefData "O",448,256,26
DefData "P",0,320,24
DefData "Q",64,320,26
DefData "R",128,320,24
DefData "S",192,320,23
DefData "T",256,320,21
DefData "U",320,320,25
DefData "V",384,320,23
DefData "W",448,320,31
DefData "X",0,384,28
DefData "Y",64,384,24
DefData "Z",128,384,27


Png file :


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?

It must be something wrong with your drawimagerect code. This works using the drawimagepart function...
Graphics 800,600,0

Type TFont

	Field x : Int[255]
	Field y : Int[255]
	Field w : Int[255]
	Field hImage:Timage
	
	Const CTE_NBRE_CARACTERES = 50	

	Function Charger_police:TFont(FileName$)
	
		Local i:Int
		Local ValeurAscii:Int
		Local Caractere$
		
		Local f:TFont = New TFONT
		
		For i=0 To CTE_NBRE_CARACTERES - 1
			
			ReadData Caractere$
			Valeur_ascii = Asc (Caractere$)
						
			ReadData f.x[Valeur_ascii]
			ReadData f.y[Valeur_ascii]
			ReadData f.w[Valeur_ascii]

		Next
		
		f.hImage = LoadImage (FileName$)
		
		Return f
		
	End Function

	Function DrawImageRect(img:TImage, x#, y#, rx#, ry#, rw#, rh#)
		SetViewport x, y, rw, rh
		DrawImage img, x - rx, x - ry
		
		SetViewport 0, 0, GraphicsWidth(), GraphicsHeight()
	End Function
		
	Method Afficher_texte (Texte$, PosX, PosY)
	
		Local i:Int
		Local Caractere : String
		Local aw = 0
		For i=0 To Len(Texte$)- 1
			
			CodeAscii = Texte$[i]
			lx = x [CodeAscii]
			ly = y [CodeAscii]
			lw = w [CodeAscii]
			DebugLog "lx " + lx + " ly " + ly
			DrawImagepart (hImage, PosX+aw,PosY,lx,ly,64,64)

			aw = aw + lw + 5
		
		Next
	
	End Method
		
		
End Type
Function DrawImagePart(Image:TImage, DrawX#, DrawY#, PartX#, PartY#, PartWidth#, PartHeight#, Frame# = 0)
	Local OldX:Int
	Local OldY:Int
	Local OldWidth:Int
	Local OldHeight:Int
	
	Local ViewportX:Int = DrawX
	Local ViewportY:Int = DrawY
	
	' Save current viewport settings
	GetViewport(OldX, OldY, OldWidth, OldHeight)
	
	' Calculate viewport coordinates based on image's handle	
	If Image.Handle_X Then
		Local PercentX:Float
		PercentX = Float(Image.Handle_X) / Float(Image.Width)
		ViewportX = DrawX - (PercentX * PartWidth)
	EndIf
	If Image.Handle_Y Then
		Local PercentY:Float
		PercentY = Float(Image.Handle_Y) / Float(Image.Height)
		ViewportY = DrawY - (PercentY * PartHeight)
	EndIf
	
	SetViewport(ViewportX, ViewportY, PartWidth, PartHeight)
	DrawImage(Image, DrawX-PartX, DrawY-PartY, Frame)
	
	' Restore old viewport settings
	SetViewport(OldX, OldY, OldWidth, OldHeight)
End Function

Local f:TFONT

f = TFont.Charger_police ("bada.png")
While Not KeyDown(KEY_ESCAPE)
	f.Afficher_texte ("!!#", 100, 100)
	Flip
Wend

	
' Symbo, x, y, width			
DefData "!",0,0,22
DefData "!",64,0,25
DefData "#",128,0,32
DefData "$",192,0,23
DefData "%",256,0,34
DefData "&",320,0,29
DefData "'",384,0,15
DefData "(",448,0,21
DefData ")",0,64,21
DefData "*",64,64,21
DefData "+",128,64,17
DefData ",",192,64,17
DefData "-",256,64,17
DefData ".",320,64,13
DefData "/",384,64,32
DefData "0",448,64,26
DefData "1",0,128,18
DefData "2",64,128,26
DefData "3",128,128,24
DefData "4",192,128,23
DefData "5",256,128,23
DefData "6",320,128,24
DefData "7",384,128,23
DefData "8",448,128,27
DefData "9",0,192,26
DefData "A",64,192,25
DefData "B",128,192,24
DefData "C",192,192,24
DefData "D",256,192,25
DefData "E",320,192,22
DefData "F",384,192,22
DefData "G",448,192,25
DefData "H",0,256,26
DefData "I",64,256,16
DefData "J",128,256,22
DefData "K",192,256,26
DefData "L",256,256,18
DefData "M",320,256,33
DefData "N",384,256,26
DefData "O",448,256,26
DefData "P",0,320,24
DefData "Q",64,320,26
DefData "R",128,320,24
DefData "S",192,320,23
DefData "T",256,320,21
DefData "U",320,320,25
DefData "V",384,320,23
DefData "W",448,320,31
DefData "X",0,384,28
DefData "Y",64,384,24
DefData "Z",128,384,27


<edited> for copy/paste error.

Many thanks for your help !

Strict
Graphics 800,600,0

Type TFont

	Field x : Int[255]
	Field y : Int[255]
	Field w : Int[255]
	Field hImage:Timage
	
	Const CTE_NBRE_CARACTERES = 50	

	Function Charger_police:TFont(FileName$)
	
		Local i:Int
		Local Valeur_ascii:Int
		Local Caractere$
		
		Local f:TFont = New TFONT
		
		For i=0 To CTE_NBRE_CARACTERES - 1
			
			ReadData Caractere$
			Valeur_ascii = Asc (Caractere$)
						
			ReadData f.x[Valeur_ascii]
			ReadData f.y[Valeur_ascii]
			ReadData f.w[Valeur_ascii]

		Next
		
		f.hImage = LoadImage (FileName$)
		
		Return f
		
	End Function

	Function DrawImageRect(img:TImage, x#, y#, rx#, ry#, rw#, rh#)
		SetViewport x, y, rw, rh
		DrawImage img, x - rx, x - ry
		
		SetViewport 0, 0, GraphicsWidth(), GraphicsHeight()
	End Function
		
	Method Afficher_texte (Texte$, PosX, PosY)
	
		Local i:Int
		Local Caractere : String
		Local aw : Int = 0, lx,ly,lw
		Local CodeAscii : Int
		
		Texte$ = Texte$.ToUpper()
		
		For i=0 To Len(Texte$)- 1
			
			CodeAscii = Texte$[i]
			
			If CodeAscii = 32 Then 
				aw = aw + 16
			Else
				lx = x [CodeAscii]
				ly = y [CodeAscii]
				lw = w [CodeAscii]
				DrawImagepart (hImage, PosX+aw,PosY,lx,ly,64,64)
				aw = aw + lw 
			End If
		
		Next
	End Method
		
		
	Function DrawImagePart(Image:TImage, DrawX#, DrawY#, PartX#, PartY#, PartWidth#, PartHeight#, Frame# = 0)
		Local OldX:Int
		Local OldY:Int
		Local OldWidth:Int
		Local OldHeight:Int
		
		Local ViewportX:Int = DrawX
		Local ViewportY:Int = DrawY
		
		' Save current viewport settings
		GetViewport(OldX, OldY, OldWidth, OldHeight)
		
		' Calculate viewport coordinates based on image's handle	
		If Image.Handle_X Then
			Local PercentX:Float
			PercentX = Float(Image.Handle_X) / Float(Image.Width)
			ViewportX = DrawX - (PercentX * PartWidth)
		EndIf
		If Image.Handle_Y Then
			Local PercentY:Float
			PercentY = Float(Image.Handle_Y) / Float(Image.Height)
			ViewportY = DrawY - (PercentY * PartHeight)
		EndIf
		
		SetViewport(ViewportX, ViewportY, PartWidth, PartHeight)
		DrawImage(Image, DrawX-PartX, DrawY-PartY, Frame)
		
		' Restore old viewport settings
		SetViewport(OldX, OldY, OldWidth, OldHeight)
	End Function
		
End Type

Local f:TFONT

f = TFont.Charger_police ("media/bada.png")
While Not KeyDown(KEY_ESCAPE)
	f.Afficher_texte ("thanks FOR YOUR HELP !!!", 100, 100)
	Flip
Wend

	
' Symbo, x, y, width			
' Symbo, x, y, width			
DefData "!",0,0,22
DefData Chr$(34),64,0,25
DefData "#",128,0,32
DefData "$",192,0,23
DefData "%",256,0,34
DefData "&",320,0,29
DefData "'",384,0,15
DefData "(",448,0,21
DefData ")",0,64,21
DefData "*",64,64,21
DefData "+",128,64,17
DefData ",",192,64,17
DefData "-",256,64,17
DefData ".",320,64,13
DefData "/",384,64,32
DefData "0",448,64,26
DefData "1",0,128,18
DefData "2",64,128,26
DefData "3",128,128,24
DefData "4",192,128,23
DefData "5",256,128,23
DefData "6",320,128,24
DefData "7",384,128,23
DefData "8",448,128,27
DefData "9",0,192,26
DefData "A",64,192,25
DefData "B",128,192,24
DefData "C",192,192,24
DefData "D",256,192,25
DefData "E",320,192,22
DefData "F",384,192,22
DefData "G",448,192,25
DefData "H",0,256,26
DefData "I",64,256,16
DefData "J",128,256,22
DefData "K",192,256,26
DefData "L",256,256,18
DefData "M",320,256,33
DefData "N",384,256,26
DefData "O",448,256,26
DefData "P",0,320,24
DefData "Q",64,320,26
DefData "R",128,320,24
DefData "S",192,320,23
DefData "T",256,320,21
DefData "U",320,320,25
DefData "V",384,320,23
DefData "W",448,320,31
DefData "X",0,384,28
DefData "Y",64,384,24
DefData "Z",128,384,27


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