author | koda |
Sun, 24 Jul 2011 19:14:56 +0200 | |
changeset 5451 | e359a79e3d08 |
parent 5207 | 4c9ae0f484da |
child 5483 | fc755bb8096d |
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" |
5201 | 31 |
#import "hwconsts.h" |
3547 | 32 |
|
33 |
||
3940
cc29628976cc
some optimizations to drawing and fetching data of new ammomenu
koda
parents:
3930
diff
changeset
|
34 |
NSInteger inline randomPort () { |
3547 | 35 |
srandom(time(NULL)); |
3971 | 36 |
NSInteger res = (random() % 64511) + 1024; |
5201 | 37 |
return (res == NETGAME_DEFAULT_PORT) ? randomPort() : res; |
3547 | 38 |
} |
39 |
||
40 |
// by http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html |
|
41 |
void print_free_memory () { |
|
42 |
mach_port_t host_port; |
|
43 |
mach_msg_type_number_t host_size; |
|
44 |
vm_size_t pagesize; |
|
3697 | 45 |
|
3547 | 46 |
host_port = mach_host_self(); |
47 |
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t); |
|
3697 | 48 |
host_page_size(host_port, &pagesize); |
49 |
||
3547 | 50 |
vm_statistics_data_t vm_stat; |
3697 | 51 |
|
3547 | 52 |
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) |
53 |
DLog(@"Failed to fetch vm statistics"); |
|
3697 | 54 |
|
55 |
/* Stats in bytes */ |
|
3547 | 56 |
natural_t mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize; |
57 |
natural_t mem_free = vm_stat.free_count * pagesize; |
|
58 |
natural_t mem_total = mem_used + mem_free; |
|
59 |
DLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total); |
|
60 |
} |
|
3668
3f7a95234d8a
tap to play piano notes, fix for audio and pause glitch
koda
parents:
3660
diff
changeset
|
61 |
|
3930 | 62 |
BOOL inline isApplePhone () { |
3996 | 63 |
return (IS_IPAD() == NO); |
3668
3f7a95234d8a
tap to play piano notes, fix for audio and pause glitch
koda
parents:
3660
diff
changeset
|
64 |
} |
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
65 |
|
5207
4c9ae0f484da
some general tweaks (more intelligent use of macros and more caching)
koda
parents:
5206
diff
changeset
|
66 |
NSString *getModelType () { |
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
67 |
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
|
68 |
// set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space |
3697 | 69 |
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
|
70 |
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
|
71 |
// 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
|
72 |
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
|
73 |
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
|
74 |
free(name); |
3697 | 75 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3668
diff
changeset
|
76 |
return modelId; |
3697 | 77 |
} |
3783 | 78 |
|
79 |
void playSound (NSString *snd) { |
|
5207
4c9ae0f484da
some general tweaks (more intelligent use of macros and more caching)
koda
parents:
5206
diff
changeset
|
80 |
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == YES) { |
4947 | 81 |
// get the filename of the sound file: |
82 |
NSString *path = [NSString stringWithFormat:@"%@/%@.wav",[[NSBundle mainBundle] resourcePath],snd]; |
|
3783 | 83 |
|
4947 | 84 |
// declare a system sound id and get a URL for the sound file |
85 |
SystemSoundID soundID; |
|
86 |
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; |
|
3783 | 87 |
|
4947 | 88 |
// use audio sevices to create and play the sound |
89 |
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); |
|
90 |
AudioServicesPlaySystemSound(soundID); |
|
91 |
} |
|
3917 | 92 |
} |
93 |
||
5201 | 94 |
NSArray *getAvailableColors (void) { |
5207
4c9ae0f484da
some general tweaks (more intelligent use of macros and more caching)
koda
parents:
5206
diff
changeset
|
95 |
// by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing |
5201 | 96 |
unsigned int colors[] = HW_TEAMCOLOR_ARRAY; |
97 |
NSMutableArray *array = [[NSMutableArray alloc] init]; |
|
98 |
||
99 |
int i = 0; |
|
100 |
while(colors[i] != 0) |
|
101 |
[array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]]; |
|
102 |
||
103 |
NSArray *final = [NSArray arrayWithArray:array]; |
|
104 |
[array release]; |
|
105 |
return final; |
|
3917 | 106 |
} |
3983
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
107 |
|
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
108 |
UILabel *createBlueLabel (NSString *title, CGRect frame) { |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
109 |
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
|
110 |
} |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
111 |
|
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
112 |
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
|
113 |
UILabel *theLabel = [[UILabel alloc] initWithFrame:frame]; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
114 |
theLabel.backgroundColor = backgroundColor; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
115 |
|
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
116 |
if (title != nil) { |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
117 |
theLabel.text = title; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
118 |
theLabel.textColor = UICOLOR_HW_YELLOW_TEXT; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
119 |
theLabel.textAlignment = UITextAlignmentCenter; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
120 |
theLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]*80/100]; |
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 |
|
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
123 |
[theLabel.layer setBorderWidth:borderWidth]; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
124 |
[theLabel.layer setBorderColor:borderColor.CGColor]; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
125 |
[theLabel.layer setCornerRadius:8.0f]; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
126 |
[theLabel.layer setMasksToBounds:YES]; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
127 |
|
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
128 |
return theLabel; |
aa24192417a8
use labels instead of images, should save ram and space
koda
parents:
3982
diff
changeset
|
129 |
} |
4290 | 130 |
|
131 |
// this routine checks for the PNG size without loading it in memory |
|
132 |
// https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m |
|
133 |
CGSize PSPNGSizeFromMetaData (NSString *aFileName) { |
|
134 |
// File Name to C String. |
|
135 |
const char *fileName = [aFileName UTF8String]; |
|
136 |
// source file |
|
137 |
FILE *infile = fopen(fileName, "rb"); |
|
138 |
if (infile == NULL) { |
|
139 |
DLog(@"Can't open the file: %@", aFileName); |
|
140 |
return CGSizeZero; |
|
141 |
} |
|
142 |
||
143 |
// Bytes Buffer. |
|
144 |
unsigned char buffer[30]; |
|
145 |
// Grab Only First Bytes. |
|
146 |
fread(buffer, 1, 30, infile); |
|
147 |
// Close File. |
|
148 |
fclose(infile); |
|
149 |
||
150 |
// PNG Signature. |
|
151 |
unsigned char png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; |
|
152 |
||
153 |
// Compare File signature. |
|
154 |
if ((int)(memcmp(&buffer[0], &png_signature[0], 8))) { |
|
155 |
DLog(@"The file (%@) is not a PNG file", aFileName); |
|
156 |
return CGSizeZero; |
|
157 |
} |
|
158 |
||
159 |
// Calc Sizes. Isolate only four bytes of each size (width, height). |
|
160 |
int width[4]; |
|
161 |
int height[4]; |
|
162 |
for (int d = 16; d < (16 + 4); d++) { |
|
163 |
width[d-16] = buffer[d]; |
|
164 |
height[d-16] = buffer[d+4]; |
|
165 |
} |
|
166 |
||
167 |
// Convert bytes to Long (Integer) |
|
168 |
long resultWidth = (width[0] << (int)24) | (width[1] << (int)16) | (width[2] << (int)8) | width[3]; |
|
169 |
long resultHeight = (height[0] << (int)24) | (height[1] << (int)16) | (height[2] << (int)8) | height[3]; |
|
170 |
||
171 |
// Return Size. |
|
172 |
return CGSizeMake(resultWidth,resultHeight); |
|
173 |
} |
|
4510 | 174 |
|
175 |
@implementation NSString (extra) |
|
176 |
||
4512 | 177 |
-(NSString *)MD5hash { |
4510 | 178 |
const char *cStr = [self UTF8String]; |
179 |
unsigned char result[16]; |
|
180 |
CC_MD5( cStr, strlen(cStr), result ); |
|
181 |
return [NSString stringWithFormat: |
|
4512 | 182 |
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", |
4510 | 183 |
result[0], result[1], result[2], result[3], result[4], result[5], |
184 |
result[6], result[7], result[8], result[9], result[10], result[11], |
|
185 |
result[12], result[13], result[14], result[15]]; |
|
186 |
} |
|
187 |
||
188 |
||
189 |
@end |