Author Topic: Blue 3D Starfield  (Read 5417 times)

0 Members and 1 Guest are viewing this topic.

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Blue 3D Starfield
« on: May 19, 2008 »
Hiya all,

Here the 3D Starfield (I will post the cobra one later)

Not bad 3D Starfield. How do u do window in freebasic?

Code: [Select]
Option Explicit
Option Static

Const MAX_STAR=5000
Const STAR_SPEED=2
Const WIDTHS=640
Const HEIGHT=480

Dim Shared star_x(MAX_STAR) As Integer
Dim Shared star_y(MAX_STAR) As Integer
Dim Shared star_z(MAX_STAR) As Integer
Dim Shared c As Integer
Dim Shared col As Integer
Dim Shared s_x As Integer
Dim Shared s_y As Integer

'-------------------------------------------------------------------------
' Includes.
'-------------------------------------------------------------------------
'#define PTC_WIN
#Include Once "tinyptc.bi"

Declare Sub Setup_Stars()
Declare Sub UpdateStar()
'-------------------------------------------------------------------------
' Open Screen;
'-------------------------------------------------------------------------
 
If( ptc_open( "3D STARFIELD ", 640, 480 ) = 0 ) Then
        End -1
End If

Const XRES = 640
Const YRES = 480

Dim Shared buffer(XRES*YRES) As Uinteger

setup_stars()

Do
        Cls
        UpdateStar()
        ptc_update(@buffer(0))
        Erase buffer
        Sleep 25
Loop Until Inkey$ = Chr$(27)

ptc_close()

End

Sub setup_stars()
        For c=0 To MAX_STAR
            star_x(c)=Rnd*WIDTHS-WIDTHS/2
            star_y(c)=Rnd*HEIGHT-HEIGHT/2
            star_z(c)=Rnd*255
        Next
End Sub

Sub UpdateStar()
        For c=0 To MAX_STAR
                star_z(c)=star_z(c)-STAR_SPEED
                If star_z(c)<=STAR_SPEED Then star_z(c)=255
                s_x=(star_x(c)/star_z(c))+(WIDTHS/2)
                s_y=(star_y(c)/star_z(c))+(HEIGHT/2)
                col=255-star_z(c)
                buffer(s_x+s_y*640)=col
        Next
End Sub

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Blue 3D Starfield
« Reply #1 on: May 19, 2008 »
Quote
How do u do window in freebasic
like this.
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Blue 3D Starfield
« Reply #2 on: May 19, 2008 »
Hi Hotshot, not a bad starfield for a first attempt.

Some of that code was actually begging for a crash so I re-wrote it a little and added comments for you.
You need to be careful where you plot pixels with tinyptc!

Code: [Select]
Option Static

Const MAX_STAR=500:' NOT SO MANY ARE NEEDED TO LOOK GOOD..
DIM SHARED AS DOUBLE STAR_SPEED=.05 :' USE A DOUBLE FOR BETTER MOVEMENT


Dim Shared star_x(MAX_STAR) As DOUBLE:' USE DOUBLES FOR MORE PRECISION
Dim Shared star_y(MAX_STAR) As DOUBLE:' USE DOUBLES FOR MORE PRECISION
Dim Shared star_z(MAX_STAR) As DOUBLE:' USE DOUBLES FOR MORE PRECISION
Dim Shared c As Integer
Dim Shared col As Integer
Dim Shared s_x As Integer
Dim Shared s_y As Integer

'-------------------------------------------------------------------------
' Includes.
'-------------------------------------------------------------------------

'THIS LINE SETS WINDOWED MODE;
#define PTC_WIN

#Include Once "tinyptc.bi"

Declare Sub Setup_Stars()
Declare Sub UpdateStar()
'-------------------------------------------------------------------------
' Open Screen;
'-------------------------------------------------------------------------
 
If( ptc_open( "3D STARFIELD ", 640, 480 ) = 0 ) Then
        End -1
End If

Const XRES = 640
Const YRES = 480

DIM SHARED AS INTEGER WIDTHS
DIM SHARED AS INTEGER HEIGHT

' PRE-DEFINE HERE TO SAVE DIVIDES LATER..
WIDTHS=XRES/2
HEIGHT=YRES/2

Dim Shared buffer(XRES*YRES) As Uinteger

setup_stars()

Do
'       CLS HERE IS NOT NEEDED BECAUSE YOU ARE ERASING THE BUFFER ANYWAY SO I REMOVED IT
        UpdateStar()
        ptc_update(@buffer(0))
        Erase buffer
       
