22 #import "HedgewarsAppDelegate.h" |
22 #import "HedgewarsAppDelegate.h" |
23 #import "PascalImports.h" |
23 #import "PascalImports.h" |
24 #import "ObjcExports.h" |
24 #import "ObjcExports.h" |
25 #import "CommodityFunctions.h" |
25 #import "CommodityFunctions.h" |
26 #import "MainMenuViewController.h" |
26 #import "MainMenuViewController.h" |
|
27 #import "AVFoundation/AVAudioPlayer.h" |
27 #import "Appirater.h" |
28 #import "Appirater.h" |
28 #include <unistd.h> |
29 #include <unistd.h> |
29 |
30 |
30 |
31 |
31 @implementation SDLUIKitDelegate (customDelegate) |
32 @implementation SDLUIKitDelegate (customDelegate) |
35 } |
36 } |
36 |
37 |
37 @end |
38 @end |
38 |
39 |
39 @implementation HedgewarsAppDelegate |
40 @implementation HedgewarsAppDelegate |
40 @synthesize mainViewController, uiwindow, secondWindow, isInGame; |
41 @synthesize mainViewController, uiwindow, secondWindow, isInGame, backgroundMusic; |
41 |
42 |
42 // convenience method |
43 // convenience method |
43 +(HedgewarsAppDelegate *)sharedAppDelegate { |
44 +(HedgewarsAppDelegate *)sharedAppDelegate { |
44 return (HedgewarsAppDelegate *)[[UIApplication sharedApplication] delegate]; |
45 return (HedgewarsAppDelegate *)[[UIApplication sharedApplication] delegate]; |
45 } |
46 } |
46 |
47 |
|
48 #pragma mark - |
|
49 #pragma mark Music control |
|
50 +(void) playBackgroundMusic { |
|
51 if ([HedgewarsAppDelegate sharedAppDelegate].backgroundMusic == nil) |
|
52 [HedgewarsAppDelegate loadBackgroundMusic]; |
|
53 [[HedgewarsAppDelegate sharedAppDelegate].backgroundMusic play]; |
|
54 } |
|
55 |
|
56 +(void) pauseBackgroundMusic { |
|
57 [[HedgewarsAppDelegate sharedAppDelegate].backgroundMusic pause]; |
|
58 } |
|
59 |
|
60 +(void) stopBackgroundMusic { |
|
61 [[HedgewarsAppDelegate sharedAppDelegate].backgroundMusic stop]; |
|
62 } |
|
63 |
|
64 +(void) loadBackgroundMusic { |
|
65 NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"]; |
|
66 AVAudioPlayer *background = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:nil]; |
|
67 |
|
68 background.delegate = nil; |
|
69 background.volume = 0.5f; |
|
70 background.numberOfLoops = -1; |
|
71 [background prepareToPlay]; |
|
72 [HedgewarsAppDelegate sharedAppDelegate].backgroundMusic = background; |
|
73 [background release]; |
|
74 } |
|
75 |
|
76 #pragma mark - |
|
77 #pragma mark AppDelegate methods |
47 -(id) init { |
78 -(id) init { |
48 if (self = [super init]){ |
79 if (self = [super init]){ |
49 mainViewController = nil; |
80 mainViewController = nil; |
50 uiwindow = nil; |
81 uiwindow = nil; |
51 secondWindow = nil; |
82 secondWindow = nil; |
52 isInGame = NO; |
83 isInGame = NO; |
|
84 backgroundMusic = nil; |
53 } |
85 } |
54 return self; |
86 return self; |
55 } |
87 } |
56 |
88 |
57 -(void) dealloc { |
89 -(void) dealloc { |
58 [mainViewController release]; |
90 [mainViewController release]; |
59 [uiwindow release]; |
91 [uiwindow release]; |
60 [secondWindow release]; |
92 [secondWindow release]; |
|
93 [backgroundMusic release]; |
61 [super dealloc]; |
94 [super dealloc]; |
62 } |
95 } |
63 |
96 |
64 // override the direct execution of SDL_main to allow us to implement our own frontend |
97 // override the direct execution of SDL_main to allow us to implement our own frontend |
65 -(void) postFinishLaunch { |
98 -(void) postFinishLaunch { |
98 [super applicationWillTerminate:application]; |
131 [super applicationWillTerminate:application]; |
99 } |
132 } |
100 |
133 |
101 -(void) applicationDidReceiveMemoryWarning:(UIApplication *)application { |
134 -(void) applicationDidReceiveMemoryWarning:(UIApplication *)application { |
102 // don't clean mainMenuViewController here!!! |
135 // don't clean mainMenuViewController here!!! |
|
136 [self.backgroundMusic stop]; |
|
137 self.backgroundMusic = nil; |
103 MSG_MEMCLEAN(); |
138 MSG_MEMCLEAN(); |
104 print_free_memory(); |
139 print_free_memory(); |
105 } |
140 } |
106 |
141 |
107 //TODO: when the SDLUIKitDelegate methods applicationWillResignActive and applicationDidBecomeActive do work |
142 //TODO: when the SDLUIKitDelegate methods applicationWillResignActive and applicationDidBecomeActive do work |