This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorials:media_class [2011/07/01 17:41] – [Implementation] daniel | tutorials:media_class [2013/03/05 10:19] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== A Media class ====== | ||
+ | Textures and sounds are used throughout any game, so they should be collected in one place where they are easy to access. One way to do that is with the " | ||
+ | |||
+ | Add the class to your game and modify it to your needs (loading the correct texture atlas, etc.). This is how you use it: | ||
+ | |||
+ | <code objc> | ||
+ | // At the start of the game, call: | ||
+ | [Media initTextures]; | ||
+ | [Media initAudio]; | ||
+ | |||
+ | // Then, wherever you need access to a texture or play a sound: | ||
+ | SPTexture *texture = [Media atlasTexture: | ||
+ | [Media playSound: | ||
+ | |||
+ | // and at the end, release all data. | ||
+ | [Media releaseTextures]; | ||
+ | [Media releaseSound]; | ||
+ | </ | ||
+ | |||
+ | ===== Implementation ===== | ||
+ | |||
+ | <code objc Media.h> | ||
+ | @interface Media : NSObject | ||
+ | |||
+ | + (void)initTextures; | ||
+ | + (void)releaseTextures; | ||
+ | + (SPTexture *)atlasTexture: | ||
+ | |||
+ | + (void)initAudio; | ||
+ | + (void)releaseAudio; | ||
+ | |||
+ | + (void)playSound: | ||
+ | + (SPSoundChannel *)soundChannel: | ||
+ | |||
+ | @end | ||
+ | </ | ||
+ | |||
+ | <code objc Media.m> | ||
+ | #import " | ||
+ | |||
+ | @implementation Media | ||
+ | |||
+ | static SPTextureAtlas *atlas = NULL; | ||
+ | static NSMutableDictionary *sounds = NULL; | ||
+ | |||
+ | #pragma mark Texture Atlas | ||
+ | |||
+ | + (void)initTextures | ||
+ | { | ||
+ | [atlas release]; | ||
+ | atlas = [[SPTextureAtlas alloc] initWithContentsOfFile: | ||
+ | } | ||
+ | |||
+ | + (void)releaseTextures | ||
+ | { | ||
+ | [atlas release]; | ||
+ | atlas = nil; | ||
+ | } | ||
+ | |||
+ | + (SPTexture *)atlasTexture: | ||
+ | { | ||
+ | if (!atlas) | ||
+ | [NSException raise: | ||
+ | return [atlas textureByName: | ||
+ | } | ||
+ | |||
+ | #pragma mark Audio | ||
+ | |||
+ | + (void)initAudio | ||
+ | { | ||
+ | if (sounds) return; | ||
+ | |||
+ | [SPAudioEngine start]; | ||
+ | sounds = [[NSMutableDictionary alloc] init]; | ||
+ | |||
+ | // enumerate all sounds | ||
+ | |||
+ | NSString *soundDir = [[NSBundle mainBundle] resourcePath]; | ||
+ | NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] | ||
+ | enumeratorAtPath: | ||
+ | |||
+ | NSString *filename; | ||
+ | while (filename = [dirEnum nextObject]) | ||
+ | { | ||
+ | if ([[filename pathExtension] isEqualToString: | ||
+ | { | ||
+ | SPSound *sound = [[SPSound alloc] initWithContentsOfFile: | ||
+ | SPSoundChannel *channel = [sound createChannel]; | ||
+ | [sounds setObject: | ||
+ | [sound release]; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | + (void)releaseAudio | ||
+ | { | ||
+ | [sounds release]; | ||
+ | sounds = nil; | ||
+ | [SPAudioEngine stop]; | ||
+ | } | ||
+ | |||
+ | + (void)playSound: | ||
+ | { | ||
+ | [[sounds objectForKey: | ||
+ | } | ||
+ | |||
+ | + (SPSoundChannel *)soundChannel: | ||
+ | { | ||
+ | return [sounds objectForKey: | ||
+ | } | ||
+ | |||
+ | @end | ||
+ | </ | ||
+ | |||
+ | In this code, I simply load all " | ||
+ | |||
+ | For other ideas and a discussion of this class, see [[http:// |