ImageGadget, SetGadgetState and 24 bit...

Just starting out? Need help? Post your questions and find answers here.
User avatar
Michael Vogel
Addict
Addict
Posts: 2567
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

ImageGadget, SetGadgetState and 24 bit...

Post by Michael Vogel »

I'd like to update an imagegadget which results in flickering from time to time, so I changed my code to use 24 bit for doing this...

Init part:

Code: Select all

#FlickeringLights=0
CompilerIf #FlickeringLights
	CreateImage(#WaitIcon,#Wait_End,#WaitHeight,32,#PB_Image_Transparent)
CompilerElse
	CreateImage(#WaitIcon,#Wait_End,#WaitHeight,24,GetSysColor_(#COLOR_BTNFACE))
CompilerEndIf

StartDrawing(ImageOutput(#WaitIcon))
Box(0,0,#Wait_End,#WaitHeight,#DrawOpaque|#Red)
StopDrawing()
Image update:

Code: Select all

GrabImage(#WaitIcon,#WaitTemp,n*#WaitBox,0,#WaitBox,#WaitHeight)
SetGadgetState(#MakeStateProgress,ImageID(#WaitTemp))
But when using the 24 bits variant, the imagegadget is never updated (it looks empty :shock: ). The 32 bit version works perfect. I saved the grabbed image as a file, it looks fine. And the most irritating thing - when I change the setgadgetstate command by using the image id of #WaitIcon, the gadget gets the correct content...

..any ideas what I am doing wrong?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 3945
Joined: Sun Apr 12, 2009 6:27 am

Re: ImageGadget, SetGadgetState and 24 bit...

Post by RASHAD »

Why you are sure that 24 bit image will get rid of the flicker ?
Anyhow

Code: Select all

GrabImage(#WaitIcon,#WaitTemp,n*#WaitBox,0,#WaitBox, #WaitHeight)
*buffer = EncodeImage(#WaitTempm,#PB_ImagePlugin_BMP,0,24)
CatchImage(#WaitTemp2,*buffer)
SetGadgetState(#MakeStateProgress,ImageID(#WaitTemp2))

;*********************************************************
GrabImage(#WaitIcon,#WaitTemp,n*#WaitBox,0,#WaitBox, #WaitHeight)
imgID = CopyImage_(ImageID(#WaitTemp),#IMAGE_BITMAP	,#WaitBox,#WaitHeight,#LR_COPYDELETEORG	)
SetGadgetState(#MakeStateProgress,imgID)
Egypt my love
User avatar
chi
Enthusiast
Enthusiast
Posts: 799
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: ImageGadget, SetGadgetState and 24 bit...

Post by chi »

Why don't you use the CanvadGadget instead?
CanvasOutput() wrote:Drawing on a CanvasGadget() is double-buffered. This means that the drawing operations only become visible at the StopDrawing() command to avoid visible flicker during the drawing.
Et cetera is my worst enemy
User avatar
Michael Vogel
Addict
Addict
Posts: 2567
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: ImageGadget, SetGadgetState and 24 bit...

Post by Michael Vogel »

Thanks, good points...

Rashad, tried both variants, the first hasn't worked here (or I did something wrong), but the second method works excellent.

I am still confused about what's going on in two points:
1. why I need to polish the #WaitTemp image after grabbing? The color depth is already 24 bit, so what is missing?
2. what about memory management? I am updating the imagegadget thousand of times, how can I free the generated images?

PS: I avoided CanvasGadget because I tried to preserve the background of the dialog and draw an overlaying image - but meantime I also think about grabbing the background content at the beginning which saves me to do overlays on the fly...

Here's a small example 'bout the actual trends... :lol:

Code: Select all


#AntiFlickering=0

Procedure Init()

	Enumeration
		#Win
		#MakeStateProgress
		#WaitIcon
		#WaitTemp
	EndEnumeration

	#WaitSize=	21
	#WaitSpace=3
	#WaitBox=	#WaitSize+#WaitSpace*2
	#WaitHeight=#WaitSize
	#WaitWidth=	#WaitSize/3
	#WaitLineX=	#WaitSize
	#WaitLineY=	#WaitSize-3
	#WaitLoop=	(#WaitLineX+#WaitLineY-#WaitWidth*2)*4
	#WaitColor=	#Blue
	#Wait_End=		#WaitLoop*#WaitBox

	OpenWindow(#Win,0,0,320,100,"Image Refresh",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
	CompilerIf #AntiFlickering<2
		ImageGadget(#MakeStateProgress,148,40,#WaitBox,#WaitHeight,0)
	CompilerElse
		CanvasGadget(#MakeStateProgress,148,40,#WaitBox,#WaitHeight)
	CompilerEndIf
	AddWindowTimer(#Win,0,5)


	CompilerIf #AntiFlickering
		CreateImage(#WaitIcon,#Wait_End,#WaitHeight,24,GetSysColor_(#COLOR_BTNFACE))
	CompilerElse
		CreateImage(#WaitIcon,#Wait_End,#WaitHeight,32,#PB_Image_Transparent)
	CompilerEndIf

	StartDrawing(ImageOutput(#WaitIcon))

	For i=0 To #WaitLoop-1
		o=#WaitLineY*4+#WaitLineX*2-#WaitWidth*6
		If i>=o
			n=#WaitLineX-#WaitWidth
			direction=3
		Else
			o=#WaitLineY*2+#WaitLineX*2-#WaitWidth*4
			If i>=o
				n=#WaitLineY-#WaitWidth
				direction=2
			Else
				o=#WaitLineY*2-#WaitWidth*2
				If i>=o
					n=#WaitLineX-#WaitWidth
					direction=1
				Else
					o=0
					n=#WaitLineY-#WaitWidth
					direction=0
				EndIf
			EndIf
		EndIf

		mode=i-o
		m=mode/n*n
		n+#WaitWidth
		phase=mode-m

		pos=Bool(m)*phase
		If m
			len=n-phase
		Else
			len=#WaitWidth+phase
		EndIf

		w=#WaitWidth
		h=len
		If direction&1
			Swap w,h
		EndIf

		Select direction
		Case 0
			x=0 : y=n-pos-len
		Case 1
			x=pos : y=0
		Case 2
			x=#WaitLineX-#WaitWidth : y=pos
		Case 3
			x=n-pos-len : y=#WaitLineY-#WaitWidth
		EndSelect

		DrawingMode(#PB_2DDrawing_AllChannels|#PB_2DDrawing_Outlined)
		Box(i*#WaitBox,0,#WaitBox,#WaitHeight,$FFFFFFFF)

		x+i*#WaitBox+#WaitSpace
		y+#WaitSpace-1
		DrawingMode(#PB_2DDrawing_AllChannels)
		Box(x,y,w,h,$FF000000)
		Box(x+1,y+1,w-2,h-2,$FFff7060)
	Next i

	StopDrawing()

EndProcedure
Procedure Update(n)

	GrabImage(#WaitIcon,#WaitTemp,(n%#WaitLoop)*#WaitBox,0,#WaitBox,#WaitHeight)

	CompilerSelect #AntiFlickering
	CompilerCase 0;			32-Bit Image
		If GetKeyState_(#VK_SHIFT)&128
			n=GadgetID(#MakeStateProgress)
			GetClientRect_(n,r)
			ValidateRect_(n,r)
		EndIf
		SetGadgetState(#MakeStateProgress,ImageID(#WaitTemp))

	CompilerCase 1;			24-Bit Image
		Protected Image
		Image=CopyImage_(ImageID(#WaitTemp),#IMAGE_BITMAP,#WaitBox,#WaitHeight,#LR_COPYDELETEORG)
		SetGadgetState(#MakeStateProgress,Image)

	CompilerCase 2;			24-Bit Image & CanvasGadget
		StartDrawing(CanvasOutput(#MakeStateProgress))
		DrawImage(ImageID(#WaitTemp),0,0)
		StopDrawing()

	CompilerEndSelect

EndProcedure

Init()

While #True
	Select WaitWindowEvent()
	Case #PB_Event_CloseWindow
		End
	Case #PB_Event_Timer
		Update(n)
		n+1
	EndSelect
Wend
Post Reply