Page 1 of 3

Black Hole effect using sprites / 5.20 beta required

Posted: Sat Jul 06, 2013 5:18 am
by BasicallyPure
Ok, so I got "pulled into" this black hole thing by this topic by em_uk.

Here I use one of the new sprite commands 'ZoomSprite()', it works great!
This might be ok for a screen saver if I knew how to make one.

Code: Select all

; BlackHole - BasicallyPure
; 7.5.2013
; PureBasic 5.20 beta <--- YOU NEED THIS

EnableExplicit

#PIx2  = #PI * 2
#Stars = 250
#BlackHoleSpeed = 12 ; larger = slower

Declare Error(message$)

ExamineDesktops()
Define.i dw = DesktopWidth(0), xMid = dw / 2
Define.i dh = DesktopHeight(0), yMid = dh / 2
Define.i dd = DesktopDepth(0)
Define.i a, r, g, b, z, sprite, spriteColor, frameCount
Define.f angle, BH_angle = #PI
Define.i xRadius = dw / 3
Define.i yRadius = dh / 3

Structure starType
   x.f
   y.f
   speed.f
   nSprite.i
EndStructure

Define blackHole.Point
blackHole\x = xMid - xRadius
blackHole\y = yMid

Dim star.starType(#Stars)
For a = 0 To #Stars
   star(a)\x = Random(dw - 1)
   star(a)\y = Random(dh - 1)
   star(a)\nSprite = Random(13)
   star(a)\speed = star(a)\nSprite / 2.0 + 1
Next

If InitSprite() And InitKeyboard() And OpenScreen(dw, dh, dd, "Black Hole")
Else : Error("Failed To 'InitSprite/InitKeyboard/OpenScreen'")
EndIf

For sprite = 0 To 13
   r = Random(255) : b = Random(255) : g = 255 - (r + b) >> 1
   spriteColor = RGB(r, g, b)
   If CreateSprite(sprite, 16, 16) = 0 : Error("CreateSprite") : EndIf
   StartDrawing(SpriteOutput(sprite))
      Circle(7, 7, 3, spriteColor)
      LineXY(0, 7, 14, 7, spriteColor)
      LineXY(7, 0, 7, 14, spriteColor)
   StopDrawing()
   z = 6 + sprite * 2
   ZoomSprite(sprite, z, z) ; <-- does not belong inside start/stop drawing block!
Next sprite

Repeat ; animation loop
   
   ClearScreen(#Black)
   ;StartDrawing(ScreenOutput()) ; <--- don't need this!
      For a = 0 To #Stars
         With star(a)
            angle = ATan2(blackHole\x - \x, blackHole\y - \y)
            \x + \speed * Cos(angle)
            \y + \speed * Sin(angle)
            
            If Abs(\x - blackHole\x) < 5 And Abs(\y - blackHole\y) < 5
               Select Random(4, 1)
                  Case 1 : \x = Random(dw - 1) : \y = 0
                  Case 2 : \x = Random(dw - 1) : \y = dh - 1
                  Case 3 : \x = 0 : \y = Random(dh - 1)
                  Case 4 : \x = dw - 1 : \y = Random(dh - 1)
               EndSelect
            EndIf
            
            DisplayTransparentSprite(\nSprite,\x,\y)
         EndWith
      Next
      
      If frameCount < #BlackHoleSpeed : frameCount + 1
      Else : frameCount = 0
         BH_angle + Radian(1)
         If BH_angle > #PIx2 : BH_angle - #PIx2 : EndIf
         blackHole\x = Cos(BH_angle) * xRadius + xMid
         blackHole\y = Sin(BH_angle) * yRadius + yMid
      EndIf
      
   ;StopDrawing()
   FlipBuffers()
   ExamineKeyboard()
   
   If KeyboardPushed(#PB_Key_Escape) : End : EndIf
   
ForEver

Procedure Error(message$)
   MessageRequester("Fatal Error!", message$) : End
EndProcedure
BP

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sat Jul 06, 2013 6:46 am
by flaith
:shock: :D Beautiful, thanks for sharing :)

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sat Jul 06, 2013 9:44 am
by davido
Another delightful, instructive demo.

Thank you very much for sharing. :D

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sat Jul 06, 2013 11:38 am
by rsts
Love it. :D

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sun Jul 07, 2013 12:22 pm
by Olliv
5.20 ! Ow! I plug my computer on. Since many months I didn't use it and I'll test your code (and the beta by the same way). Thank you.

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sun Jul 07, 2013 12:54 pm
by luis
The demo above starts, I see some stars, the screen goes black then the stars come back, then my nVidia driver crashes and autorecover.

The debugger stops on line 61 (startdrawing) telling "The specified output is NULL (0 value)."

Tried with subsystem opengl, I got only a black screen.

On this same computer modern/heavy games work without problems obviously :)

BTW: the older subsystems (directx9, directx7) have been removed ?

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sun Jul 07, 2013 4:57 pm
by BasicallyPure
That is very puzzling luis.
I would suspect that the problem is somehow related to the sprites.
Maybe you could try some experiments like this.

Maybe your computer doesn't like zoomed sprites.
Remove or disable this line of code:

Code: Select all

ZoomSprite(sprite, z, z)
If it still crashes, replace this line of code:

Code: Select all

DisplayTransparentSprite(\nSprite, \x, \y)
with this:

Code: Select all

Circle(\x, \y, \nSprite, #White)
If it still crashes try this, remove or comment this block of code:

Code: Select all

For sprite = 0 To 13
   r = Random(255) : b = Random(255) : g = 255 - (r + b) >> 1
   spriteColor = RGB(r, g, b)
   If CreateSprite(sprite, 16, 16) = 0 : Error("CreateSprite") : EndIf
   StartDrawing(SpriteOutput(sprite))
      Circle(7, 7, 3, spriteColor)
      LineXY(0, 7, 14, 7, spriteColor)
      LineXY(7, 0, 7, 14, spriteColor)
      z = 6 + sprite * 2
      ZoomSprite(sprite, z, z)
   StopDrawing()
Next sprite
BP

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sun Jul 07, 2013 5:14 pm
by luis
BasicallyPure wrote: Maybe you could try some experiments like this.
Sure!
BasicallyPure wrote: Remove or disable this line of code:

Code: Select all

ZoomSprite(sprite, z, z)
The same.
BasicallyPure wrote: If it still crashes, replace this line of code:

Code: Select all

DisplayTransparentSprite(\nSprite, \x, \y)
with this:

Code: Select all

Circle(\x, \y, \nSprite, #White)
It works. Also with the zoomsprite re-enabled.

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sun Jul 07, 2013 5:22 pm
by BasicallyPure
So what do we do now? Post a bug report? :shock:

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sun Jul 07, 2013 5:37 pm
by luis
Probably yes, pointing at this thread. Problem is it's happening only with a certain hardware it seems.
Maybe it would be better to see if this happen to someone else ?

EDIT: wrote a bug report -> http://www.purebasic.fr/english/viewtop ... =4&t=55346

EDIT: maybe it's not a bug, see comment by DK_PETER in that thread.
luis wrote: BTW: the older subsystems (directx9, directx7) have been removed ?
PB 5.10 - Removed: DirectX7 and NT4 subsystem on Windows

What about Directx9 ? I tried but it doesn't seem available.

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sun Jul 07, 2013 9:00 pm
by luis
Checked the documentation, since I practically never used PB sprites commands I was a little in the dark.

DisplayTransparentSprite has nothing to do with the target specified by StartDrawing(), so it does not need it at all.
Just remove StartDrawing()/StopDrawing() from your code.

Interestingly only my computer behaved badly with it whilst the ones from the other users in this thread didn't.

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sun Jul 07, 2013 9:07 pm
by DK_PETER
Interestingly only my computer behaved badly it whilst the ones from the other users in this thread didn't.
No luis.

Just tried the code (as posted) and it totally crashes my pc.
(Have to do a reset. Everything freezes).

My configuration:
Windows 7 64bit
Gfx: Inno GTX 580 with latest nvidia drivers.
Mem: 16 gb.

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sun Jul 07, 2013 9:08 pm
by luis
... the ones from the other users in this thread didn't.
Until now :D
Thanks.
Uhmm... I wonder why my drivers went in autorecovery and yours froze instead.

@flaith, @davido, @rsts

What kind of graphic cards do you have ? Can be nice to know for future reference since yours swallowed the code above without blinking an eye.

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Sun Jul 07, 2013 11:16 pm
by BasicallyPure
Thank you luis and DK_PETER, for solving this mystery.
I have commented out the startDrawing() / StopDrawing() in the code and it still works.
That is surprising, I assumed DisplayTransparentSprite was required inside start/stop drawing.
I agree that it would be very good if the compiler would check for this condition.

BP

edit: my graphics card is ATI Radeon / msi R5450.
1GB DDR3
supports directX 11
purchased 10/31/2012

driver by ATI Technologies Inc.
driver date: 1/26/2011
driver version: 8.821.0.0

edit2: ZoomSprite() works outside start/stop drawing block too. (code updated)

Re: Black Hole effect using sprites / 5.20 beta required

Posted: Mon Jul 08, 2013 9:49 am
by Kwai chang caine
Splendid, mesmerizing !! :shock:
Thanks for sharing 8)