Multiple instances of a type

BlitzMax Forums/MaxGUI Module/Multiple instances of a type

rod54(Posted 2008) [#1]
I am trying to get a handle on this type thing ...

The MaxGui example files give the code below
I tried to add 2 additional instances ot TApplet
(applet1 and applet2)



Why does this not work... ?

Strict

Type TApplet
   
   Field	window:TGadget
   Field	canvas:TGadget
   Field	timer:TTimer
   
   Method Render()
   	Local x,y,width,height,t,i
   	width=GraphicsWidth()
   	height=GraphicsHeight()
   	t=MilliSecs()
   	SetBlend ALPHABLEND
   	SetViewport 0,0,width,height
   	SetClsColor 0,0,0
   	SetOrigin width/2,height/2
   	Cls
   	For x=-width/2 To width/2 Step 2
   		y=Sin(t*0.3+x)*height/2
   		DrawLine 0,0,x,y
   	Next
   End Method
   
   Method OnEvent(event:TEvent)
   	Select event.id
   	Case EVENT_WINDOWCLOSE
   		End
   	Case EVENT_TIMERTICK
   		RedrawGadget canvas
   	Case EVENT_GADGETPAINT
   		SetGraphics CanvasGraphics(canvas)
   		Render
   		Flip
   	End Select
   End Method

   Function eventhook:Object(id,data:Object,context:Object)
   	Local	event:TEvent
   	Local	app:TApplet
   	event=TEvent(data)
   	app=TApplet(context)
   	app.OnEvent event	
   End Function
   
   Method Create:TApplet(name$)
   	Local	a:TApplet
   	Local	w,h
   	window=CreateWindow(name,20,20,512,512)
   	w=ClientWidth(window)
   	h=ClientHeight(window)
   	canvas=CreateCanvas(0,0,w,h,window)
   	canvas.SetLayout 1,1,1,1
   	timer=CreateTimer(100)
   	AddHook EmitEventHook,eventhook,Self
   	Return Self		
   End Method
   
End Type

AutoMidHandle True

Local	applet:TApplet
Local	applet1:TApplet
Local	applet2:TApplet

applet=New TApplet.Create("Render Applet")
applet1=New TApplet.Create("Render1 Applet")
applet2=New TApplet.Create("Render2 Applet")


While True
   WaitEvent
Wend



SebHoll(Posted 2008) [#2]
It's because the first event hook isn't returning an object back, and so the second and third object's receive a null object in their event hooks. Have a look at the [i]Information[/b] section under RunHooks in the docs.

In addition, for multiple type instances, there is only really a need to create one timer which they can all respond to, and they should also only paint when their own canvas requires updating. Before, we had effectively 3 times (effectively running the same code 3 times over for each tick) and each canvas was being painted 3 times per tick. I've tweaked the code to fix these problems, and have posted it below:



P.S. You can use [code ] / [codebox ] tags when posting code so that it is properly formatted. I've edited your original post to show you what it looks like. See this FAQ entry for more info:

What are the forum codes?


rod54(Posted 2008) [#3]
Thanks for the help ..

Hope not to be such a newbie someday

Give me 5 years or so...