Draw a Galaxy

Share your advanced PureBasic knowledge/code with the community.
User avatar
thyphoon
Enthusiast
Enthusiast
Posts: 356
Joined: Sat Dec 25, 2004 2:37 pm

Draw a Galaxy

Post by thyphoon »

Hello

(Always excuse me for my bad english :oops: )
A little code to Draw a Galaxy ! It's a Team work from the french forum
Image
You must to download this 3 images
Image
Image
Image

If you have some idea to render better or optimize the code share your idea !

and use this code =>

Code: Select all

UsePNGImageDecoder()
#dobro=1
#Police=1
#sprite=1
; ***********************************
InitSprite()
InitSprite3D()
InitKeyboard()
FontID = LoadFont(#Police, "arial", 50, #PB_Font_Bold )
EcranX = GetSystemMetrics_(#SM_CXSCREEN):;=largeur de l'ecran
EcranY = GetSystemMetrics_(#SM_CYSCREEN):;=hauteur de l'ecran
    WindowID = OpenWindow(1, 0, 0, EcranX, EcranY, "hello", #PB_Window_SystemMenu|#PB_Window_BorderLess |#PB_Window_ScreenCentered )
    WindowID = WindowID(1)
    Result = OpenWindowedScreen(WindowID,0,0, EcranX, EcranY, 1, 0,0)
   
   Structure Prefs
     ScreenWidth.l
     ScreenHeight.l
   EndStructure
   Global Prefs.prefs
   Prefs\ScreenWidth=EcranX
   Prefs\Screenheight=EcranY
      
   CompilerIf Subsystem("Directx9") 
    Structure D3DTLVERTEX
     x.f
     y.f
     z.f
     rhw.f
     Color.l
     tu.f
     tv.f
   EndStructure

   Structure PB_Sprite3D
     TexRes.l                    ; TexRes
     Vertice.D3DTLVERTEX[4]       ; The 4 vertices for the rectangle sprite
     TmpVertice.D3DTLVERTEX[4]   ; The 4 vertices for the rectangle sprite
     Width.l                     ; width set with ZoomSprite3D()
     Height.l                     ; height set with ZoomSprite3D()
     RealWidth.l
     RealHeight.l
     Angle.f
     Transformed.l
   EndStructure 
   
   
     CompilerElse ;Directx7
     
    ;Thanks Cpl.Bator !!!
    Structure vertex
          sx.f
          sy.f
          sz.f
          rhw.f
          Color.l
          specular.l
          tu.f
          tv.f
    EndStructure

    Structure PB_Sprite3D
             Texture.l         
             Vertice.vertex[4]
             Width.w
             Height.w
    EndStructure 
CompilerEndIf

    Procedure SetSpriteLuminosityEx(spriteid.l,lum.c)
      Protected RGB_LUM.l,*S.PB_Sprite3D
      ;doit être dans un Start3D
      ; 255 = Pleine luminosité 0 = noir
      
      RGB_LUM = RGBA(255,Lum,Lum,255)
      *S=IsSprite3D(spriteid)
      *S\Vertice[0]\Color = RGB_LUM
      *S\Vertice[1]\Color = RGB_LUM
      *S\Vertice[3]\Color = RGB_LUM
      *S\Vertice[2]\Color = RGB_LUM
    EndProcedure

    LoadSprite(20,"Gfx/holehand.png",#PB_Sprite_Texture|#PB_Sprite_AlphaBlending)
    CreateSprite3D(20,20)
    LoadSprite(21,"Gfx/sun.png",#PB_Sprite_Texture|#PB_Sprite_AlphaBlending)
    CreateSprite3D(21,21)
     LoadSprite(22,"Gfx/star.png",#PB_Sprite_Texture|#PB_Sprite_AlphaBlending)
    CreateSprite3D(22,22)
Structure MyHole
  Rayon.l
  StarAngle.l
EndStructure

Global NewList MyHole.MyHole()

NbStar=40
For z=NbStar To 1 Step -1
  AddElement(MyHole())
  MyHole()\Rayon=Pow(z,1.3)*(Prefs\ScreenWidth/4)/(NbStar*2)
  MyHole()\StarAngle.l=Random(360)
Next


Procedure Spiral() 
  Degres=-15
  Coef=7

  t.f=ElapsedMilliseconds()/40000
  CordX=Prefs\ScreenWidth/2
  CordY=Prefs\ScreenHeight/2
  AngleNova.f=degres*#PI/180
  
  BA.f=#PI
  BB.f=#PI/2
  BC.f=#PI+#PI/2

  ForEach MyHole()
    AngleStar.f=t+ListIndex(MyHole())/Coef
    x=MyHole()\Rayon*Cos(AngleStar)
    y=MyHole()\Rayon/2*Sin(AngleStar)
    x2=MyHole()\Rayon*Cos(BA+AngleStar)
    y2=MyHole()\Rayon/2*Sin(BA+AngleStar)
    x3=MyHole()\Rayon*Cos(BB+AngleStar)
    y3=MyHole()\Rayon/2*Sin(BB+AngleStar)
    x4=MyHole()\Rayon*Cos(BC+AngleStar)
    y4=MyHole()\Rayon/2*Sin(BC+AngleStar)
  
  ; ********* voici la formule de la rotation d'image *********
    ;<------------ en degres !!! :o)
    
     x= x * Cos (AngleNova) +( Y)* Sin (AngleNova)
     Y=x * Sin (AngleNova) + (Y)* Cos (AngleNova) 
               
     x2= x2 * Cos (AngleNova) +( y2)* Sin (AngleNova)
     y2= x2 * Sin (AngleNova) + (y2)* Cos (AngleNova) 
                 
     x3= x3 * Cos (AngleNova) +( y3)* Sin (AngleNova)
     y3= x3 * Sin (AngleNova) + (y3)* Cos (AngleNova) 
                 
     x4= x4 * Cos (AngleNova) +( Y4)* Sin (AngleNova)
     Y4= x4 * Sin (AngleNova) + (Y4)* Cos (AngleNova) 
                 
     ;***************************************************** 
  
    size=(ListSize(MyHole())-ListIndex(MyHole()))*128/ListSize(MyHole())
    
    Angle=(ListSize(MyHole())-ListIndex(MyHole()))*360/ListSize(MyHole())
    Alpha=255-((ListSize(MyHole())-ListIndex(MyHole()))*255/ListSize(MyHole()))
    ZoomSprite3D(20,size,size)
    ZoomSprite3D(22,size,size)
    RotateSprite3D(22,MyHole()\StarAngle,#PB_Absolute)
    RotateSprite3D(20,Angle,#PB_Absolute)
    SetSpriteLuminosityEx(20,Alpha)
    DisplaySprite3D(20,CordX+x-Size/2,CordY+y-Size/2,Alpha)
    DisplaySprite3D(22,CordX+x-Size/2,CordY+y-Size/2,255)
    DisplaySprite3D(20,CordX+x2-Size/2,CordY+y2-Size/2,Alpha)
    DisplaySprite3D(22,CordX+x2-Size/2,CordY+y2-Size/2,255)
    DisplaySprite3D(20,CordX+x3-Size/2,CordY+y3-Size/2,Alpha)
    DisplaySprite3D(22,CordX+x3-Size/2,CordY+y3-Size/2,255)
    DisplaySprite3D(20,CordX+x4-Size/2,CordY+y4-Size/2,Alpha)
    DisplaySprite3D(22,CordX+x4-Size/2,CordY+y4-Size/2,255)
  Next
  ZoomSprite3D(21,64,64)
  DisplaySprite3D(21,CordX-32,CordY-32,200)
  
EndProcedure

    Repeat
        ExamineKeyboard()
        Event=WindowEvent()
        Delay(1)
        ClearScreen(RGB(0,0,0)) ;  *****<------- si tu Rem ceci le sprite laisse une trace :D
        Start3D()
        Spiral()
        Stop3D()
        FlipBuffers():; affiche l'ecran 
     

        Event=WindowEvent()
         If KeyboardPushed(#PB_Key_Escape)
            End
        EndIf
        
                
    Until Event=#PB_Event_CloseWindow
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Draw a Galaxy

Post by PB »

Not bad. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
thyphoon
Enthusiast
Enthusiast
Posts: 356
Joined: Sat Dec 25, 2004 2:37 pm

Re: Draw a Galaxy

Post by thyphoon »

PB wrote:Not bad. :)
thanks

if you want to compile with older PB version than 4.40b5 add this code

Code: Select all

Macro RGBA(r,g,b,a)
 a << 24 + b << 16 +g << 8 + r
EndMacro
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Re: Draw a Galaxy

Post by GeoTrail »

It runs, but looks really weird on my machine. Running Vista Enterprise using BootCamp on a Mac.
Image
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
User avatar
thyphoon
Enthusiast
Enthusiast
Posts: 356
Joined: Sat Dec 25, 2004 2:37 pm

Re: Draw a Galaxy

Post by thyphoon »

GeoTrail wrote:It runs, but looks really weird on my machine. Running Vista Enterprise using BootCamp on a Mac.
Image
Mmmm if you use < PB 4.40B7 you must add DirectX9 in Subsystem ! but il you use PB 4.40B7 you must change some stuff.. I think post the last version most optimised for PB 4.40B7
Do you try with PB Mac ?
User avatar
thyphoon
Enthusiast
Enthusiast
Posts: 356
Joined: Sat Dec 25, 2004 2:37 pm

Re: Draw a Galaxy

Post by thyphoon »

New optimized version for PB4.40Beta7 or >
The 2 images you need
Image1 (sun.png):Image
Image2 (fullhand.png):Image

Code: Select all

UsePNGImageDecoder()
#dobro=1
#Police=1
#sprite=1
; ***********************************
InitSprite()
InitSprite3D()
InitKeyboard()
FontID = LoadFont(#Police, "arial", 50, #PB_Font_Bold )
EcranX = 800
EcranY = 600
    WindowID = OpenWindow(1, 0, 0, EcranX, EcranY, "hello", #PB_Window_SystemMenu|#PB_Window_BorderLess |#PB_Window_ScreenCentered )
    WindowID = WindowID(1)
    Result = OpenWindowedScreen(WindowID,0,0, EcranX, EcranY, 1, 0,0)
   
   Structure Prefs
     ScreenWidth.l
     ScreenHeight.l
   EndStructure
   Global Prefs.prefs
   Prefs\ScreenWidth=EcranX
   Prefs\Screenheight=EcranY
     
CompilerIf Subsystem("Directx7") 
 ;Thanks Cpl.Bator !!!
    Structure vertex
          sx.f
          sy.f
          sz.f
          rhw.f
          Color.l
          specular.l
          tu.f
          tv.f
    EndStructure

    Structure PB_Sprite3D
             Texture.l         
             Vertice.vertex[4]
             Width.w
             Height.w
    EndStructure 
   
   
     CompilerElse ;Directx7
         Structure D3DTLVERTEX
     x.f
     y.f
     z.f
     rhw.f
     Color.l
     tu.f
     tv.f
   EndStructure

   Structure PB_Sprite3D
     TexRes.l                    ; TexRes
     Vertice.D3DTLVERTEX[4]       ; The 4 vertices for the rectangle sprite
     TmpVertice.D3DTLVERTEX[4]   ; The 4 vertices for the rectangle sprite
     Width.l                     ; width set with ZoomSprite3D()
     Height.l                     ; height set with ZoomSprite3D()
     RealWidth.l
     RealHeight.l
     Angle.f
     Transformed.l
   EndStructure 
   
CompilerEndIf

    Procedure SetSpriteLuminosityEx(spriteid.l,lum.c)
      Protected RGB_LUM.l,*S.PB_Sprite3D
      ;doit être dans un Start3D
      ; 255 = Pleine luminosité 0 = noir
      
      RGB_LUM = RGBA(255,Lum,Lum,Lum);RGBA(255,Lum,255-(Lum/2),Lum)
      *S=IsSprite3D(spriteid)
      *S\Vertice[0]\Color = RGB_LUM
      *S\Vertice[1]\Color = RGB_LUM
      *S\Vertice[3]\Color = RGB_LUM
      *S\Vertice[2]\Color = RGB_LUM
    EndProcedure

#Spr_HoleHand=0
#Spr_HoleSun=1

    LoadSprite(#Spr_HoleHand,"Gfx/fullhand.png",#PB_Sprite_Texture|#PB_Sprite_AlphaBlending)
    CreateSprite3D(#Spr_HoleHand,#Spr_HoleHand)
    LoadSprite(#Spr_HoleSun,"Gfx/sun.png",#PB_Sprite_Texture|#PB_Sprite_AlphaBlending)
    CreateSprite3D(#Spr_HoleSun,#Spr_HoleSun)

Structure MyHole
  Rayon.l
  StarAngle.l
  Size.l
  Angle.l
  Alpha.l
EndStructure

Global NewList MyHole.MyHole()

NbStar=38 ;Normalement 40
For z=NbStar To 1 Step -1
  AddElement(MyHole())
  MyHole()\Rayon=Pow(z,1.3)*(Prefs\ScreenWidth/4)/(NbStar*2)
  MyHole()\StarAngle.l=Random(360)
  MyHole()\Size=(NbStar-ListIndex(MyHole()))*128/NbStar
  MyHole()\Angle=(NbStar-ListIndex(MyHole()))*360/NbStar
  MyHole()\Alpha=255-((NbStar-ListIndex(MyHole()))*255/NbStar)
Next

Structure MyHolePrefs
  CordX.l
  CordY.l
  AngleNova.f
  CosAngleNova.f
  SinAngleNova.f
  Coef.l
EndStructure
Global MyHolePrefs.MyHolePrefs

MyHolePrefs\Coef=7
MyHolePrefs\CordX=Prefs\ScreenWidth/2
MyHolePrefs\CordY=Prefs\ScreenHeight/2
;Quelques Précalcule
MyHolePrefs\AngleNova=-15*#PI/180 ;POur un Angle de 15°
MyHolePrefs\CosAngleNova=Cos (MyHolePrefs\AngleNova)
MyHolePrefs\SinAngleNova=Sin (MyHolePrefs\AngleNova)
#BA=#PI
#BB=#PI/2
#BC=#PI+#PI/2

Procedure Spiral() 
  t.f=ElapsedMilliseconds()/10000
  Sprite3DBlendingMode(5, 7)
  ForEach MyHole()
    AngleStar.f=t+ListIndex(MyHole())/MyHolePrefs\Coef
    Halfrayon=MyHole()\Rayon/2
    x=MyHole()\Rayon*Cos(AngleStar)
    y=Halfrayon*Sin(AngleStar)
    x2=MyHole()\Rayon*Cos(#BA+AngleStar)
    y2=Halfrayon*Sin(#BA+AngleStar)
    x3=MyHole()\Rayon*Cos(#BB+AngleStar)
    y3=Halfrayon*Sin(#BB+AngleStar)
    x4=MyHole()\Rayon*Cos(#BC+AngleStar)
    y4=Halfrayon*Sin(#BC+AngleStar)
 
  ; ********* voici la formule de la rotation d'image *********
    ;<------------ en degres !!! :o)
   
     x= x * MyHolePrefs\CosAngleNova +( Y)* MyHolePrefs\SinAngleNova
     y=x * MyHolePrefs\SinAngleNova + (Y)* MyHolePrefs\CosAngleNova
               
     x2= x2 * MyHolePrefs\CosAngleNova +( y2)* MyHolePrefs\SinAngleNova
     y2= x2 * MyHolePrefs\SinAngleNova + (y2)* MyHolePrefs\CosAngleNova
                 
     x3= x3 * MyHolePrefs\CosAngleNova +( y3)* MyHolePrefs\SinAngleNova
     y3= x3 * MyHolePrefs\SinAngleNova + (y3)* MyHolePrefs\CosAngleNova
                 
     x4= x4 * MyHolePrefs\CosAngleNova +( Y4)* MyHolePrefs\SinAngleNova
     Y4= x4 * MyHolePrefs\SinAngleNova + (Y4)* MyHolePrefs\CosAngleNova 
                 
     ;***************************************************** 
    ZoomSprite3D(#Spr_HoleHand,MyHole()\Size,MyHole()\Size)
    RotateSprite3D(#Spr_HoleHand,MyHole()\StarAngle,#PB_Absolute)

    SetSpriteLuminosityEx(#Spr_HoleHand,MyHole()\Alpha)
    NewSize=MyHole()\Size/2
      NewCordX=MyHolePrefs\CordX+x:NewCordY=MyHolePrefs\CordY+y
    DisplaySprite3D(#Spr_HoleHand,NewCordX-NewSize,NewCordY-NewSize,MyHole()\Alpha)
      NewCordX=MyHolePrefs\CordX+x2:NewCordY=MyHolePrefs\CordY+y2
    DisplaySprite3D(#Spr_HoleHand,NewCordX-NewSize,NewCordY-NewSize,MyHole()\Alpha)

        NewCordX=MyHolePrefs\CordX+x2:NewCordY=MyHolePrefs\CordY+y2
    DisplaySprite3D(#Spr_HoleHand,NewCordX-NewSize,NewCordY-NewSize,MyHole()\Alpha)

        NewCordX=MyHolePrefs\CordX+x3:NewCordY=MyHolePrefs\CordY+y3
    DisplaySprite3D(#Spr_HoleHand,NewCordX-NewSize,NewCordY-NewSize,MyHole()\Alpha)

        NewCordX=MyHolePrefs\CordX+x4:NewCordY=MyHolePrefs\CordY+y4
    DisplaySprite3D(#Spr_HoleHand,NewCordX-NewSize,NewCordY-NewSize,MyHole()\Alpha)

  Next
  ZoomSprite3D(#Spr_HoleSun,32,32)
  DisplaySprite3D(#Spr_HoleSun,MyHolePrefs\CordX-16,MyHolePrefs\CordY-16,200)
EndProcedure
    Repeat
        ExamineKeyboard()
        Event=WindowEvent()
        Delay(1)
        ClearScreen(RGB(0,0,0)) ;  *****<------- si tu Rem ceci le sprite laisse une trace :D
        Start3D()
        Spiral()
        Stop3D()
        
        
     ;#####
    ;-FPS
    ;#####
    If Second<ElapsedMilliseconds()
      Second = ElapsedMilliseconds() + 1000
      fps = Frame_Counter
      Frame_Counter = 0
    Else
      Frame_Counter + 1
    EndIf
   StartDrawing(ScreenOutput())    

    DrawText(5, 5, "FPS : " + Str(fps),$ffffff)
    ;
  StopDrawing()
        
        
        FlipBuffers():; affiche l'ecran
     

        Event=WindowEvent()
         If KeyboardPushed(#PB_Key_Escape)
            End
        EndIf
       
               
    Until Event=#PB_Event_CloseWindow
Post Reply