summary refs log tree commit diff stats
path: root/Classes/Cart_CollectAppDelegate.m
blob: c92d231c108ced8e0581d8685438469750d7fb1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
//  Cart_CollectAppDelegate.m
//  Cart Collect
//
//  Created by iD Student Account on 7/18/11.
//  Copyright __MyCompanyName__ 2011. All rights reserved.
//

#import "cocos2d.h"

#import "Cart_CollectAppDelegate.h"
#import "GameConfig.h"
#import "GameMode.h"
#import "RootViewController.h"
#import "MainMenuLayer.h"
#import "TestFlight.h"
#import "CCNotifications.h"
#import "notificationDesign.h"

@implementation Cart_CollectAppDelegate

@synthesize window, viewController;

- (void) removeStartupFlicker
{
	//
	// THIS CODE REMOVES THE STARTUP FLICKER
	//
	// Uncomment the following code if you Application only supports landscape mode
	//
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
	
	//	CC_ENABLE_DEFAULT_GL_STATES();
	//	CCDirector *director = [CCDirector sharedDirector];
	//	CGSize size = [director winSize];
	//	CCSprite *sprite = [CCSprite spriteWithFile:@"Default.png"];
	//	sprite.position = ccp(size.width/2, size.height/2);
	//	sprite.rotation = -90;
	//	[sprite visit];
	//	[[director openGLView] swapBuffers];
	//	CC_ENABLE_DEFAULT_GL_STATES();
	
#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController	
}

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
    // REMOVE THIS LINE FOR RELEASE BUILDS
    [TestFlight takeOff:@"66a3925c85c93e7628c14d167ff6c1b7_MjM4MTEyMDExLTA4LTE3IDEzOjEyOjQ4Ljg2NDE2OQ"];
    
	[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
	
	NSMutableDictionary* registerDefaults = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                             @"", @"username",
                                             [NSNumber numberWithBool:YES], @"submitScore",
                                             nil];
	[[NSUserDefaults standardUserDefaults] registerDefaults:registerDefaults];
	
	// Init the window
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
	// Try to use CADisplayLink director
	// if it fails (SDK < 3.1) use the default director
	if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
		[CCDirector setDirectorType:kCCDirectorTypeDefault];
	
	
	CCDirector *director = [CCDirector sharedDirector];
    CCNotifications* notifications = [CCNotifications sharedManager];
    [director setNotificationNode:notifications];
	
	// Init the View Controller
	viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
	viewController.wantsFullScreenLayout = YES;
	
	//
	// Create the EAGLView manually
	//  1. Create a RGB565 format. Alternative: RGBA8
	//	2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
	//
	//
	EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
								   pixelFormat:kEAGLColorFormatRGB565	// kEAGLColorFormatRGBA8
								   depthFormat:0						// GL_DEPTH_COMPONENT16_OES
						];
	
	// attach the openglView to the director
	[director setOpenGLView:glView];
	
//	// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
	//if( ! [director enableRetinaDisplay:YES] )
	//	CCLOG(@"Retina Display Not supported");
	
	//
	// VERY IMPORTANT:
	// If the rotation is going to be controlled by a UIViewController
	// then the device orientation should be "Portrait".
	//
	// IMPORTANT:
	// By default, this template only supports Landscape orientations.
	// Edit the RootViewController.m file to edit the supported orientations.
	//
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
	[director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
	[director setDeviceOrientation:kCCDeviceOrientationPortrait];
#endif
	
	[director setAnimationInterval:1.0/60];
	//[director setDisplayFPS:YES];
	
	
	// make the OpenGLView a child of the view controller
	[viewController setView:glView];
	
	// make the View Controller a child of the main window
    [window setRootViewController:viewController];
	//[window addSubview: viewController.view];
	
	[window makeKeyAndVisible];
	
	// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
	// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
	// You can change anytime.
	[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];

	
	// Removes the startup flicker
	[self removeStartupFlicker];
	
	// Run the intro Scene
	[[CCDirector sharedDirector] runWithScene: [MainMenuLayer scene]];
}


- (void)applicationWillResignActive:(UIApplication *)application {
	[[CCDirector sharedDirector] pause];
    
    [[NSUserDefaults standardUserDefaults] synchronize];
	
	if ([[CCDirector sharedDirector] runningScene].tag == GAME_SCENE)
	{
        [((GameMode*)[[[CCDirector sharedDirector] runningScene] getChildByTag:GAME_LAYER]) pause];
	}
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
	[[CCDirector sharedDirector] resume];
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
	[[CCDirector sharedDirector] purgeCachedData];
}

-(void) applicationDidEnterBackground:(UIApplication*)application {
	[[CCDirector sharedDirector] stopAnimation];
}

-(void) applicationWillEnterForeground:(UIApplication*)application {
	[[CCDirector sharedDirector] startAnimation];
}

- (void)applicationWillTerminate:(UIApplication *)application {
	CCDirector *director = [CCDirector sharedDirector];
	
	sqlite3_close([Cart_CollectAppDelegate database]);
	
	[[director openGLView] removeFromSuperview];
	
	[viewController release];
	
	[window release];
	
	[director end];	
}

- (void)applicationSignificantTimeChange:(UIApplication *)application {
	[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
}

- (void)dealloc {
	//[[CCDirector sharedDirector] release];
	[window release];
	[super dealloc];
}

+ (sqlite3*)database
{
	static sqlite3* database = nil;
	
	if (database == nil)
	{
		NSString* databaseName = @"cartdata.sqlite3";
		NSArray* documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString* documentsDir = [documentPaths objectAtIndex:0];
		NSString* databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
		NSFileManager* fileManager = [NSFileManager defaultManager];
        NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
		
		if (![fileManager fileExistsAtPath:databasePath])
		{
			NSString* databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
			[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
			
			[userDefaults setInteger:2 forKey:@"databaseVersion"];
		}
        
        if (sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK)
		{
			NSLog(@"Failed to open database");
		}
		
        // Database migrations!
		if ([userDefaults integerForKey:@"databaseVersion"] < 1)
		{
			// Upgrade the database to version 1, which is completely unnecessary as no prior version exists
		}
        
        if ([userDefaults integerForKey:@"databaseVersion"] < 2)
		{
			// Database version 2 adds a "gameMode" column to highscores to allow for multiple highscore lists
            const char* sqlQuery = "ALTER TABLE highscores ADD gameMode varchar(255)";
            sqlite3_stmt* compiled_statement;
            
            if (sqlite3_prepare_v2(database, sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK)
            {
                sqlite3_step(compiled_statement);
                
                sqlQuery = "UPDATE highscores SET gameMode = \"Collect\"";
                if (sqlite3_prepare_v2(database, sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK)
                {
                    sqlite3_step(compiled_statement);
                
                    NSLog(@"Updated database to version 2.");
                    [userDefaults setInteger:2 forKey:@"databaseVersion"];
                } else {
                    NSLog(@"Error while updating database to version 2. '%s'", sqlite3_errmsg(database));
                }
            } else {
                NSLog(@"Error while updating database to version 2. '%s'", sqlite3_errmsg(database));
            }
		}
        
        // It's rather important that the database version gets saved
        [userDefaults synchronize];
	}
	
	return database;
}

@end