Multiple graphics windows

BlitzMax Forums/BlitzMax Programming/Multiple graphics windows

Hello all,

At first, I'm sorry if a similar topic already showed up, but I didn't find any so I hereby give it a try.

I've been hacking around a bit to create more than one graphics window. I do not use (or own, for that matter) the GUI-module.

This is my code:

SuperStrict

SetGraphicsDriver GLMax2DDriver() 'this doesn't work with DirectX-drivers

AppTitle = "window a"
Local a:TGraphics = CreateGraphics (320,240,0,0,GRAPHICS_BACKBUFFER)

AppTitle = "window b"
Local b:TGraphics = CreateGraphics (240,320,0,0,GRAPHICS_BACKBUFFER)

EnablePolledInput() 'Manually enable polled input

While Not AppTerminate() Or Not Confirm( "Terminate?" )

	SetGraphics(a)
	SetColor (Rand(0,255),Rand(0,255),Rand(0,255))
	Plot (Rand(0,319),Rand(0,239))
	Flip

	Print "a - x: " + MouseX() + ", y: " + MouseY()

	SetGraphics(b)
	SetColor (Rand(0,255),Rand(0,255),Rand(0,255))
	DrawLine (Rand(0,239),Rand(0,319),Rand(0,239),Rand(0,319))
	Flip
	
	Print "b - x: " + MouseX() + ", y: " + MouseY()
	
Wend


As you can see, it is possible to render graphics to more than one window, altough it crashes when using more than one DirectX-window (it should be possible even to use different drivers for each window, as SetGraphics would change the driver accordingly, but I haven't tried that).

However, it doesn't seem possible to differentiate the input generated by each window. MouseX() and MouseY() just depend on over which window the mouse is.

I tried looking up EnablePolledInput:

Rem
Currently only called by Graphics/bglCreateContext.
Private for now, as it really needs a source:object parameter.
End Rem
Function EnablePolledInput( source:Object=Null )
	If enabled Return
	inputSource=source
	FlushKeys
	FlushMouse
	AddHook EmitEventHook,Hook,Null,0
	enabled=True
End Function


It says it requires an object, yet the 'Graphics' function doesn't pass an object - in contrary to what the comment here says. I tried to 'EnablePolledInput(a)', hoping that it then only would return input from 'window a', but that doesn't work.

Does any of you have a clue how I can differentiate the input from the two windows?

Only by using event based programming. In this case you could use EventSource() (or currentevent.source

Thanks for your answer, bud sadly enough, it doesn't work out..

I removed the EnablePulledInput and the 'print mousex()' stuff and added the following:

Function Hook:Object(id:Int,data:Object,context:Object)

	Local ev:TEvent=TEvent(data)
	If Not ev Return data
	Print ev.id
	If ev.source Print "event has a source" Else Print "event didn't have a source"
	
	Return data	

End Function

AddHook EmitEventHook,Hook,Null,0

As it turns out, the events show up nicely but the events don't have a source. This is also why the 'EnablePolledInput(a)' doesn't work, see in polledinput.bmx:

Function Hook:Object( id,data:Object,context:Object )

	Local ev:TEvent=TEvent(data)
	If Not ev Return data
	
	If inputSource And inputSource<>ev.source Return data

	'more stuff
End Function


InputSource is set to 'source' object in the EnablePolledInput(source:Object) function, so in my example it'd be 'a:TGraphics', but since the EventSource is empty, the function quits.

I'm sorry if I'm not clear enough, I find these things hard to explain in Human.

Some other funny thing: if you close one window, the graphics meant for the closed window will be drawn on the remaining window. I guessed this is because the TGraphics object would be destroyed as the window closes and that setGraphics won't switch to a non-existing TGraphics object. This, however, is not true - the object still exists (even if both windows are closed).

Does anyone have a clue?