6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
7 // |
7 // |
8 |
8 |
9 #import "VoicesViewController.h" |
9 #import "VoicesViewController.h" |
10 #import "CommodityFunctions.h" |
10 #import "CommodityFunctions.h" |
|
11 #import "openalbridge.h" |
11 |
12 |
12 |
13 |
13 @implementation VoicesViewController |
14 @implementation VoicesViewController |
14 @synthesize teamDictionary, voiceArray, lastIndexPath; |
15 @synthesize teamDictionary, voiceArray, lastIndexPath; |
15 |
16 |
23 #pragma mark View lifecycle |
24 #pragma mark View lifecycle |
24 - (void)viewDidLoad { |
25 - (void)viewDidLoad { |
25 [super viewDidLoad]; |
26 [super viewDidLoad]; |
26 srandom(time(NULL)); |
27 srandom(time(NULL)); |
27 |
28 |
28 Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 1024); |
29 openal_init("Hedgewars", 0, 3); |
29 voiceBeingPlayed = NULL; |
30 voiceBeingPlayed = -1; |
30 |
31 |
31 // load all the voices names and store them into voiceArray |
32 // load all the voices names and store them into voiceArray |
32 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL]; |
33 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:VOICES_DIRECTORY() error:NULL]; |
33 self.voiceArray = array; |
34 self.voiceArray = array; |
34 } |
35 } |
46 } |
47 } |
47 */ |
48 */ |
48 |
49 |
49 - (void)viewWillDisappear:(BOOL)animated { |
50 - (void)viewWillDisappear:(BOOL)animated { |
50 [super viewWillDisappear:animated]; |
51 [super viewWillDisappear:animated]; |
51 if(voiceBeingPlayed != NULL) { |
52 if(voiceBeingPlayed >= 0) { |
52 Mix_HaltChannel(-1); |
53 openal_freesound(voiceBeingPlayed); |
53 Mix_FreeChunk(voiceBeingPlayed); |
54 voiceBeingPlayed = -1; |
54 voiceBeingPlayed = NULL; |
|
55 } |
55 } |
56 } |
56 } |
57 |
57 |
58 /* |
58 /* |
59 - (void)viewDidDisappear:(BOOL)animated { |
59 - (void)viewDidDisappear:(BOOL)animated { |
132 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
132 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
133 // Return NO if you do not want the item to be re-orderable. |
133 // Return NO if you do not want the item to be re-orderable. |
134 return YES; |
134 return YES; |
135 } |
135 } |
136 */ |
136 */ |
137 -(void) allowSelection { |
|
138 self.tableView.allowsSelection = YES; |
|
139 } |
|
140 |
137 |
141 #pragma mark - |
138 #pragma mark - |
142 #pragma mark Table view delegate |
139 #pragma mark Table view delegate |
143 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
140 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
144 int newRow = [indexPath row]; |
141 int newRow = [indexPath row]; |
145 int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
142 int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
146 |
|
147 // avoid a crash in sdl_mixer |
|
148 self.tableView.allowsSelection = NO; |
|
149 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(allowSelection) userInfo:nil repeats:NO]; |
|
150 |
143 |
151 if (newRow != oldRow) { |
144 if (newRow != oldRow) { |
152 [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"]; |
145 [teamDictionary setObject:[voiceArray objectAtIndex:newRow] forKey:@"voicepack"]; |
153 |
146 |
154 // tell our boss to write this new stuff on disk |
147 // tell our boss to write this new stuff on disk |
158 self.lastIndexPath = indexPath; |
151 self.lastIndexPath = indexPath; |
159 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
152 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
160 } |
153 } |
161 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
154 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
162 |
155 |
163 if (voiceBeingPlayed != NULL) { |
156 if (voiceBeingPlayed >= 0) { |
164 Mix_HaltChannel(-1); |
157 openal_stopsound(voiceBeingPlayed); |
165 Mix_FreeChunk(voiceBeingPlayed); |
158 openal_freesound(voiceBeingPlayed); |
166 voiceBeingPlayed = NULL; |
159 voiceBeingPlayed = -1; |
167 } |
160 } |
168 |
161 |
169 // the keyword static prevents re-initialization of the variable |
162 // the keyword static prevents re-initialization of the variable |
170 NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
163 NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; |
171 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
164 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; |
172 |
165 |
173 int index = random() % [array count]; |
166 int index = random() % [array count]; |
174 |
167 |
175 voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
168 voiceBeingPlayed = openal_loadfile([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); |
176 [voiceDir release]; |
169 [voiceDir release]; |
177 Mix_PlayChannel(-1, voiceBeingPlayed, 0); |
170 openal_playsound(voiceBeingPlayed); |
178 } |
171 } |
179 |
172 |
180 |
173 |
181 #pragma mark - |
174 #pragma mark - |
182 #pragma mark Memory management |
175 #pragma mark Memory management |
183 - (void)didReceiveMemoryWarning { |
176 - (void)didReceiveMemoryWarning { |
184 Mix_HaltChannel(-1); |
177 openal_stopsound(voiceBeingPlayed); |
185 Mix_FreeChunk(voiceBeingPlayed); |
178 openal_freesound(voiceBeingPlayed); |
186 voiceBeingPlayed = NULL; |
179 voiceBeingPlayed = -1; |
187 // Releases the view if it doesn't have a superview. |
180 // Releases the view if it doesn't have a superview. |
188 [super didReceiveMemoryWarning]; |
181 [super didReceiveMemoryWarning]; |
189 // Relinquish ownership any cached data, images, etc that aren't in use. |
182 // Relinquish ownership any cached data, images, etc that aren't in use. |
190 } |
183 } |
191 |
184 |
192 - (void)viewDidUnload { |
185 - (void)viewDidUnload { |
193 [super viewDidUnload]; |
186 [super viewDidUnload]; |
194 |
187 |
195 Mix_CloseAudio(); |
188 openal_close(); |
196 voiceBeingPlayed = NULL; |
189 voiceBeingPlayed = -1; |
197 self.lastIndexPath = nil; |
190 self.lastIndexPath = nil; |
198 self.teamDictionary = nil; |
191 self.teamDictionary = nil; |
199 self.voiceArray = nil; |
192 self.voiceArray = nil; |
200 } |
193 } |
201 |
194 |