Mercurial > projects > mde
annotate mde/scheduler/runTime.d @ 23:47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Improvements to window resizing, and gl draws a box as a test.
Scheduler has "on request" support to redraws only when requested by an event.
committer: Diggory Hardy <diggory.hardy@gmail.com>
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Thu, 27 Mar 2008 10:58:57 +0000 |
parents | 838577503598 |
children | 611f7b9063c6 |
rev | line source |
---|---|
20 | 1 /* LICENSE BLOCK |
2 Part of mde: a Modular D game-oriented Engine | |
3 Copyright © 2007-2008 Diggory Hardy | |
4 | |
5 This program is free software; you can redistribute it and/or modify it under the terms of | |
6 the GNU General Public License, version 2, as published by the Free Software Foundation. | |
7 | |
8 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | |
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
10 See the GNU General Public License for more details. | |
11 | |
12 You should have received a copy of the GNU General Public License along | |
13 with this program; if not, write to the Free Software Foundation, Inc., | |
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ | |
15 | |
16 /** Scheduler | |
17 */ | |
18 module mde.scheduler.runTime; | |
19 | |
20 import tango.time.Time; | |
21 | |
23
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
22 debug { |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
23 import tango.util.log.Log : Log, Logger; |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
24 private Logger logger; |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
25 } |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
26 static this() { |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
27 debug logger = Log.getLogger ("mde.scheduler.runTime"); |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
28 } |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
29 |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
30 /** Some enums used by per request functions. */ |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
31 enum RF_KEYS : uint { DRAW }; |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
32 |
20 | 33 // NOTE: Currently has no support for removing functions. To fix, assign ID and store fct pointers |
34 // in an associative array, returning the ID [on adding fct pointer]. | |
23
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
35 // NOTE: support delegates or not? |
20 | 36 /// This class can run scheduled functions per frame or every t seconds (sim-time). |
37 abstract class Scheduler | |
38 { | |
39 /** The type of function pointer to be passed to the scheduler. | |
40 * | |
41 * The double $(I time) parameter gives the number of (sim) seconds since the function was last | |
42 * called, or zero on the first run. */ | |
43 alias void function (double time) scheduleFct; | |
44 | |
45 /** Add a function to be called per frame. */ | |
46 static void perFrame (scheduleFct fct) { | |
47 frameFcts ~= fct; | |
48 } | |
49 | |
50 /** Add a function to be called per t secs or n 100-nano-sec intevals. | |
51 * | |
52 * Since the scheduler cannot guarantee a maximum time between calls, the interval at which | |
53 * functions are called is always greater than or equal to the inverval specified here. Of | |
54 * course, the actual inteval is given when the function is run. | |
55 */ | |
56 static void perTime (double t, scheduleFct fct) { | |
57 perTime (TimeSpan.interval(t), fct); | |
58 } | |
59 /** ditto */ | |
60 static void perTime (TimeSpan n, scheduleFct fct) | |
61 in { assert (n > TimeSpan (0L)); } | |
62 body { | |
23
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
63 timeFcts ~= new TimeFct (fct, n); |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
64 } |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
65 |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
66 /** Add a function to be called per requested update. |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
67 * |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
68 * A bool parameter is stored locally describing whether or not the function needs recalling, |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
69 * and is set true upon creation and when requestUpdate is called with the same key. The |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
70 * function is then called by scheduler's run() whenever this bool variable is true. |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
71 */ |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
72 static void perRequest (uint key, void function() fct) |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
73 in { |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
74 debug if ((key in requestFcts) is null) |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
75 logger.warn ("perRequest: replacing existing function with same key!"); |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
76 } |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
77 body { |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
78 requestFcts[key] = fct; |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
79 requestFctsUpdate[key] = true; |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
80 } |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
81 |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
82 /** Request an update to request function key. */ |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
83 static void requestUpdate (uint key) { |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
84 // Note: check the value for this key actually exists |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
85 bool* p = key in requestFctsUpdate; |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
86 if (p) *p = true; |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
87 else debug logger.warn ("requestUpdate called with invalid key"); |
20 | 88 } |
89 | |
90 /** This function should get called by the main loop, once per frame. | |
91 * | |
92 * The parameter time should be the current sim-time, using the tango.core.Types.Time enum; all | |
93 * time evaluations will use this. | |
94 */ | |
95 static void run (Time time) { | |
96 double interval; | |
97 | |
98 // Call all per-frame functions: | |
99 if (lastTime == Time (0L)) interval = 0.0; // 0 interval for first loop | |
100 else interval = (time-lastTime).interval(); | |
101 | |
102 foreach (fct; frameFcts) fct(interval); | |
103 | |
104 // Call all per-interval functions: | |
105 foreach (fct; timeFcts) if (time >= fct.nextCall) { | |
106 if (fct.nextCall == Time (0L)) interval = 0.0; // 0 interval for first call | |
107 else interval = (time - (fct.nextCall - fct.interval)).interval(); | |
108 fct.nextCall = time + fct.interval; // when to call next | |
109 | |
110 fct.fct (interval); // call | |
111 } | |
23
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
112 |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
113 // Call all per-request functions: |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
114 foreach (key, fct; requestFcts) { |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
115 if (requestFctsUpdate[key]) { |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
116 fct(); |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
117 requestFctsUpdate[key] = false; |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
118 } |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
119 } |
20 | 120 } |
121 | |
23
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
122 /* Holds details for functions called per time interval. |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
123 * Needs to be a reference type, and using a class is easiest for this. */ |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
124 private static class TimeFct { |
20 | 125 scheduleFct fct; // function to call |
126 | |
127 TimeSpan interval; // interval to call at | |
128 // Storing nextCall is more efficient than storing lastCall since only this number has to | |
129 // be compared to time every frame where fct is not called: | |
130 Time nextCall = Time (0L); | |
131 | |
23
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
132 this (scheduleFct f, TimeSpan t) { |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
133 fct = f; |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
134 interval = t; |
20 | 135 } |
136 } | |
137 | |
138 private static Time lastTime = Time (0L); | |
139 private static scheduleFct[] frameFcts; | |
140 private static TimeFct[] timeFcts; | |
23
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
141 private static void function()[uint] requestFcts; |
47478557428d
Implemented drawing a very basic gl box, and only drawing when necessary.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
142 private static bool[uint] requestFctsUpdate; // associated with requestFcts |
20 | 143 } |