From 5c293e2fbb4f73ec1d682949e1258340399e0c1b Mon Sep 17 00:00:00 2001 From: "techdragon.nguyen@gmail.com" Date: Sun, 5 Feb 2012 08:21:06 +0000 Subject: [PATCH] added new method to JSoundSystem to pause music. Previously, "pause" meant kill the music and "resume" effectively restarted the music. iOS will now pause and resume appropriately modified pc and android impls to ensure new calls are made. These still have the same effective outcome (kill and start) until the equivalent is coded on these platforms to pause and resume the music. fixed bug with iOS sound effects not playing. Forgot to assign the key to the associated music sample --- JGE/include/JSoundSystem.h | 10 ++++- JGE/src/android/JSfx.cpp | 9 ++++ JGE/src/iOS/EAGLView.h | 4 +- JGE/src/iOS/EAGLView.m | 44 ++++++++++++++++---- JGE/src/iOS/EAGLViewController.h | 2 + JGE/src/iOS/EAGLViewController.m | 18 +++++++- JGE/src/iOS/JSfx.cpp | 19 ++++++++- JGE/src/iOS/wagicAppDelegate.m | 38 +++++------------ JGE/src/pc/JSfx.cpp | 12 ++++++ projects/mtg/include/GameApp.h | 2 + projects/mtg/src/GameApp.cpp | 22 +++++++++- projects/mtg/src/WCachedResource.cpp | 4 +- projects/mtg/wagic.xcodeproj/project.pbxproj | 10 +++++ 13 files changed, 149 insertions(+), 45 deletions(-) diff --git a/JGE/include/JSoundSystem.h b/JGE/include/JSoundSystem.h index 10f25e8e2..56bb335c2 100644 --- a/JGE/include/JSoundSystem.h +++ b/JGE/include/JSoundSystem.h @@ -177,7 +177,7 @@ public: ////////////////////////////////////////////////////////////////////////// void StopMusic(JMusic *music); - + ////////////////////////////////////////////////////////////////////////// /// Resume playing. /// @@ -185,6 +185,14 @@ public: /// ////////////////////////////////////////////////////////////////////////// void ResumeMusic(JMusic *music); + + ////////////////////////////////////////////////////////////////////////// + /// Pause playing. + /// + /// @param music - Music to be paused. + /// + ////////////////////////////////////////////////////////////////////////// + void PauseMusic(JMusic *music); ////////////////////////////////////////////////////////////////////////// /// Load sound effect. diff --git a/JGE/src/android/JSfx.cpp b/JGE/src/android/JSfx.cpp index 4611566de..f75a391ea 100644 --- a/JGE/src/android/JSfx.cpp +++ b/JGE/src/android/JSfx.cpp @@ -231,6 +231,15 @@ void JSoundSystem::StopMusic(JMusic *music) } } +void JSoundSystem::PauseMusic(JMusic *music) +{ + StopMusic(music); +} + +void JSoundSystem::ResumeMusic(JMusic *music) +{ + PlayMusic(music); +} void JSoundSystem::SetVolume(int volume) { diff --git a/JGE/src/iOS/EAGLView.h b/JGE/src/iOS/EAGLView.h index 8d648f537..285303c5d 100755 --- a/JGE/src/iOS/EAGLView.h +++ b/JGE/src/iOS/EAGLView.h @@ -30,7 +30,7 @@ @property (nonatomic, retain) AdWhirlView *adView; @property (readonly, nonatomic, getter=isAnimating) BOOL animating; @property (nonatomic) NSInteger animationFrameInterval; -@property(nonatomic, readwrite) CGPoint currentLocation; +@property (nonatomic, readwrite) CGPoint currentLocation; - (void)startAnimation; - (void)stopAnimation; @@ -40,4 +40,6 @@ - (void)removeAds; - (void)displayAds; +- (void)destroyGame; + @end diff --git a/JGE/src/iOS/EAGLView.m b/JGE/src/iOS/EAGLView.m index 9905845c4..220916ada 100755 --- a/JGE/src/iOS/EAGLView.m +++ b/JGE/src/iOS/EAGLView.m @@ -362,6 +362,37 @@ static NSString *_MY_AD_WHIRL_APPLICATION_KEY_IPAD = @"2e70e3f3da40408588b9a3170 } } +#pragma mark - +#pragma mark Game life cycle methods + +- (void)destroyGame +{ + g_engine->SetApp(NULL); + if (g_app) + { + g_app->Destroy(); + delete g_app; + g_app = NULL; + } + + JGE::Destroy(); + + g_engine = NULL; +} + + +- (void)pauseGame +{ + [self stopAnimation]; + g_engine->Pause(); +} + +- (void)resumeGame +{ + [self startAnimation]; + g_engine->Resume(); +} + #pragma mark - #pragma mark Gesture Recognizer callbacks @@ -429,6 +460,11 @@ static NSString *_MY_AD_WHIRL_APPLICATION_KEY_IPAD = @"2e70e3f3da40408588b9a3170 - (void)handleSingleTap: (UITapGestureRecognizer *) recognizer { [[[recognizer view] layer] removeAllAnimations]; + if (g_engine->IsPaused()) + { + [self resumeGame]; + return; + } currentLocation = [recognizer locationInView: self]; ES2Renderer* es2renderer = (ES2Renderer*)renderer; int actualWidth = (int) JRenderer::GetInstance()->GetActualWidth(); @@ -559,14 +595,6 @@ static NSString *_MY_AD_WHIRL_APPLICATION_KEY_IPAD = @"2e70e3f3da40408588b9a3170 //These are the methods for the AdWhirl Delegate, you have to implement them #pragma mark AdWhirlDelegate methods -- (void) resumeGame { - g_engine->Resume(); -} - -- (void) pauseGame -{ - g_engine->Pause(); -} - (void)adWhirlWillPresentFullScreenModal { //It's recommended to invoke whatever you're using as a "Pause Menu" so your diff --git a/JGE/src/iOS/EAGLViewController.h b/JGE/src/iOS/EAGLViewController.h index ae53d0a76..eb09270e3 100755 --- a/JGE/src/iOS/EAGLViewController.h +++ b/JGE/src/iOS/EAGLViewController.h @@ -5,6 +5,8 @@ BOOL bannerIsVisible; } +void pauseGame(); +void resumeGame(); @property (nonatomic, retain) id eaglView; @property (nonatomic, retain) UITextField *inputField; diff --git a/JGE/src/iOS/EAGLViewController.m b/JGE/src/iOS/EAGLViewController.m index 9a77ca56c..07a696489 100755 --- a/JGE/src/iOS/EAGLViewController.m +++ b/JGE/src/iOS/EAGLViewController.m @@ -50,12 +50,28 @@ - (void)viewWillAppear:(BOOL)animated { NSLog(@"EAGL ViewController - view Will Appear"); - + [self.view resumeGame]; } - (void)viewWillDisappear:(BOOL)animated { + [self.view pauseGame]; +} + +- (void)pauseGame +{ + [self.view pauseGame]; +} + +- (void)resumeGame +{ + [self.view resumeGame]; +} + +- (void)endGame +{ + [self.view endGame]; } - (void)viewDidAppear:(BOOL)animated { diff --git a/JGE/src/iOS/JSfx.cpp b/JGE/src/iOS/JSfx.cpp index 260774823..b7c060920 100644 --- a/JGE/src/iOS/JSfx.cpp +++ b/JGE/src/iOS/JSfx.cpp @@ -118,6 +118,18 @@ JMusic *JSoundSystem::LoadMusic(const char *fileName) } +void JSoundSystem::ResumeMusic(JMusic *music) +{ + [[SoundManager sharedSoundManager] resumeMusic]; +} + + +void JSoundSystem::PauseMusic(JMusic *music) +{ + [[SoundManager sharedSoundManager] pauseMusic]; +} + + void JSoundSystem::PlayMusic(JMusic *music, bool looping) { NSString *key = [NSString stringWithCString: music->key.c_str() encoding: NSUTF8StringEncoding]; @@ -156,10 +168,13 @@ JSample *JSoundSystem::LoadSample(const char *fileName) { NSArray *components = [[NSString stringWithCString:fileName encoding:NSUTF8StringEncoding] componentsSeparatedByString:@"."]; string fullpath = JFileSystem::GetInstance()->GetResourceFile(fileName); - sample->filename = fullpath; - sample->ext = [[components lastObject] cStringUsingEncoding: NSUTF8StringEncoding]; NSString *key = [components objectAtIndex:0]; NSString *musicFile = [NSString stringWithCString: fullpath.c_str() encoding:NSUTF8StringEncoding]; + sample->filename = fullpath; + sample->ext = [[components lastObject] cStringUsingEncoding: NSUTF8StringEncoding]; + if ([key isEqualToString: @""]) + return sample; + sample->key = [key cStringUsingEncoding: NSUTF8StringEncoding]; [[SoundManager sharedSoundManager] loadSoundWithKey: key musicFile: musicFile]; } return sample; diff --git a/JGE/src/iOS/wagicAppDelegate.m b/JGE/src/iOS/wagicAppDelegate.m index 712cd7104..c27dd861b 100755 --- a/JGE/src/iOS/wagicAppDelegate.m +++ b/JGE/src/iOS/wagicAppDelegate.m @@ -253,11 +253,20 @@ NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; [dnc removeObserver: self name: @"intializeGame" object: nil]; [dnc removeObserver: self name: @"readyToStartGame" object: nil]; + [dnc addObserver: glViewController selector:@selector(pauseGame) name: UIApplicationWillResignActiveNotification object: nil]; + [dnc addObserver: glViewController selector:@selector(resumeGame) name: UIApplicationDidBecomeActiveNotification object: nil]; + [dnc addObserver: glViewController selector:@selector(resumeGame) name:UIApplicationWillEnterForegroundNotification object: nil]; + [dnc addObserver: glViewController selector:@selector(destroyGame) name:UIApplicationWillTerminateNotification object: nil]; } - (void)dealloc { + NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; + [dnc removeObserver: glViewController name: UIApplicationDidBecomeActiveNotification object: nil]; + [dnc removeObserver: glViewController name: UIApplicationDidEnterBackgroundNotification object: nil]; + [dnc removeObserver: glViewController name: UIApplicationWillTerminateNotification object: nil]; + [dnc removeObserver: glViewController name: UIApplicationWillResignActiveNotification object: nil]; [window release]; [glViewController release]; [hostReach release]; @@ -312,36 +321,9 @@ return YES; } -- (void)applicationWillResignActive:(UIApplication *)application -{ - if ( [self.glViewController.view respondsToSelector: @selector(stopAnimation)]) - [self.glViewController.view stopAnimation]; -} - -- (void)applicationDidBecomeActive:(UIApplication *)application -{ - if ( [self.glViewController.view respondsToSelector: @selector(stopAnimation)]) - [self.glViewController.view startAnimation]; -} - - -- (void)applicationWillEnterForeground:(UIApplication *)application -{ - if ( [self.glViewController.view respondsToSelector: @selector(stopAnimation)]) - [self.glViewController.view startAnimation]; -} - -- (void)applicationDidEnterBackground:(UIApplication *)application -{ - if ( [self.glViewController.view respondsToSelector: @selector(stopAnimation)]) - [self.glViewController.view stopAnimation]; -} - - - (void)applicationWillTerminate:(UIApplication *)application { - if ( [self.glViewController.view respondsToSelector: @selector(stopAnimation)]) - [self.glViewController.view stopAnimation]; + [self.glViewController.view destroyGame]; } - (void)initializeKeyboard: (id) initialState diff --git a/JGE/src/pc/JSfx.cpp b/JGE/src/pc/JSfx.cpp index 6065eb8cd..243811d26 100644 --- a/JGE/src/pc/JSfx.cpp +++ b/JGE/src/pc/JSfx.cpp @@ -220,6 +220,18 @@ void JSoundSystem::StopMusic(JMusic *music) } +void JSoundSystem::PauseMusic(JMusic *music) +{ + StopMusic(music); +} + + +void JSoundSystem::ResumeMusic(JMusic *music) +{ + PlayMusic(music); +} + + void JSoundSystem::SetVolume(int volume) { SetMusicVolume(volume); diff --git a/projects/mtg/include/GameApp.h b/projects/mtg/include/GameApp.h index e9aaf75af..52a38f859 100644 --- a/projects/mtg/include/GameApp.h +++ b/projects/mtg/include/GameApp.h @@ -94,6 +94,8 @@ public: static string currentMusicFile; static void playMusic(string filename = "", bool loop = true); static void stopMusic(); + static void pauseMusic(); + static void resumeMusic(); static PlayerType players[2]; }; diff --git a/projects/mtg/src/GameApp.cpp b/projects/mtg/src/GameApp.cpp index 232341d96..3b08ee9bc 100644 --- a/projects/mtg/src/GameApp.cpp +++ b/projects/mtg/src/GameApp.cpp @@ -474,12 +474,12 @@ void GameApp::SetCurrentState(GameState * state) void GameApp::Pause() { - stopMusic(); + pauseMusic(); } void GameApp::Resume() { - playMusic(); + resumeMusic(); } void GameApp::DoTransition(int trans, int tostate, float dur, bool animonly) @@ -555,6 +555,24 @@ void GameApp::playMusic(string filename, bool loop) } } +void GameApp::pauseMusic() +{ + if (music && currentMusicFile != "") + { + JSoundSystem::GetInstance()->PauseMusic(music); + } +} + + + +void GameApp::resumeMusic() +{ + if (music && currentMusicFile != "") + { + JSoundSystem::GetInstance()->ResumeMusic(music); + } +} + void GameApp::stopMusic() { if (music && currentMusicFile != "") diff --git a/projects/mtg/src/WCachedResource.cpp b/projects/mtg/src/WCachedResource.cpp index bd185cf21..200476878 100644 --- a/projects/mtg/src/WCachedResource.cpp +++ b/projects/mtg/src/WCachedResource.cpp @@ -278,8 +278,8 @@ void WCachedSample::Refresh() bool WCachedSample::Attempt(const string& filename, int submode, int & error) { loadedMode = submode; - - sample = JSoundSystem::GetInstance()->LoadSample(WResourceManager::Instance()->sfxFile(filename).c_str()); + string sfxFile = WResourceManager::Instance()->sfxFile(filename); + sample = JSoundSystem::GetInstance()->LoadSample(sfxFile.c_str()); if (!isGood()) { diff --git a/projects/mtg/wagic.xcodeproj/project.pbxproj b/projects/mtg/wagic.xcodeproj/project.pbxproj index 7b029797c..171340a87 100755 --- a/projects/mtg/wagic.xcodeproj/project.pbxproj +++ b/projects/mtg/wagic.xcodeproj/project.pbxproj @@ -569,6 +569,7 @@ 12272FC314CD57CF00192DC7 /* SimpleButton.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SimpleButton.cpp; sourceTree = ""; }; 12272FC614CD68FB00192DC7 /* InteractiveButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InteractiveButton.h; sourceTree = ""; }; 12272FC714CD6A3900192DC7 /* InteractiveButton.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InteractiveButton.cpp; sourceTree = ""; }; + 1235D03C14DE396D00B02B42 /* JSfx.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = JSfx.cpp; path = android/JSfx.cpp; sourceTree = ""; }; 12769483144127380088F6D3 /* AIPlayerBaka.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AIPlayerBaka.cpp; sourceTree = ""; }; 12769484144127380088F6D3 /* AIPlayerBakaB.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AIPlayerBakaB.cpp; sourceTree = ""; }; 12769485144127380088F6D3 /* TestSuiteAI.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TestSuiteAI.cpp; sourceTree = ""; }; @@ -1166,6 +1167,14 @@ name = Controllers; sourceTree = ""; }; + 1235D03D14DE397200B02B42 /* android */ = { + isa = PBXGroup; + children = ( + 1235D03C14DE396D00B02B42 /* JSfx.cpp */, + ); + name = android; + sourceTree = ""; + }; 125C5CF213B09AA200DF2F2C /* Tools */ = { isa = PBXGroup; children = ( @@ -1845,6 +1854,7 @@ CEE232AF128A01F400C34032 /* src */ = { isa = PBXGroup; children = ( + 1235D03D14DE397200B02B42 /* android */, 12DCD02B14DBE1AF0023B966 /* ios */, 12B8121D1404B9E10092E303 /* zipFS */, CEE232B3128A01F400C34032 /* hge */,