Black Hole effect using sprites / 5.20 beta required

Share your advanced PureBasic knowledge/code with the community.
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Black Hole effect using sprites / 5.20 beta required

Post 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
Last edited by BasicallyPure on Mon Jul 08, 2013 12:20 am, edited 5 times in total.
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: Black Hole effect using sprites / 5.20 beta required

Post by flaith »

:shock: :D Beautiful, thanks for sharing :)
“Fear is a reaction. Courage is a decision.” - WC
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Black Hole effect using sprites / 5.20 beta required

Post by davido »

Another delightful, instructive demo.

Thank you very much for sharing. :D
DE AA EB
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Black Hole effect using sprites / 5.20 beta required

Post by rsts »

Love it. :D
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Black Hole effect using sprites / 5.20 beta required

Post 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.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Black Hole effect using sprites / 5.20 beta required

Post 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 ?
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Black Hole effect using sprites / 5.20 beta required

Post 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
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Black Hole effect using sprites / 5.20 beta required

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Black Hole effect using sprites / 5.20 beta required

Post by BasicallyPure »

So what do we do now? Post a bug report? :shock:
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Black Hole effect using sprites / 5.20 beta required

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Black Hole effect using sprites / 5.20 beta required

Post 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.
Last edited by luis on Sun Jul 07, 2013 9:07 pm, edited 1 time in total.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Black Hole effect using sprites / 5.20 beta required

Post 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.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Black Hole effect using sprites / 5.20 beta required

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Black Hole effect using sprites / 5.20 beta required

Post 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)
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Black Hole effect using sprites / 5.20 beta required

Post by Kwai chang caine »

Splendid, mesmerizing !! :shock:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply