Skip to content

Instantly share code, notes, and snippets.

@Thomvis
Created October 2, 2009 10:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thomvis/199600 to your computer and use it in GitHub Desktop.
Save Thomvis/199600 to your computer and use it in GitHub Desktop.
//
// Loader.m
// MatchUp
//
// Created by Thomas Visser on 12/06/2009.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "Loader.h"
@interface Loader ()
+ (NSOperationQueue *) sharedQueue;
@end
@implementation Loader
- (id) initWithLoadables: (NSArray *) ploadables asynchronous: (BOOL) asyn {
self = [super init];
loadables = [ploadables retain];
asynchronous = asyn;
progresses = calloc([loadables count], sizeof(float));
for( int i = 0; i < [loadables count]; i++ ) {
progresses[i] = 0;
}
return self;
}
+ (id) loaderWithLoadables: (NSArray *) loadables asynchronous: (BOOL) asyn {
return [[[Loader alloc] initWithLoadables: loadables asynchronous: asyn] autorelease];
}
- (id) initWithLoadable: (NSObject <Loadable> *) loadable asynchronous: (BOOL) asyn {
self = [super init];
asynchronous = asyn;
progresses = calloc(0, sizeof(float));
progresses[0] = 0;
loadables = [[NSArray alloc] initWithObjects: loadable, nil];
return self;
}
+ (id) loaderWithLoadable: (NSObject <Loadable> *) loadable asynchronous: (BOOL) asyn {
return [[[Loader alloc] initWithLoadable: loadable asynchronous: asyn] autorelease];
}
- (void) loadWithDelegate: (NSObject <LoadableDelegate> *) pdelegate {
delegate = [pdelegate retain];
if( asynchronous ) {
[[Loader sharedQueue] addOperation: self];
} else {
[self main];
}
}
- (BOOL) loadingUsesOpenGL {
BOOL res = NO;
for( id<Loadable> l in loadables ) {
res = res || [l loadingUsesOpenGL];
}
return res;
}
- (void) loadable: (id <Loadable>) ldble reportingProgress: (float) progrs {
progresses[[loadables indexOfObject:ldble]] = progrs;
float sum = 0;
for( int i = 0; i < [loadables count]; i++ ) {
sum += progresses[i];
}
[delegate loadable: self reportingProgress: sum/[loadables count]];
}
- (void) main {
// the real loading
if( asynchronous && ![NSThread isMainThread] ) {
[EAGLContext setCurrentContext: [[[Director sharedDirector] openGLView] context]];
}
for( NSObject <Loadable> * loadable in loadables ) {
[loadable loadWithDelegate: self];
}
}
+ (NSOperationQueue *) sharedQueue {
static NSOperationQueue *queue;
if (queue == nil) {
// create dict
queue = [[NSOperationQueue alloc] init];
}
return queue;
}
- (BOOL) isFinished {
float sum = 0;
for( int i = 0; i < [loadables count]; i++ ) {
sum += progresses[i];
}
return (sum/[loadables count]) == 1;
}
- (void) dealloc {
free(progresses);
[delegate release];
[loadables release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment