Author Topic: My First FB Program :D  (Read 4895 times)

0 Members and 1 Guest are viewing this topic.

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
My First FB Program :D
« on: May 05, 2007 »
Okay I have given up learning C++ with Visual studio its simply to finicky for my taste hell I found assembler a whole lot easier to pick up than that so I turned my attention back towards basic and well this is my first proper program in FreeBasic that I actually understand (Except for the triangle drawing subroutine which was borrowed from one of Shockys post over on the FreeBasic Forum)

Code: [Select]
'Declarations For The Program ...
Option Static
Option Explicit

Const XRES = 800
Const YRES = 600
Const GridX = 63
Const GridY = 63
Const METER = 5

Dim Shared As Uinteger BUFFER(XRES*YRES)
Dim Shared As Double   OldTime, GAdd
Dim Shared As Uinteger NewTime, Ticks

#include Once "tinyptc.bi"
Declare Sub FLAT_TRIANGLE( Byval X1 As Integer , Byval Y1 As Integer, Byval X2 As Integer , Byval Y2 As Integer , Byval X3 As Integer, Byval Y3 As Integer , Byval TC As Integer)
Declare Sub FPS()

'Open Graphics Screen ...
If (ptc_open("Triangles", XRES, YRES) = 0) Then
    End -1
End If



' Initialise The Vertex Array & Variables For Accessing Them ...
Type Vertex
    X As Integer
    Y As Integer
End Type

Dim As Vertex  V(0 To GridX, 0 To Gridy)
Dim AS Integer X, Y
Dim As Single ROTX,     ROTY,     ROTZ
Dim As Single CX,SX,    CY,SY,    CZ,SZ
Dim As Single XX,XY,XZ, YX,YY,YZ, ZX,ZY,ZZ
Dim As Single X1,Y1,Z1, X2,Y2,Z2, X3,Y3,Z3
Dim As Single ANG
ANG = 3.14159/180




'Main Drawing Routine ...
OldTime = Timer
Do
    FPS()
    GAdd += 0.02

    'Rotate the planes ...
    ROTX = ROTX + ANG
    ROTY = ROTY + ANG
    ROTZ = ROTZ + ANG
    CX = Cos(ROTX)
    CY = Cos(ROTY)
    CZ = Cos(ROTZ)
    SX = Sin(ROTX)
    SY = Sin(ROTY)
    SZ = Sin(ROTZ)
   
    'Optimised Rotation Shortuct ...
    XX = CY*CZ*METER*300
    XY = (CX*-SZ + SX*SY*CZ)*METER*300
    XZ = (CX*SY*CZ - SX*-SZ)*METER
    YX = CY*SZ*METER*300
    YY = (CX*CZ + SX*SY*SZ)*METER*300
    YZ = (CX*SY*SZ - SX*CZ)*METER
    ZX =-SY*METER*300
    ZY = SX*CY*METER*300
    ZZ = CX*CY*METER

    X1 = -XX*GridX/2 -YX*GridY/2
    Y1 = -XY*GridX/2 -YY*GridY/2
    Z1 = -XZ*GridX/2 -YZ*GridY/2 + 300
    For Y = 0 To GridY
        X2 = X1
        Y2 = Y1
        Z2 = Z1
        For X = 0 To GridX
            With V(X, Y)
                .X = 400 + X2 / Z2
                .Y = 300 + Y2 / Z2
            End With
            X2 = X2 + XX
            Y2 = Y2 + XY
            Z2 = Z2 + XZ
        Next X
        X1 = X1 + YX
        Y1 = Y1 + YY
        Z1 = Z1 + YZ
    Next Y


    Dim C As Integer
    C = 0
    For Y = 1 To GridY
        For X = 1 To GridX
            FLAT_TRIANGLE ( _
                V(X-1,Y-1).X, V(X-1,Y-1).Y, _
                V(X-1,Y).X, V(X-1,Y).Y, _
                V(X,Y).X, V(X,Y).Y, _
                rgb( C*255, C*255, 255) _
            )
            FLAT_TRIANGLE ( _
                V(X-1,Y-1).X, V(X-1,Y-1).Y, _
                V(X,Y-1).X, V(X,Y-1).Y, _
                V(X,Y).X, V(X,Y).Y, _
                rgb( C*255, C*255, 255) _
            )
            C = 1 - C
        Next X
    Next Y
   
    PTC_UPDATE@BUFFER(0)
    Erase BUFFER
    Ticks = Ticks + 1
