author | koda |
Wed, 20 Apr 2011 05:26:47 +0200 | |
changeset 5166 | d1eb1560b4d5 |
parent 4947 | cfac6aab84a0 |
child 5201 | 7b9aa7aac336 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 |
* Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
* |
|
18 |
* File created on 08/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "CommodityFunctions.h" |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
23 |
#import <sys/types.h> |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
24 |
#import <sys/sysctl.h> |
3547 | 25 |
#import <mach/mach.h> |
26 |
#import <mach/mach_host.h> |
|
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
27 |
#import <QuartzCore/QuartzCore.h> |
4510 | 28 |
#import <AudioToolbox/AudioToolbox.h> |
29 |
#import <CommonCrypto/CommonDigest.h> |
|
3926
668b71f31e51
use dynamic data from engine instead of using hardcoded values
koda
parents:
3917
diff
changeset
|
30 |
#import "PascalImports.h" |
3547 | 31 |
|
3940
cc29628976cc
some optimizations to drawing and fetching data of new ammomenu
koda
parents:
3930
diff
changeset
|
32 |
BOOL inline rotationManager (UIInterfaceOrientation interfaceOrientation) { |
4356 | 33 |
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || |
34 |
(interfaceOrientation == UIInterfaceOrientationLandscapeLeft); |
|
3547 | 35 |
} |
36 |
||
3940
cc29628976cc
some optimizations to drawing and fetching data of new ammomenu
koda
parents:
3930
diff
changeset
|
37 |
NSInteger inline randomPort () { |
3547 | 38 |
srandom(time(NULL)); |
3971 | 39 |
NSInteger res = (random() % 64511) + 1024; |
40 |
return (res == DEFAULT_NETGAME_PORT) ? randomPort() : res; |
|
3547 | 41 |
} |
42 |
||
43 |
void popError (const char *title, const char *message) { |
|
44 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithUTF8String:title] |
|
45 |
message:[NSString stringWithUTF8String:message] |
|
46 |
delegate:nil |
|
47 |
cancelButtonTitle:@"Ok" |
|
48 |
otherButtonTitles:nil]; |
|
49 |
[alert show]; |
|
50 |
[alert release]; |
|
51 |
} |
|
52 |
||
53 |
// by http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html |
|
54 |
void print_free_memory () { |
|
55 |
mach_port_t host_port; |
|
56 |
mach_msg_type_number_t host_size; |
|
57 |
vm_size_t pagesize; |
|
3697 | 58 |
|
3547 | 59 |
host_port = mach_host_self(); |
60 |
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t); |
|
3697 | 61 |
host_page_size(host_port, &pagesize); |
62 |
||
3547 | 63 |
vm_statistics_data_t vm_stat; |
3697 | 64 |
|
3547 | 65 |
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) |
66 |
DLog(@"Failed to fetch vm statistics"); |
|
3697 | 67 |
|
68 |
/* Stats in bytes */ |
|
3547 | 69 |
natural_t mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize; |
70 |
natural_t mem_free = vm_stat.free_count * pagesize; |
|
71 |
natural_t mem_total = mem_used + mem_free; |
|
72 |
DLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total); |
|
73 |
} |
|
3668
3f7a95234d8a
tap to play piano notes, fix for audio and pause glitch
koda
parents:
3660
diff
changeset
|
74 |
|
3930 | 75 |
BOOL inline isApplePhone () { |
3996 | 76 |
return (IS_IPAD() == NO); |
3668
3f7a95234d8a
tap to play piano notes, fix for audio and pause glitch
koda
parents:
3660
diff
changeset
|
77 |
} |
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
78 |
|
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
79 |
NSString *modelType () { |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
80 |
size_t size; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
81 |
// set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space |
3697 | 82 |
sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
83 |
char *name = (char *)malloc(sizeof(char) * size); |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
84 |
// get the platform name |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
85 |
sysctlbyname("hw.machine", name, &size, NULL, 0); |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
86 |
NSString *modelId = [NSString stringWithUTF8String:name]; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
87 |
free(name); |
3697 | 88 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
89 |
return modelId; |
3697 | 90 |
} |
3783 | 91 |
|
92 |
void playSound (NSString *snd) { |
|
4947 | 93 |
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; |
94 |
NSNumber *audio = [prefs objectForKey:@"audio"]; |
|
95 |
||
96 |
if (audio == nil || [audio boolValue] == YES) { |
|
97 |
// get the filename of the sound file: |
|
98 |
NSString *path = [NSString stringWithFormat:@"%@/%@.wav",[[NSBundle mainBundle] resourcePath],snd]; |
|
3783 | 99 |
|
4947 | 100 |
// declare a system sound id and get a URL for the sound file |
101 |
SystemSoundID soundID; |
|
102 |
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; |
|
3783 | 103 |
|
4947 | 104 |
// use audio sevices to create and play the sound |
105 |
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); |
|
106 |
AudioServicesPlaySystemSound(soundID); |
|
107 |
} |
|
3917 | 108 |
} |
109 |
||
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
110 |
NSArray inline *getAvailableColors (void) { |
4213 | 111 |
return [NSArray arrayWithObjects:[NSNumber numberWithUnsignedInt:0x3376E9], // bluette |
3917 | 112 |
[NSNumber numberWithUnsignedInt:0x3e9321], // greeeen |
113 |
[NSNumber numberWithUnsignedInt:0xa23dbb], // violett |
|
114 |
[NSNumber numberWithUnsignedInt:0xff9329], // oranngy |
|
115 |
[NSNumber numberWithUnsignedInt:0xdd0000], // reddish |
|
116 |
[NSNumber numberWithUnsignedInt:0x737373], // graaaay |
|
4213 | 117 |
[NSNumber numberWithUnsignedInt:0x00FFFF], // cyannnn |
118 |
[NSNumber numberWithUnsignedInt:0xFF8888], // peachyj |
|
3917 | 119 |
nil]; |
120 |
} |
|
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
121 |
|
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
122 |
UILabel *createBlueLabel (NSString *title, CGRect frame) { |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
123 |
return createLabelWithParams(title, frame, 1.5f, UICOLOR_HW_YELLOW_BODER, UICOLOR_HW_DARKBLUE); |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
124 |
} |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
125 |
|
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
126 |
UILabel *createLabelWithParams (NSString *title, CGRect frame, CGFloat borderWidth, UIColor *borderColor, UIColor *backgroundColor) { |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
127 |
UILabel *theLabel = [[UILabel alloc] initWithFrame:frame]; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
128 |
theLabel.backgroundColor = backgroundColor; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
129 |
|
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
130 |
if (title != nil) { |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
131 |
theLabel.text = title; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
132 |
theLabel.textColor = UICOLOR_HW_YELLOW_TEXT; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
133 |
theLabel.textAlignment = UITextAlignmentCenter; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
134 |
theLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]*80/100]; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
135 |
} |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
136 |
|
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
137 |
[theLabel.layer setBorderWidth:borderWidth]; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
138 |
[theLabel.layer setBorderColor:borderColor.CGColor]; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
139 |
[theLabel.layer setCornerRadius:8.0f]; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
140 |
[theLabel.layer setMasksToBounds:YES]; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
141 |
|
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
142 |
return theLabel; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
143 |
} |
4290 | 144 |
|
145 |
// this routine checks for the PNG size without loading it in memory |
|
146 |
// https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m |
|
147 |
CGSize PSPNGSizeFromMetaData (NSString *aFileName) { |
|
148 |
// File Name to C String. |
|
149 |
const char *fileName = [aFileName UTF8String]; |
|
150 |
// source file |
|
151 |
FILE *infile = fopen(fileName, "rb"); |
|
152 |
if (infile == NULL) { |
|
153 |
DLog(@"Can't open the file: %@", aFileName); |
|
154 |
return CGSizeZero; |
|
155 |
} |
|
156 |
||
157 |
// Bytes Buffer. |
|
158 |
unsigned char buffer[30]; |
|
159 |
// Grab Only First Bytes. |
|
160 |
fread(buffer, 1, 30, infile); |
|
161 |
// Close File. |
|
162 |
fclose(infile); |
|
163 |
||
164 |
// PNG Signature. |
|
165 |
unsigned char png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; |
|
166 |
||
167 |
// Compare File signature. |
|
168 |
if ((int)(memcmp(&buffer[0], &png_signature[0], 8))) { |
|
169 |
DLog(@"The file (%@) is not a PNG file", aFileName); |
|
170 |
return CGSizeZero; |
|
171 |
} |
|
172 |
||
173 |
// Calc Sizes. Isolate only four bytes of each size (width, height). |
|
174 |
int width[4]; |
|
175 |
int height[4]; |
|
176 |
for (int d = 16; d < (16 + 4); d++) { |
|
177 |
width[d-16] = buffer[d]; |
|
178 |
height[d-16] = buffer[d+4]; |
|
179 |
} |
|
180 |
||
181 |
// Convert bytes to Long (Integer) |
|
182 |
long resultWidth = (width[0] << (int)24) | (width[1] << (int)16) | (width[2] << (int)8) | width[3]; |
|
183 |
long resultHeight = (height[0] << (int)24) | (height[1] << (int)16) | (height[2] << (int)8) | height[3]; |
|
184 |
||
185 |
// Return Size. |
|
186 |
return CGSizeMake(resultWidth,resultHeight); |
|
187 |
} |
|
4510 | 188 |
|
189 |
@implementation NSString (extra) |
|
190 |
||
4512 | 191 |
-(NSString *)MD5hash { |
4510 | 192 |
const char *cStr = [self UTF8String]; |
193 |
unsigned char result[16]; |
|
194 |
CC_MD5( cStr, strlen(cStr), result ); |
|
195 |
return [NSString stringWithFormat: |
|
4512 | 196 |
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", |
4510 | 197 |
result[0], result[1], result[2], result[3], result[4], result[5], |
198 |
result[6], result[7], result[8], result[9], result[10], result[11], |
|
199 |
result[12], result[13], result[14], result[15]]; |
|
200 |
} |
|
201 |
||
202 |
||
203 |
@end |