New in STYX 1.10 are the commands SET INTERVAL SYNC ON and SET INTERVAL SYNC OFF. The latter allows you to run Intervals* independently from the main loop. That means that you can do time intensive calculations while there's still something going on in the actual game.
In the example "Intervals2.exe" there's a rotating cube while DBPro does 6,000,000 square root calculations at the same time. Additionally it updates the clock display every second.
The example "No Intervals.exe" shows an implemenation without intervals.
Source code "Intervals2.exe"
remstart
STYX v1.10
This example shows how to call two functions that run independently
from the main loop in the background (threads).
The Calc() function does 6,000,000 square root calculation at once
while the main loop rotates the cube. At the same time a clock function
is called every second to update the current time on the screen.
Use the space key to toggle the calculation on and off and see
how much it affect the FPS.
remend
rem some global variables that we need to interchange data between
rem the Intervals and the main loop.
Global CalcIteration = 0
Global CalcTime = 0
Global Clock$ = ""
rem global variable that stores whether calculations take place
Global DoCalcs = 1
rem Register our two intervals (see help file on MAKE INTERVAL)
Register_Calc()
Register_Clock()
rem and start the first interval
rem the first parameter is the Interval Number
rem the second the Delay (time in ms before the Interval starts)
rem the last parameter is the number of repeats (0=infinite repeats)
Start Interval 1, 0, 1
rem set the interval synchonization off so that it runs independantly
Set Interval Sync Off 1
rem and start the second interval
Start Interval 2, 1000, 0
rem set the interval synchonization off so that it runs independantly
Set Interval Sync Off 2
rem standard 3D setup
Sync On : Sync
rem create an object to show
Make Object Cube 1, 10
rem our main loop
Do
rem rotate our cube
a = Wrapvalue(a+1)
XRotate Object 1, a
YRotate Object 1, a
rem check if spacekey is pressed to turn calcs on or off
If SpaceKey()
if DoCalcs = 1
rem stop our calculation interval
Stop Interval 1
DoCalcs = 0
else
rem start our calculation interval
Start Interval 1, 0, 1
DoCalcs = 1
endif
rem wait till the user has released the spacekey
repeat
until SpaceKey() = 0
endif
rem show some stuff
Text 1, 00, "FPS : " + str$(Screen Fps())
Text 1, 60, "Clock : " + Clock$
Text 1, 100, "Press Space Key to toggle calculations"
if DoCalcs = 1
Text 1, 20, "Calculations: " + str$(CalcIteration)
Text 1, 40, "Time (ms) : " + str$(CalcTime)
else
Text 1, 20, "Calculations: Calculation Stopped"
Text 1, 40, "Time (ms) : Calculation Stopped"
endif
Sync
Loop
rem our register functions
Function Register_Calc()
Make Interval 1 : Calc()
Endfunction
function Register_Clock()
Make Interval 2 : Clock()
Endfunction
rem the actual calc function
Function Calc()
rem We can check if this function call comes from our register function
rem with INTERVAL REG CALL and exit the function.
If Interval Reg Call(1) Then ExitFunction
Do
t = Timer()
for i = 1 to 6000000
x = sqrt(i)
next i
inc CalcIteration
CalcTime = timer() - t
Loop
Endfunction
function Clock()
If Interval Reg Call(2) Then ExitFunction
Clock$ = Get Time$()
endfunction
Source code "No Intervals.exe"
Global CalcIteration = 0
Global CalcTime = 0
Global Clock$ = ""
Global DoCalcs = 1
rem standard 3D setup
Sync On : Sync
rem create an object to show
Make Object Cube 1, 10
t = timer()
rem our main loop
Do
rem rotate our cube
a = Wrapvalue(a+1)
XRotate Object 1, a
YRotate Object 1, a
if timer()-t >= 1000
t = timer()
Clock()
endif
rem check if spacekey is pressed to turn calcs on or off
If SpaceKey()
if DoCalcs = 1
DoCalcs = 0
else
DoCalcs = 1
endif
rem wait till the user has released the spacekey
repeat
until SpaceKey() = 0
endif
if DoCalcs then Calc()
rem show some stuff
Text 1, 00, "FPS : " + str$(Screen Fps())
Text 1, 60, "Clock : " + Clock$
Text 1, 100, "Press Space Key to toggle calculations"
if DoCalcs = 1
Text 1, 20, "Calculations: " + str$(CalcIteration)
Text 1, 40, "Time (ms) : " + str$(CalcTime)
else
Text 1, 20, "Calculations: Calculation Stopped"
Text 1, 40, "Time (ms) : Calculation Stopped"
endif
Sync
Loop
Function Calc()
t = Timer()
for i = 1 to 6000000
x = sqrt(i)
next i
inc CalcIteration
CalcTime = timer() - t
Endfunction
function Clock()
Clock$ = Get Time$()
endfunction
* Intervals is a command set in STYX that allow you to automatically call DBPro functions in definable intervals for a given number of times.