Loop Until Inkey$ <> ""




'SubRoutine For Calculating Framerate ...
Sub FPS()
    Dim Z As Uinteger
    If (Timer-OldTime) >= 1 Then
        NewTime = Ticks
        OldTime = Timer
        Print "FPS ... " + Str$(NewTime)
        Ticks = 0
    End IF
End Sub




'SubRoutine For Drawing Flat Shaded Triangles ... (Borrowed From Shackwaves Code)
Sub FLAT_TRIANGLE(Byval X1 As Integer , Byval Y1 As Integer, Byval X2 As Integer , Byval Y2 As Integer , Byval X3 As Integer, Byval Y3 As Integer , Byval TC As Integer)
'-------------------------------------------------------------------------
' FLAT TRIANGLE RENDERER WITH ASSEMBLY LANGUAGE RASTERISING BY SHOCKWAVE ^ DBF ^ S!P 2006.
'-------------------------------------------------------------------------
'-------------------------------------------------------------------------
' WE NEED TO SORT THESE POINTS INTO ORDER FROM TOP TO BOTTOM, AN EXCHANGE SORT IS OK.
' AS WE ONLY HAVE GOT 3 POINTS TO ARRANGE.
'-------------------------------------------------------------------------
Dim As Integer TEMPX,TEMPY,LO,LI
                Dim As Integer PX(3)
                Dim As Integer PY(3)
                Dim TFLAG As Integer
                Dim pp As Uinteger Ptr
                Dim As Integer IL1,IL2,SLICE
                TFLAG=0
        PX(1)= X1
        PX(2)= X2
        PX(3)= X3
       
        PY(1)= Y1
        PY(2)= Y2
        PY(3)= Y3
For LO = 1 To 2
    For LI =1 To 2     
        If PY(LI+1) <= PY(LI) Then
        TEMPX = PX(LI) : TEMPY = PY(LI)
        PX(LI) = PX(LI+1)
        PY(LI) = PY(LI+1)
        PX(LI+1) = TEMPX
        PY(LI+1) = TEMPY
        End If   
    Next LI
Next LO

        Dim As Double XP1,XP2:' SCREEN POSITIONS.
        Dim As Double XI1,XI2:' INTERPOLATIONS.
       
'***
'*** REGULAR TRIANGLE (Y1<Y2 Y2<Y3)
'***

If PY(1)<PY(2) And PY(2)<PY(3) Or (PY(2) = PY(3)) Then
    TFLAG=1
XP1 = PX(1)
XP2 = PX(1)
XI1 = (PX(1)-PX(2)) / (PY(2) - PY(1))
XI2 = (PX(1)-PX(3)) / (PY(3) - PY(1))

For LO = PY(1) To PY(2)-1
   
If LO>=0 And LO<YRES Then

    If XP1<=XP2 Then
        IL1=XP1
        IL2=XP2
    Else
        IL1=XP2
        IL2=XP1
    End If
   
    If IL2>XRES Then IL2=XRES
    If IL1<0 Then IL1=0

    SLICE = IL2-IL1
    If SLICE>0 Then
    PP = @BUFFER(IL1+(LO*XRES))   
    asm
        mov eax,dword Ptr[TC]
        mov ecx, [slice]
        mov edi, [PP]
        rep stosd
    End asm   
    End If
   

End If

