Canvas Refresh when resizing a window

BlitzMax Forums/MaxGUI Module/Canvas Refresh when resizing a window

alain(Posted 2007) [#1]
Hi there,

I have the following program:
SuperStrict

Local MyWindow:TGadget=CreateWindow("Canvas Example", 200,200,320,240)
Local MyCanvas:TGadget=CreateCanvas(0,0,ClientWidth(MyWindow),ClientHeight(MyWindow),MyWindow)
SetGadgetLayout MyCanvas,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED
Repeat
  WaitEvent()
  Select EventID()
  Case EVENT_WINDOWCLOSE
     End
  Case EVENT_GADGETPAINT
    SetGraphics CanvasGraphics (MyCanvas)
	Cls
    DrawRect  20,20,50,80
    Flip
   End Select
Forever


Problem is: when I resize I have garbage on the bottom and on the right of the canvas... I think the real canvas zone is not resized but I am not able to tweak this program to make it work....

Can somebody help me?

thanks in advance.


Grisu(Posted 2007) [#2]
Try this example:

SuperStrict

Local window:TGadget = CreateWindow("",200,100,400,400)

Local canvas:TGadget = CreateCanvas(0, 0, ClientWidth(window), ClientHeight(window), window)
SetGadgetLayout canvas, EDGE_CENTERED, EDGE_ALIGNED, EDGE_CENTERED, EDGE_ALIGNED

SetGraphics CanvasGraphics(canvas)

Local rectsize:Int = 33

Repeat

	PollEvent()
	Select EventID()
		Case EVENT_WINDOWCLOSE
			End
		Case EVENT_WINDOWSIZE
			SetGadgetShape(canvas, 0, 0, ClientWidth(window), ClientHeight(window))
			SetViewport 0, 0, ClientWidth(window), ClientHeight(window)
			RedrawGadget(canvas)
		Case EVENT_GADGETPAINT
			SetGraphics CanvasGraphics(canvas)
			Cls
	EndSelect
	
	SetColor Rnd(255), 0, 0
	DrawRect Rand(GadgetWidth(canvas) - rectsize), Rand(GadgetHeight(canvas) - rectsize), rectsize ,rectsize
	
	Flip
	
Forever




TaskMaster(Posted 2007) [#3]
I found that I needed to delete a canvas and create a new larger one when the window size was increased.

Is the SetViewPort command the one that solves the problem?


Dreamora(Posted 2007) [#4]
Yes, it will set the drawing area to the correct one.
Problem with resizing is that you alter the viewable area, but not the area that the graphic context will actually draw onto :)
thats what setviewport is for.


alain(Posted 2007) [#5]
thanks for the tip.


JoshK(Posted 2007) [#6]
Use SetGraphics CanvasGraphics(canvas) before each render.


TaskMaster(Posted 2007) [#7]
Sorry to bring back an old topic, but I was just trying this, and the method Grisu posted has a nasty side effect of changing the origin, so that it is no longer in the top left corner.

I draw text at 0,0 then as I resize the window bigger, the text moves down the window, even though it is suppose to be drawn at 0,0/.

My solution was to do this:

FreeGadget(Canvas) 
Canvas=CreateCanvas(0,0,ClientWidth(Window),ClientHeight(Window),Window)
SetGadgetLayout (Canvas, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED) 
SetGraphics(CanvasGraphics(Canvas)) 


This leaves the origin in the upper left where it belongs.

Is there something I am missing with the other method?

Also, why would you need to call "SetGraphics CanvasGraphics(canvas)" before each render?