project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m
changeset 6000 dbcebcd3d79f
parent 5975 c3a8cab64fcb
child 6017 24631fd2fb9e
equal deleted inserted replaced
5998:e8f44e9433f0 6000:dbcebcd3d79f
    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"
       
    28 #include <unistd.h>
    27 #include <unistd.h>
    29 
    28 
    30 
    29 
    31 @implementation SDLUIKitDelegate (customDelegate)
    30 @implementation SDLUIKitDelegate (customDelegate)
    32 
    31 
    35 }
    34 }
    36 
    35 
    37 @end
    36 @end
    38 
    37 
    39 @implementation HedgewarsAppDelegate
    38 @implementation HedgewarsAppDelegate
    40 @synthesize mainViewController, uiwindow, secondWindow, isInGame, backgroundMusic;
    39 @synthesize mainViewController, uiwindow, secondWindow, isInGame;
    41 
    40 
    42 // convenience method
    41 // convenience method
    43 +(HedgewarsAppDelegate *)sharedAppDelegate {
    42 +(HedgewarsAppDelegate *)sharedAppDelegate {
    44     return (HedgewarsAppDelegate *)[[UIApplication sharedApplication] delegate];
    43     return (HedgewarsAppDelegate *)[[UIApplication sharedApplication] delegate];
    45 }
       
    46 
       
    47 #pragma mark -
       
    48 #pragma mark Music control
       
    49 +(void) playBackgroundMusic {
       
    50     if ([HedgewarsAppDelegate sharedAppDelegate].backgroundMusic == nil)
       
    51         [HedgewarsAppDelegate loadBackgroundMusic];
       
    52     [[HedgewarsAppDelegate sharedAppDelegate].backgroundMusic play];
       
    53 }
       
    54 
       
    55 +(void) pauseBackgroundMusic {
       
    56     [[HedgewarsAppDelegate sharedAppDelegate].backgroundMusic pause];
       
    57 }
       
    58 
       
    59 +(void) stopBackgroundMusic {
       
    60     [[HedgewarsAppDelegate sharedAppDelegate].backgroundMusic stop];
       
    61 }
       
    62 
       
    63 +(void) loadBackgroundMusic {
       
    64     NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"];
       
    65     AVAudioPlayer *background = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:nil];
       
    66 
       
    67     background.delegate = nil;
       
    68     background.volume = 0.4f;
       
    69     background.numberOfLoops = -1;
       
    70     [background prepareToPlay];
       
    71     [HedgewarsAppDelegate sharedAppDelegate].backgroundMusic = background;
       
    72     [background release];
       
    73 }
    44 }
    74 
    45 
    75 #pragma mark -
    46 #pragma mark -
    76 #pragma mark AppDelegate methods
    47 #pragma mark AppDelegate methods
    77 -(id) init {
    48 -(id) init {
    78     if (self = [super init]){
    49     if (self = [super init]){
    79         mainViewController = nil;
    50         mainViewController = nil;
    80         uiwindow = nil;
    51         uiwindow = nil;
    81         secondWindow = nil;
    52         secondWindow = nil;
    82         isInGame = NO;
    53         isInGame = NO;
    83         backgroundMusic = nil;
       
    84     }
    54     }
    85     return self;
    55     return self;
    86 }
    56 }
    87 
    57 
    88 -(void) dealloc {
    58 -(void) dealloc {
    89     [mainViewController release];
    59     [mainViewController release];
    90     [uiwindow release];
    60     [uiwindow release];
    91     [secondWindow release];
    61     [secondWindow release];
    92     [backgroundMusic release];
       
    93     [super dealloc];
    62     [super dealloc];
    94 }
    63 }
    95 
    64 
    96 // override the direct execution of SDL_main to allow us to implement our own frontend
    65 // override the direct execution of SDL_main to allow us to implement our own frontend
    97 -(void) postFinishLaunch {
    66 -(void) postFinishLaunch {
   123 }
    92 }
   124 
    93 
   125 -(void) applicationDidReceiveMemoryWarning:(UIApplication *)application {
    94 -(void) applicationDidReceiveMemoryWarning:(UIApplication *)application {
   126     // don't stop music when it is playing
    95     // don't stop music when it is playing
   127     if (self.isInGame) {
    96     if (self.isInGame) {
   128         [self.backgroundMusic stop];
    97         [AudioManagerController didReceiveMemoryWarning];
   129         self.backgroundMusic = nil;
       
   130         MSG_MEMCLEAN();
    98         MSG_MEMCLEAN();
   131     }
    99     }
   132     print_free_memory();
   100     print_free_memory();
   133     // don't clean mainMenuViewController here!!!
   101     // don't clean mainMenuViewController here!!!
   134 }
   102 }
   135 
   103 
   136 // true multitasking with sdl works only on 4.2 and above; we close the game to avoid a black screen at return
   104 // true multitasking with sdl works only on 4.2 and above; we close the game to avoid a black screen at return
   137 -(void) applicationWillResignActive:(UIApplication *)application {
   105 -(void) applicationWillResignActive:(UIApplication *)application {
   138     if (self.isInGame && [[[UIDevice currentDevice] systemVersion] floatValue] < 4.2f)
   106     if (self.isInGame && [[[UIDevice currentDevice] systemVersion] floatValue] < 4.2f)
   139             HW_terminate(NO);
   107          HW_terminate(NO);
   140 
   108 
   141     [super applicationWillResignActive:application];
   109     [super applicationWillResignActive:application];
   142 }
   110 }
   143 
   111 
   144 @end
   112 @end