XP1=XP1-XI1
XP2=XP2-XI2
Next

XI1 = (PX(2)-PX(3)) / (PY(3) - PY(2))
XP1 = PX(2)

For LO = PY(2) To PY(3)
If LO>=0 And LO<YRES Then
    If XP1<=XP2 Then
        IL1=XP1
        IL2=XP2
    Else
        IL1=XP2
        IL2=XP1
    End If

    If IL2>XRES Then IL2=XRES
    If IL1<0 Then IL1=0

    SLICE = IL2-IL1
    If SLICE>0 Then
    PP = @BUFFER(IL1+(LO*XRES))   
    asm
        mov eax,dword Ptr[TC]
        mov ecx, [slice]
        mov edi, [PP]
        rep stosd
    End asm   
    End If
End If
XP1=XP1-XI1
XP2=XP2-XI2
Next

End If


'***
'*** FLAT TOPPED TRIANGLE Y1=Y2
'***

If TFLAG=0 And PY(1) = PY(2) Then
   
        TFLAG=1
        XP1 = PX(1)
        XP2 = PX(2)
        XI1 = (PX(1)-PX(3)) / (PY(3) - PY(1))
        XI2 = (PX(2)-PX(3)) / (PY(3) - PY(2))
For LO = PY(1) To PY(3)
If LO>=0 And LO<YRES Then
    If XP1<=XP2 Then
        IL1=XP1
        IL2=XP2
    Else
        IL1=XP2
        IL2=XP1
    End If
   
    If IL2>XRES Then IL2=XRES
    If IL1<0 Then IL1=0
   
    SLICE = IL2-IL1
    If SLICE>0 Then
    PP = @BUFFER(IL1+(LO*XRES))   
    asm
        mov eax,dword Ptr[TC]
        mov ecx, [slice]
        mov edi, [PP]
        rep stosd
    End asm   
    End If
End If
    XP1=XP1-XI1
    XP2=XP2-XI2

Next
End If
End Sub


So how did I do? Anything in there that looks improper please point it out
« Last Edit: May 05, 2007 by rain_storm »

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: My First FB Program :D
« Reply #1 on: May 05, 2007 »
It looks ok to me :) I had a quick scan through the code and couldn't find any errors. Thanks for the credit for the triangle routie by the way, it's appreciated. My nick is not shAckwave tho :P
Shockwave ^ Codigos
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: My First FB Program :D
« Reply #2 on: May 05, 2007 »
 :P Sorry bout that, my bad

Edit -
Im just playing with this code to see what I can do I got a wavy thing going on with it and its got a xor texture now
Code: [Select]
'Declarations For The Program ...
Option Static
Option Explicit

Const XRES = 800
Const YRES = 600
Const GridX = 63
Const GridY = 63
Const METER = 05

Dim Shared As Uinteger BUFFER(XRES*YRES)
Dim Shared As Double   OldTime, GAdd
Dim Shared As Uinteger NewTime, Ticks

#include Once "tinyptc.bi"
Declare Sub FLAT_TRIANGLE( Byval X1 As Integer , Byval Y1 As Integer, Byval X2 As Integer , Byval Y2 As Integer , Byval X3 As Integer, Byval Y3 As Integer , Byval TC As Integer)
Declare Sub FPS()

'Open Graphics Screen ...
If (ptc_open("Triangles", XRES, YRES) = 0) Then
    End -1
End If



' Initialise The Vertex & Hieghtmap Arrays & Variables For Accessing Them ...
Type Vertex
    X As Integer
    Y As Integer
End Type

Type Hieght
    Z As Integer
End Type

