Dark Bit Factory & Gravity

ARCHIVE => Archive => Cobra => Topic started by: Clyde on January 03, 2007

Title: 3D in 2D Starfield
Post by: Clyde on January 03, 2007
This was made using the demo version of Cobra; download information is available in the useful links section of this board,

My first attempt with the language. It's nothing jaw dropping, but gives an idea of the program structure / syntax.


///////////////////////////////////
// 3D Starfield With Cobra Demo  //
// In January '07                //
// By Clyde Based on Shockwave's //
// BB Tutorials                  //
///////////////////////////////////
program uses pure2d,keyset

Const XRES=640
Const YRES=480

Const XRES2=320
Const YRES2=240

Const MAXSTARS=1000

Var
    StarX:Array[MAXSTARS] Of Integer
    StarY:Array[MAXSTARS] Of Integer
    StarZ:Array[MAXSTARS] Of Real


Procedure SetupStars()

  Var a

    Begin
       
        For a=0 To MAXSTARS-1
          StarX[ a ]=Rand(-30000,30000)
          StarY[ a ]=Rand(-30000,30000)
          StarZ[ a ]=Rnd(1,30)
        Next
   
    End
//
// End Of Procedure SetupStars()
//


Procedure UpdateStars()
 
  Var TX, TY, a
 
 
  Begin
   
    For a=0 To MAXSTARS-1
       
     
        If Starz[ a ]>1 Then   
   
          TX=( StarX[ a ] / StarZ[ a ])+( XRES2 )
          TY=( StarY[ a ] / StarZ[ a ])+( YRES2 )
       
        EndIf
       
        If (TX>0) And (TX<XRES-1) And (TY>0) And (TY<YRES-1) Then

                PixelFast( TX, TY, $FFFFFF )
           
                StarZ[ a ] = StarZ[ a ] - 0.25
        Else
           
            StarX[ a ]=Rand(-30000,30000)
            StarY[ a ]=Rand(-30000,30000)
            StarZ[ a ]=30
       
        Endif           
       

    Next
   
End
//
// End Of Procedure UpdateStars()
//



Procedure Main()
Begin
 
    OpenScreen( XRES, YRES, 16, TRUE )
 
    SetupStars()


    While Not KeyDown(VK_Escape)
    Cls
   
        UpdateStars()
   
    Flip
    Wend
   
    CloseScreen()
   
End
//
// End Of Procedure Main()
//

Begin
  Main()
End


There's some pretty funky gfx effects in the examples directory too.
Title: Re: Cobra - 3D Starfield
Post by: Shockwave on January 03, 2007
Looks a little bit like Pascal :) +Karma for posting the first Cobra listing in this board.
Title: Re: Cobra - 3D Starfield
Post by: mike_g on January 03, 2007
Hey well done! I'm still trying to get my first function to work o_0. 
Title: Re: Cobra - 3D Starfield
Post by: Clyde on January 03, 2007
Hehe, Cheers Mike_G :)
I've been baffled by what flags to use for fullscreen displays. As it defaults to a window area.
Title: Re: Cobra - 3D Starfield
Post by: Ghost^BHT on January 03, 2007
the demo version only runs in a window Clyde  :(
Title: Re: Cobra - 3D Starfield
Post by: Hotshot on January 08, 2007
nice and neat Coding there  :clap:
Title: Re: Cobra - 3D Starfield
Post by: Clyde on January 08, 2007
@Wham: Cheers mate.
@Hotshot: Thanks, basically it's a conversion of Shockwave's Starfield tutorial / code.