Learning some powerbasic does no harm I suppose.
Although, converting simple code from freebasic to powerbasic is very easy.
I suppose they both stem from the old quickbasic anyway.
For anybody interested I have created the graphics dll (originally for C++) for use with powerbasic.
one.bas for the dll
Code: Select all
'one.bas --- a dll for powerbasic or FreeBASIC
'compile with -dll
Sub screen1 alias "screen1"(Byval X As integer, Byval Y As integer,xres as integer,yres as integer,b as integer) EXPORT
Screenres xres,yres,b
SCREENCONTROL(100, X, Y)
End Sub
sub clearcolor alias "clearcolor"(c as ulong) export
color ,c
cls
end sub
function rgbcolor alias "rgbcolor"(r as long,g as long,b as long) as ulong export
return rgb(r,g,b)
end function
Sub drawstring alias "drawstring"(x as long,y as long, text As zstring ptr,c as ulong) EXPORT
draw string (x,y),*text,c
End Sub
sub printdbl alias "printdbl"(n as double,flag as long=0) export
select case as const flag
case 0:print n
case 1:print n,
case 2:print n;
case else
print n
end select
end sub
sub line1 alias "line1"(x1 as long,y1 as long,x2 as long,y2 as long,c as ulong) EXPORT
line (x1,y1)-(x2,y2),c
end sub
sub sleep1 alias "sleep1"(t as long) export
sleep t
end sub
sub waitkey alias "waitkey" export
sleep
end sub
function randoms alias "randoms"(n as long) as double export
return rnd*n
end function
sub circlefill1 alias "circlefill1"(x1 as long,_
y1 as long,_
rad as long,_
c as ulong) export
circle (x1,y1),rad,c,,,,f
end sub
sub circle1 alias "circle1"(x1 as long,_
y1 as long,_
rad as long,_
c as ulong) export
circle (x1,y1),rad,c
end sub
sub pset1 alias "pset1"(x1 as double,y1 as double,c as ulong,im as any pointer=0) EXPORT
pset im,(x1,y1),c
end sub
sub clearscreen1 alias "clearscreen1" export
cls
end sub
sub endkey1 alias "endkey1"export
if inkey=chr(27) then end
end sub
sub screenlock1 alias "screenlock1"export
screenlock
end sub
sub screenunlock1 alias "screenunlock1" export
screenunlock
end sub
sub getmouse1 alias "getmouse1"( mx as long ptr, my as long ptr, mw as long ptr, mb as long ptr) export
getmouse(*mx,*my,*mw,*mb)
end sub
sub locate1 alias "locate1"(x as long,y as long) export
locate x,y
end sub
Function framecounter alias "framecounter" As long export
dim as double t2=timer
Static As Double t3,frames,answer
frames=frames+1
If (t2-t3)>=1 Then
t3=t2
answer=frames
frames=0
End If
Return answer
End Function
Function regulate alias "regulate"( MyFps As long, fps As long ptr) As long export
Static As Double timervalue,lastsleeptime,t3,frames
frames+=1
If (Timer-t3)>=1 Then t3=Timer:*fps=frames:frames=0
Var sleeptime=lastsleeptime+((1/myfps)-Timer+timervalue)*1000
If sleeptime<1 Then sleeptime=1
lastsleeptime=sleeptime
timervalue=Timer
Return sleeptime
End Function
And the powerbasic code:
Code: Select all
#Compile Exe
#Dim All
Declare Sub screen1 StdCall Lib "one.dll" Alias "screen1@20"(ByVal x As Integer, ByVal y As Integer, ByVal xres As Integer, ByVal yres As Integer, ByVal b As Integer)
Declare Sub drawstring StdCall Lib "one.dll" Alias "drawstring@16"(ByVal x As Long, ByVal y As Long, ByVal TEXT As String Pointer, ByVal c As Long)
Declare Sub printdbl StdCall Lib "one.dll" Alias "printdbl@12"(ByVal n As Double, ByVal flag As Long)
Declare Sub circlefill1 StdCall Lib "one.dll" Alias "circlefill1@16"(ByVal x1 As Long, ByVal y1 As Long, ByVal rad As Long, ByVal c As Long)
Declare Sub circle1 StdCall Lib "one.dll" Alias "circle1@16"(ByVal x1 As Long, ByVal y1 As Long, ByVal rad As Long, ByVal c As Long)
Declare Function rgbcolor StdCall Lib "one.dll" Alias "rgbcolor@12"(ByVal r As Long, ByVal g As Long, ByVal b As Long) As Long
Declare Sub screenlock1 StdCall Lib "one.dll" Alias "screenlock1@0"()
Declare Sub screenunlock1 StdCall Lib "one.dll" Alias "screenunlock1@0"
Declare Sub sleep1 StdCall Lib "one.dll" Alias "sleep1@4"(ByVal n As Long)
Declare Function randoms StdCall Lib "one.dll" Alias "randoms@16"(ByVal n As Long) As Double
Declare Sub clearscreen1 StdCall Lib "one.dll" Alias "clearscreen1@0"
Declare Sub endkey1 StdCall Lib "one.dll" Alias "endkey1@0"
Declare Sub locate1 StdCall Lib "one.dll" Alias "locate1@8"(ByVal x As Long, ByVal y As Long)
'declare sub getmouse1 StdCall Lib "one.dll" Alias "getmouse1@16"(byval mx as long ptr, byval my as long ptr, byval mw as long ptr, byval mb as long ptr)
Declare Function regulate StdCall Lib "one.dll" Alias "regulate@8"(ByVal myfps As Long, ByVal fps As Long Ptr) As Long
Declare Sub clearcolor StdCall Lib "one.dll" Alias "clearcolor@4"(ByVal c As Long)
'end extern
Type ball
x As Single
y As Single
dx As Single
dy As Single
col As Long
End Type
Sub move_(b() As ball)
Dim n As Integer
For n =0 To 2
b(n).x=b(n).x+b(n).dx:b(n).y=b(n).y+b(n).dy
Next n
End Sub
Sub edges(b() As ball )
Dim r As Long
r=50
Dim n As Integer
For n =0 To 2
If(b(n).x<r) Then b(n).dx=-b(n).dx
If(b(n).x>1000-r)Then b(n).dx=-b(n).dx
If(b(n).y<r)Then b(n).dy=-b(n).dy
If(b(n).y>768-r)Then b(n).dy=-b(n).dy
Next n
End Sub
Sub draw_(b() As ball )
Dim n As Integer
For n =0 To 2
circlefill1(b(n).x,b(n).y,50,b(n).col)
circle1(b(n).x,b(n).y,50,rgbcolor(255,255,255))
Next n
End Sub
Sub BallCollisions(b() As ball)
Dim L As Single
Dim impulsex As Single
Dim impulsey As Single
Dim dot As Single
Dim impactx As Single
Dim impacty As Single
Dim n1 As Integer
Dim n2 As Integer
For n1 =0 To 2
For n2 =n1+1 To 2
L=Sqr( (b(n1).x-b(n2).x)*(b(n1).x-b(n2).x) + (b(n1).y-b(n2).y)*(b(n1).y-b(n2).y))
If (L<100) Then
impulsex=(b(n1).x-b(n2).x)/L
impulsey=(b(n1).y-b(n2).y)/L
b(n1).x=b(n2).x+100*impulsex
b(n1).y=b(n2).y+100*impulsey
impactx=b(n1).dx-b(n2).dx
impacty=b(n1).dy-b(n2).dy
dot=impactx*impulsex+impacty*impulsey
b(n1).dx=b(n1).dx-dot*impulsex
b(n1).dy=b(n1).dy-dot*impulsey
b(n2).dx=b(n2).dx+dot*impulsex
b(n2).dy=b(n2).dy+dot*impulsey
End If
Next n2
Next n1
End Sub
Function PBMain () As Long
Dim b(0 To 2) As ball
Dim fps As Long
Dim sleeptime As Long
Dim mx As Long
Dim my As Long
Dim mw As Long
Dim mb As Long
Dim st As String
st=" Press escape key to end"
Dim st2 As String
st2=" Framerate"
b(0).x = 100
b(0).y = 100
b(0).dx = 5.2
b(0).dy = 3.2
b(0).col = rgbcolor(0, 100, 200)
b(1).x = 600
b(1).y = 200
b(1).dx = -5.2
b(1).dy = 3.2
b(1).col = rgbcolor(200, 100, 0)
b(2).x = 700
b(2).y = 500
b(2).dx = -1.2
b(2).dy = 3.2
b(2).col = rgbcolor(0, 200, 0)
screen1(50, 20, 1000, 768, 32)
clearcolor(rgbcolor(0, 0, 100))
While 1
move_(b())
edges(b())
BallCollisions(b())
screenlock1()
clearscreen1()
draw_(b())
drawstring(50, 10, StrPtr(st), rgbcolor(255, 200, 0))
drawstring(0, 55, StrPtr(st2), rgbcolor(0, 200, 0))
locate1(8, 11)
printdbl(fps, 0)
screenunlock1()
sleeptime = regulate(65, VarPtr(fps))
sleep1(sleeptime)
endkey1()
Wend
End Function