EDIT: I fixed a small typo that made a miniscule difference in the limiting code
I've got a couple snippets of code.. They work pretty well. Checks for vsync once, and holds to a given refresh rate.
There's still some snippets in the "tinyptclib.bi" file that are under construction =)
Hopefully, my efforts may help someone
In this file you have to change vsyncrate to your monitor's refresh rate
#Define PTC_WIN
#include "TinyPTCLib.bi"
#include "RAnd.bi"
SeedRnd timer
Graphics 640,480,"Test"
dim inky as string
dim mytime as double
dim ttime as double
dim timeval as double
dim ctime as double
dim diff as double
dim am as double
dim sttime as double
dim vsyncrate as double
dim blah as string
'Set vsync rate
vsyncrate = 75
timeval = (1000.0/vsyncrate)/1000.0
WAIT &h3da, 8
mytime = timer
sttime = timer
do
inky=inkey$
'WAIT &H3DA, 8
FlipSurf
ClsSurf
ColorSet rand(0,255),rand(0,255),rand(0,255)
dim x as integer
dim y as integer
dim i as integer
'for i=0 to rand(rand(1,15),rand(1,15))
for x=0 to GFXWID-1
for y=0 to GFXHT-1
plot(x,y)
next
next
'next
'buffer[5*GFXWID+5] = &hFFFFFF
ttime = timer-mytime
ctime = timer'ttime+mytime
if ttime < timeval then
diff = timeval-ttime
'print diff
do
loop until timer-ctime >= diff-(timeval/1000.0) or asc(inky) = 27
elseif ttime > timeval then
diff = ttime-timeval
'ctime = timer
am = ((cint(diff / timeval)+1)*timeval)-diff
do
loop until timer-ctime >= am or asc(inky) = 27
endif
ttime = timer-mytime
mytime = timer
print 1.0/ttime
'print (mytime-sttime)/timeval
loop until asc(inky) = 27
tinyptclib.bi
#Include "tinyptc.bi"
'TinyPTC Graphics Interface Lib
'Copyright Chris Gaiter 2006
'
'
'This Library takes the basic functionality of TinyPTC and allows more advanced
'drawing operations with simplified commands. Commands such as Graphics,
'FlipSurf, ClsSurf, Plot, ColorSet, DrawLine, and Oval (Not Yet implemented)
'
'
'
'Command reference is coming soon, until then, commands should be fairly simply
'Understood. Redirect any questions either to "Http://www.dbfinteractive.com"
'Or to blitzamateur@yahoo[dot]com
Dim shared as integer ptr buffer
dim shared as integer GFXWID, GFXHT, GFXWIDH, GFXHTH, CurColor, CLEARCOUNT
declare sub Graphics(ByVal wid as integer, ByVal ht as integer, Byval appname as string)
declare sub FlipSurf()
declare sub ClsSurf()
declare sub Plot(ByVal x as integer, ByVal y as integer)
declare sub ColorSet(ByVal r as integer, ByVal g as integer, ByVal b as integer)
declare sub DrawLine(ByVal x1 as single, ByVal y1 as single, ByVal x2 as single, ByVal y2 as single)
declare sub Oval(ByVal x as single, ByVal y as single, ByVal w as single, ByVal h as single)
declare function GetVsyncRate()
Sub Graphics(ByVal Wid as integer, ByVal Ht as integer, ByVal AppName as string="FB w/ TinyPTC")
if ptc_open(AppName, Wid, Ht) = 0 then
print "Cannot open graphics display at "+str(wid)+", "+str(ht)
sleep
end -1
endif
buffer = callocate(wid*ht, Len(integer))
CLEARCOUNT = wid*ht*Len(integer)
GFXWID = Wid
GFXHT = Ht
GFXWIDH = GFXWID shr 1
GFXHTH = GFXHT shr 1
End Sub
Sub FlipSurf()
ptc_update(buffer)
End Sub
Sub ClsSurf()
clear *buffer, 0, CLEARCOUNT
End Sub
Sub Plot(ByVal x as integer, ByVal y as integer)
buffer[(y*GFXWID+x)] = CurColor
End Sub
Sub ColorSet(ByVal r as integer, ByVal g as integer, ByVal b as integer)
CurColor = ( r shl 16 ) + ( g shl 8 ) + b
End Sub
Sub Oval (ByVal x as single, ByVal y as single, ByVal w as single, ByVal h as single)
End Sub
Sub DrawLine(ByVal x1 as single,ByVal y1 as single,ByVal x2 as single,ByVal y2 as single)
dim temp as single
dim xd as double
dim yd as double
dim y as double
dim x as double
dim xs as double
dim ys as double
if x2 < x1 then
xd = abs(x1-x2)
else
xd = abs(x2-x1)
endif
if y2 < y1 then
yd = abs(y1-y2)
else
yd = abs(y2-y1)
endif
if xd > yd then
if x2 < x1 then
temp = x1
x1 = x2
x2 = temp
temp = y1
y1 = y2
y2 = temp
endif
xd = abs(x2-x1)
yd = abs(y2-y1)
ys = yd/xd
y=y1
for x=x1 to x2
buffer[cint(y+.49)*GFXWID+cint(x+.49)] = CurColor
y=y+ys
next
else
if y2 < y1 then
temp = x1
x1 = x2
x2 = temp
temp = y1
y1 = y2
y2 = temp
endif
xd = abs(x2-x1)
yd = abs(y2-y1)
xs = xd/yd
x=x1
for y=y1 to y2
buffer[cint(y+.49)*GFXWID+cint(x+.49)] = CurColor
x = x + xs
next
endif
end sub
function getvsyncrate()
dim ttime as double
dim count as integer
ttime = timer
count = 0
if WAIT(&h3da, 8)=0 then ttime = timer
do
WAIT(&h3da, 8)
'out(&h3da, 0)
count = count + 1
print count
print count
loop until timer-ttime >= 1.0
return count-1
end function
Rand.bi
declare function Rand(ByVal lower as integer, byVal upper as integer)
declare function FRand(ByVal lower as double, byVal upper as double) as double
Declare sub SeedRnd(ByVal seed as double)
sub SeedRnd(byval seed as double)
randomize seed
end sub
function Rand(ByVal lower as integer, ByVal upper as integer)
dim temp as integer
if upper < lower then
temp=upper
upper=lower
lower=temp
endif
dim value as integer
dim dist as integer
value=lower
dist = abs(lower-upper)
return (rnd(1)*dist) + value
End function
function FRand(ByVal lower as double, ByVal upper as double) as double
dim temp as double
if upper < lower then
temp=upper
upper=lower
lower=temp
endif
dim value as double
dim dist as double
value=lower
dist = abs(lower-upper)
return (rnd(1)*dist) + value
End function