Loop Until Inkey$ = Chr$(27)

ptc_close()

End


'
' SLIGHTLY CHANGED YOUR CALC BELOW TO DEFINE STARS;
'
Sub setup_stars()
        For c=0 To MAX_STAR
            star_x(c)=(Rnd(1)*20000)-10000
            star_y(c)=(Rnd(1)*20000)-10000
            star_z(c)=Rnd*255
        Next
End Sub


' ALTERED YOUR DRAW ROUTINE TO MAKE IT SAFE. YOU SHOULD CHECK IF YOUR STAR IS
' ON SCREEN BEFORE YOU DRAW IT OTHERWISE YOUR PROGRAM MAY CRASH BADLY WHEN YOU
' WRITE TO MEMORY THAT DOESNT BELONG TO YOU.

Sub UpdateStar()
        For c=0 To MAX_STAR
                star_z(c)=star_z(c)-STAR_SPEED
                If star_z(c)<=1 Then star_z(c)=star_z(c)+254
               
                s_x=(star_x(c)/star_z(c))+(WIDTHS)
                s_y=(star_y(c)/star_z(c))+(HEIGHT)
               
                'CHECK IS STAR ON SCREEN;
                IF S_X>0 AND S_X<XRES AND s_Y>0 AND s_Y < YRES THEN
                   
                    'COLOUR CALC;
                    col=INT(255-star_z(c))
                    'PLOT;
                    buffer(s_x+s_y*640)=RGB(COL,COL,COL)
                   
                END IF
        Next
End Sub
Shockwave ^ Codigos
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Blue 3D Starfield
« Reply #3 on: May 20, 2008 »
Nice work Hotshot! and nice amendments Shockwave!
Code is simple to understand so thanks for that also!

Regards
Andy (DrewPee)
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline nkk_kan

  • C= 64
  • **
  • Posts: 32
  • Karma: 4
  • poisoning the world with goodness
    • View Profile
Re: Blue 3D Starfield
« Reply #4 on: May 21, 2008 »
That looks nice!

hey guys...what FB version are you using?
Hotshot's code screams out errors about undeclared variables..
and Option is not supported in -lang fb now

anyways, i tweaked it a bit and it runs fine and looked nice

A question though...why do you people use TinyPtc?
I use FBGFX and it works fine for me (i made that spiral in FBGFX)
any reason why i should use tinyptc?

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Blue 3D Starfield
« Reply #5 on: May 21, 2008 »
FBGFX makes larger exe's, has an ugly refresh rate.
The original tinyptc by gaffer also has problems.

There is a version of tinyptc available only on this forum called tinyptc_ext which adds functionality to tinyptc and fixes things such as the refresh rate and compatibility with mmx.

However don't expect an easy time if you are going to use tinyptc, as essentially all it allows you to do is to plot pixels and update a double buffered screen with a chunk of memory.

For those of us who like to code our own routines for drawing graphics, lines, circles, text etc though it is really really cool and although it's been said by Jim before, I'll echo him. It's the best library available for creating oldschool software rendered intros.

If you want an easier time, stick to fbgfx or sdl or something like that, there's nothing wrong with them. (apart from the file size and/or shit refresh)

A lot of people here (including me) use older versions of freebasic, you should be able to compile Hotshots example using -lang deprecated

hth
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Re: Blue 3D Starfield
« Reply #6 on: May 26, 2008 »
thanks shocky  :)  i got two different version of freebasic.
One latest version and other one called FBEDIT

Offline nkk_kan

  • C= 64
  • **
  • Posts: 32
  • Karma: 4
  • poisoning the world with goodness
    • View Profile
Re: Blue 3D Starfield
« Reply #7 on: May 27, 2008 »
@HotShot : the latest version is 0.18.5. is that what you got?
                 and FBEdit is just an IDE for FreeBASIC compiler. It's not a different version of FB.
 

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Re: Blue 3D Starfield
« Reply #8 on: May 27, 2008 »
@HotShot : the latest version is 0.18.5. is that what you got?  Yes  :)

 :cheers:

Offline ferris

  • Pentium
  • *****
  • Posts: 841
  • Karma: 84
    • View Profile
    • Youth Uprising Home
Re: Blue 3D Starfield
« Reply #9 on: June 30, 2008 »
@Shocky: Begging? It DID crash here :D

Good looking afterwords though :)
http://iamferris.com/
http://youth-uprising.com/

Where the fun's at.
Challenge Trophies Won: