Computer Science Canada

Real-time delaying

Author:  Raknarg [ Thu Dec 22, 2011 8:48 pm ]
Post subject:  Real-time delaying

Is there a way to make it so that two computers can run an application on turing at roughly the same speed? I made a program on this computer and it was fine, and then I tried it on a different one and it was way faster. I know part of it is the speed at which it reads the code, but could it also be the length of time it uses the delay function? Like would two computers run delay (1000) at different speeds, or does it use the internal clock?

Author:  Tony [ Thu Dec 22, 2011 8:51 pm ]
Post subject:  RE:Real-time delaying

you can easily test this out by delaying for a few seconds and timing it yourself, to see how accurate that is.

For framerate control (the problem you are having), Time.DelaySinceLast is the easiest way to go about it.

Author:  evildaddy911 [ Fri Dec 23, 2011 12:25 pm ]
Post subject:  RE:Real-time delaying

Time.DelaySinceLast only really works if both computers can run the program without more delay than your function causes

Author:  Aange10 [ Fri Dec 23, 2011 2:42 pm ]
Post subject:  RE:Real-time delaying

Untrue. That's what Delay () is. Time.DelaySinceLast delays shorter if it takes the computer longer to execute the other functions. If my Time.DelaySinceLast is 300 ms, and it takes my computer 50 ms to complete the functions, it will delay me for 250ms. However, if my computer takes 200 ms to execute the functions, it will only delay me for 100 ms. But, at both senarios, it is taking each computer the same amount of time to finish the execution (300 ms).

Now this obviously doesn't fix the problem if it takes the computer 500ms to execute. But it would help drastically. Instead of being 750ms difference in speed, it'd be a 200ms difference. That's nearly, what, 4x?

Author:  mirhagk [ Fri Dec 23, 2011 3:27 pm ]
Post subject:  RE:Real-time delaying

@aange, I believe what evildaddy meant was that if the computer can't run the function in less than the delay specified.

Author:  Aange10 [ Fri Dec 23, 2011 4:18 pm ]
Post subject:  RE:Real-time delaying

Ahh. I must have misunderstood.

Author:  copthesaint [ Sun Dec 25, 2011 4:28 pm ]
Post subject:  RE:Real-time delaying

For real distance drawing try this

Turing:
View.Set ("graphics:800;600,offscreenonly,nobuttonbar,title:Real-Time Drawing")

const EXPECTEDFPS : int := 30
const Velocity : real := 4
type XY :
    record
        x : real
        y : real
    end record
var dimension : flexible array 0 .. -1 of XY
var boundSize : int := -1

proc createAndShowBox (mouseX : int, mouseY : int)
    boundSize := boundSize + 1
    if upper (dimension) < boundSize then
        new dimension, boundSize
    end if
    dimension (boundSize).x := mouseX
    dimension (boundSize).y := mouseY
    drawfillbox (round (dimension (boundSize).x), round (dimension (boundSize).y), round (dimension (boundSize).x + 2), round (dimension (boundSize).y + 2), blue)
end createAndShowBox

proc checkAndUpdateBox (idx : int)
    var timeDelay : int := Time.ElapsedCPU
    if dimension (idx).x > maxx or dimension (idx).x < 0 or dimension (idx).y > maxy or dimension (idx).y < 0 then
        dimension (idx).x := dimension (boundSize).x
        dimension (idx).y := dimension (boundSize).y
        boundSize := boundSize - 1
    end if
    drawfillbox (round (dimension (idx).x), round (dimension (idx).y), round (dimension (idx).x + 2), round (dimension (idx).y + 2), blue)
end checkAndUpdateBox

proc updateVelBox (idx : int, lastTimeSet, firstTimeSet : int)
    dimension (idx).x := dimension (idx).x + ((Velocity * EXPECTEDFPS) / (1000 / (firstTimeSet - lastTimeSet + 0.00001))) %cannot be divisable by 0
    %expected pixels per second = velocity * EXPECTEDFPS
    %Frames per second = 1000 (how many ms in a second) / firstTimeSet (time set after drawing) - lastTimeSet (time before drawing) + 0.00001 (proves != 0)
    %distance = pixels per second / frames per second
end updateVelBox

var mX, mY, mB : int
var timeSetAfter : int := Time.ElapsedCPU
var timeSetBefore : int := Time.ElapsedCPU
loop
    mousewhere (mX, mY, mB)
    for i : 0 .. boundSize
        checkAndUpdateBox (i)
    end for
    if mB = 1 then
        createAndShowBox (mX, mY)
    end if
    timeSetAfter := Time.ElapsedCPU % placed after drawing items to screen
    for i : 0 .. boundSize
        updateVelBox (i, timeSetBefore, timeSetAfter)
    end for
    timeSetBefore := Time.ElapsedCPU     %Placed before drawing items to the screen

    View.Update
    cls
end loop


This will generally draw things at the same speeds. the only problem is that with all the math done by the program, frames may end up skipping every once and a while.

Author:  Raknarg [ Sun Dec 25, 2011 9:16 pm ]
Post subject:  RE:Real-time delaying

Haha thatnks guys, i appreciate the suggestions. In the end, I tested it out on his computer and then changed the delay settings instead Razz


: