Page 1 of 1

Party with Windows, Sprites & Threads

Posted: Thu Oct 26, 2017 11:16 pm
by Lunasole
Any ideas hot to resolve THIS? ^^
I'm already tired of playfoolness with it.

Run following simplified example with /opengl subsystem + threaded runtime (or without both of them, doesn't matters):

Code: Select all

EnableExplicit


; GENERIC PB RULE: ALL THE SCREEN/SPRITE OPERATIONS MUST BE DONE IN A SAME THREAD (where screen opened)
; 					While CloseScreen() must be called from main thread (where window events processed), wtf. Else thread locks on it
Procedure Thread(*param.integer)
	While Not IsWindow(0)
		Delay(1)
	Wend
	
	OpenWindowedScreen(WindowID(0), 0, 0, 200, 200, #False, 0, 0, #PB_Screen_NoSynchronization)	
	Define Sprite = CreateSprite(#PB_Any, 20, 20)
	
	Repeat
		If Random(2, 0) = 0 And StartDrawing(SpriteOutput(Sprite))
			Box(0, 0, 20, 20, Random(255, 10)<<16 + Random(255,10))
			StopDrawing()
		EndIf
		
		ClearScreen(1)
		DisplaySprite(Sprite, Random(180, 0), Random(180, 0))
		FlipBuffers()

		Delay(16)
	Until *param\i
	
	; CloseScreen()
EndProcedure

; init
InitSprite()
OpenWindow(0, 100, 100, 200, 200, "", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)

Define State	; thread reads this and quits if non-zero
Define Thread = CreateThread(@Thread(), @State) 

; The problem is here. 
; Main loop locks up when you try to move window using mouse (for example), or receives other events
; Putting WaitWindowEvent under mutex doesn't help
Repeat
	Define E = WaitWindowEvent(1)
	If E = #PB_Event_CloseWindow
		State = #True
		WaitThread(Thread)
		CloseScreen()
		End
	EndIf
ForEver

Re: Party with Windows, Sprites & Threads

Posted: Thu Oct 26, 2017 11:22 pm
by Lunasole
PS. The aim is to make screen drawing separated from events handling (run in different thread).

Because if run it in single thread window events processing causes lags + completely stops loop in some cases.
Using custom WindowCallback (running draw code from it) partially resolves that, but also lags.

Re: Party with Windows, Sprites & Threads

Posted: Fri Oct 27, 2017 2:37 am
by JHPJHP
Hi Lunasole,

I read what you wrote about Sprite operations in the same thread, but the only thing that worked was moving OpenWindowedScreen back to the main thread.

Code removed; did not work in OSX.

Re: Party with Windows, Sprites & Threads

Posted: Fri Oct 27, 2017 1:17 pm
by Lunasole
JHPJHP wrote:Hi Lunasole,

I read what you wrote about Sprite operations in the same thread, but the only thing that worked was moving OpenWindowedScreen back to the main thread.

I tested your solution in my own games, and it worked so good that I ended up keeping the changes. Thank you :!:
Nice if it helped you ^^

I'm still confused of it all, as there are different results if using OpenGL/DX9 or DX11 stuff.
Really stable it works (your code) with DirectX 11, are your games using it?

Also using any DirectX I'm not sure about those "in the same thread" (as the screen opened in main thread and all is done in auxiliary, which doesn't work with OpenGL).

Re: Party with Windows, Sprites & Threads

Posted: Fri Oct 27, 2017 2:12 pm
by Kukulkan
As far as I know, all Window or Gadget functions inside a thread are a recipe for trouble. On Windows it might work, on Linux and Mac it crashes directly. Not sure if it will work on Windows somehow, but I would not bet on it. The only way I know is to either do everything inside of the thread or the main function. Do not mix anything else.

Re: Party with Windows, Sprites & Threads

Posted: Sat Oct 28, 2017 6:37 pm
by walbus
All "tricky" things that work with windows, you can mostly forget for Mac and Linux
Even if it works now, it won't work with the next PB version.