-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqcSimpleHarmonicMotionPlugIn.mm
executable file
·260 lines (207 loc) · 7.02 KB
/
qcSimpleHarmonicMotionPlugIn.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
//
// qcSimpleHarmonicMotionPlugin.mm
// qcSimpleHarmonicMotionPlugin
//
// Created by Joel Pryde on 12/19/13.
// Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//
#import "qcSimpleHarmonicMotionPlugIn.h"
#import "Pendulum.h"
#include "cinder/cocoa/CinderCocoa.h"
#include "cinder/ImageSourceFileQuartz.h"
#include "cinder/ImageTargetFileQuartz.h"
#include "cinder/CinderMath.h"
#define kQCPlugIn_Name @"qcSimpleHarmonicMotion"
#define kQCPlugIn_Description @"A Cinder port of Memo's Simple Harmonic Motion test"
@implementation qcSimpleHarmonicMotionPlugIn
@dynamic inputWidth;
@dynamic inputHeight;
@dynamic inputCount;
@dynamic inputSize;
@synthesize size = mSize;
static NSMutableDictionary *sAttributes = nil;
+ (NSDictionary *)attributes;
{
return [NSDictionary dictionaryWithObjectsAndKeys:kQCPlugIn_Name, QCPlugInAttributeNameKey, kQCPlugIn_Description, QCPlugInAttributeDescriptionKey, nil];
}
+ (NSDictionary *)attributesForPropertyPortWithKey:(NSString *)key;
{
if (sAttributes == nil)
{
sAttributes = [[NSMutableDictionary alloc] init];
[qcSimpleHarmonicMotionPlugIn addFloatAttribute:@"inputWidth" Default:640.0f Min:0.0f Max:4000.0f];
[qcSimpleHarmonicMotionPlugIn addFloatAttribute:@"inputHeight" Default:640.0f Min:0.0f Max:4000.0f];
[qcSimpleHarmonicMotionPlugIn addFloatAttribute:@"inputCount" Default:15.0f Min:0.0f Max:100.0f];
[qcSimpleHarmonicMotionPlugIn addFloatAttribute:@"inputSize" Default:10.0f Min:0.0f Max:1000.0f];
}
return [sAttributes objectForKey:key];
}
+ (void)addFloatAttribute:(NSString *)key Default:(float)def Min:(float)min Max:(float)max
{
[sAttributes setObject: [NSDictionary dictionaryWithObjectsAndKeys: key, QCPortAttributeNameKey, [NSNumber numberWithFloat:def], QCPortAttributeDefaultValueKey, [NSNumber numberWithFloat:min], QCPortAttributeMinimumValueKey, [NSNumber numberWithFloat:max], QCPortAttributeMaximumValueKey, nil] forKey:key];
}
+ (void)addBoolAttribute:(NSString *)key Default:(bool)def
{
[sAttributes setObject: [NSDictionary dictionaryWithObjectsAndKeys: key, QCPortAttributeNameKey, [NSNumber numberWithBool:def], QCPortAttributeDefaultValueKey, nil] forKey:key];
}
- (float)floatParam:(NSString*)paramName
{
return [[self valueForInputKey:paramName] floatValue];
}
- (int)intParam:(NSString*)paramName
{
return [[self valueForInputKey:paramName] intValue];
}
- (bool)boolParam:(NSString*)paramName
{
return [[self valueForInputKey:paramName] boolValue];
}
- (NSString*)stringParam:(NSString*)paramName
{
return [self valueForInputKey:paramName];
}
+ (QCPlugInExecutionMode)executionMode;
{
return kQCPlugInExecutionModeConsumer;
}
+ (QCPlugInTimeMode)timeMode;
{
return kQCPlugInTimeModeTimeBase;
}
- (void) finalize;
{
/*
Release any non garbage collected resources created in -init.
*/
[super finalize];
}
- (void) dealloc;
{
/*
Release any resources created in -init.
*/
[super dealloc];
}
- (void)setup
{
mCount = 15;
for (int i=0; i<mCount; i++)
{
float freq = (51.0 + i)/60.0;
mPendulums[i] = new Pendulum(mCount, i, freq, 0);
}
mWidth = 640.0f; mHeight = 480.0f;
// save time we have finished the setup, to ensure pendulums start at t==0
//startTime = getElapsedSeconds();
}
fs::path getQCResourcePath( const fs::path &rsrcRelativePath )
{
fs::path path = rsrcRelativePath.parent_path();
fs::path fileName = rsrcRelativePath.filename();
if( fileName.empty() )
return string();
NSString *pathNS = 0;
if( ( ! path.empty() ) && ( path != rsrcRelativePath ) )
pathNS = [NSString stringWithUTF8String:path.c_str()];
NSString *resultPath;
for ( NSBundle* bundle in [NSBundle allBundles])
{
resultPath = [bundle pathForResource:[NSString stringWithUTF8String:fileName.c_str()] ofType:nil inDirectory:pathNS];
if( resultPath != nil )
break;
}
return fs::path([resultPath cStringUsingEncoding:NSUTF8StringEncoding]);
}
DataSourceRef loadQCResource( const string &macPath )
{
fs::path resourcePath = getQCResourcePath( macPath );
if( resourcePath.empty() )
throw ResourceLoadExc( macPath );
else
return DataSourcePath::create( resourcePath );
}
- (void)glSetup
{
ImageSourceFileQuartz::registerSelf();
mIsGLSetup = true;
}
- (void)update:(NSTimeInterval)elapsed
{
// update
// seconds since we started the app (minus setup time)
float secs = mTime / 2.0f;
if ([self didValueForInputKeyChange:@"inputCount"])
{
mCount = (int)[self floatParam:@"inputCount"];
for (int i=0; i<mCount; i++)
{
float freq = (51.0 + i)/60.0;
mPendulums[i] = new Pendulum(mCount, i, freq, 0);
}
}
mPendulumSize = [self floatParam:@"inputSize"];
mWidth = [[self valueForInputKey:@"inputWidth"] floatValue];
mHeight = [[self valueForInputKey:@"inputHeight"] floatValue];
// loop through, update and draw pendulums
for (int c=0; c<mCount; c++)
mPendulums[c]->update(secs, c, mWidth, mHeight, mPendulumSize);
}
- (void)render
{
gl::setMatricesWindow(mWidth, mHeight);
// clear out the window with black
gl::clear( Color( 0, 0, 0 ) );
//gl::setMatricesWindowPersp(size.width, size.height);
//gl::clear( Color( 0, 0, 0 ) );
// loop through, update and draw pendulums
for (int c=0; c<mCount; c++)
mPendulums[c]->draw(mPendulums, mCount);
}
- (BOOL)startExecution:(id<QCPlugInContext>)context;
{
mWidth = 640.0f; mHeight = 480.0f;
mSize = CGSizeMake(mWidth, mHeight);
mLastTime = 0.0;
mIsGLSetup = false;
[self setup];
// save time we have finished the setup, to ensure pendulums start at t==0
//startTime = getElapsedSeconds();
return YES;
}
- (void) enableExecution:(id<QCPlugInContext>)context
{
/*
Called by Quartz Composer when the plug-in instance starts being used by Quartz Composer.
*/
}
- (BOOL) execute:(id<QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary *)arguments
{
CGLContextObj cgl_ctx = [context CGLContextObj];
CGLSetCurrentContext(cgl_ctx);
if (!mIsGLSetup)
[self glSetup];
mWidth = [self floatParam:@"inputWidth"];
mHeight = [self floatParam:@"inputHeight"];
mSize = CGSizeMake(mWidth, mHeight);
// update
double elapsed = (time-mLastTime) * 1.0f;//[self floatParam:@"inputSpeedScale"];
mTime = mTime + elapsed * 1.0f;//[self floatParam:@"inputSpeedScale"];
[self update:elapsed];
mLastTime = time;
// draw
[self render];
return YES;
}
- (void) disableExecution:(id<QCPlugInContext>)context
{
/*
Called by Quartz Composer when the plug-in instance stops being used by Quartz Composer.
*/
}
- (void) stopExecution:(id<QCPlugInContext>)context
{
/*
Called by Quartz Composer when rendering of the composition stops: perform any required cleanup for the plug-in.
*/
}
@end