/*
 * EKApplication
 * description: a generic object for managing app resources
 * history:
 *	5/13/93 [Erik Kay] - created
 *	5/15/93 [Erik Kay] - added sound and image preloading w/progress bar
 */

#import "EKApplication.h"
#import "EKSoundManager.h"
#import "EKProgressView.h"

@implementation EKApplication

+ new
{
    self = [super new];
    imageResources = [[List alloc] init];
    soundResources = [[List alloc] init];
    return self;
}

// all of these instance variables should be connected up in the nib file
- defaultsManager
{
    return defaultsManager;
}

- infoManager
{
    return infoManager;
}

- soundManager
{
    return soundManager;
}

// add an image filename to the image resource list
- addImageResource:(const char *)r
{
    //! we cheat here a bit and take advantage of the fact that a list just
    //! takes id's which are pointers, and therefore, character pointers work
    //! just fine too.  Probably not the best thing to do...
    [imageResources addObject:(id)NXCopyStringBuffer(r)];
    return self;
}

// add an sound filename to the sound resource list
- addSoundResource:(const char *)r
{
    //! we cheat here a bit and take advantage of the fact that a list just
    //! takes id's which are pointers, and therefore, character pointers work
    //! just fine too.  Probably not the best thing to do...
    [soundResources addObject:(id)NXCopyStringBuffer(r)];
    return self;
}

// preload resources for the app

- loadResources
{
    int i, count, total, progress;
    const char *str;
    id img;
    
    // set up the progress bar window
    //! this should be optional too I suppose, considering that this is
    //! supposed to be a generic object
    total = [imageResources count] + [soundResources count];    
    [[progressView setMin:0] setMax:total];
    progress = 0;
    [progressWindow makeKeyAndOrderFront:self];
    NXPing();

    // actually load the images
    [progressWindow setTitle:"Loading Images..."];
    count = [imageResources count];
    for (i = 0; i < count; i++) {
	str = (const char *)[imageResources objectAt:i];
	img = [NXImage findImageNamed:str];
	// doing a lock/unlock caches the image
	[img lockFocus];
	[img unlockFocus];
	// update the progress bar
	progress++;
	[progressView setProgress:progress];
	NXPing();
    }
    
    // actually load/cache the sounds.
    // we don't actually cache them here like we do for images, but the
    // current sound manager does a convert as it loads, effectively doing
    // the cache for us.
    count = [soundResources count];
    [progressWindow setTitle:"Loading Sounds..."];
    for (i = 0; i < count; i++) {
	str = (const char *)[soundResources objectAt:i];
	[soundManager addSound:str];
	// update the progress bar
	progress++;
	[progressView setProgress:progress];
	NXPing();
    }
    // all done
    [progressWindow close];
    return self;
}

@end
