• R/O
  • SSH
  • HTTPS

mani2: コミット


コミットメタ情報

リビジョン52 (tree)
日時2009-06-15 00:46:08
作者hikarin

ログメッセージ

[mani2/cocoa] * use thread instead of timer at drawing

変更サマリ

差分

--- trunk/cocoa/Classes/AppController.m (revision 51)
+++ trunk/cocoa/Classes/AppController.m (revision 52)
@@ -28,10 +28,18 @@
2828 //
2929
3030 #import "AppController.h"
31+#import "AppController+Private.h"
3132
3233 @implementation AppController
3334
34-@synthesize speed, frameCount, totalFrameCount, durationString, statusString, movieFrameCount, movieTotalFrameCount;
35+@synthesize speed;
36+@synthesize frameCount;
37+@synthesize totalFrameCount;
38+@synthesize durationString;
39+@synthesize statusString;
40+@synthesize movieFrameCount;
41+@synthesize movieTotalFrameCount;
42+@synthesize isActive;
3543
3644 - (id)init
3745 {
@@ -42,10 +50,9 @@
4250 durationString = @"0 / 0";
4351 beforeInput = @"";
4452 player = [[MMPlayer alloc] init];
45- playTimer = nil;
4653 error = nil;
4754 movieTimer = nil;
48- active = NO;
55+ isActive = NO;
4956 movieFrameCount = movieTotalFrameCount = 0;
5057 }
5158 return self;
@@ -55,27 +62,39 @@
5562 {
5663 [player release];
5764 [beforeInput release];
58- if (playTimer != nil) {
59- [playTimer invalidate];
60- [playTimer release];
61- playTimer = nil;
62- }
6365 [super dealloc];
6466 }
6567
6668 - (IBAction)play:(id)sender
6769 {
68- // 単純な再生機能のみを持つ
69- [self initBeforePlaying];
70+ NSString *inputtedValue = [canvasIdInput stringValue];
71+ // 前回入力したキャンバスのIDと異なる
72+ if (![inputtedValue isEqualToString:beforeInput]) {
73+ self.isActive = NO;
74+ [beforeInput release];
75+ beforeInput = [inputtedValue retain];
76+ // 前回のキャンバスを破棄する
77+ if (player != nil)
78+ [player release];
79+ player = [MMPlayer playerWithSource:inputtedValue
80+ error:&error];
81+ self.durationString = @"0 / 0";
82+ self.frameCount = 0;
83+ if (player != nil) {
84+ self.totalFrameCount = player.totalFrameCount;
85+ view.size = player.size;
86+ [view scale];
87+ [self start];
88+ }
89+ else {
90+ self.statusString = [error localizedDescription];
91+ }
92+ }
7093 }
7194
7295 - (IBAction)togglePlaying:(id)sender
7396 {
74- // 呼び出す毎に再生及び停止を切り替えることが出来る
75- if (active)
76- playTimer == nil ? [self start] : [self stop];
77- else
78- [self initBeforePlaying];
97+ self.isActive ? [self stop] : [self start];
7998 }
8099
81100 - (IBAction)exportAsImage:(id)sender
@@ -167,7 +186,7 @@
167186 NSDictionary *userInfo = [timer userInfo];
168187 MMPlayer *play = [userInfo objectForKey:@"player"];
169188 QTMovie *movie = [userInfo objectForKey:@"movie"];
170- if ([play next]) {
189+ if ([play nextFrame]) {
171190 QTTime duration = QTMakeTime(30, 600);
172191 AddMovieFrame(movie, duration, [userInfo objectForKey:@"attrs"], play.bitmap);
173192 self.movieFrameCount++;
@@ -196,113 +215,52 @@
196215 }
197216 }
198217
199-- (void)initBeforePlaying
200-{
201- NSString *inputtedValue = [canvasIdInput stringValue];
202- // 前回入力したキャンバスのIDと異なる
203- if (![inputtedValue isEqualToString:beforeInput]) {
204- [beforeInput release];
205- beforeInput = [inputtedValue retain];
206- // 前回のキャンバスを破棄する
207- if (player != nil)
208- [player release];
209- player = [MMPlayer playerWithSource:inputtedValue
210- error:&error];
211- self.durationString = @"0 / 0";
212- self.frameCount = 0;
213- if (player != nil) {
214- self.totalFrameCount = player.totalFrameCount;
215- view.size = player.size;
216- [view scale];
217- [self start];
218- }
219- else {
220- self.statusString = [error localizedDescription];
221- }
222- }
223-}
224-
225218 - (void)start
226219 {
227- SEL selector = @selector(render);
228- NSMethodSignature *signature = [self methodSignatureForSelector:selector];
229- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
230- [invocation setTarget:self];
231- [invocation setSelector:selector];
232- playTimer = [[NSTimer scheduledTimerWithTimeInterval:kFrameRate
233- invocation:invocation
234- repeats:YES] retain];
220+ self.isActive = YES;
235221 self.statusString = NSLocalizedString(@"Started", @"");
222+ [NSThread detachNewThreadSelector:@selector(render)
223+ toTarget:self
224+ withObject:nil];
236225 }
237226
238227 - (void)stop
239228 {
240- if (playTimer != nil) {
241- [playTimer invalidate];
242- [playTimer release];
243- playTimer = nil;
229+ self.isActive = NO;
230+ if (self.frameCount == self.totalFrameCount)
231+ self.statusString = NSLocalizedString(@"Finished", @"");
232+ else
244233 self.statusString = NSLocalizedString(@"Stopped", @"");
245- }
246234 }
247235
248-- (BOOL)prepareRenderingWithProgress:(BOOL)showProgress
249- error:(NSError **)err
236+- (void)update
250237 {
251- NSUInteger speedCount = self.speed;
252- while (speedCount--) {
253- if (![player next])
254- return NO;
255- if (showProgress) {
256- self.frameCount = self.frameCount + 1;
257- self.durationString = [[NSString alloc] initWithFormat:@"%d / %d",
258- self.frameCount, self.totalFrameCount];
259- }
260- }
261- return YES;
238+ [view setNeedsDisplay:YES];
262239 }
263240
264241 - (void)render
265242 {
266- NSError *err = nil;
267- if ([self prepareRenderingWithProgress:YES
268- error:&err]) {
269- CGContextRef bitmap = player.bitmap;
270- view.bitmap = bitmap;
271- [view setNeedsDisplay:YES];
272- }
273- else if (err != nil) {
274- [self stop];
275- self.statusString = [err localizedDescription];
276- }
277- else {
278- [self stop];
279- self.statusString = NSLocalizedString(@"Finished", @"");
280- active = NO;
281- }
282-}
283-
284-- (void)seek:(NSUInteger)position
285-{
286- player = [MMPlayer playerWithSource:beforeInput
287- error:&error];
288- if (player != nil) {
289- NSUInteger swappedSpeed = self.speed;
290- self.speed = position;
291- NSError *err = nil;
292- if ([self prepareRenderingWithProgress:NO
293- error:&err]) {
294- view.bitmap = player.bitmap;
295- [view setNeedsDisplay:YES];
243+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
244+ while (self.isActive) {
245+ NSUInteger speedCount = self.speed;
246+ while (speedCount--) {
247+ if (![player nextFrame]) {
248+ [self stop];
249+ break;
250+ }
251+ self.frameCount = self.frameCount + 1;
252+ self.durationString = [[NSString alloc] initWithFormat:@"%d / %d",
253+ self.frameCount, self.totalFrameCount];
296254 }
297- else if (err != nil) {
298- error = err;
299- self.statusString = [err localizedDescription];
300- }
301- self.speed = swappedSpeed;
255+ view.bitmap = player.bitmap;
256+ [self performSelectorOnMainThread:@selector(update)
257+ withObject:nil
258+ waitUntilDone:YES];
259+ NSDate *dateToWait = [[NSDate alloc] initWithTimeIntervalSinceNow:kFrameRate];
260+ [NSThread sleepUntilDate:dateToWait];
261+ [dateToWait release];
302262 }
303- else {
304- self.statusString = [error localizedDescription];
305- }
263+ [pool release];
306264 }
307265
308266 @end
--- trunk/cocoa/Classes/AppController.h (revision 51)
+++ trunk/cocoa/Classes/AppController.h (revision 52)
@@ -47,10 +47,9 @@
4747 NSString *durationString;
4848 MMPlayer *player;
4949 NSString *statusString;
50- NSTimer *playTimer;
5150 NSError *error;
5251 NSString *beforeInput;
53- BOOL active;
52+ BOOL isActive;
5453 NSTimer *movieTimer;
5554 NSUInteger movieFrameCount;
5655 NSUInteger movieTotalFrameCount;
@@ -60,16 +59,7 @@
6059 - (IBAction)togglePlaying:(id)sender;
6160 - (IBAction)exportAsImage:(id)sender;
6261 - (IBAction)exportAsMovie:(id)sender;
63-- (void)initBeforePlaying;
6462 - (void)start;
6563 - (void)stop;
6664
67-@property NSUInteger speed;
68-@property NSUInteger frameCount;
69-@property NSUInteger totalFrameCount;
70-@property NSUInteger movieFrameCount;
71-@property NSUInteger movieTotalFrameCount;
72-@property(retain) NSString *durationString;
73-@property(assign) NSString *statusString;
74-
7565 @end
旧リポジトリブラウザで表示