| 1 | // |
|---|
| 2 | // SapphireTheme.m |
|---|
| 3 | // Sapphire |
|---|
| 4 | // |
|---|
| 5 | // Created by Graham Booker on 6/27/07. |
|---|
| 6 | // Copyright 2007 www.nanopi.net. All rights reserved. |
|---|
| 7 | // |
|---|
| 8 | |
|---|
| 9 | #import "SapphireTheme.h" |
|---|
| 10 | #import "SapphireFrontRowCompat.h" |
|---|
| 11 | |
|---|
| 12 | @implementation SapphireTheme |
|---|
| 13 | |
|---|
| 14 | /*! |
|---|
| 15 | * @brief Get the shared theme |
|---|
| 16 | * |
|---|
| 17 | * @return The shared theme |
|---|
| 18 | */ |
|---|
| 19 | + (id)sharedTheme |
|---|
| 20 | { |
|---|
| 21 | static SapphireTheme *shared = nil; |
|---|
| 22 | if(shared == nil) |
|---|
| 23 | shared = [[SapphireTheme alloc] init]; |
|---|
| 24 | |
|---|
| 25 | return shared; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | - (id)init |
|---|
| 29 | { |
|---|
| 30 | self = [super init]; |
|---|
| 31 | if(!self) |
|---|
| 32 | return nil; |
|---|
| 33 | |
|---|
| 34 | gemDict = [NSMutableDictionary new]; |
|---|
| 35 | gemFiles = [[NSDictionary alloc] initWithObjectsAndKeys: |
|---|
| 36 | @"Contents/Resources/Orange_Red.png", RED_GEM_KEY, |
|---|
| 37 | @"Contents/Resources/Blue.png", BLUE_GEM_KEY, |
|---|
| 38 | @"Contents/Resources/Green.png", GREEN_GEM_KEY, |
|---|
| 39 | @"Contents/Resources/Yellow.png", YELLOW_GEM_KEY, |
|---|
| 40 | @"Contents/Resources/Gear.png", GEAR_GEM_KEY, |
|---|
| 41 | @"Contents/Resources/Cone.png", CONE_GEM_KEY, |
|---|
| 42 | @"Contents/Resources/Eye.png", EYE_GEM_KEY, |
|---|
| 43 | nil]; |
|---|
| 44 | |
|---|
| 45 | return self; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | - (void)dealloc |
|---|
| 49 | { |
|---|
| 50 | [gemDict release]; |
|---|
| 51 | [scene release]; |
|---|
| 52 | [gemFiles release]; |
|---|
| 53 | [super dealloc]; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /*! |
|---|
| 57 | * @brief Sets the scene |
|---|
| 58 | * |
|---|
| 59 | * @param theScene The new scene |
|---|
| 60 | */ |
|---|
| 61 | - (void)setScene:(BRRenderScene *)theScene |
|---|
| 62 | { |
|---|
| 63 | /*Flush cache in case the scene is different and it matters*/ |
|---|
| 64 | [gemDict removeAllObjects]; |
|---|
| 65 | scene = [theScene retain]; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /*! |
|---|
| 69 | * @brief Load an image from a path |
|---|
| 70 | * |
|---|
| 71 | * @param path The image path |
|---|
| 72 | * @return A CGImageRef (retained) from the path |
|---|
| 73 | */ |
|---|
| 74 | - (CGImageRef)loadImage:(NSString *)path |
|---|
| 75 | { |
|---|
| 76 | NSString *bundlePath = [[NSBundle bundleForClass:[self class]] bundlePath]; |
|---|
| 77 | NSURL *url = [NSURL fileURLWithPath:[bundlePath stringByAppendingPathComponent:path]]; |
|---|
| 78 | CGImageRef imageRef = NULL; |
|---|
| 79 | CGImageSourceRef sourceRef; |
|---|
| 80 | sourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL); |
|---|
| 81 | if(sourceRef) { |
|---|
| 82 | imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, NULL); |
|---|
| 83 | CFRelease(sourceRef); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | return imageRef; |
|---|
| 87 | // return CreateImageForURL(url); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /*! |
|---|
| 91 | * @brief Load a gem for a type |
|---|
| 92 | * |
|---|
| 93 | * @param type The gem type |
|---|
| 94 | * @return The gem's texture |
|---|
| 95 | */ |
|---|
| 96 | - (BRTexture *)gem:(NSString *)type |
|---|
| 97 | { |
|---|
| 98 | /*Check cache*/ |
|---|
| 99 | BRTexture *ret = [gemDict objectForKey:type]; |
|---|
| 100 | if(ret != nil) |
|---|
| 101 | return ret; |
|---|
| 102 | |
|---|
| 103 | if([SapphireFrontRowCompat usingFrontRow]) |
|---|
| 104 | { |
|---|
| 105 | NSString *bundlePath = [[NSBundle bundleForClass:[self class]] bundlePath]; |
|---|
| 106 | id ret = [SapphireFrontRowCompat imageAtPath:[bundlePath stringByAppendingPathComponent:[gemFiles objectForKey:type]]]; |
|---|
| 107 | if(ret != nil) |
|---|
| 108 | [gemDict setObject:ret forKey:type]; |
|---|
| 109 | return ret; |
|---|
| 110 | } |
|---|
| 111 | /*Load it*/ |
|---|
| 112 | CGImageRef image = [self loadImage:[gemFiles objectForKey:type]]; |
|---|
| 113 | if(image != NULL) |
|---|
| 114 | { |
|---|
| 115 | /*Create a texture*/ |
|---|
| 116 | ret = [BRBitmapTexture textureWithImage:image context:[scene resourceContext] mipmap:YES]; |
|---|
| 117 | CFRelease(image); |
|---|
| 118 | } |
|---|
| 119 | /*Save in the cache*/ |
|---|
| 120 | if(ret != nil) |
|---|
| 121 | [gemDict setObject:ret forKey:type]; |
|---|
| 122 | /*return it*/ |
|---|
| 123 | return ret; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | @end |
|---|