Party with Windows, Sprites & Threads

Just starting out? Need help? Post your questions and find answers here.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Party with Windows, Sprites & Threads

Post 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
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Party with Windows, Sprites & Threads

Post 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.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: Party with Windows, Sprites & Threads

Post 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.
Last edited by JHPJHP on Sat Oct 28, 2017 5:22 am, edited 1 time in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Party with Windows, Sprites & Threads

Post 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).
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Party with Windows, Sprites & Threads

Post 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.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Party with Windows, Sprites & Threads

Post 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.
Post Reply