Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: Hotshot on May 19, 2008

Title: Blue 3D Starfield
Post by: Hotshot 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
Title: Re: Blue 3D Starfield
Post by: hellfire on May 19, 2008
Quote
How do u do window in freebasic
like this (http://dbfinteractive.com/index.php?cat=142).
Title: Re: Blue 3D Starfield
Post by: Shockwave 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
Title: Re: Blue 3D Starfield
Post by: DrewPee on May 20, 2008
Nice work Hotshot! and nice amendments Shockwave!
Code is simple to understand so thanks for that also!

Regards
Andy (DrewPee)
Title: Re: Blue 3D Starfield
Post by: nkk_kan 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?
Title: Re: Blue 3D Starfield
Post by: Shockwave 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
Title: Re: Blue 3D Starfield
Post by: Hotshot on May 26, 2008
thanks shocky  :)  i got two different version of freebasic.
One latest version and other one called FBEDIT
Title: Re: Blue 3D Starfield
Post by: nkk_kan 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.
 
Title: Re: Blue 3D Starfield
Post by: Hotshot on May 27, 2008
@HotShot : the latest version is 0.18.5. is that what you got?  Yes  :)

 :cheers:
Title: Re: Blue 3D Starfield
Post by: ferris on June 30, 2008
@Shocky: Begging? It DID crash here :D

Good looking afterwords though :)