Dim As Vertex  V(0 To GridX, 0 To Gridy)
Dim AS Integer X, Y
Dim As Single ROTX,     ROTY,     ROTZ
Dim As Single CX,SX,    CY,SY,    CZ,SZ
Dim As Single XX,XY,XZ, YX,YY,YZ, ZX,ZY,ZZ
Dim As Single X1,Y1,Z1, X2,Y2,Z2, X3,Y3,Z3
Dim As Single CSX,SNX, CSY,SNY
Dim As Single ANG, ANGX, ANGY, AX, AY
ANG  = 3.14159/180
ANGX = 6.28318/GridX
ANGY = 6.28318/GridY
AX = 0
AY = 0


'Main Drawing Routine ...
OldTime = Timer
Do
    FPS()
    GAdd += 0.02

    'Rotate the planes ...
    ROTX = ROTX + ANG
    ROTY = ROTY + ANG
    ROTZ = ROTZ + ANG
    CX = Cos(ROTX)
    CY = Cos(ROTY)
    CZ = Cos(ROTZ)
    SX = Sin(ROTX)
    SY = Sin(ROTY)
    SZ = Sin(ROTZ)
   
    'Optimised Rotation Shortuct ...
    XX = CY*CZ*METER*300
    XY = (CX*-SZ + SX*SY*CZ)*METER*300
    XZ = (CX*SY*CZ - SX*-SZ)*METER
    YX = CY*SZ*METER*300
    YY = (CX*CZ + SX*SY*SZ)*METER*300
    YZ = (CX*SY*SZ - SX*CZ)*METER
    ZX =-SY*METER*300
    ZY = SX*CY*METER*300
    ZZ = CX*CY*METER

    X1 = -XX*GridX/2 -YX*GridY/2
    Y1 = -XY*GridX/2 -YY*GridY/2
    Z1 = -XZ*GridX/2 -YZ*GridY/2 + 300
    For Y = 0 To GridY
        X2 = X1
        Y2 = Y1
        Z2 = Z1
        AY = AY + ANGY
        SNY = Sin(AY)*10
        AX = 0
        For X = 0 To GridX
            AX = AX + ANGX
            SNX = Sin(AX)*10 + SNY
            X3 = SNX*ZX + X2
            Y3 = SNX*ZY + Y2
            Z3 = SNX*ZZ + Z2
            With V(X, Y)
                .X = 400 + X3 / Z3
                .Y = 300 + Y3 / Z3
            End With
            X2 = X2 + XX
            Y2 = Y2 + XY
            Z2 = Z2 + XZ
        Next X
        X1 = X1 + YX
        Y1 = Y1 + YY
        Z1 = Z1 + YZ
    Next Y


    Dim C As Integer
    C = 0
    For Y = 1 To GridY
        For X = 1 To GridX
            C = X Xor Y
            FLAT_TRIANGLE ( _
                V(X-1,Y-1).X, V(X-1,Y-1).Y, _
                V(X-1,Y).X, V(X-1,Y).Y, _
                V(X,Y).X, V(X,Y).Y, _
                rgb( C*4, C*4, C*4) _
            )
            FLAT_TRIANGLE ( _
                V(X-1,Y-1).X, V(X-1,Y-1).Y, _
                V(X,Y-1).X, V(X,Y-1).Y, _
                V(X,Y).X, V(X,Y).Y, _
                rgb( C*4, C*4, C*4) _
            )
            C = 1 - C
        Next X
    Next Y
   
    PTC_UPDATE@BUFFER(0)
    Erase BUFFER
    Ticks = Ticks + 1
Loop Until Inkey$ <> ""




'SubRoutine For Calculating Framerate ...
Sub FPS()
    Dim Z As Uinteger
    If (Timer-OldTime) >= 1 Then
        NewTime = Ticks
        OldTime = Timer
        Print "FPS ... " + Str$(NewTime)
        Ticks = 0
    End IF
End Sub




