| 1 |
# updateloop.nas - Generic Nasal update loop for implementing sytems, |
| 2 |
# instruments or physics simulation. |
| 3 |
# |
| 4 |
# Copyright (C) 2014 Anton Gomez Alvedro |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or |
| 7 |
# modify it under the terms of the GNU General Public License as |
| 8 |
# published by the Free Software Foundation; either version 2 of the |
| 9 |
# License, or (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, but |
| 12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
# General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, write to the Free Software |
| 18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 |
|
| 20 |
|
| 21 |
## |
| 22 |
# The UpdateLoop accepts a vector of Updatable components that it will call |
| 23 |
# at regular intervals. In order to define custom objects to be controlled by |
| 24 |
# the loop, the Updatable interface must be implemented: |
| 25 |
# |
| 26 |
# var MyComponent = { |
| 27 |
# parents: [Updatable], |
| 28 |
# |
| 29 |
# reset: func { |
| 30 |
# ... |
| 31 |
# }, |
| 32 |
# update: func(dt) { |
| 33 |
# ... |
| 34 |
# }, |
| 35 |
# ... |
| 36 |
# }; |
| 37 |
|
| 38 |
var Updatable = { |
| 39 |
|
| 40 |
## |
| 41 |
# When reset() is called, the component shall clean all internal state and |
| 42 |
# reinitialize itself as if starting from scratch. |
| 43 |
# |
| 44 |
# Reset will be called automatically after a teleport, and then on demand |
| 45 |
# when UpdateLoop.reset() is explicitly called. |
| 46 |
|
| 47 |
reset: func { }, |
| 48 |
|
| 49 |
## |
| 50 |
# Called at regular intervals from the UdateLoop. |
| 51 |
# dt: Elapsed time in seconds since the last call. |
| 52 |
|
| 53 |
update: func(dt) { }, |
| 54 |
}; |
| 55 |
|
| 56 |
## |
| 57 |
# Wraps a set of user provided Updatable objects and calls them periodically. |
| 58 |
# Takes care of critical sim signals (reinit, fdm-initialized, speed-up). |
| 59 |
|
| 60 |
var UpdateLoop = { |
| 61 |
|
| 62 |
# UpdateLoop.new(components, [update_period], [enable]) |
| 63 |
# |
| 64 |
# components: Vector of components to update on every iteration. |
| 65 |
# update_period: Time in seconds between updates. |
| 66 |
# enable: Enable the loop immediately after creation. |
| 67 |
# ignore_fdm: Do not wait for the fdm to be ready for starting the loop. |
| 68 |
|
| 69 |
new: func(components, update_period = 0, enable = 1, ignore_fdm = 0) { |
| 70 |
|
| 71 |
var m = { parents: [UpdateLoop] }; |
| 72 |
m.initialized = 0; |
| 73 |
m.enabled = enable; |
| 74 |
m.ignore_fdm = ignore_fdm; |
| 75 |
m.update_period = update_period; |
| 76 |
m.time_last = 0; |
| 77 |
m.sim_speed = 1; |
| 78 |
m.components = (components != nil)? components : []; |
| 79 |
|
| 80 |
m.timer = maketimer(update_period, |
| 81 |
func { call(me._update, [], m) }); |
| 82 |
|
| 83 |
m.lst = []; |
| 84 |
|
| 85 |
append(m.lst, setlistener("/sim/speed-up", |
| 86 |
func(n) { m.sim_speed = n.getValue() }, 1, 0)); |
| 87 |
|
| 88 |
append(m.lst, setlistener("sim/signals/reinit", |
| 89 |
func(n) { m._on_teleport(n) })); |
| 90 |
|
| 91 |
if (ignore_fdm or getprop("sim/signals/fdm-initialized")) { |
| 92 |
m.reset(); |
| 93 |
enable and m.timer.start(); |
| 94 |
} |
| 95 |
|
| 96 |
if (!ignore_fdm) { |
| 97 |
append(m.lst, setlistener("sim/signals/fdm-initialized", |
| 98 |
func(n) { m._on_teleport(n) })); |
| 99 |
} |
| 100 |
|
| 101 |
return m; |
| 102 |
}, |
| 103 |
|
| 104 |
del: func { |
| 105 |
me.disable(); |
| 106 |
foreach (var l; me.lst) removelistener(l); |
| 107 |
}, |
| 108 |
|
| 109 |
## |
| 110 |
# Resets internal state for all components controlled by the loop. |
| 111 |
# It is of course responsibility of every component to clean their internal |
| 112 |
# state appropriately. |
| 113 |
|
| 114 |
reset: func { |
| 115 |
me.time_last = getprop("/sim/time/elapsed-sec"); |
| 116 |
|
| 117 |
foreach (var component; me.components) |
| 118 |
component.reset(); |
| 119 |
|
| 120 |
me.initialized = 1; |
| 121 |
}, |
| 122 |
|
| 123 |
## |
| 124 |
# The loop will start updating the components under its control. |
| 125 |
|
| 126 |
enable: func { |
| 127 |
if (me.initialized) me.timer.start(); |
| 128 |
me.enabled = 1; |
| 129 |
}, |
| 130 |
|
| 131 |
## |
| 132 |
# The loop will freeze and its components will not get updates until |
| 133 |
# enabled again. |
| 134 |
|
| 135 |
disable: func { |
| 136 |
me.timer.stop(); |
| 137 |
me.enabled = 0; |
| 138 |
}, |
| 139 |
|
| 140 |
_update: func { |
| 141 |
var time_now = getprop("/sim/time/elapsed-sec"); |
| 142 |
var dt = (time_now - me.time_last) * me.sim_speed; |
| 143 |
if (dt == 0) return; |
| 144 |
|
| 145 |
me.time_last = time_now; |
| 146 |
|
| 147 |
foreach (var component; me.components) |
| 148 |
component.update(dt); |
| 149 |
}, |
| 150 |
|
| 151 |
_on_teleport: func(n) { |
| 152 |
var signal = n.getName(); |
| 153 |
|
| 154 |
if (signal == "reinit" and n.getValue()) { |
| 155 |
me.timer.stop(); |
| 156 |
me.initialized = 0; |
| 157 |
} |
| 158 |
elsif (me.ignore_fdm or signal == "fdm-initialized") { |
| 159 |
me.timer.isRunning and me.timer.stop(); |
| 160 |
me.reset(); |
| 161 |
me.enabled and me.timer.start(); |
| 162 |
} |
| 163 |
} |
| 164 |
}; |