-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleMain.hx
237 lines (189 loc) · 5.08 KB
/
SampleMain.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package;
import haxe.Timer;
import hxd.Window;
import graphics.LayerID;
import graphics.BatchID;
import animation.SheetID;
import graphics.ParentID;
import animation.AnimRequest;
import animation.Spritesheet;
import input.KeyboardInput;
import interactive.Interactive;
import input.MouseInput;
import input.InputCommand;
import hxd.Key;
import input.Action;
import input.Input;
import command.Command;
import graphics.Sprite;
import timing.Timing;
import ecs.Universe;
import ecs.Phase;
import hxd.Res;
import hxd.App;
import animation.AnimSystem;
import graphics.RenderSystem;
import command.CommandSystem;
import timing.TimingSystem;
import input.InputSystem;
import input.MouseSystem;
import interactive.InteractiveSystem;
import audio.AudioSystem;
import timing.Updater;
import graphics.RenderCommand;
import graphics.DisplayListCommand;
import animation.AnimCommand;
#if js
import utils.ResTools;
#end
class Main extends App {
var ecs:Universe;
var updateLoop:Updater;
var renderLoop:Updater;
var fixedUpdate:Bool = true;
var fixedRender:Bool = true;
var updateFPS:Int = 60;
var renderFPS:Int = 60;
var maxAccumulatedTime:Float;
var updatePhase:Phase;
var lastStamp:Float;
var loadedIn:Bool;
static function main() {
#if !js
Res.initEmbed();
#end
new Main();
}
override function init() {
loadedIn = false; // some stuff is null at the beginning on JS
@:privateAccess haxe.MainLoop.add(() -> {}); // bug that prevents sound from playing past 1 sec
#if !js
realInit();
#else
ResTools.initPakAuto("assets", () -> { // i need to write a multi use preloader
realInit();
}, p -> { });
#end
}
function realInit() {
engine.backgroundColor = 0xff888888;
ecs = Universe.create({
entities : 400,
phases : [
{
name : "update",
enabled : false,
systems : [
InteractiveSystem,
InputSystem,
MouseSystem,
RenderSystem,
AnimSystem,
TimingSystem,
AudioSystem,
CommandSystem // we usually want this to be the final system
]
}
]
});
// manually managing main phase, may change later
updatePhase = ecs.getPhase("update");
updatePhase.enable();
lastStamp = haxe.Timer.stamp();
updateLoop = Timing.every(1 / updateFPS, onUpdate); // prepUpdate?
renderLoop = Timing.every(1 / renderFPS, prepRender);
s2d.setElapsedTime(1 / renderFPS);
maxAccumulatedTime = 2 / renderFPS - 0.001; // don't update two or more frames at a time in fixed-time loops
Window.getInstance().vsync = fixedUpdate = false; // no vsync, framerate equals real FPS
postInit();
}
function postInit() {
loadedIn = true;
var sheet = new Spritesheet();
sheet.loadAnimation(Res.bitmap_200x184, "test", 1, 2);
ecs.setResources(sheet);
var input = new Input();
var kmap = new InputMapping();
kmap[Action.SELECT] = [Key.SPACE, Key.Z, Key.F, Key.ENTER];
kmap[Action.MUTE] = [Key.M];
input.addDevice(new KeyboardInput(kmap));
var mmap = new InputMapping();
mmap[Action.SELECT] = [Key.MOUSE_LEFT, Key.MOUSE_RIGHT];
input.addDevice(new MouseInput(mmap));
var anims:Array<AnimRequest> = [
{
name : "default",
frameNames : ["test0", "test1"],
fps : 2,
loop : true
}
];
var ent = ecs.createEntity();
Command.queueMany(
ADD_PARENT(s2d, ParentID.S2D),
ADD_SHEET(sheet, BatchID.MAIN),
CREATE_BATCH(BatchID.MAIN, ParentID.S2D, LayerID.S2D_GAME),
ALLOC_SPRITE(ent, BatchID.MAIN),
CREATE_ANIMATIONS(ent, SheetID.MAIN, anims, "default"),
ADD_INPUT(input, P1)
// PLAY(MUSIC, "audio/music/1122420_Streambeat.ogg", true, 1, "")
);
}
override function mainLoop() {
if (!Window.getInstance().vsync) {
final targetDT = 1 / updateFPS;
final safeTime = 1 / 1000;
while (Timer.stamp() - lastStamp < targetDT - safeTime) {
// limit !vsync to around the updateFPS
// if not, the framerate will go into the thousands
// @trethaller at Shiro Games
}
}
final newTime = Timer.stamp();
final dt = newTime - lastStamp;
lastStamp = newTime;
if (isDisposed || !loadedIn) return;
update(dt);
}
override function update(dt:Float) {
hxd.Timer.update(); // is this necessary?
if (fixedUpdate) {
// update at the desired FPS
@:privateAccess
if (updateLoop.counter + dt > maxAccumulatedTime) {
// limit the effect of lag causing multiple "catch-up" updates at once
updateLoop.counter = maxAccumulatedTime;
updateLoop.update(0);
}
else updateLoop.update(dt);
}
else {
// force update using whatever the real FPS ends up being
s2d.setElapsedTime(dt);
sevents.checkEvents();
updatePhase.update(dt);
}
if (fixedRender) {
// render at desired FPS
renderLoop.update(dt);
}
else {
// render as often as possible
prepRender();
}
}
function onUpdate() {
s2d.setElapsedTime(1 / updateFPS);
sevents.checkEvents();
updatePhase.update(1 / updateFPS);
}
function prepRender() {
if (!engine.begin()) return;
onRender();
engine.end();
}
function onRender() {
s2d.render(engine);
// trace("draw calls: " + engine.drawCalls);
}
}