/
Simulation.mm
294 lines (232 loc) · 6.85 KB
/
Simulation.mm
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//
// Simulation.m
// Core3D
//
// Created by CoreCode on 30.10.10.
// Copyright 2007 - 2012 CoreCode. Licensed under the MIT License, see LICENSE.txt
//
#import "Core3D.h"
#ifndef TARGET_OS_IPHONE
#import "Editor.h"
#endif
@implementation Simulation
@synthesize paused;
@synthesize simTime;
- (id)init
{
if ((self = [super init]))
{
#ifdef __BLOCKS__
delayedActionBlocks = [[NSMutableArray alloc] init];
animationBlocks = [[NSMutableArray alloc] init];
timerBlocks = [[NSMutableArray alloc] init];
tmpTimerBlocks = [[NSMutableArray alloc] init];
tmpDelayedActionBlocks = [[NSMutableArray alloc] init];
tmpAnimationBlocks = [[NSMutableArray alloc] init];
tmpRenderActionBlocks = [[NSMutableArray alloc] init];
renderActionBlocks = [[NSMutableArray alloc] init];
#endif
paused = FALSE;
}
return self;
}
- (void)dealloc
{
#ifdef __BLOCKS__
[delayedActionBlocks release];
[animationBlocks release];
[timerBlocks release];
[tmpTimerBlocks release];
[tmpDelayedActionBlocks release];
[tmpAnimationBlocks release];
[tmpRenderActionBlocks release];
[renderActionBlocks release];
#endif
[super dealloc];
}
#ifdef __BLOCKS__
- (void)addAnimationWithDuration:(NSTimeInterval)duration animation:(DoubleInBlock)animationBlock completion:(BasicBlock)cleanupBlock
{
DoubleInBlock anim = [animationBlock copy];
BasicBlock clean = [cleanupBlock copy];
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:$numd(simTime), @"starttime",
$numd(simTime + duration), @"endtime",
anim, @"animationblock",
clean, @"cleanupblock", nil];
[tmpAnimationBlocks addObject:dict];
[dict release];
[anim release];
[clean release];
}
- (void)addTimerWithInterval:(NSTimeInterval)interval timer:(DoubleInBlock)timerBlock completion:(BasicBlock)cleanupBlock endCondition:(BoolOutBlock)conditionBlock
{
BoolOutBlock cond = [conditionBlock copy];
DoubleInBlock timer = [timerBlock copy];
BasicBlock clean = [cleanupBlock copy];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:$numd(simTime), @"starttime",
$numd(0.0), @"performtime",
$numd(interval), @"interval",
cond, @"conditionblock",
timer, @"animationblock",
clean, @"cleanupblock", nil];
[tmpTimerBlocks addObject:dict];
[dict release];
[timer release];
[clean release];
[cond release];
}
- (void)performBlockAfterDelay:(NSTimeInterval)delay block:(BasicBlock)_block
{
@synchronized (self)
{
BasicBlock block = [_block copy];
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:$numd(simTime + delay), @"endtime", block, @"block", nil];
[tmpDelayedActionBlocks addObject:dict];
[dict release];
[block release];
}
}
- (void)performBlockOnRenderThread:(BasicBlock)_block
{
@synchronized (self)
{
BasicBlock block = [_block copy];
[tmpRenderActionBlocks addObject:block];
[block release];
}
}
#endif
- (void)update
{
// NSLog(@"sim up start");
simTime += globalInfo.frameDiff;
// if (simTime < 0.0)
// cout << simTime << endl;
#ifdef __BLOCKS__
@synchronized (self)
{
for (id item in tmpTimerBlocks) [timerBlocks addObject:item];
for (id item in tmpAnimationBlocks) [animationBlocks addObject:item];
for (id item in tmpDelayedActionBlocks) [delayedActionBlocks addObject:item];
[tmpAnimationBlocks removeAllObjects];
[tmpDelayedActionBlocks removeAllObjects];
[tmpTimerBlocks removeAllObjects];
}
// delayed action
NSMutableArray *blocksToRemove = [[NSMutableArray alloc] init];
for (NSDictionary *dict in delayedActionBlocks)
{
if (simTime > [[dict objectForKey:@"endtime"] doubleValue])
{
[blocksToRemove addObject:dict];
BasicBlock block = [dict objectForKey:@"block"];
block();
}
}
[delayedActionBlocks removeObjectsInArray:blocksToRemove];
[blocksToRemove removeAllObjects];
// animation
for (NSDictionary *dict in animationBlocks)
{
NSNumber *endTime = [dict objectForKey:@"endtime"];
if ((endTime && simTime > [endTime doubleValue]))
// || (!endTime && ((BoolOutBlock)[dict objectForKey:@"completionblock"])()))
{
[blocksToRemove addObject:dict];
BasicBlock block = [dict objectForKey:@"cleanupblock"];
// NSLog(@"executing cleanup block at %f", simTime);
block();
}
else
{
DoubleInBlock block = [dict objectForKey:@"animationblock"];
block(simTime - [[dict objectForKey:@"starttime"] doubleValue]);
}
}
[animationBlocks removeObjectsInArray:blocksToRemove];
[blocksToRemove removeAllObjects];
// timers
for (NSMutableDictionary *dict in timerBlocks)
{
if (((BoolOutBlock) [dict objectForKey:@"conditionblock"])())
{
[blocksToRemove addObject:dict];
BasicBlock block = [dict objectForKey:@"cleanupblock"];
block();
}
else
{
NSNumber *performtime = [dict objectForKey:@"performtime"];
NSNumber *interval = [dict objectForKey:@"interval"];
if ([performtime doubleValue] + [interval doubleValue] < simTime)
{
DoubleInBlock block = [dict objectForKey:@"animationblock"];
block(simTime - [[dict objectForKey:@"starttime"] doubleValue]);
[dict setObject:$numd(simTime) forKey:@"performtime"];
}
}
}
[timerBlocks removeObjectsInArray:blocksToRemove];
[blocksToRemove removeAllObjects];
[blocksToRemove release];
#endif
}
- (void)render
{
#ifdef __BLOCKS__
@synchronized (self)
{
for (id item in tmpRenderActionBlocks)
{
[renderActionBlocks addObject:item];
}
[tmpRenderActionBlocks removeAllObjects];
}
for (BasicBlock block in renderActionBlocks)
{
block();
}
[renderActionBlocks removeAllObjects];
#endif
}
#ifndef TARGET_OS_IPHONE
- (void)keyDown:(NSEvent *)theEvent
{
#ifndef RELEASEBUILD
unichar c = [[theEvent charactersIgnoringModifiers] characterAtIndex:0];
if ((([theEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask) == NSCommandKeyMask) && (c == 'e'))
{
[Editor loadEditor:self];
}
#endif
}
- (void)keyUp:(NSEvent *)theEvent
{
}
- (void)mouseDown:(NSEvent *)theEvent
{
}
- (void)mouseUp:(NSEvent *)theEvent
{
}
- (void)rightMouseDown:(NSEvent *)theEvent
{
}
- (void)rightMouseUp:(NSEvent *)theEvent
{
}
- (void)mouseDragged:(NSEvent *)theEvent
{
}
- (void)scrollWheel:(NSEvent *)theEvent
{
}
#else
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
#endif
@end