Dark Bit Factory & Gravity

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

Title: Super Plotball
Post by: Clyde on January 12, 2007
For this I was very impressed by a program that Shockwave made in Blitz, and just couldnt resist seeing what it would look and behave like in Cobra.

I had to reduce the rotation speeds by half, as it was whizzing about ( revert them to one to see what I mean ) :)

Code: [Select]
//
// Demonstration of the use of Rectangles.
// utilizing a snazzy BB Plotball Routine By Shockwave.
// Converted By Clyde January '07
//
Program Uses Pure2D,keyset

Const XRES=640
Const YRES=480

Const XRES2=320
Const YRES2=240

Const Points=360

Var
   
    size:           Real=1.00   
    xr,yr,zr:       Real
   
    cs:Array[ Points ] Of Real
    sn:Array[ Points ] Of Real
   
    vbx:Array[ Points ] Of Real                                 
    vby:Array[ Points ] Of Real
    vbz:Array[ Points ] Of Real
   
    vbtx: Array[ Points ] Of Real
    vbty: Array[ Points ] Of Real
    vbtz: Array[ Points ] Of Real


Procedure DrawPlotball()

Var
    a, Value
    Red, Grn, Blu, Col
    sz:Real

Begin
   
    For a=1 To points
 
      If (vbtz[a]<0) Then
   
            Value=(Abs(vbtz[a]))
            sz   =(Value/20)

            Red  =(Value+50)
            Grn  =0
            Blu  =0
           
            If (Red>255) Then Red=255
            If (Grn>255) Then Grn=255
            If (Blu>255) Then Blu=255
           
            Col=( 255 Shl 24 ) or ( Red Shl 16 ) or ( Grn Shl 8 ) Or ( Blu )
           
            Rect ( vbtx[a], vbty[a], sz, sz, Col, true )
           
      EndIf
Next   

 
End



Procedure InitializePlotball()

  Var
    a
    Ang, Rad, Long, Lat:Real

  Begin

    OpenScreen(XRES,YRES)
 
    //
    // Setup SLUTS.
    //
    For a=0 To 360

        cs[a]=Cos( a )
        sn[a]=Sin( a )
       
    Next
   
    //
    // Define Plotball Points.
    //
    rad=200
    long=0
    lat=0

    For a=1 To points
       
        If (long>360) Then
            long=long-360
            lat=lat+10
        EndIf

        vbx[a]=rad*(Sin(long)*Sin( (lat*200)))
        vby[a]=rad*(Sin(long)*Cos( (lat*200)))
        vbz[a]=(Cos(long)*200)

        long=long+10

    Next

 

End

Procedure RotatePlotball()

  Var
    a
    x1,y1,z1:Real
    xx,yy,zz,dv:Real
 
  Begin
   
    //
  // Rotate And Scale Each Point! Store Result
    //
  For a=1 To points
   
      x1 = vbx[a]
    y1 = vby[a]
    z1 = vbz[a]

  //
  // X,Y,Z rotations!
    //
  xx=x1
  yy=y1*cs[xr]+z1*sn[xr]
  zz=z1*cs[xr]-y1*sn[xr]
  y1=yy
  x1=xx*cs[yr]-zz*sn[yr]
  z1=xx*sn[yr]+zz*cs[yr]
  zz=z1
  xx=x1*cs[zr]-y1*sn[zr]
  yy=x1*sn[zr]+y1*cs[zr]

  //
  // Apply Perspective!
    //
  dv=(zz/1000)+1
    xx=size*(xx/dv)+XRES2
    yy=size*(yy/dv)+YRES2
 
  vbtx[a]=xx
    vbty[a]=yy
    vbtz[a]=zz
 
  Next

//
// Rotations!
//
xr=xr+0.5
yr=yr+0.5
zr=zr+0.5

If (xr>360) Then xr=xr-360
If (yr>360) Then yr=yr-360
If (zr>360) Then zr=zr-360

End
 
Procedure RunPlotball()

  Begin

    While Not KeyDown(VK_Escape)
      Cls
        RotatePlotball()
        DrawPlotball()
      Flip
    Wend

End



Begin

  InitializePlotball()
  RunPlotball()
  CloseScreen()
End

Cheers & Enjoy,
Clyde.
Title: Re: Super Plotball
Post by: GrahamK on January 12, 2007
Nice.
btw, do you mind if I stick these in the examples folder of the full release ?

Here is a version using some specific features of Cobra
Code: [Select]
//
// Demonstration of the use of Rectangles.
// utilizing a snazzy BB Plotball Routine By Shockwave.
// Converted By Clyde January '07
// Cobradised By GrahamK
Program Uses Pure2D,keyset

Const XRES=640
Const YRES=480

Const XRES2=320
Const YRES2=240

Const Points=360

type point=record
    cs,sn:real
    vbx,vby,vbz:real
    vbtx,vbty,vbtz:real
endtype
 

Var
   
    size:           Real=1.00   
    xr,yr,zr:       Real
   
    pointlist:list of point   
   
    cs:Array[ Points ] Of Real
    sn:Array[ Points ] Of Real

Procedure DrawPlotball()

Var
    a, Value
    Rd, Grn, Blu, Col
    sz:Real
   
    apoint:^point

Begin

   loop apoint through pointlist

      If (apoint.vbtz<0) Then
   
            Value=(Abs(apoint.vbtz))
            sz   =(Value/20)

            Rd  =(Value+50)
            Grn  =0
            Blu  =0
           
            If (Rd>255) Then Rd=255
            If (Grn>255) Then Grn=255
            If (Blu>255) Then Blu=255
           
            col=ToRGBA(rd,grn,blu,255)
           
            Rect ( apoint.vbtx, apoint.vbty, sz, sz, Col, true )
           
      EndIf
       
   
   endloop
 
