Some Stars

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Some Stars

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by benny.

Hello,

maybe someone likes this oldschool-fx [ could also be found on the
resource site ]

Have a nice day ...

benny

Code: Select all

; some stars
; benny! 2oo2


#scrw = 640
#scrh = 480

#centerofscrw = #scrw/2
#centerofscrh = #scrh/2

#scrd = 16

SSum.w = 1000 ; Amount of Stars


Gosub InitStarField

;-------- Init all needed Stuff --------

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
  MessageRequester("Error", "Can't open DirectX 7 or later", #PB_MessageRequester_Ok)
  End
EndIf

If OpenScreen(#scrw,#scrh,#scrd,"benny's code", #PB_Screen_SmartSynchronization) = 0
  MessageRequester("Error", "Could not open screen", #PB_MessageRequester_Ok)
  End
EndIf

SetFrameRate(90)


;-------- MainLoop --------

Repeat
  
  ClearScreen(RGB(0,0,0))
  
  
  StartDrawing(ScreenOutput())
  
  DrawingMode(1)
  
  Gosub DoStarField
  
  StopDrawing()
  FlipBuffers()
  
  ExamineKeyboard()
  
Until KeyboardPushed(#PB_Key_Escape)

End


;--------- Procs / Subs -------------



; #### INITSTARFIELD ####

InitStarField:


; Structure of a star ...

Structure _3DStar
  
  x.f ; X-Coordinate
  y.f ; Y-Coordinate
  z.f ; Z-Coordinate
  zv.f ; Z-Velocity
  
EndStructure

; Init Starfield ...

Dim Stars._3DStar(SSum)

For dummy = 0 To SSum
  
  Stars(dummy)\x = Random(1000)-500
  Stars(dummy)\y = Random(1000)-500
  Stars(dummy)\z = 100 + Random(900)
  Stars(dummy)\zv = 0.5 + Random (45)/10
  
Next dummy


Return


; #### DoStarField ####

DoStarField:


For dummy = 0 To SSum
  
  Stars(dummy)\z = Stars(dummy)\z - Stars(dummy)\zv ; Star comes closer ....
  
  
  SX = Stars(dummy)\x / Stars(dummy)\z * 100 + #centerofscrw
  SY = Stars(dummy)\y / Stars(dummy)\z * 100 + #centerofscrh
  
  If SX >= #scrw Or SY >= #scrh Or Stars(dummy)\z < 1
    
    Stars(dummy)\x = Random(1000)-500
    Stars(dummy)\y = Random(1000)-500
    Stars(dummy)\z = 100 + Random(900)
    Stars(dummy)\zv = 0.5 + ( Random(45) / 10 )
    
  Else
    
    b = 255 - (Stars(dummy)\z *(255./1000.)) ; Calc the color of star
    
    Debug "sx: " + sx
    Debug "sy: " + sy
    
    If SX > 0 And SY > 0 And SX < #scrw And SY < #scrh
      Plot ( SX, SY, RGB ( b,b,b ) )
    EndIf
    
  EndIf
  
Next dummy

Return
Last edited by fsw on Wed Aug 21, 2013 9:59 pm, edited 1 time in total.
Reason: Code updated for 5.20+
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

[quote]Originally posted by benny

Code: Select all

    If SX  #scrw Or SY > #scrh Or Stars(dummy)\z = in those checks since the screen will range from 0...639 and 0...479, not 0...640 and 0...480.


-- 
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.30)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by benny.


You should probably check for >= in those checks since the screen will range from 0...639 and 0...479, not 0...640 and 0...480.




Hmm .. makes sense :)

Thanx, benny!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.

@ 1280*1024*32 , you're name is in the middle like the Enterprise :)
@ 1600*1200*32 , i need glasses to read it :)
Works nice in high res

Regards,

Berikco

http://www.benny.zeb.be
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by benny.

[quote]Originally posted by Berikco

@ 1280*1024*32 , you're name is in the middle like the Enterprise :)
@ 1600*1200*32 , i need glasses to read it :)
Works nice in high res

Regards,

Berikco



Argh :)

you're right ... did not think about it :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by benny.

Hi ..

just chatted on irc with AlphaSND and he optimized the following
lines :

FrontColor (b,b,b)
Plot ( SX, SY)

to simply :

Plot ( SX, SY, RGB (b,b,b) )

Now the Plot-Command is faster and you can play with even more stars!

Bye.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.
Originally posted by benny

Hi ..

just chatted on irc with AlphaSND and he optimized the following
lines :

FrontColor (b,b,b)
Plot ( SX, SY)

to simply :

Plot ( SX, SY, RGB (b,b,b) )

Now the Plot-Command is faster and you can play with even more stars!

Bye.
No modification possible...
On my computer I get an error message:

Plot() is outside the drawing area.

The original code works slow but fine.

Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

better fixe the code, it has really an error in it.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Fred, do you mean the Plot() command code :)

BTW: its Sunday morning in France... you should sleep

Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.


The 'SX' or 'SY' are negative sometime. Add a check about it.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by benny.

Ups
never realized that (never had that probs) !
Hmm .. maybe this could help :

[snippet on]

If SX #scrw Or SY > #scrh Or Stars(dummy)\z
Originally posted by fred


The 'SX' or 'SY' are negative sometime. Add a check about it.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by benny.

Hey ...

Here an updated version ...
Does it run better now ?

greetz ... benny!



; some stars
; benny! 2oo2


#scrw = 640
#scrh = 480

#centerofscrw = #scrw/2
#centerofscrh = #scrh/2

#scrd = 16

SSum.w = 1000 ; Amount of Stars


Gosub InitStarField

;-------- Init all needed Stuff --------

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
MessageBox_ (0,"Can't open DirectX 7 or later", "blahhhh", #MB_ICONINFORMATION|#MB_OK)
End
EndIf

If OpenScreen(#scrw,#scrh,#scrd,"benny's code") = 0
MessageBox_ (0,"Could not open screen", "blahhh blaa", #MB_ICONINFORMATION|#MB_OK)
End
EndIf

SetFrameRate(90)


;-------- MainLoop --------

Repeat

ClearScreen(0,0,0)


StartDrawing(ScreenOutput())

DrawingMode(1)

Gosub DoStarField

StopDrawing()
FlipBuffers()

ExamineKeyboard()

Until KeyboardPushed(#PB_Key_Escape)

End


;--------- Procs / Subs -------------



; #### INITSTARFIELD ####

InitStarField:


; Structure of a star ...

Structure 3DStar

x.f ; X-Coordinate
y.f ; Y-Coordinate
z.f ; Z-Coordinate
zv.f ; Z-Velocity

EndStructure

; Init Starfield ...

Dim Stars.3DStar(SSum)

For dummy = 0 To SSum

Stars(dummy)\x = Random(1000)-500
Stars(dummy)\y = Random(1000)-500
Stars(dummy)\z = 100 + Random(900)
Stars(dummy)\zv = 0.5 + Random (45)/10

Next dummy


Return


; #### DoStarField ####

DoStarField:


For dummy = 0 To SSum

Stars(dummy)\z = Stars(dummy)\z - Stars(dummy)\zv ; Star comes closer ....


SX = Stars(dummy)\x / Stars(dummy)\z * 100 + #centerofscrw
SY = Stars(dummy)\y / Stars(dummy)\z * 100 + #centerofscrh

If SX = #scrw Or SY >= #scrh Or Stars(dummy)\z < 1

Stars(dummy)\x = Random(1000)-500
Stars(dummy)\y = Random(1000)-500
Stars(dummy)\z = 100 + Random(900)
Stars(dummy)\zv = 0.5 + ( Random(45) / 10 )

Else

b = 255-(Stars(dummy)\z *(255./1000.)) ; Calc the color of star

Plot ( SX, SY, RGB ( b,b,b ) )

EndIf

Next dummy

Return
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TheBeck.

SSum.l = 1000000 ; Amount of Stars = 1 frame per 3 seconds - One million stars:)
SSum.l = 10000000 ; Amount of Stars = 1 frame per 30 seconds - :)
Post Reply