'SubRoutine For Drawing Flat Shaded Triangles ... (Borrowed From Shockwaves Code)
Sub FLAT_TRIANGLE(Byval X1 As Integer , Byval Y1 As Integer, Byval X2 As Integer , Byval Y2 As Integer , Byval X3 As Integer, Byval Y3 As Integer , Byval TC As Integer)
'-------------------------------------------------------------------------
' FLAT TRIANGLE RENDERER WITH ASSEMBLY LANGUAGE RASTERISING BY SHOCKWAVE ^ DBF ^ S!P 2006.
'-------------------------------------------------------------------------
'-------------------------------------------------------------------------
' WE NEED TO SORT THESE POINTS INTO ORDER FROM TOP TO BOTTOM, AN EXCHANGE SORT IS OK.
' AS WE ONLY HAVE GOT 3 POINTS TO ARRANGE.
'-------------------------------------------------------------------------
Dim As Integer TEMPX,TEMPY,LO,LI
                Dim As Integer PX(3)
                Dim As Integer PY(3)
                Dim TFLAG As Integer
                Dim pp As Uinteger Ptr
                Dim As Integer IL1,IL2,SLICE
                TFLAG=0
        PX(1)= X1
        PX(2)= X2
        PX(3)= X3
       
        PY(1)= Y1
        PY(2)= Y2
        PY(3)= Y3
For LO = 1 To 2
    For LI =1 To 2     
        If PY(LI+1) <= PY(LI) Then
        TEMPX = PX(LI) : TEMPY = PY(LI)
        PX(LI) = PX(LI+1)
        PY(LI) = PY(LI+1)
        PX(LI+1) = TEMPX
        PY(LI+1) = TEMPY
        End If   
    Next LI
Next LO

        Dim As Double XP1,XP2:' SCREEN POSITIONS.
        Dim As Double XI1,XI2:' INTERPOLATIONS.
       
'***
'*** REGULAR TRIANGLE (Y1<Y2 Y2<Y3)
'***

If PY(1)<PY(2) And PY(2)<PY(3) Or (PY(2) = PY(3)) Then
    TFLAG=1
XP1 = PX(1)
XP2 = PX(1)
XI1 = (PX(1)-PX(2)) / (PY(2) - PY(1))
XI2 = (PX(1)-PX(3)) / (PY(3) - PY(1))

For LO = PY(1) To PY(2)-1
   
If LO>=0 And LO<YRES Then

    If XP1<=XP2 Then
        IL1=XP1
        IL2=XP2
    Else
        IL1=XP2
        IL2=XP1
    End If
   
    If IL2>XRES Then IL2=XRES
    If IL1<0 Then IL1=0

    SLICE = IL2-IL1
    If SLICE>0 Then
    PP = @BUFFER(IL1+(LO*XRES))   
    asm
        mov eax,dword Ptr[TC]
        mov ecx, [slice]
        mov edi, [PP]
        rep stosd
    End asm   
    End If
   

End If

XP1=XP1-XI1
XP2=XP2-XI2
Next

XI1 = (PX(2)-PX(3)) / (PY(3) - PY(2))
XP1 = PX(2)

For LO = PY(2) To PY(3)
If LO>=0 And LO<YRES Then
    If XP1<=XP2 Then
        IL1=XP1
        IL2=XP2
    Else
        IL1=XP2
        IL2=XP1
    End If

    If IL2>XRES Then IL2=XRES
    If IL1<0 Then IL1=0

    SLICE = IL2-IL1
    If SLICE>0 Then
    PP = @BUFFER(IL1+(LO*XRES))   
    asm
        mov eax,dword Ptr[TC]
        mov ecx, [slice]
        mov edi, [PP]
        rep stosd
    End asm   
    End If
End If
XP1=XP1-XI1
XP2=XP2-XI2
Next

End If


'***
'*** FLAT TOPPED TRIANGLE Y1=Y2
'***

If TFLAG=0 And PY(1) = PY(2) Then
   
        TFLAG=1
        XP1 = PX(1)
        XP2 = PX(2)
        XI1 = (PX(1)-PX(3)) / (PY(3) - PY(1))
        XI2 = (PX(2)-PX(3)) / (PY(3) - PY(2))
For LO = PY(1) To PY(3)
If LO>=0 And LO<YRES Then
    If XP1<=XP2 Then
        IL1=XP1
        IL2=XP2
    Else
        IL1=XP2
        IL2=XP1
    End If
   
    If IL2>XRES Then IL2=XRES
    If IL1<0 Then IL1=0
   
    SLICE = IL2-IL1
    If SLICE>0 Then
    PP = @BUFFER(IL1+(LO*XRES))   
    asm
        mov eax,dword Ptr[TC]
        mov ecx, [slice]
        mov edi, [PP]
        rep stosd
    End asm   
    End If
End If
    XP1=XP1-XI1
    XP2=XP2-XI2

Next
End If
End Sub

« Last Edit: May 05, 2007 by rain_storm »

Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: My First FB Program :D
« Reply #3 on: May 05, 2007 »
Okay I have given up learning C++ with Visual studio its simply to finicky for my taste hell I found assembler a whole lot easier to pick up than that so I turned my attention back towards basic

Rain , My first step to learning C/C++ is using BCX , it is a Basic Like Language which translate the basic code to C/C++ code then uses a C/C++ compiler to compile to EXE.

It has a syntax just similar to Visual Basic and powerBasic .

Her is the link to BCX

http://tech.groups.yahoo.com/group/BCX/

may be this help you to get to C/C++.

my best wishes.  :)

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: My First FB Program :D
« Reply #4 on: May 05, 2007 »
cool link emil!

but do these things actually work very good my experiance with them is that they are not very efficiant at the conversions having said that they are very good for people wanting to learn c/cpp but i think rains problem was the gdi aspects of cpp not the languge its self.

i think jim has made a yabasic to c converter too but im not sure.

anyway thats really good going for a first program rain!! it ran at really good speed at lookd the ballocs so keep it up and you will be showing us how its done soon!

k+ :goodpost:

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: My First FB Program :D
« Reply #5 on: May 05, 2007 »
That looks absolutely lovely Rain! Well done, please now add some check to see which side is in front so you can draw it from back to front, then add a sine scroll and logo and then release it :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: My First FB Program :D
« Reply #6 on: May 05, 2007 »
Oh  Nino, if the problem is just the IDE of Visual Studio , I am using CodeBlocks  IDE for all C++ compilers , it is very good.

About the BCX , yes the c/c++ code that BCX translated may be are not very efficient , but you can re-Edit it , also do not forget that the c/c++ compiler will optimiz the code when compiling. 

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: My First FB Program :D
« Reply #7 on: May 05, 2007 »
yeah thats true emil!

and yeah some crossproduct test to draw backfaces then draw front faces would be cool rain.
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: My First FB Program :D
« Reply #8 on: May 09, 2007 »
Cheers for the link I will look into BCX anything that will smooth the transition into c is worth checking out thanks for the link emil

Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: My First FB Program :D
« Reply #9 on: May 09, 2007 »

Rain happy BCXing.  :)

Here is a link to my old work with BCX , may be help you.

http://www.freewebs.com/bcxdx/index.htm 

Offline ferris

  • Pentium
  • *****
  • Posts: 841
  • Karma: 84
    • View Profile
    • Youth Uprising Home
Re: My First FB Program :D
« Reply #10 on: May 10, 2007 »
That's an awesome first FB prod, keep it up dude!
http://iamferris.com/
http://youth-uprising.com/

Where the fun's at.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: My First FB Program :D
« Reply #11 on: May 10, 2007 »
Quote
Okay I have given up learning C++ with Visual studio its simply to finicky for my taste
That's a real shame.  If you stick with it you'll realise how powerful it is, and why it's the industry standard tool for Windows software development...

Nevertheless, Freebasic is pretty good for demos, and this is a really nice first effort.  K+.

Jim
Challenge Trophies Won: