Where did my gradiated pixmap go?

BlitzMax Forums/BlitzMax Beginners Area/Where did my gradiated pixmap go?

The following code generates and should display a 4 corner/colour gradient rectangle...

Import brl.Graphics
Import brl.Max2D

Type TColour
	Field red:Float
	Field green:Float
	Field blue:Float
	Field alpha:Float
	
	Function Create:TColour(r:Int, g:Int, b:Int, a:Int = 255) 
		Local newColour:TColour = New TColour
		newColour.set(r, g, b, a) 
		Return newColour
	End Function
	
	Method set(r:Int, g:Int, b:Int, a:Int) 
		red = r
		green = g
		blue = b
		alpha = a
	End Method
	
	Method getARGB:Int() 
		Local r:Byte = red
		Local g:Byte = green
		Local b:Byte = blue
		Local a:Byte = alpha
		Local argb:Int = (a Shl 24) | (r Shl 16) | (g Shl 8) | (b) 
		Return argb
	End Method
	
	Method asString:String() 
		Local str:String = Hex(getARGB()) + " r:" + red + " g:" + green + " b:" + blue + " a:" + alpha
		Return str
	End Method
	
	Method use()
		SetColor(red,green,blue)
	End Method
	
	Method clone:TColour() 
		Local clonedColour:TColour = TColour.Create(red, green, blue, alpha) 
		Return clonedColour
	End Method
	
	Method minus(colour:TColour) 
		red:-colour.red
		blue:-colour.blue
		green:-colour.green
		alpha:-colour.alpha
	End Method
	
	Method subtract:TColour(colour:TColour) 
		Local result:TColour = clone() 
		result.minus(colour) 
		Return result
	End Method
	
	Method scale(scalar:Float) 
		red:*scalar
		green:*scalar
		blue:*scalar
		alpha:*scalar
	End Method
	
	Method add(colour:TColour) 
		red:+colour.red
		blue:+colour.blue
		green:+colour.green
		alpha:+colour.alpha
	End Method
	
End Type

Const TOPLEFT:Int = 0
Const TOPRIGHT:Int = 1
Const BOTTOMLEFT:Int = 2
Const BOTTOMRIGHT:Int = 3

Global tl:TColour = TColour.Create(163, 163, 35)      ' Yellow
Global tr:TColour = TColour.Create(163, 35, 35)      ' Red
Global bl:TColour = TColour.Create(35, 163, 35)      ' Blue
Global br:TColour = TColour.Create(35, 35, 163)     ' Green

Global defaultColours:TColour[] =[tl, tr, bl, br] 

'Rem ###  Quick Demo
'?linux
SetGraphicsDriver GLMax2DDriver()
'?

Graphics 800,600,0
'Load your picture

DebugLog "About to generate Image..."
image:TImage = ImageGradient() 
DebugLog "Image generated"
test:TImage = LoadImage("candyCascade2.jpeg")
assert test <> Null

While Not KeyHit(KEY_ESCAPE)

Cls
	
DrawImage(test, 0, 0)

DrawImageRect(image, 0, 0, GraphicsWidth(), GraphicsHeight()) 

Flip

Wend

'End Rem


Function ImageGradient:TImage(colours:TColour[] = Null) 
	
	If colours = Null Then
		colours = defaultColours
		DebugLog "defalut TL colour = "+defaultColours[TOPLEFT].asString()
		DebugLog "defalut TR colour = "+defaultColours[TOPRIGHT].asString()
		DebugLog "defalut BL colour = "+defaultColours[BOTTOMLEFT].asString()
		DebugLog "defalut BR colour = "+defaultColours[BOTTOMRIGHT].asString()
	End If
	
	pixmap:TPixmap = CreatePixmap(256,256,PF_RGBA8888)

	pixmap = PixmapColour(pixmap, colours) 

	DrawPixmap(Pixmap,0,0)
	gradientImage:TImage = CreateImage(256,256,1, DYNAMICIMAGE)
	GrabImage(gradientImage,0,0)
	
	Return gradientImage

End Function



Function PixmapColour:TPixmap(pixmap:TPixmap, colours:TColour[] = Null) 

	Local colour:TColour = Null

	Local hScalar:Float = 1.0/pixmap.height
	Local wScalar:Float = 1.0/pixmap.width

	' Gradient left
	Local leftGradient:TColour = colours[BOTTOMLEFT].subtract(colours[TOPLEFT] ) 
	leftGradient.scale(hScalar) 

	'DebugLog "leftGradient = "+leftGradient.asString()
	
	' Gradient right
	Local rightGradient:TColour = colours[BOTTOMRIGHT].subtract(colours[TOPRIGHT] ) 
	rightGradient.scale(hScalar) 
	
	'DebugLog "rightGradient = "+rightGradient.asString()
	
	' current left
	Local leftColour:TColour = colours[TOPLEFT] 
	' current right
	Local rightColour:TColour = colours[TOPRIGHT] 
	
	Local xGradient:TColour
	
	Local out:TPixmap = CreatePixmap(pixmap.width, pixmap.height, pixmap.format) 
	
	For y = 0 Until PixmapHeight(pixmap)
		colour = leftColour.clone() 
		xGradient = rightColour.subtract(leftColour) 
		xGradient.scale(wScalar) 
		
		'DebugLog "xGradient = "+xGradient.asString() 
  	
		For x = 0 Until PixmapWidth(pixmap) 					
			WritePixel(out, x, y, colour.getARGB()) 
			'DebugLog colour.asString() 
			colour.add(xGradient) 
		Next
		
		leftColour.add(leftGradient) 
		rightColour.add(rightGradient) 
	Next

	Return out
End Function 



This should now work on both OpenGL and DirectX, hopefully!

What's it do when you use OpenGL?

You get nothing - No errors and an image that when drawn does not appear??

I've been hunting around in the forum and it would appear that there was an issue with Pixmaps and OpenGL but this was a while ago, with no apparent solution?

It works fine on my computer with out any modifications and a substitute 640x480.bmp. A four corner gadient window is displayed.

windows xp sp2
AMD Athlon 64 x2 dual 4200+
Nvidia GeForce 7300GT
Bmax 1.28

It should work fine on windows as it defaults to DirectX rendering, I have now updated it so that it will use the OpenGL driver, now what does it do?

Well I think I've fixed it at least for opengl, would interested to know if it will work on Vista?

For some bizzar reason I was setting the alpha to 0 and this should have made the image transparent but it was appearing in DirectX (?) but transparent in OpenGL which was correct!

A classic case of it's not a bug it's a feature! ;o)

works fine with Vista x64 Ultimate, all m$ updates installed. no sign of the bitmap I supplied though, even resized to 800*600. I can only see the gradient. Is that correct?