End



Procedure InitializePlotball()

  Var
    a
    Ang, Rad, Long, Lat:Real
    apoint:^point
  Begin

    OpenScreen(XRES,YRES)
 
    //
    // Setup SLUTS.
    //
    For a=0 To 360

        cs[a]=Cos( a )
        sn[a]=Sin( a )
       
    Next
   
    //
    // Define Plotball Points.
    //
    rad=200
    long=0
    lat=0

    For a=1 To points
   
        apoint = newitem(pointlist)
       
        If (long>360) Then
            long=long-360
            lat=lat+10
        EndIf
   
        apoint.vbx=rad*(Sin(long)*Sin( (lat*200)))
        apoint.vby=rad*(Sin(long)*Cos( (lat*200)))
        apoint.vbz=(Cos(long)*200)
   
        long=long+10

    Next

 

End

Procedure RotatePlotball()

  Var
    a
    x1,y1,z1:Real
    xx,yy,zz,dv:Real
    apoint:^point
 
  Begin

    //
      // Rotate And Scale Each Point! Store Result
       //           
   
      loop apoint through pointlist
       
        x1 = apoint.vbx
        y1 = apoint.vby
        z1 = apoint.vbz
       
          //   
          // X,Y,Z rotations!
        //
          xx=x1
          yy=y1*cs[xr]+z1*sn[xr]
          zz=z1*cs[xr]-y1*sn[xr]
          y1=yy
          x1=xx*cs[yr]-zz*sn[yr]
          z1=xx*sn[yr]+zz*cs[yr]
          zz=z1
          xx=x1*cs[zr]-y1*sn[zr]
          yy=x1*sn[zr]+y1*cs[zr]
       
          //
          // Apply Perspective!
        //
          dv=(zz/1000)+1
          xx=size*(xx/dv)+XRES2
          yy=size*(yy/dv)+YRES2
 
          apoint.vbtx=xx
          apoint.vbty=yy
          apoint.vbtz=zz
     
      endloop

    //
    // Rotations!
    //
    xr=xr+0.5
    yr=yr+0.5
    zr=zr+0.5

    If (xr>360) Then xr=xr-360
    If (yr>360) Then yr=yr-360
    If (zr>360) Then zr=zr-360

End
 
Procedure RunPlotball()

  Begin

    While Not KeyDown(VK_Escape)
      Cls
        RotatePlotball()
        DrawPlotball()
      Flip
    Wend

End



Begin

  InitializePlotball()
  RunPlotball()
  CloseScreen()
End
Title: Re: Super Plotball
Post by: Clyde on January 12, 2007
Cheers glad you like it bud.
By all means dude, I'd be very happy for them to feature in the full release.

Thanks,
Clyde.
Title: Re: Super Plotball
Post by: Shockwave on January 13, 2007
Looks cool :)
Title: Re: Super Plotball
Post by: Clyde on January 15, 2007
Graham dude, could you explain what the alterations you made to the original do please matey?
I know what the toRGBA is upto. :)
Title: Re: Super Plotball
Post by: GrahamK on January 15, 2007
toRGBA is the way to calculate the colour torgba(red,green,blue,alpha). The way you use will work, but is not definate (eg. opengl calculates it differently) so by using toRGBA to always get the correct calculation, regardless of the engine used.

The rest was switching it to use types and type lists rather than fixed size arrays.
Title: Re: Super Plotball
Post by: Clyde on January 15, 2007
Thanks Graham :)

Does the following mean start with the first instance of the type called point?

Code: [Select]
apoint:^point

Cheers,
Clyde.
Title: Re: Super Plotball
Post by: GrahamK on January 15, 2007
that creates a variable called "apoint", which is a pointer to a type called "Point"

this means that later on :
Code: [Select]
loop apoint through pointlist
at each loop through this statement, the "apoint" will point to the next entry in the list, starting at the beginning.


Title: Re: Super Plotball
Post by: Clyde on January 15, 2007
Cheers for explaining that, nice one :)
Title: Re: Super Plotball
Post by: GrahamK on January 18, 2007
Quick note Clyde,
When the next patch, and full release are issued the code above (and I think one of the other demos) will break.

Red is a reserved word, and I found out I'd forgotten to check function variables against keywords when checking if they are valid, doh..

just change red to rd and everything should be fine.
Title: Re: Super Plotball
Post by: Clyde on January 18, 2007
OK, thanks man :)
Title: Re: Super Plotball
Post by: GrahamK on January 18, 2007
Did I mention it's available to purchase now ;)

Runs off before plug police get me....  :whack:
Title: Re: Super Plotball
Post by: Shockwave on January 18, 2007
Congratulations Graham and good luck with it!
Title: Re: Super Plotball
Post by: GrahamK on January 18, 2007
Thanks Shockwave, Much appreciated.
Title: Re: Super Plotball
Post by: Shockwave on January 18, 2007
I'll be buying a copy anyway, 30 quid is very reasnoble.
Title: Re: Super Plotball
Post by: GrahamK on January 18, 2007
I try to be reasnoble as much as possible (however painful)  O0
Title: Re: Super Plotball
Post by: Clyde on January 18, 2007
Congrats too Graham and Wicked! :)
Title: Re: Super Plotball
Post by: mike_g on January 19, 2007
Yeah congratulations with the release graham  :clap:

I hope things all go well. Theres a lot of other languages I will have to learn for college over the next year or so, so I dont think I'll be buying Cobra now. Still its certainly a future possibility :)
Title: Re: Super Plotball
Post by: GrahamK on January 19, 2007
I aim for Cobra to be around for quite a while, so I'm sure it'll still be here when you are ready  :updance: