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)
'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