I'm going mad here!

BlitzMax Forums/BlitzMax Beginners Area/I'm going mad here!

JBR(Posted 2016) [#1]
Hi, should be obvious but can't work it out. I'm increasing the red component but can't see it.


Strict
SetGraphicsDriver GLMax2DDriver()

Const C_Screen_Width = 1920
Const C_Screen_Depth = 1080

Graphics( C_Screen_Width, C_Screen_Depth, 32,60, GRAPHICS_BACKBUFFER | GRAPHICS_ALPHABUFFER )
SetVirtualResolution( C_Screen_Width, C_Screen_Depth )
SetBlend( ALPHABLEND )

'--------------------------------------------------------------------------------------------------------------

Global I_PA:TImage = CreateImage( 18*64, 12*64 ,1, DYNAMICIMAGE )



Global A_Map:Int[ 18 * 12 ]			' the entire map

	For Local y%=0 To 12-1
		For Local x%=0 To 18-1
		
			Local color% = $ff000000				' all black

			A_Map:Int[ x% + y%*18 ] = color%
		Next
	Next
	

	
	
'--------------------------------------------------------------------------------------------------------------
'--------------------------------------------------------START-------------------------------------------------
'--------------------------------------------------------------------------------------------------------------

	Global G_Pixmap:TPixmap = LockImage(I_PA)			'extract it's pixmap
	Global G_Pixels:Int Ptr = Int Ptr(G_Pixmap.pixels)	'access pixmap memory area.


	SetClsColor 255,0,255

Repeat

	Local time% = MilliSecs()
	Cls
	GCCollect() 
	
	For Local y%=0 To 12-1
		For Local x%=0 To 18-1
		
			Local color% = A_Map:Int[x% + y%*18]
			
			Local blu% = (color% Shr 16) & $ff
			Local gre% = (color% Shr  8) & $ff
			Local red% = (color% Shr  0) & $ff
			
			'blu% :+ 1
			'gre% :+ 1
			red% :+ 1
		
			A_Map:Int[x% + y%*18] = $ff000000 | (blu% Shl 16) | (gre% Shl 8) | red%
			
		Next
	Next

	
	For Local y%=0 To 12-1
		For Local x%=0 To 18-1
		
			Local color% = A_Map:Int[ x% + y%*18 ]
			

				For Local yy% = y%*64 To (y%+1)*64-1
					For Local xx% = x%*64 To (x%+1)*64-1
				
				
						G_Pixels[ xx% + yy%*18*64 ] = color%
						
					Next
				Next
			
		Next
	Next




	SetScale 1.0,1.0
	SetColor 255,255,255
	DrawImage( I_PA, (1920-18*64)/2, (1080-12*64)/2 )
	
	

	
	DrawText String(A_Map:Int[0] & $ff), 100, 140											' red component in blocks
	
	DrawText String(G_Pixels[ 100*100 ] & $ff), 100, 160									' red component in image
	
	Flip(True)

Until KeyDown(KEY_ESCAPE)

WaitKey()

Flip


WaitKey()




grable(Posted 2016) [#2]
The pixmap returned from LockImage is only valid until the next DrawImage. So you need to call LockImage inside the loop.


Derron(Posted 2016) [#3]
The pixmap returned from LockImage is only valid until the next DrawImage. So you need to call LockImage inside the loop.


Wouldn't that be a bit hmm bad performance wise? I mean, if BlitzMax creates a new pixmap each time it draws the texture?


Always assumed:
- pixmap is the raw data of the texture used when drawing
- timage is the "texture handling" object wrapper

But: if you adjust the pixmap, you need to reload it into the image (img = LoadImage(pix) or via alternative approaches)


@ JBR
To see if things work, replace your "DrawImage()" with "DrawPixmap()" - but keep in mind, that this is slower (as it uploads the new/modified texture each time you call DrawPixmap()).

Many effects could be "faked" by using overlays (your tinting gradient is drawn after the un-altered image).



bye
Ron


JBR(Posted 2016) [#4]
Thanks guys.

@grable - yes works but takes about 6ms

@Derron - yes works but takes about 3ms

Jim


grable(Posted 2016) [#5]

Wouldn't that be a bit hmm bad performance wise? I mean, if BlitzMax creates a new pixmap each time it draws the texture?
It doesnt create a new TPixmap, its about the locking+upload behavior.
When you call LockImage with write=True it prepares the frame for re-upload on draw.
The TPixmap and its raw memory stays the same (unless it has to process the image somehow, like non power of 2, wrong format etc)

Theres 3 ways to force an upload that i know of.
LockImage(image)
LoadImage(pixmap)
image.SetPixmap( frame, pixmap)

They all take 3ms for me (including the DrawImage), so this seems more limited by the PCIX bus than anything else.

- pixmap is the raw data of the texture used when drawing
- timage is the "texture handling" object wrapper
Nothing wrong with that assumption :)


@JBR
Yes, uploading takes time. There is no way around this.
Now, you can ditch Max2D and go straight GL and this would be a little faster. Especially if using partial updates or streaming apis. But it wouldnt be as convenient ;)


Derron(Posted 2016) [#6]
When you call LockImage with write=True it prepares the frame for re-upload on draw.


See...what you miss when not having autocomplete nor looking through all the sources ;-)


bye
Ron


Kryzon(Posted 2016) [#7]
I would try using StretchBlt from GDI to stretch the modified pixmap over the DC of the BlitzMax window.
It's an accelerated function.


TomToad(Posted 2016) [#8]
I find that the quickest way to make an image with components that change color is to create a mask with all changing pixels set to $FFFFFFFF and all non changing pixels to 0 alpha. then use setcolor to set the color to what you want it and draw the mask over the original image. Like in this example.



JBR(Posted 2016) [#9]
I have another problem - when I use createpixmap and drawpixmap I'm finding that I can't control the alpha.

I can fine with createimage & drawimage.

Jim


Derron(Posted 2016) [#10]
You need to create the pixmap with the right "format"

local pix:TPixmap = CreatePixmap(width,height, PF_RGBA8888)

Now you would have a pixmap, capable of storing RGBA values (A for alpha).
If you create a new pixmap, you have no warranty to get an "empty" one. So call "pix.ClearPixels(color)" before modifying it with your stuff.

"color" should be the background color of your choice (might have "alpha component = 0").


Saving a alpha-transparent PNG:
SuperStrict

Framework Brl.StandardIO
Import Brl.PngLoader


global pix:TPixMap = CreatePixmap(100,100, PF_RGBA8888)
pix.clearPixels(0)

For local x:int = 0 until 100
	For local y:int = 0 until 100
		'reading rgba from int would be:
		'Local r:Byte = col	
		'Local g:Byte = col Shr 8
		'Local b:Byte = col Shr 16
		'Local a:Byte = col Shr 24
		'storing rgba in int would be:
		'Int( r | g Shl 8 | b Shl 16 | a Shl 24 )
		local col:int = Int((y*2) Shl 24 | 255 Shl 16 | 255 Shl 8 | 0)
		pix.WritePixel(x, y, col)
	Next
Next

SavePixmapPNG(pix, "saveTransparentPNG.png")



bye
Ron


Kryzon(Posted 2016) [#11]
when I use createpixmap and drawpixmap I'm finding that I can't control the alpha.

Yeah, in those functions they change the blending mode or transfer pixels without using blending. Alpha transparency is not involved.

GL: https://github.com/blitz-research/blitzmax/blob/master/mod/brl.mod/glmax2d.mod/glmax2d.bmx#L487

D3D: https://github.com/blitz-research/blitzmax/blob/master/mod/brl.mod/d3d9max2d.mod/d3d9max2d.bmx#L563