project_files/HedgewarsMobile/Classes/CommodityFunctions.m
author koda
Fri, 12 Nov 2010 22:47:13 +0100
changeset 4281 e033cf015b2c
parent 4213 3875481fcc86
child 4290 82f1f1d819c0
permissions -rw-r--r--
redo once again file updating, moving stuff around
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     1
/*
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     3
 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     4
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     8
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    12
 * GNU General Public License for more details.
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    13
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    14
 * You should have received a copy of the GNU General Public License
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    15
 * along with this program; if not, write to the Free Software
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    17
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    18
 * File created on 08/04/2010.
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    19
 */
81db3c85784b headers ftw, also right project file
koda
parents: 3823
diff changeset
    20
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    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
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
#import <mach/mach.h>
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    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>
3783
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
    28
#import "AudioToolbox/AudioToolbox.h"
3926
668b71f31e51 use dynamic data from engine instead of using hardcoded values
koda
parents: 3917
diff changeset
    29
#import "PascalImports.h"
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3930
diff changeset
    31
BOOL inline rotationManager (UIInterfaceOrientation interfaceOrientation) {
4098
40df542b5f62 i give up and disable rotation on the iphone; also fix smaller compilation issues
koda
parents: 3996
diff changeset
    32
    if (IS_IPAD())
40df542b5f62 i give up and disable rotation on the iphone; also fix smaller compilation issues
koda
parents: 3996
diff changeset
    33
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
40df542b5f62 i give up and disable rotation on the iphone; also fix smaller compilation issues
koda
parents: 3996
diff changeset
    34
               (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
40df542b5f62 i give up and disable rotation on the iphone; also fix smaller compilation issues
koda
parents: 3996
diff changeset
    35
    else
40df542b5f62 i give up and disable rotation on the iphone; also fix smaller compilation issues
koda
parents: 3996
diff changeset
    36
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
3940
cc29628976cc some optimizations to drawing and fetching data of new ammomenu
koda
parents: 3930
diff changeset
    39
NSInteger inline randomPort () {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    srandom(time(NULL));
3971
5c82ee165ed5 minor stuff
koda
parents: 3941
diff changeset
    41
    NSInteger res = (random() % 64511) + 1024;
5c82ee165ed5 minor stuff
koda
parents: 3941
diff changeset
    42
    return (res == DEFAULT_NETGAME_PORT) ? randomPort() : res;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
void popError (const char *title, const char *message) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithUTF8String:title]
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
                                                    message:[NSString stringWithUTF8String:message]
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
                                                   delegate:nil
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
                                          cancelButtonTitle:@"Ok"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
                                          otherButtonTitles:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
    [alert show];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    [alert release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
// by http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
void print_free_memory () {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
    mach_port_t host_port;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    mach_msg_type_number_t host_size;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
    vm_size_t pagesize;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    60
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
    host_port = mach_host_self();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
    host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    63
    host_page_size(host_port, &pagesize);
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    64
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
    vm_statistics_data_t vm_stat;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    66
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
    if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
        DLog(@"Failed to fetch vm statistics");
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    69
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    70
    /* Stats in bytes */
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    natural_t mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    natural_t mem_free = vm_stat.free_count * pagesize;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
    natural_t mem_total = mem_used + mem_free;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
    DLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
}
3668
3f7a95234d8a tap to play piano notes, fix for audio and pause glitch
koda
parents: 3660
diff changeset
    76
3930
8b00b4f93242 remove a useless field from weaponfile format
koda
parents: 3926
diff changeset
    77
BOOL inline isApplePhone () {
3996
eb549fd864a5 the ios port is also iphone compatible
koda
parents: 3983
diff changeset
    78
    return (IS_IPAD() == NO);
3668
3f7a95234d8a tap to play piano notes, fix for audio and pause glitch
koda
parents: 3660
diff changeset
    79
}
3670
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    80
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    81
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
    82
    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
    83
    // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    84
    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
    85
    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
    86
    // 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
    87
    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
    88
    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
    89
    free(name);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    90
3670
4c673e57f0d7 use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents: 3668
diff changeset
    91
    return modelId;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3670
diff changeset
    92
}
3783
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
    93
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
    94
void playSound (NSString *snd) {
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
    95
    //Get the filename of the sound file:
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
    96
    NSString *path = [NSString stringWithFormat:@"%@/%@.wav",[[NSBundle mainBundle] resourcePath],snd];
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
    97
    
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
    98
    //declare a system sound id
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
    99
    SystemSoundID soundID;
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
   100
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
   101
    //Get a URL for the sound file
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
   102
    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
   103
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
   104
    //Use audio sevices to create the sound
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
   105
    AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
   106
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
   107
    //Use audio services to play the sound
8e9daf967406 X&Y buttons, sounds for some buttons, cleanup
koda
parents: 3781
diff changeset
   108
    AudioServicesPlaySystemSound(soundID);
3917
4c243b1eac97 playing a bit with colors
koda
parents: 3878
diff changeset
   109
}
4c243b1eac97 playing a bit with colors
koda
parents: 3878
diff changeset
   110
3983
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   111
NSArray inline *getAvailableColors (void) {
4213
3875481fcc86 smaller update (color and modes)
koda
parents: 4211
diff changeset
   112
    return [NSArray arrayWithObjects:[NSNumber numberWithUnsignedInt:0x3376E9],     // bluette
3917
4c243b1eac97 playing a bit with colors
koda
parents: 3878
diff changeset
   113
                                     [NSNumber numberWithUnsignedInt:0x3e9321],     // greeeen
4c243b1eac97 playing a bit with colors
koda
parents: 3878
diff changeset
   114
                                     [NSNumber numberWithUnsignedInt:0xa23dbb],     // violett
4c243b1eac97 playing a bit with colors
koda
parents: 3878
diff changeset
   115
                                     [NSNumber numberWithUnsignedInt:0xff9329],     // oranngy
4c243b1eac97 playing a bit with colors
koda
parents: 3878
diff changeset
   116
                                     [NSNumber numberWithUnsignedInt:0xdd0000],     // reddish
4c243b1eac97 playing a bit with colors
koda
parents: 3878
diff changeset
   117
                                     [NSNumber numberWithUnsignedInt:0x737373],     // graaaay
4213
3875481fcc86 smaller update (color and modes)
koda
parents: 4211
diff changeset
   118
                                     [NSNumber numberWithUnsignedInt:0x00FFFF],     // cyannnn  
3875481fcc86 smaller update (color and modes)
koda
parents: 4211
diff changeset
   119
                                     [NSNumber numberWithUnsignedInt:0xFF8888],     // peachyj
3917
4c243b1eac97 playing a bit with colors
koda
parents: 3878
diff changeset
   120
                                     nil];
4c243b1eac97 playing a bit with colors
koda
parents: 3878
diff changeset
   121
}
3983
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
UILabel *createBlueLabel (NSString *title, CGRect frame) {
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   124
    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
   125
}
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   126
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   127
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
   128
    UILabel *theLabel = [[UILabel alloc] initWithFrame:frame];
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   129
    theLabel.backgroundColor = backgroundColor;
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   130
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   131
    if (title != nil) {
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   132
        theLabel.text = title;
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   133
        theLabel.textColor = UICOLOR_HW_YELLOW_TEXT;
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   134
        theLabel.textAlignment = UITextAlignmentCenter;
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   135
        theLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]*80/100];
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
    
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   138
    [theLabel.layer setBorderWidth:borderWidth];
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   139
    [theLabel.layer setBorderColor:borderColor.CGColor];
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   140
    [theLabel.layer setCornerRadius:8.0f];
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   141
    [theLabel.layer setMasksToBounds:YES];
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   142
    
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   143
    return theLabel;
aa24192417a8 use labels instead of images, should save ram and space
koda
parents: 3982
diff changeset
   144
}