annotate mde/scheduler/Scheduler.d @ 101:71f0f1f83620

Some path adjustments for windows (untested) and fonts. All types of option can be edited. paths: support for getting the full path for a font when just the file name is entered, in order to unify usage on windows and linux. paths: Used getSpecialPath for some windows paths; needs testing. Content: Moved line-editing code to abstract ValueContent class and added some conversion functions, so that any type of ValueContent can be edited as text.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sun, 16 Nov 2008 17:03:47 +0000
parents 2a364c7d82c9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
1 /* LICENSE BLOCK
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
2 Part of mde: a Modular D game-oriented Engine
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
3 Copyright © 2007-2008 Diggory Hardy
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
4
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify it under the terms
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
6 of the GNU General Public License as published by the Free Software Foundation, either
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
7 version 2 of the License, or (at your option) any later version.
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
8
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
11 See the GNU General Public License for more details.
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
12
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
13 You should have received a copy of the GNU General Public License
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
15
63
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 39
diff changeset
16 /** A fairly generic scheduler.
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 39
diff changeset
17 *
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 39
diff changeset
18 * This class implements most functionality a generic scheduler might want, however currently it
66d555da083e Moved many modules/packages to better reflect usage.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 39
diff changeset
19 * doesn't any uses where equivalent functionality couldn't be achived very easily anyway. */
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
20 module mde.scheduler.Scheduler;
95
2a364c7d82c9 Boolean options can be adjusted from the gui now (using a very basic widget). Also some bug-fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 85
diff changeset
21 import mde.util;
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
22
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
23 public import tango.time.Time;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
24
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
25 debug {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
26 import tango.util.log.Log : Log, Logger;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
27 private Logger logger;
101
71f0f1f83620 Some path adjustments for windows (untested) and fonts. All types of option can be edited.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 95
diff changeset
28 static this() {
71f0f1f83620 Some path adjustments for windows (untested) and fonts. All types of option can be edited.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 95
diff changeset
29 logger = Log.getLogger ("mde.scheduler.Scheduler");
71f0f1f83620 Some path adjustments for windows (untested) and fonts. All types of option can be edited.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 95
diff changeset
30 }
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
31 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
32
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
33 /// This class can run scheduled functions per frame, per time interval and per request.
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
34 class Scheduler
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
35 {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
36 /** The type of function callback to be added to the scheduler.
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
37 *
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
38 * The parameter time gives the time since the function was last called, or zero on the first
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
39 * run. */
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
40 alias void function (TimeSpan time) scheduleFct;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
41 alias void delegate (TimeSpan time) scheduleDlg; /// ditto
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
42
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
43 alias uint ID; /// This is the type of identifier used by add/get/remove/request
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
44
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
45 /** The struct used to store the function and scheduling information */
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
46 class ScheduleFunc {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
47 /// Set the function this represents
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
48 this (scheduleDlg f) {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
49 fct = f;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
50 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
51
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
52 /** Quick way to set scheduling.
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
53 *
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
54 * Function will be scheduled each _frame if frame is true, or when requested, or each
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
55 * interval if interval is positive. */
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
56 ScheduleFunc set (bool frame, TimeSpan interval) {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
57 this.frame = frame;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
58 if (interval < TimeSpan.zero) interval = TimeSpan.zero;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
59 this.interval = interval;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
60
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
61 return this;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
62 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
63
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
64 bool frame; /// Call function each time execute() runs
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
65 bool request; /// Call function the next time execute() runs
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
66 TimeSpan interval; /// Call the function when the last call was longer than interval ago
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
67
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
68 package:
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
69 scheduleDlg fct = null; // function to call
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
70 Time lastCall = zero; // time of last call; zero at start (special case)
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
71 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
72
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
73 /** Add a function to be scheduled.
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
74 *
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
75 * This function should have a unique identifier, which can be used with get/remove/request.
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
76 * The identifier can be supplied or generated by getNewID().
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
77 *
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
78 * Use the returned pointer to set the scheduling, e.g.:
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
79 * -----
85
56c0ddd90193 Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
80 * scheduler.add(scheduler.getNewID, myFunction).set(false, TimeSpan.fromMillis (10));
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
81 * scheduler.get(15).frame = true;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
82 */
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
83 ScheduleFunc add (ID id, scheduleFct func) {
95
2a364c7d82c9 Boolean options can be adjusted from the gui now (using a very basic widget). Also some bug-fixes.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 85
diff changeset
84 return add (id, toDg(func));
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
85 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
86 /** ditto */
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
87 ScheduleFunc add (ID id, scheduleDlg func)
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
88 in {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
89 debug if ((id in funcs) !is null) logger.error ("Duplicate ID used!");
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
90 } body {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
91 ScheduleFunc sf = new ScheduleFunc (func);
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
92 funcs[id] = sf; // add
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
93 return sf; // and return for chain-calling
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
94 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
95
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
96 /** Get function with ID id. */
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
97 ScheduleFunc get (ID id) {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
98 try {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
99 return funcs[id];
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
100 } catch (Exception) {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
101 debug logger.error ("get(): ID does not exist!");
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
102 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
103 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
104
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
105 /** Remove function with ID id. */
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
106 void remove (ID id) {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
107 try {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
108 funcs.remove(id);
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
109 } catch (Exception) {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
110 debug logger.error ("remove(): ID does not exist!");
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
111 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
112 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
113
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
114 /** Request that function with ID id is called next time execute() runs. */
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
115 void request (ID id) {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
116 get(id).request = true;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
117 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
118
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
119 /** Generate an ID. All generated IDs are >= 0xF000_0000 to provide plenty of room for other
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
120 * IDs. */
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
121 ID getNewID () {
39
5132301e9ed7 Implemented widget saving.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 30
diff changeset
122 if (funcs.length == 0) return 0xF000_0000; // otherwise would get an out-of-bounds error
5132301e9ed7 Implemented widget saving.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 30
diff changeset
123 // Take the last used ID and add one, making sure it's at least 0xF000_0000.
5132301e9ed7 Implemented widget saving.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 30
diff changeset
124 // Don't bother checking if it's out of bounds since there's 2^28 available IDs.
5132301e9ed7 Implemented widget saving.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 30
diff changeset
125 ID i = funcs.keys[$-1] + 1;
5132301e9ed7 Implemented widget saving.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 30
diff changeset
126 if (i < 0xF000_0000) i = 0xF000_0000;
5132301e9ed7 Implemented widget saving.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 30
diff changeset
127 return i;
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
128 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
129
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
130 /** This function should get called by the main loop, once per frame.
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
131 *
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
132 * Params:
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
133 * time = the current sim-time (tango.time.Time.Time); all time evaluations will use this
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
134 * all = skip normal tests and call all functions (still cancelling requests and updating call
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
135 * times for correct running next time execute() is called with all = false)
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
136 */
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
137 void execute (Time time, bool all = false) {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
138 foreach (func; funcs) {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
139 // The interval since the function was last run. In order to be correct for more
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
140 // complex cases, it must be calculated per function.
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
141 TimeSpan interval;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
142
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
143 // Per frame/request:
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
144 if (func.frame || func.request || all) {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
145 if (func.lastCall == zero) interval = TimeSpan.zero; // first call
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
146 else interval = (time - func.lastCall);
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
147 func.fct (interval); // call
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
148
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
149 func.request = false; // cancel regardless of last value
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
150 func.lastCall = time;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
151 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
152 // Per-interval functions:
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
153 else if ((func.interval != TimeSpan.zero) && // has a per-interval schedule
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
154 (time >= (func.lastCall + func.interval))) // time to call again
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
155 {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
156 if (func.lastCall == zero) interval = TimeSpan.zero; // first call
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
157 else interval = (time - func.lastCall);
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
158 func.fct (interval); // call
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
159
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
160 func.lastCall = time;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
161 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
162 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
163 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
164
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
165 private:
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
166 static const Time zero = Time(0L);
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
167 Time lastTime = zero;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
168 ScheduleFunc[ID] funcs;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
169
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
170 debug (mdeUnitTest) unittest {
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
171 Scheduler s = new Scheduler;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
172
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
173 int ctr1 = 0;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
174 void inc1 (TimeSpan) { ++ctr1; }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
175 s.add(1,&inc1).frame = true;
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
176
85
56c0ddd90193 Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
177 TimeSpan interval = TimeSpan.fromMillis(1);// non-zero so we can check zero after first call
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
178 void perInt (TimeSpan i) { interval = i; }
85
56c0ddd90193 Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
179 s.add(2,&perInt).set(false, TimeSpan.fromMillis(10));
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
180
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
181 Time t = Time.epoch1970; // starting time (value isn't important)
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
182 s.execute (t);
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
183 assert (ctr1 == 1); // called once
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
184 assert (interval == TimeSpan.zero); // initial interval
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
185
85
56c0ddd90193 Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
186 t += TimeSpan.fromMillis (5); // 5ms later...
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
187 s.execute (t);
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
188 assert (ctr1 == 2);
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
189 assert (interval == TimeSpan.zero); // perInt shouldn't get called
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
190
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
191 s.get(1).frame = false; // don't call per-frame anymore
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
192 s.get(1).request = true; // but request next call
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
193
85
56c0ddd90193 Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
194 t += TimeSpan.fromMillis (5);
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
195 s.execute (t);
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
196 assert (ctr1 == 3); // as requested
85
56c0ddd90193 Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
197 assert (interval == TimeSpan.fromMillis (10)); // perInt should get called (just!)
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
198
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
199 s.request(2); // request this
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
200
85
56c0ddd90193 Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
201 t += TimeSpan.fromMillis (8);
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
202 s.execute (t);
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
203 assert (ctr1 == 3); // inc1 shouldn't run
85
56c0ddd90193 Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
204 assert (interval == TimeSpan.fromMillis (8)); // perInt was requested
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
205
85
56c0ddd90193 Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
206 t += TimeSpan.fromMillis (4);
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
207 s.execute (t);
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
208 // check perInt's last call-time was updated by the request, so it doesn't get run now:
85
56c0ddd90193 Intermediate commit (not stable). Changes to init system.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 63
diff changeset
209 assert (interval == TimeSpan.fromMillis (8));
30
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
210
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
211 logger.info ("Unittest complete.");
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
212 }
467c74d4804d Major changes to the scheduler, previously only used by the main loop.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
213 }