Codebase list brainparty / debian/0.61+dfsg-4 BPGame.cpp
debian/0.61+dfsg-4

Tree @debian/0.61+dfsg-4 (Download .tar.gz)

BPGame.cpp @debian/0.61+dfsg-4raw · history · 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
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
// Brain Party
// Copyright (C) 2010 Paul Hudson (http://www.tuxradar.com/brainparty)

// Brain Party is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include <ctime>
#include "BPGame.h"
#include "SDL.h"
#include "SDL_opengl.h" 
#include "SDL_ttf.h" 
#include <string>


#include "bombhunt.h"
#include "balloonblaster.h"
#include "bpsays.h"
#include "bubbletrouble.h"
#include "cardmatch.h"
#include "connex.h"
#include "cupsnballs.h"
#include "diceoff.h"
#include "flashcounting.h"
#include "flashlight.h"
#include "iqtest.h"
#include "jewelflip.h"
#include "jeweljam.h"
#include "marbledrop.h"
#include "memoryblox.h"
#include "memorybox.h"
#include "memorymaths.h"
#include "minesweep.h"
#include "moonjump.h"
#include "nextinline.h"
#include "numbersnake.h"
#include "oddoneout.h"
#include "patchmatch.h"
#include "perfectpaths.h"
#include "routefinder.h"
#include "rps.h"
#include "scrambled.h"
#include "setfinder.h"
#include "sharpshooter.h"
#include "shortcircuitsudoku.h"
#include "shufflepuzzler.h"
#include "strangerdanger.h"
#include "symboliclogic.h"
#include "underthehat.h"
#include "untangler.h"
#include "wordsmash.h"

Texture* BPGame::sfcLogo;
bool BPGame::ShowingMessageBox;
bool BPGame::ShowingClearScores;
SpriteFont* BPGame::sfcMessageBoxText;
SpriteFont* BPGame::sfcMessageBoxTitle;

void BPGame::Init(int width, int height) {
	srand(time(0));
	
	EnableSound = true;
	EnableMusic = true;
	
	EmptyPoint = BPPoint(0,0);
	LastStateChange = 0;
	
	LastBGObject = 0;
	
	Black = new Colour(0.0f, 0.0f, 0.0f, 1.0f);
	White = new Colour(1.0f, 1.0f, 1.0f, 1.0f);
	TransparentWhite = new Colour(1.0f, 1.0f, 1.0f, 0.0f);
	
	Blue = new Colour(0.0f, 0.0f, 1.0f, 1.0f);
	Cyan = new Colour(0.0f, 1.0f, 1.0f, 1.0f);
	Green = new Colour(0.0f, 1.0f, 0.0f, 1.0f);
	Orange = new Colour(1.0f, 0.75f, 0.0f, 1.0f);
	Red = new Colour(1.0f, 0.0f, 0.0f, 1.0f);
	LightRed = new Colour(1.0f, 0.5f, 0.6f, 1.0f);
	DarkRed = new Colour(0.5f, 0.0f, 0.0f, 1.0f);
	Yellow = new Colour(1.0f, 1.0f, 0.0f, 1.0f);
	DarkGrey = new Colour(0.5f, 0.5f, 0.5f, 1.0f);
	ConnexGreen = new Colour(0.0f, 0.8f, 0.0f, 0.7f);
	
	CheatMainTaps = CheatOptionsTaps = 0;

	InTestMode = false;
	
	TickCount = 0;
	ElapsedSeconds = 0;
	AnimCounter = 0.0f;
	PractiseAnimCounter = 0.0f;
	
	PractisePageNumber = 0;
	PractisePageMoveLeft = true;
	
	sfcTestWeight = NULL;
	sfcTestJob = NULL;
	sfcHighestBrainWeight = NULL;
	
	HighestBrainWeight = -1;
	CurrentBrainWeight = -1;
	
	TestBrainJobs.Add("Fast Food Assistant");
	TestBrainJobs.Add("Fast Food Manager");
	TestBrainJobs.Add("Bingo Club Manager");
	TestBrainJobs.Add("Web Designer");
	TestBrainJobs.Add("Personal Shopper");
	TestBrainJobs.Add("Aromatherapist");
	TestBrainJobs.Add("Solicitor");
	TestBrainJobs.Add("Psychotherapist");
	TestBrainJobs.Add("Lifestyle Consultant");
	TestBrainJobs.Add("Librarian");
	TestBrainJobs.Add("Acupuncturist");
	TestBrainJobs.Add("Insurance Broker");
	TestBrainJobs.Add("Bank Manager");
	TestBrainJobs.Add("Fashion Stylist");
	TestBrainJobs.Add("Interior Designer");
	TestBrainJobs.Add("Fashion Consultant");
	TestBrainJobs.Add("Sales Assistant");
	TestBrainJobs.Add("Actor");
	TestBrainJobs.Add("Sales Executive");
	TestBrainJobs.Add("Movie Director");
	TestBrainJobs.Add("Hotel Manager");
	TestBrainJobs.Add("Botanist");
	TestBrainJobs.Add("Marketing Executive");
	TestBrainJobs.Add("Economist");
	TestBrainJobs.Add("Statistician");
	TestBrainJobs.Add("Theatre Director");
	TestBrainJobs.Add("Nurse");
	TestBrainJobs.Add("Police Officer");
	TestBrainJobs.Add("Meteorologist");
	TestBrainJobs.Add("Art Valuer");
	TestBrainJobs.Add("Museum Curator");
	TestBrainJobs.Add("Marine Biologist");
	TestBrainJobs.Add("School Teacher");
	TestBrainJobs.Add("Dentist");
	TestBrainJobs.Add("Software Engineer");
	TestBrainJobs.Add("Forensic Scientist");
	TestBrainJobs.Add("Psychiatrist");
	TestBrainJobs.Add("Archaeologist");
	TestBrainJobs.Add("Engineer");
	TestBrainJobs.Add("Investment Analyst");
	TestBrainJobs.Add("Physicist");
	TestBrainJobs.Add("Sculptor");
	TestBrainJobs.Add("Painter");
	TestBrainJobs.Add("Poet");
	TestBrainJobs.Add("Doctor");
	TestBrainJobs.Add("Composer");
	TestBrainJobs.Add("Fighter Pilot");
	TestBrainJobs.Add("Architect");
	TestBrainJobs.Add("Brain Surgeon");
	TestBrainJobs.Add("Professor");
	TestBrainJobs.Add("Philosopher");
	TestBrainJobs.Add("Astronaut");
	TestBrainJobs.Add("Nobel Prize Winner");
	TestBrainJobs.Add("Genius");
	TestBrainJobs.Add("Super Genius");
	TestBrainJobs.Add("Albert Einstein");
	
	PeopleNames.Add("Andrea");
	PeopleNames.Add("Andrew");
	PeopleNames.Add("Bob");
	PeopleNames.Add("Catherine");
	PeopleNames.Add("Dawn");
	PeopleNames.Add("Diana");
	PeopleNames.Add("Esther");
	PeopleNames.Add("Graham");
	PeopleNames.Add("Hannah");
	PeopleNames.Add("Jack");
	PeopleNames.Add("James");
	PeopleNames.Add("Jeff");
	PeopleNames.Add("Jeremy");
	PeopleNames.Add("John");
	PeopleNames.Add("Kathryn");
	PeopleNames.Add("Laura");
	PeopleNames.Add("Louise");
	PeopleNames.Add("Lyn");
	PeopleNames.Add("Mark");
	PeopleNames.Add("Matthew");
	PeopleNames.Add("Mike");
	PeopleNames.Add("Paul");
	PeopleNames.Add("Rebecca");
	PeopleNames.Add("Richard");
	PeopleNames.Add("Sandra");
	PeopleNames.Add("Scott");
	PeopleNames.Add("Tanya");
	PeopleNames.Add("Terry");
	PeopleNames.Add("Tim");
	PeopleNames.Add("William");
	
	
	sfcMiniGameUnknown = LoadBitmap("minigame_unknown", 90, 90);
	MiniGames[BALLOONBLASTER] = new BPMiniGame_Container(BALLOONBLASTER, "balloonblaster", "Balloon Blaster!", LoadBitmap("minigame_balloonblaster", 90, 90), false);
	MiniGames[BOMBHUNT] = new BPMiniGame_Container(BOMBHUNT, "bombhunt", "Bomb Hunt", LoadBitmap("minigame_bombhunt", 90, 90), false);
	MiniGames[BPSAYS] = new BPMiniGame_Container(BPSAYS, "bpsays", "Brain Party says...", LoadBitmap("minigame_bpsays", 90, 90), false);
	MiniGames[BUBBLETROUBLE] = new BPMiniGame_Container(BUBBLETROUBLE, "bubbletrouble", "Bubble Trouble", LoadBitmap("minigame_bubbletrouble", 90, 90), false);
	MiniGames[CARDMATCH] = new BPMiniGame_Container(CARDMATCH, "cardmatch", "Card Match", LoadBitmap("minigame_cardmatch", 90, 90), false);
	MiniGames[CUPSNBALLS] = new BPMiniGame_Container(CUPSNBALLS, "cupsnballs", "Cups 'n' Balls", LoadBitmap("minigame_cupsnballs", 90, 90), false);
	MiniGames[CONNEX] = new BPMiniGame_Container(CONNEX, "connex", "Connex", LoadBitmap("minigame_connex", 90, 90), false);
	MiniGames[DICEOFF] = new BPMiniGame_Container(DICEOFF, "diceoff", "Dice Off", LoadBitmap("minigame_unknown", 90, 90), true);
	MiniGames[FLASHCOUNTING] = new BPMiniGame_Container(FLASHCOUNTING, "flashcounting", "Flash Counting", LoadBitmap("minigame_flashcounting", 90, 90), false);
	MiniGames[FLASHLIGHT] = new BPMiniGame_Container(FLASHLIGHT, "flashlight", "Flashlight", LoadBitmap("minigame_flashlight", 90, 90), false);
	MiniGames[IQTEST] = new BPMiniGame_Container(IQTEST, "iqtest", "IQ Test", LoadBitmap("minigame_iqtest", 90, 90), false);
	MiniGames[JEWELFLIP] = new BPMiniGame_Container(JEWELFLIP, "jewelflip", "Jewel Flip", LoadBitmap("minigame_jewelflip", 90, 90), false);
	MiniGames[JEWELJAM] = new BPMiniGame_Container(JEWELJAM, "jeweljam", "Jewel Jam", LoadBitmap("minigame_jeweljam", 90, 90), false);
	MiniGames[MARBLEDROP] = new BPMiniGame_Container(MARBLEDROP, "marbledrop", "Marble Drop", LoadBitmap("minigame_marbledrop", 90, 90), false);
	MiniGames[MEMORYBLOX] = new BPMiniGame_Container(MEMORYBLOX, "memoryblox", "MemoryBlox", LoadBitmap("minigame_memoryblox", 90, 90), false);
	MiniGames[MEMORYBOX] = new BPMiniGame_Container(MEMORYBOX, "memorybox", "Memory Box", LoadBitmap("minigame_memorybox", 90, 90), false);
	MiniGames[MEMORYMATHS] = new BPMiniGame_Container(MEMORYMATHS, "memorymaths", "Memory Maths", LoadBitmap("minigame_memorymaths", 90, 90), false);
	MiniGames[MINESWEEP] = new BPMiniGame_Container(MINESWEEP, "minesweep", "Mine Sweep", LoadBitmap("minigame_minesweep", 90, 90), false);
	MiniGames[MOONJUMP] = new BPMiniGame_Container(MOONJUMP, "moonjump", "Moon Jump", LoadBitmap("minigame_moonjump", 90, 90), false);
	MiniGames[NEXTINLINE] = new BPMiniGame_Container(NEXTINLINE, "nextinline", "Next in Line", LoadBitmap("minigame_nextinline", 90, 90), false);
	MiniGames[NUMBERSNAKE] = new BPMiniGame_Container(NUMBERSNAKE, "numbersnake", "Number Snake", LoadBitmap("minigame_numbersnake", 90, 90), false);
	MiniGames[ODDONEOUT] = new BPMiniGame_Container(ODDONEOUT, "oddoneout", "Odd One Out", LoadBitmap("minigame_oddoneout", 90, 90), false);
	MiniGames[PATCHMATCH] = new BPMiniGame_Container(PATCHMATCH, "patchmatch", "Patch Match", LoadBitmap("minigame_patchmatch", 90, 90), false);
	MiniGames[PERFECTPATHS] = new BPMiniGame_Container(PERFECTPATHS, "perfectpaths", "Perfect Paths", LoadBitmap("minigame_perfectpaths", 90, 90), false);
	MiniGames[ROUTEFINDER] = new BPMiniGame_Container(ROUTEFINDER, "routefinder", "Route Finder", LoadBitmap("minigame_routefinder", 90, 90), false);
	MiniGames[RPS] = new BPMiniGame_Container(RPS, "rockpaperscissors", "Rock, Paper, Scissors", LoadBitmap("minigame_rps", 90, 90), false);
	MiniGames[SCRAMBLED] = new BPMiniGame_Container(SCRAMBLED, "scrambled", "Scrambled", LoadBitmap("minigame_scrambled", 90, 90), false);
	MiniGames[SETFINDER] = new BPMiniGame_Container(SETFINDER, "setfinder", "Set Finder", LoadBitmap("minigame_setfinder", 90, 90), false);
	MiniGames[SHARPSHOOTER] = new BPMiniGame_Container(SHARPSHOOTER, "sharpshooter", "Sharpshooter", LoadBitmap("minigame_sharpshooter", 90, 90), true);
	MiniGames[SHORTCIRCUITSUDOKU] = new BPMiniGame_Container(SHORTCIRCUITSUDOKU, "shortcircuitsudoku", "Short-circuit Sudoku", LoadBitmap("minigame_shortcircuitsudoku", 90, 90), false);
	MiniGames[SHUFFLEPUZZLER] = new BPMiniGame_Container(SHUFFLEPUZZLER, "shufflepuzzler", "Shuffle Puzzler", LoadBitmap("minigame_shufflepuzzler", 90, 90), true);
	MiniGames[STRANGERDANGER] = new BPMiniGame_Container(STRANGERDANGER, "strangerdanger", "Stranger Danger", LoadBitmap("minigame_unknown", 90, 90), true);
	MiniGames[SYMBOLICLOGIC] = new BPMiniGame_Container(SYMBOLICLOGIC, "symboliclogic", "Symbolic Logic", LoadBitmap("minigame_symboliclogic", 90, 90),  false);
	MiniGames[UNDERTHEHAT] = new BPMiniGame_Container(UNDERTHEHAT, "underthehat", "Under the Hat", LoadBitmap("minigame_underthehat", 90, 90), true);
	MiniGames[UNTANGLER] = new BPMiniGame_Container(UNTANGLER, "untangler", "Untangler", LoadBitmap("minigame_untangler", 90, 90), false);
	MiniGames[WORDSMASH] = new BPMiniGame_Container(WORDSMASH, "wordsmash", "Word Smash", LoadBitmap("minigame_unknown", 90, 90), true);

	sfcLogo = LoadBitmap("brainparty", 312, 227);
	ShowingMessageBox = false;
	ShowingClearScores = false;
	sfcMessageBoxTitle = NULL;
	sfcMessageBoxText = NULL;
	
	sfcTestStatus.Add(LoadBitmap("teststatus1", 320, 75));
	sfcTestStatus.Add(LoadBitmap("teststatus2", 320, 75));
	sfcTestStatus.Add(LoadBitmap("teststatus3", 320, 75));
	sfcTestStatus.Add(LoadBitmap("teststatus4", 320, 75));
	sfcTestStatus.Add(LoadBitmap("teststatus5", 320, 75));
	
	sfcResults90[FAIL] = LoadBitmap("results_fail_90", 90, 90);
	sfcResults90[BRONZE] = LoadBitmap("results_bronze_90", 90, 90);
	sfcResults90[SILVER] = LoadBitmap("results_silver_90", 90, 90);
	sfcResults90[GOLD] = LoadBitmap("results_gold_90", 90, 90);
	sfcResults90[PLATINUM] = LoadBitmap("results_platinum_90", 90, 90);

	sfcFirstRun = LoadBitmap("firstrun", 320, 480);
	sfcHelp = LoadBitmap("menu_help", 320, 480);
	sfcLoading = LoadBitmap("loading", 320, 480);
	
	sfcOptions = LoadBitmap("menu_options", 320, 480);
	sfcOptionsSoundsOff = LoadBitmap("sound_off", 320, 48);
	sfcOptionsMusicOff = LoadBitmap("music_off", 320, 48);
	
	sfcTestBackground = LoadBitmap("bg_test", 512, 512);
	sfcResultsBackground = LoadBitmap("bg_results", 512, 512);
	sfcResultsBackground2 = LoadBitmap("bg_results2", 320, 480);
	sfcHistoryBackground = LoadBitmap("history", 320, 480);
	
	sfcMarathonMode = LoadBitmap("menu_marathonmode", 320, 480);
	sfcBrainBoost = LoadBitmap("menu_brainboost", 320, 480);
	
	sfcBGObjectTypes.Add(LoadBitmap("emptycircle", 256, 256));
	sfcBGObjectTypes.Add(LoadBitmap("doubleemptycircle", 256, 256));
	sfcBGObjectTypes.Add(LoadBitmap("doublecircle", 256, 256));
	sfcBGObjectTypes.Add(LoadBitmap("circlering", 256, 256));
	sfcBGObjectTypes.Add(LoadBitmap("filledcircle", 256, 256));
	
	sfcSecretLo = LoadBitmap("secret_lo", 71, 66);
	sfcSecretHi = LoadBitmap("secret_hi", 71, 66);
	
	sfcStarTypes.Add(LoadBitmap("star_dark", 3, 3));
	sfcStarTypes.Add(LoadBitmap("star_medium", 3, 3));
	sfcStarTypes.Add(LoadBitmap("star_bright", 3, 3));

	Sounds["baa"] = LoadSound("baa");
	Sounds["baa2"] = LoadSound("baa2");
	Sounds["baa3"] = LoadSound("baa3");
	Sounds["balloon_pop"] = LoadSound("balloon_pop");
	Sounds["beep_hi"] = LoadSound("beep_hi");
	Sounds["bubble"] = LoadSound("bubble");
	Sounds["card_flip"] = LoadSound("card_flip");
	Sounds["card_flip2"] = LoadSound("card_flip2");
	Sounds["click"] = LoadSound("click");
	Sounds["correct"] = LoadSound("correct");
	Sounds["down"] = LoadSound("down");
	Sounds["explosion"] = LoadSound("explosion");
	Sounds["gem_select"] = LoadSound("gem_select");
	Sounds["glass_break"] = LoadSound("glass_break");
	Sounds["gun1"] = LoadSound("gun1");
	Sounds["gun2"] = LoadSound("gun2");
	Sounds["happy_ring"] = LoadSound("happy_ring");
	Sounds["meow"] = LoadSound("meow");
	Sounds["mouse_click"] = LoadSound("mouse_click");
	Sounds["result"] = LoadSound("result");
	Sounds["slide"] = LoadSound("slide");
	Sounds["soft_boom"] = LoadSound("soft_boom");
	Sounds["swoosh_long"] = LoadSound("swoosh_long");
	Sounds["whack"] = LoadSound("whack");
	Sounds["wrong"] = LoadSound("wrong");
	Sounds["wrong2"] = LoadSound("wrong2");
	Sounds["wrong3"] = LoadSound("wrong3");
	Sounds["zap"] = LoadSound("zap");
	
	PlaySound("mouse_click", true, false);
	
	Music = NULL;
		
	sfcMainMenu = LoadBitmap("menu_main", 320, 480);
	sfcPlayMenu = LoadBitmap("menu_play", 320, 480);
	sfcPractiseMenu = LoadBitmap("menu_practise", 320, 480);
	sfcPractiseMenuHider = LoadBitmap("menu_practise_hider", 134, 34);
	sfcProfessorTalk = LoadBitmap("professor_talk", 320, 480);
	sfcCredits = LoadBitmap("menu_credits", 320, 480);
	
	sfcBottomBar = LoadBitmap("bottombar", 320, 64);
	
	sfcQuitTest = LoadBitmap("quit_test", 320, 480);
	
	sfcResultsFail = LoadBitmap("results_fail", 320, 480);
	sfcResultsBronze = LoadBitmap("results_bronze", 320, 480);
	sfcResultsSilver = LoadBitmap("results_silver", 320, 480);
	sfcResultsGold = LoadBitmap("results_gold", 320, 480);
	sfcResultsPlatinum = LoadBitmap("results_platinum", 320, 480);
	
	sfcWhite = LoadBitmap("white", 64, 64);
	
	sfcCorrect = LoadBitmap("correct", 320, 92);
	sfcWrong = LoadBitmap("wrong", 320, 92);

	ActiveMiniGame = NULL;
	
	LoadSettings();
	
	if (FirstRun) {
		SetGameState(FIRST_RUN);
	} else {
		SetGameState(MAIN_MENU);		
	}
	
	PlayMusic("theme");
	
	
	// Hackage for stress testing!
	
//	for (map<MiniGameType, BPMiniGame_Container*>::const_iterator it = MiniGames.begin(); it != MiniGames.end(); ++it) {
//		for (int i = 0; i < 1000000; ++i) {
//			NSLog("Creating %", it->second->ShortName);
//			CreateMiniGame(UNTANGLER, false, true);
//			ActiveMiniGame->Start();
//		}
//	}
}

void BPGame::Update(float elapsed) {
	ElapsedSeconds = elapsed;
	ElapsedTickCount = floor(elapsed * 1000);
	TickCount += ElapsedTickCount;
	
	if (ShowingMessageBox) {
		return;
	}

	if (!GameStateJustChanged) {
		// some gamestates need to increment the animcounter - do that now!
		switch (GameState) {
			case MAIN_MENU_PLAY:
			case PLAY_MAIN_MENU:
			case PRACTISE_PROFESSOR:
			case MAIN_MENU_OPTIONS:
			case OPTIONS_MAIN_MENU:
			case PLAY_HISTORY:
			case HISTORY_CREDITS:
			case CREDITS_HISTORY:
			case HISTORY_PLAY:
			case TEST_RESULTS_PLAY:
			case MAIN_MENU_HELP:
			case HELP_MAIN_MENU:
				if (AnimCounter < 1.0f) {
					AnimCounter += 3.5f * elapsed;
					if (AnimCounter > 1.0f) {
						AnimCounter = 1.0f;
					}
				}
				
				break;
				
			case PLAY_PRACTISE:
			case PRACTISE_PLAY:
				if (AnimCounter < 1.0f) {
					AnimCounter += 3.5f * elapsed;
					if (AnimCounter > 1.0f) {
						AnimCounter = 1.0f;
					}
				}
				
				// don't break - we need to execute the following one too!
				
			case PRACTISE_MENU:
				if (PractiseAnimCounter < 1.0f) {
					PractiseAnimCounter += 3.5f * elapsed;
					if (PractiseAnimCounter > 1.0f) {
						PractiseAnimCounter = 1.0f;
					}
				}
				
				break;

			case TEST_STATUS:
				if (LastStateChange + 5000 < TickCount) {
					NextTest();
				}
				
				if (LastBGObject + 1000 < TickCount) {
					BGObject* obj = new BGObject();
					obj->Scale = 0.0f;
					obj->Pos = BPPoint(RandomRange(0, 320), RandomRange(0, 480));
					
					switch (RandomRange(0, 17)) {
					case 0:
					case 1:
					case 2:
					case 3:
					case 4:
					case 5:
					case 6:
					case 7:
						obj->Type = sfcBGObjectTypes[0];
						break;
						
					case 8:
					case 9:
					case 10:
					case 11:
						obj->Type = sfcBGObjectTypes[1];
						break;
						
					case 12:
					case 13:
					case 14:
						obj->Type = sfcBGObjectTypes[2];
						break;
						
					case 15:
					case 16:
						obj->Type = sfcBGObjectTypes[3];
						break;
						
					default:
						obj->Type = sfcBGObjectTypes[4];
						break;
					}
					
					BackgroundObjects.Add(obj);
					LastBGObject = TickCount;
				}
					

				for (int i = BackgroundObjects.Count - 1; i >= 0; --i) {
					BackgroundObjects[i]->Scale += 0.3f * ElapsedSeconds;
					
					if (BackgroundObjects[i]->Scale >= 1.0f) {
						BackgroundObjects.RemoveAt(i);
					}
				}
				
				
				break;
				
			case TEST_RESULTS:
				for (int i = 0; i < TestResultContainers.Count; ++i) {
					TestResultContainer* result = TestResultContainers[i];
					
					if (result->GoingUp) {
						result->FadeUpAnim += 0.2f * ElapsedSeconds;
						if (result->FadeUpAnim > 1.0f) {
							result->FadeUpAnim = 1.0f;
							result->GoingUp = false;
						}
					} else {
						result->FadeUpAnim -= 0.4f * ElapsedSeconds;
						if (result->FadeUpAnim < -1.0f) {
							result->FadeUpAnim = -1.0f;
							result->GoingUp = true;
						}
					}
				}
				
				break;
				
		}
	} else {
		GameStateJustChanged = false; // we skipped our animcounter update - now continue as normal
	}
	
	switch (GameState) {
		case PLAY_PRACTISE:
		case PRACTISE_PLAY:
			if (AnimCounter == 1.0f) {
				if (GameState == PLAY_PRACTISE) {
					SetGameState(PRACTISE_MENU);
				} else {
					SetGameState(PLAY_MENU);					
				}
			}
			// don't break - we need to update the practise screen behind the scenes too
			
		case PRACTISE_MENU:		
				for (map<MiniGameType, BPMiniGame_Container*>::const_iterator it = MiniGames.begin(); it != MiniGames.end(); ++it) {
					it->second->X = SmoothStep(it->second->StartX, it->second->DestX, PractiseAnimCounter);
					it->second->Y = SmoothStep(it->second->StartY, it->second->DestY, PractiseAnimCounter);
				}

				break;
			
		case PLAYING:
			ActiveMiniGame->TickMiniGame();
			break;
			
		case MAIN_MENU_PLAY:
			if (AnimCounter == 1.0f) SetGameState(PLAY_MENU);
			break;
			
		case PLAY_MAIN_MENU:
			if (AnimCounter == 1.0f) SetGameState(MAIN_MENU);
			break;

		case PLAY_HISTORY:
			if (AnimCounter == 1.0f) SetGameState(HISTORY);
			break;
			
		case HISTORY_CREDITS:
			if (AnimCounter == 1.0f) SetGameState(CREDITS);
			break;
			
		case CREDITS_HISTORY:
			if (AnimCounter == 1.0f) SetGameState(HISTORY);
			break;
			
		case HISTORY_PLAY:
			if (AnimCounter == 1.0f) SetGameState(PLAY_MENU);
			break;

		case PRACTISE_PROFESSOR:
			if (AnimCounter == 1.0f) SetGameState(PROFESSOR);
			break;
			
		case MAIN_MENU_OPTIONS:
			if (AnimCounter == 1.0f) SetGameState(OPTIONS);
			break;
			
		case OPTIONS_MAIN_MENU:
			if (AnimCounter == 1.0f) SetGameState(MAIN_MENU);
			break;
			
		case TEST_RESULTS_PLAY:
			if (AnimCounter == 1.0f) SetGameState(PLAY_MENU);
			break;
			
		case MAIN_MENU_HELP:
			if (AnimCounter == 1.0f) SetGameState(HELP);
			break;
			
		case HELP_MAIN_MENU:
			if (AnimCounter == 1.0f) SetGameState(MAIN_MENU);
			break;
	}
}

void BPGame::Draw() {
	Clear(Black);
		
	switch (GameState) {
		case FIRST_RUN:
			sfcFirstRun->Draw(0, 0);
			break;
		
		case MAIN_MENU:
			RenderMainMenu(0, 0);
			break;
			
		case MAIN_MENU_PLAY:
			{
				float offval = SmoothStep(0, -320, AnimCounter);
				float onval = SmoothStep(320, 0, AnimCounter);
				
				RenderMainMenu(offval, 0.0f);
				RenderPlayMenu(onval, 0.0f);			
			}
			
			break;
			
		case PLAY_MAIN_MENU:
			{				
				float offval = SmoothStep(-320, 0, AnimCounter);
				float onval = SmoothStep(0, 320, AnimCounter);
				
				RenderMainMenu(offval, 0.0f);
				RenderPlayMenu(onval, 0.0f);			
			}
			
			break;
			
		case PLAY_HISTORY:
			{
				float offval = SmoothStep(0, -480, AnimCounter);
				float onval = SmoothStep(480, 0, AnimCounter);
				
				RenderPlayMenu(0.0f, offval);
				RenderHistory(0.0f, onval);			
			}
			
			break;
			
		case HISTORY_CREDITS:
			{
				float offval = SmoothStep(0, -480, AnimCounter);
				float onval = SmoothStep(480, 0, AnimCounter);
				
				RenderHistory(0.0f, offval);
				RenderCredits(0.0f, onval);			
			}
			
			break;

		case CREDITS_HISTORY:
			{
				float offval = SmoothStep(0, 480, AnimCounter);
				float onval = SmoothStep(-480, 0, AnimCounter);
				
				RenderCredits(0.0f, offval);
				RenderHistory(0.0f, onval);			
			}
			
			break;
			
		case HISTORY_PLAY:
			{
				float offval = SmoothStep(0, 480, AnimCounter);
				float onval = SmoothStep(-480, 0, AnimCounter);
				
				RenderHistory(0.0f, offval);
				RenderPlayMenu(0.0f, onval);			
			}
			
			break;
			
		case MAIN_MENU_OPTIONS:
			RenderOptionsMenu();
			
			glColor4f(1.0f, 1.0f, 1.0f, 1 - AnimCounter);
			RenderMainMenu(0.0f, 0.0f);
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			
			break;
			
		case OPTIONS_MAIN_MENU:
			RenderMainMenu(0.0f, 0.0f);
			
			glColor4f(1.0f, 1.0f, 1.0f, 1 - AnimCounter);
			RenderOptionsMenu();
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			
			break;
			
		case MAIN_MENU_HELP:
		{
			float onval = SmoothStep(480, 0, AnimCounter);
			
			RenderMainMenu(0.0f, 0.0f);
			RenderHelp(0.0f, onval);			
		}
			
			break;
			
		case HELP_MAIN_MENU:
		{
			float offval = SmoothStep(0, 480, AnimCounter);

			RenderMainMenu(0.0f, 0.0f);
			RenderHelp(0.0f, offval);
		}
			
			break;
			
		case TEST_RESULTS_PLAY:
			{				
				float offval = SmoothStep(0, 480, AnimCounter);
				float onval = SmoothStep(-480, 0, AnimCounter);
				
				RenderTestResults(0.0f, offval);
				RenderPlayMenu(0.0f, onval);			
			}
			
			break;
			
		case PLAY_MENU:
			RenderPlayMenu(0.0f, 0.0f);
			break;
			
		case PRACTISE_PLAY:
			RenderPractiseMenu();
			
			glColor4f(1.0f, 1.0f, 1.0f, AnimCounter);
			RenderPlayMenu(0.0f, 0.0f);
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			
			break;
			
		case PLAY_PRACTISE:
			RenderPractiseMenu();
			
			glColor4f(1.0f, 1.0f, 1.0f, 1 - AnimCounter);
			RenderPlayMenu(0.0f, 0.0f);
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			
			break;
			
		case PRACTISE_MENU:
			RenderPractiseMenu();
			break;
			
		case PROFESSOR:
			RenderProfessor();
			break;

		case PRACTISE_PROFESSOR:		
			RenderPractiseMenu();

			glColor4f(1.0f, 1.0f, 1.0f, AnimCounter);
			RenderProfessor();
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			break;
			
		case PLAYING:
			RenderPlaying();
			break;
			
		case TEST_STATUS:
			RenderTestStatus();
			break;
			
		case TEST_RESULTS:
			RenderTestResults(0.0f, 0.0f);
			break;
			
		case OPTIONS:
			RenderOptionsMenu();
			break;
			
		case HISTORY:
			RenderHistory(0.0f, 0.0f);
			break;

		case BRAINBOOST:
			RenderBrainBoost();
			break;
			
		case MARATHON:
			RenderMarathon();
			break;
			
		case CREDITS:
			RenderCredits(0.0f, 0.0f);
			break;
			
		case HELP:
			RenderHelp(0.0f, 0.0f);
			break;
	}
	
	if (ShowingMessageBox) {
		FillRectangle(Colour(0.0f, 0.0f, 0.0f, 0.85f), 0, 0, 320, 480);
		
		Colour col = Colour(1.0f, 1.0f, 1.0f, 1.0f);
		sfcLogo->Draw(160, 90, 0.0f, 0.6f, col);
		sfcMessageBoxTitle->drawAtPoint(0, 170);
		
		glColor4f(0.8f, 0.8f, 0.8f, 1.0f);
		sfcMessageBoxText->drawAtPoint(10, 205);
		glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	}
}

Texture* BPGame::LoadBitmap(const char*	 filename, int actualwidth, int actualheight) {
	return new Texture(filename, actualwidth, actualheight);
}

void BPGame::AddTestScore(int score) {
	if (InTestMode) {
		TestScores.Add(score);
		
		if (TestScores.Count == TestMiniGames.Count) {
			// that was the last game in this test run!
			CalculateTestResults();
			SetGameState(TEST_RESULTS);
			InTestMode = false;
			PlayMusic("results");
		} else {
			SetGameState(TEST_STATUS);
		}
	}
}

void BPGame::FillRectangle(Colour col, float x, float y, float width, float height) {
	glColor4f(col.R, col.G, col.B, col.A);
	sfcWhite->Draw(x, y, width, height);
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}

void BPGame::DrawImage(Texture* tex, float x, float y) {
	tex->Draw(x, y);
}

void BPGame::DrawImage(Texture* tex, float x, float y, Colour &col) {
	tex->Draw(x, y, col);
}


void BPGame::DrawImage(Texture* tex, float x, float y, float rotation, float scale, Colour &col) {
	tex->Draw(x, y, rotation, scale, col);	
}

void BPGame::Clear(Colour* col) {
	glClearColor(col->R, col->G, col->B, col->A);
	glClear(GL_COLOR_BUFFER_BIT);
}

bool BPGame::PointOverRect(int x1, int y1, int x2, int y2, int width, int height) {
	if (x1 >= x2 && x1 <= x2 + width) {
		if (y1 >= y2 && y1 <= y2 + height) {
			return true;
		}
	}
	
	return false;
}

bool BPGame::RectOverRect(int x1, int y1, int width1, int height1, int x2, int y2, int width2, int height2) {
	if (x1 + width1 < x2) return false;
	if (x1 > x2 + width2) return false;
	if (y1 + height1 < y2) return false;
	if (y1 > y2 + height2) return false;
	
	return true;
}

void BPGame::TouchStart(float x, float y) {
	if (ShowingMessageBox) {
		return;
	}
	
	TouchEvent.X = x;
	TouchEvent.Y = y;

	switch (GameState) {
		case PLAYING:
			if (ActiveMiniGame != NULL) {
				ActiveMiniGame->HandleMouseDown(TouchEvent);
			}
			
			break;
	}
}

void BPGame::TouchStop(float x, float y) {
	if (ShowingMessageBox) {
		ShowingMessageBox = false;
		ShowingClearScores = false;
		return;
	}
	
	TouchEvent.X = x;
	TouchEvent.Y = y;
	
	switch (GameState) {
		case FIRST_RUN:
			SetGameState(MAIN_MENU);
			PlaySound("mouse_click");
			break;
			
		case MAIN_MENU:
			if (PointOverRect(TouchEvent.X, TouchEvent.Y, 37, 207, 248, 78)) {
				SetGameState(MAIN_MENU_PLAY);
				PlaySound("mouse_click");
			} else if (PointOverRect(TouchEvent.X, TouchEvent.Y, 22, 309, 272, 58)) {
				SetGameState(MAIN_MENU_OPTIONS);
				PlaySound("mouse_click");
			} else if (PointOverRect(TouchEvent.X, TouchEvent.Y, 73, 370, 170, 58)) {
				SetGameState(MAIN_MENU_HELP);
				PlaySound("mouse_click");
			} else if (TouchEvent.Y < 207) {
				++CheatMainTaps;
			}
			
			break;
			
		case PLAY_MENU:
			if (PointOverRect(TouchEvent.X, TouchEvent.Y, 0, 48, 320, 128)) {
				if (AllTestScores.Count == 0) {
					MessageBox::Show("Once you have completed your first test, this will show your brain weight.", "Brain weight");
				} else {
					MessageBox::Show("This is your current brain weight, as measured by the last test you did.", "Brain weight");
				}
			} else if (PointOverRect(TouchEvent.X, TouchEvent.Y, 16, 192, 288, 48)) {
				RunTest();
			} else if (PointOverRect(TouchEvent.X, TouchEvent.Y, 15, 252, 289, 48)) {
				if (NumUnlockedGames > 0) {
					SetGameState(PLAY_PRACTISE);
					PlaySound("mouse_click");
				} else {
					MessageBox::Show("You can't practise any games until you have completed at least one test. Go on, it won't take long!", "No games to practise");
				}
			} else if (PointOverRect(TouchEvent.X, TouchEvent.Y, 28, 312, 264, 48)) {
				SetGameState(PLAY_HISTORY);
				PlaySound("mouse_click");
			} else if (PointOverRect(TouchEvent.X, TouchEvent.Y, 74, 410, 172, 48)) {
				SetGameState(PLAY_MAIN_MENU);
				PlaySound("mouse_click");
			}
			break;
			
		case PRACTISE_MENU:
			if (PointOverRect(TouchEvent.X, TouchEvent.Y, 91, 437, 137, 27)) {
				SetGameState(PRACTISE_PLAY);
				PlaySound("mouse_click");
			} else if (PointOverRect(TouchEvent.X, TouchEvent.Y, 15, 384, 134, 34)) {
				// back
				if (PractiseAnimCounter == 1.0f) {
					if (PractisePageNumber != 0) {
						PractisePageNumber -= 9;
						if (PractisePageNumber < 0) PractisePageNumber = 0;
						PractisePageMoveLeft = false;
						PositionPractiseGames();
						PlaySound("mouse_click");
					}
				}
			} else if (PointOverRect(TouchEvent.X, TouchEvent.Y, 173, 383, 134, 34)) {
				// more
				if (PractiseAnimCounter == 1.0f) {
					if (PractisePageNumber + 9 < NumUnlockedGames) {
						PractisePageNumber += 9;
						PractisePageMoveLeft = true;
						PositionPractiseGames();
						PlaySound("mouse_click");
					}
				}
			} else {
				// did the player ask to play a minigame?
				int i = 0;
				int j = 0;
				int xpos = 0;
				
				for (map<MiniGameType, BPMiniGame_Container*>::const_iterator it = MiniGames.begin(); it != MiniGames.end(); ++it) {
					if (it->second->IsSecret) continue;
					if (!it->second->Unlocked) continue;
					
					if (i < PractisePageNumber) {
						++i;
						continue;
					}
					
					xpos = i - PractisePageNumber;
					
					y = 75 + (xpos * 100);
					x = 15 + (j * 100);
					
					if (PointOverRect(TouchEvent.X, TouchEvent.Y, x, y, 90, 90)) {
						if (it->second->Unlocked) {
							PlaySound("mouse_click");
							CreateMiniGame((MiniGameType)it->second->GameCode, false, true);
						}
						
						break;
					}
					
					++j;
					
					if (j == 3) {
						j = 0;
						++i;
					}
					
					if (i > PractisePageNumber + 2) break;	
				}
			}
			break;
			
		case PROFESSOR:
			ActiveMiniGame->PlayMusic();
			ActiveMiniGame->Start();
			SetGameState(PLAYING);
			break;
			
		case PLAYING:
			ActiveMiniGame->HandleMouseUp(TouchEvent);
			break;
			
		case TEST_STATUS:
			NextTest();
			break;
			
		case TEST_RESULTS:
			if (LastStateChange + 1600 < TickCount) {
				SetGameState(TEST_RESULTS_PLAY);
				PlayMusic("theme");
			}
			
			break;
			
		case OPTIONS:
			if (PointOverRect(x, y, 21, 193, 277, 39)) {
				EnableSound = !EnableSound;
				PlaySound("click", false, true);
				SaveSettings();
			} else if (PointOverRect(x, y, 29, 259, 262, 37)) {
				EnableMusic = !EnableMusic;
				PlaySound("click");
				
				if (EnableMusic == false) {
					StopMusic();
				} else {
					PlayMusic("theme");
				}
				
				SaveSettings();	
			} else if (PointOverRect(x, y, 15, 353, 290, 31)) {
				PromptResetScores();
				PlaySound("click");
				
			} else if (PointOverRect(x, y, 0, 416, 320, 64)) {
				SetGameState(OPTIONS_MAIN_MENU);
				PlaySound("mouse_click");
			} else if (TouchEvent.Y < 193) {
				++CheatOptionsTaps;
			}
			
			break;
			
		case HISTORY:
			if (PointOverRect(x, y, 17, 45, 285, 161)) {
				MessageBox::Show("This graph shows your most recent test results.", "Your test history");
			} else if (PointOverRect(x, y, 21, 251, 285, 57)) {
				MessageBox::Show("This is the highest brain weight you have scored in any test. Each minigame is worth up to 500g, and you get five in a test, so the maximum brain weight is 2500g.", "Highest Brain Weight");
			} else if (PointOverRect(x, y, 0, 349, 74, 66)) {
				if (Secret1) {
					CreateMiniGame(DICEOFF, false, false);
				} else {
					MessageBox::Show("This secret game is unlocked when you get a Platinum score in Balloon Blaster.", "Secret locked!");
				}
			} else if (PointOverRect(x, y, 82, 349, 74, 66)) {
				if (Secret2) {
					CreateMiniGame(WORDSMASH, false, false);
				} else {
					MessageBox::Show("This secret game is unlocked when you get a Platinum score in Jewel Jam.", "Secret locked!");
				}				
			} else if (PointOverRect(x, y, 162, 349, 74, 66)) { 
				if (Secret3) {
					SetGameState(MARATHON);
					PlaySound("mouse_click");
				} else {
					MessageBox::Show("This secret area is unlocked when you get a Platinum score in Odd One Out.", "Secret locked!");
				}
			} else if (PointOverRect(x, y, 245, 349, 74, 66)) {
				if (Secret4) {
					SetGameState(BRAINBOOST);
					PlaySound("mouse_click");
				} else {
					MessageBox::Show("This secret area is unlocked when you get a Platinum score in Untangler.", "Secret locked!");
				}				
			} else if (PointOverRect(x, y, 2, 418, 110, 54)) {
				SetGameState(HISTORY_PLAY);
				PlaySound("mouse_click");
			} else if (PointOverRect(x, y, 182, 418, 136, 54)) {
				SetGameState(HISTORY_CREDITS);
				PlaySound("mouse_click");
			}
			
			break;
				
		case CREDITS:
			if (PointOverRect(x, y, 193, 203, 126, 148)) {
				// clicked on the professor!
				if (CheatMainTaps >= 5 && CheatOptionsTaps >= 5) {
					MessageBox::Show("Hey, it looks like you activated the cheat code - all the minigames and secrets have been unlocked. We hope you're proud of yourself!", "Cheat activated!");	
					
					for (map<MiniGameType, BPMiniGame_Container*>::const_iterator it = MiniGames.begin(); it != MiniGames.end(); ++it) {
						it->second->Unlocked = true;
					}
					
					Secret1 = Secret2 = Secret3 = Secret4 = true;
					
					SaveSettings();
					
					PlaySound("meow");
				}
			}
			
			SetGameState(CREDITS_HISTORY);
			PlaySound("mouse_click");
			break;
			
		case MARATHON:
			if (PointOverRect(x, y, 12, 201, 90, 90)) {
				CreateMiniGame(BALLOONBLASTER, true, false);
			} else if (PointOverRect(x, y, 116, 201, 90, 90)) {
				CreateMiniGame(BPSAYS, true, false);
			} else if (PointOverRect(x, y, 220, 201, 90, 90)) {
				CreateMiniGame(JEWELJAM, true, false);
			} else if (PointOverRect(x, y, 12, 305, 90, 90)) {
				CreateMiniGame(SHORTCIRCUITSUDOKU, true, false);
			} else if (PointOverRect(x, y, 116, 305, 90, 90)) {
				CreateMiniGame(SYMBOLICLOGIC, true, false);
			} else if (PointOverRect(x, y, 220, 305, 90, 90)) {
				CreateMiniGame(UNTANGLER, true, false);
			} else if (PointOverRect(x, y, 0, 423, 320, 64)) {
				SetGameState(HISTORY);
				PlaySound("mouse_click");
			}
			
			break;
			
		case BRAINBOOST:
			if (PointOverRect(x, y, 53, 184, 90, 90)) {
				CreateMiniGame(SHARPSHOOTER, true, false);
			} else if (PointOverRect(x, y, 179, 184, 90, 90)) {
				CreateMiniGame(SHUFFLEPUZZLER, true, false);
			} else if (PointOverRect(x, y, 53, 305, 90, 90)) {
				CreateMiniGame(STRANGERDANGER, true, false);
			} else if (PointOverRect(x, y, 179, 305, 90, 90)) {
				CreateMiniGame(UNDERTHEHAT, true, false);
			} else if (PointOverRect(x, y, 0, 423, 320, 64)) {
				SetGameState(HISTORY);
				PlaySound("mouse_click");
			}
			
			break;
			
		case HELP:
			SetGameState(HELP_MAIN_MENU);
			PlaySound("mouse_click");
			break;
	}
}

void BPGame::TouchDrag(float x, float y) {
	if (ShowingMessageBox) {
		return;
	}
	
	TouchEvent.X = x;
	TouchEvent.Y = y;

	switch (GameState) {
		case PLAYING:
			if (ActiveMiniGame != NULL) {
				ActiveMiniGame->HandleMouseMove(TouchEvent);
			}
			
			break;
	}
}

const string TrimString(const std::string& str) {
	string::size_type First = str.find_first_not_of( ' ' );

	if (First == string::npos) {
		First = 0;
	}

	string::size_type Last = str.find_last_not_of( ' ' );
	if (Last != string::npos) {
		Last = (Last + 1) - First;
	}

  	return str.substr(First, Last);
}


void BPGame::AllocString(SpriteFont** tex, const char* str, FontSizes size, float width, float height, Alignments align, bool bold) {
	SAFE_DELETE((*tex));
	
	// it's incredibly lazy to open and close the font each time - hurray!
	TTF_Font* fnt = TTF_OpenFont("Content/freesans.ttf", size - 3); // NB: the -3 is here because the freesans.ttf font we're using is a big chunkier than the iPhone font
	
	static SDL_Color white = { 255, 255, 255, 255 };
	
	vector<string> lines; // we'll store the split lines here
	string temp(str); // the current text we're working on
	temp += " ";  // always add a space to the end of the text to enable wrapping - this gets removed later
	int n = 0; // current index
	int p = 0; // previous space

	while (n != -1) {		
		std::string strSub;
		n = temp.find(" ", p + 1 );		// -- Find the next " "
		
		std::string line_so_far = temp.substr(0, n);
		int w,h;
		
		const char* c_str = line_so_far.c_str();
		TTF_SizeText(fnt, c_str, &w, &h);
		
		if (w >= width)	{
			strSub = temp.substr(0, p);
			lines.push_back(strSub);
			
			if (n != -1) temp = temp.substr( p+1, string::npos );			
			p = 0;
		} else {
			p = n;
		}
	}

	// do the last line too
	string strSub = temp.substr(0, p);
	lines.push_back(strSub);
	
		
	// create a SpriteFont to hold all the information - this is used for drawing later
	(*tex) = new SpriteFont();
	(*tex)->Width = width;
	(*tex)->Height = height;
	(*tex)->Align = align;
	
	// render all the text
	for (int i = 0; i < lines.size(); ++i) {
		SDL_Surface* sfc = TTF_RenderText_Blended(fnt, TrimString(lines[i]).c_str(), white);
		
		if (sfc != NULL) {
			(*tex)->Surfaces.Add(new Texture(sfc));
			SDL_FreeSurface(sfc);
		}
	}
	
	TTF_CloseFont(fnt);
}

void BPGame::DrawString(SpriteFont* tex, Colour &col, float x, float y) {
	if (tex == NULL) return;
	
	glColor4f(col.R, col.G, col.B, col.A);	
	tex->drawAtPoint(x,y);
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}

void BPGame::DrawString(SpriteFont* tex, Colours col, float x, float y) {
	if (tex == NULL) return;	
	
	if (col == WHITE) {
		glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	} else if (col == BLACK) {
		glColor4f(0.0f, 0.0f, 0.0f, 1.0f);		
	}
	
	tex->drawAtPoint(x,y);
	
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}

void BPGame::DrawString(SpriteFont* tex, float x, float y) {
	if (tex == NULL) return;
			
	tex->drawAtPoint(x,y);
}

int BPGame::RandomRange(int min, int max) {
	if (max < min) return min;
	
	int value = rand();
	return (value % ((max - min) + 1) + min);
}

int BPGame::RandomRangeExcept(int min, int max, int except) {
	int val = RandomRange(min, max);
	
	while (val == except) {
		val = RandomRange(min, max);
	}
	
	return val;
}

void BPGame::LoadSettings() {
	Secret1 = Secret2 = Secret3 = Secret4 = false;
	NumUnlockedGames = 0;

	ifstream ifs;
	ifs.open(".brainparty");
	
	FirstRun = false;
	
	if (ifs.is_open()) {
		string temp;
	
		int loading_stage = 0;
		int line_number = 0;
		int secret_num = 1;

		while (getline(ifs, temp)) {
			++line_number;
		
			if (temp.size() == 0) {
				++loading_stage;
			} else {
				switch (loading_stage) {
					case 0:
						// loading whether we have music and sound
						if (line_number == 1) {
							if (temp.compare("1") == 0) {
								EnableSound = true;
							} else {
								EnableSound = false;							
							}
						} else {
							if (temp.compare("1") == 0) {
								EnableMusic = true;
							} else {
								EnableMusic = false;							
							}
						}
						break;
				
					case 1:
						// loading secrets
						switch (secret_num) {
							case 1:
								if (temp.compare("1") == 0) {
									Secret1 = true;
								} else {
									Secret1 = false;							
								}
						
								break;
						
							case 2:
								if (temp.compare("1") == 0) {
									Secret2 = true;
								} else {
									Secret2 = false;							
								}
								break;
						
							case 3:
								if (temp.compare("1") == 0) {
									Secret3 = true;
								} else {
									Secret3 = false;							
								}
								break;
						
							case 4:
								if (temp.compare("1") == 0) {;
							
									Secret4 = true;
								} else {
									Secret4 = false;							
								}
								break;
						}

						++secret_num;
					
						break;
					
					case 2:
					{
						// loading test scores
						CurrentBrainWeight = atoi(temp.c_str());
						AllTestScores.Add(CurrentBrainWeight);
					
						if (CurrentBrainWeight > HighestBrainWeight) {
							HighestBrainWeight = CurrentBrainWeight;
						}
					
						break;
					}	

					
					case 3:
						const char* gamename = temp.c_str();
				
						for (map<MiniGameType, BPMiniGame_Container*>::const_iterator it = MiniGames.begin(); it != MiniGames.end(); ++it) {
							if (it->second->IsSecret) continue;
					
							if (strcmp(gamename, it->second->ShortName) == 0) {
								it->second->Unlocked = true;
								++NumUnlockedGames;
								continue;
							}
						}
					
						break;
				}
			}
		}
	} else {
		FirstRun = true;
	}
	
	if (CurrentBrainWeight != -1) {
		ostringstream output;
		output << CurrentBrainWeight << "g";
		AllocString(&sfcTestWeight, output.str().c_str(), BRAINWEIGHT, 320, 100, CENTRED, true);
	} else {
		AllocString(&sfcTestWeight, "???", BRAINWEIGHT, 320, 100, CENTRED, true);		
	}
	
	if (HighestBrainWeight != -1) {
		ostringstream output;
		output << HighestBrainWeight << "g";
		AllocString(&sfcHighestBrainWeight, output.str().c_str(), XXLARGE, 320, 100, CENTRED, true);
	} else {
		AllocString(&sfcHighestBrainWeight, "N/A", XXLARGE, 320, 100, CENTRED, true);		
	}
}

void BPGame::SaveSettings() {
 	ofstream savefile;
 	savefile.open(".brainparty");
	savefile << EnableSound << endl;
	savefile << EnableMusic << endl;
	savefile << endl;
	
	savefile << Secret1 << endl;
	savefile << Secret2 << endl;
	savefile << Secret3 << endl;
	savefile << Secret4 << endl;
	savefile << endl;
	
	for (int i = 0; i < AllTestScores.Count; ++i) {
		savefile << AllTestScores[i] << endl;
	}
	
	savefile << endl;
	
	for (map<MiniGameType, BPMiniGame_Container*>::const_iterator it = MiniGames.begin(); it != MiniGames.end(); ++it) {
		if (it->second->Unlocked) {
			savefile << it->second->ShortName << endl;
		}
	}
	
 	savefile.close();

	LoadSettings();
}

string BPGame::SeparateThousands(int number) {
	char buffer[20];
	
	int i, digit, out_index = 0;

	for (i = number; i != 0; i /= 10) {
		digit = i % 10;

		if ((out_index + 1) % 4 == 0) {
			buffer[out_index++] = ',';
		}
		
		buffer[out_index++] = digit + '0';
	}
	
	buffer[out_index] = '\0';
	
	string retval = string(buffer);
	reverse(retval.begin(), retval.end());
	return retval;
}

string* BPGame::TicksToTime(int ticks) {
	int seconds_elapsed = floor(ticks / 1000);
	int minutes = floor(seconds_elapsed / 60.0);
	int seconds = seconds_elapsed % 60;
	
	ostringstream output;
	
	if (seconds < 10) {
		output << minutes << ":0" << seconds;
	} else {
		output << minutes << ":" << seconds;
	}
	
	string* retval = new string(output.str());
	return retval;
}

int BPGame::DivRem(int Num, int Div, int* Rem) {
	(*Rem) = Num % Div;
	return floor(Num / Div);
}

void BPGame::DrawLine(int fromx, int fromy, int tox, int toy, Colour* col, float width) {	
	GLfloat vertices[] = { fromx, fromy, tox, toy }; 

	glDisable(GL_TEXTURE_2D);
	glLineWidth(width);
	glColor4f(col->R, col->G, col->B, col->A);
	glLoadIdentity();

	glVertexPointer(2, GL_FLOAT, 0, vertices);
	glDrawArrays(GL_LINES, 0, 2);
	
	glEnable(GL_TEXTURE_2D);
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}

float BPGame::Clamp(float value, float minval, float maxval) {
	if (value > maxval) {
		value = maxval;
	} else if (value < minval) {
		value = minval;
	}
		
	return value;
}

float BPGame::Hermite(float value1, float tangent1, float value2, float tangent2, float amount) {
	float AmountSquared = amount * amount;
	float AmountCubed = AmountSquared * amount;

	float result = 0.0f;
	
	if (amount == 0.0f) {
		result = value1;
	} else if (amount == 1.0f) {
		result = value2;
	} else {
		result = (2 * value1 - 2 * value2 + tangent2 + tangent1) * AmountCubed + (3 * value2 - 3 * value1 - 2 * tangent1 - tangent2) * AmountSquared + tangent1 * amount + value1;
	}

	return result;
}

float BPGame::Lerp(float value1, float value2, float amount) {
	float result = Clamp(amount, 0.0f, 1.0f);
	return value1 + (value2 - value1) * result;
}

float BPGame::SmoothStep(float value1, float value2, float amount) {
	float result = Clamp(amount, 0.0f, 1.0f);
	result = Hermite(value1, 0.0, value2, 0.0, result);
	return result;
}

float BPGame::SmoothStep2(float value1, float value2, float amount) {
	float result = Clamp(amount, 0.0f, 1.0f);
	
	if (result > 0.5f) {
		result = Hermite(value2, 0.0f, value1, 0.0f, (result - 0.5f) * 2.0f);
	} else {
		result = Hermite(value1, 0.0f, value2, 0.0f, result * 2.0f);
	}
	
	return result;
}

Colour BPGame::ColourSmoothStep(Colour &from, Colour &to, float amount) {
	return Colour(SmoothStep(from.R, to.R, amount), SmoothStep(from.G, to.G, amount), SmoothStep(from.B, to.B, amount), SmoothStep(from.A, to.A, amount));
}


bool BPGame::StartsWithVowel(string* word) {
	char chr = (*word)[0];
	if (chr == 'a' || chr == 'e' || chr == 'i' || chr == 'o' || chr == 'u'){
		return true;
	}
	
	return false;
}

const char* BPGame::GetName() {
	return PeopleNames[RandomRange(0, PeopleNames.Count - 1)];
}

void BPGame::RenderMainMenu(float xoff, float yoff) {
	DrawImage(sfcMainMenu, xoff, yoff);
}

void BPGame::RenderOptionsMenu() {
	DrawImage(sfcOptions, 0, 0);
	
	if (!EnableSound) {
		DrawImage(sfcOptionsSoundsOff, 0, 188);
	}
	
	if (!EnableMusic) {
		DrawImage(sfcOptionsMusicOff, 0, 254);		
	}
}

void BPGame::RenderPlayMenu(float xoff, float yoff) {
	DrawImage(sfcPlayMenu, xoff, yoff);
	DrawString(sfcTestWeight, xoff, yoff + 58.0f);	
}

void BPGame::RenderPractiseMenu() {
	DrawImage(sfcPractiseMenu, 0, 0);
	
	for (map<MiniGameType, BPMiniGame_Container*>::const_iterator it = MiniGames.begin(); it != MiniGames.end(); ++it) {
		if (!it->second->IsShowing) {
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f - PractiseAnimCounter);
		}
		
		if (it->second->Pic == NULL) {
			DrawImage(sfcMiniGameUnknown, it->second->X, it->second->Y);
		} else {
			DrawImage(it->second->Pic, it->second->X, it->second->Y);
		}

		glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	}
	
	if (PractisePageNumber == 0) {
		// hide Back
		glColor4f(1.0f, 1.0f, 1.0f, 0.85f);
		DrawImage(sfcPractiseMenuHider, 15, 384);
		glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	}
	
	if (PractisePageNumber + 9 >= NumUnlockedGames) {
		glColor4f(1.0f, 1.0f, 1.0f, 0.85f);
		DrawImage(sfcPractiseMenuHider, 173, 383);
		glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	}
}

void BPGame::RenderProfessor() {
	DrawImage(sfcProfessorTalk, 0, 0);

	ActiveMiniGame->DrawProfessorText();
}

void BPGame::RenderPlaying() {
	ActiveMiniGame->RenderMiniGame();
}

void BPGame::RenderTestStatus() {
	static float rotation = 0.0f;
	rotation += 15 * ElapsedSeconds;
	
	Clear(Black);
	DrawImage(sfcTestBackground, 160.0f, 240.0f, rotation, 1.2f, (*White));
	Colour col = Colour(1.0f, 1.0f, 1.0f, 0.2f);
	DrawImage(sfcTestBackground, 160.0f, 240.0f, -rotation, 1.2f, col);
	
	for (int i = 0; i < BackgroundObjects.Count; ++i) {
		BGObject* obj = BackgroundObjects[i];
		
		float alpha;
		
		if (obj->Scale < 0.5f) {
			// fading in
			alpha = (obj->Scale / 2.5f);
		} else {
			// fading out
			alpha = 0.4f - (obj->Scale / 2.5f);
		}
		
		alpha = Clamp(alpha, 0.0f, 1.0f);
		Colour col = Colour(1.0f, 1.0f, 1.0f, alpha);
		
		DrawImage(obj->Type, obj->Pos.X, obj->Pos.Y, 0.0f, obj->Scale, col);
	}
	
	DrawImage(sfcTestStatus[TestPosition], 0.0f, 208.0f);
}

void BPGame::RenderTestResults(float xoff, float yoff) {
	if (LastStateChange + 200 > TickCount) {
		// plain background
		DrawImage(sfcResultsBackground, xoff + 160.0f, yoff + 240.0f, 0.0f, 1.0f, (*White));
	} else if (LastStateChange + 1000 > TickCount) {
		// fade in Your Brain Weighs text
		DrawImage(sfcResultsBackground, xoff + 160.0f, yoff + 240.0f, 0.0f, 1.0f, (*White));
		Colour col = Colour(1.0f, 1.0, 1.0f, 1 - (((LastStateChange + 1000) - TickCount) / 800.0f));
		DrawImage(sfcResultsBackground2, xoff + 160.0f, yoff + 240.0f, 0.0f, 1.0f, col);
	} else if (LastStateChange + 1600 > TickCount) {
		// fade in brain weight
		DrawImage(sfcResultsBackground2, xoff + 160.0f, yoff + 240.0f, 0.0f, 1.0f, (*White));
		float alpha = 1 - (((LastStateChange + 1600) - TickCount) / 600.0f);

		glColor4f(1.0f, 1.0f, 1.0f, alpha);
		DrawString(sfcTestWeight, xoff + 0.0f, yoff + 93.0f);
		glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	} else {
		// show everything
		DrawImage(sfcResultsBackground2, xoff + 160.0f, yoff + 240.0f, 0.0f, 1.0f, (*White));
		DrawString(sfcTestWeight, xoff + 0.0f, yoff + 93.0f);
		DrawString(sfcTestJob, xoff + 0.0f, yoff + 200.0f);
	}
	
	float diff = 1 - (((LastStateChange + 500) - TickCount) / 500.0f);
	
	for (int i = 0; i < TestResultContainers.Count; ++i) {
		TestResultContainer* result = TestResultContainers[i];
		
		int x, y, rot;
		
		if (LastStateChange + 500 > TickCount) {
			// fading in
			x = SmoothStep(160, result->Pos.X, diff);
			y = SmoothStep(525, result->Pos.Y, diff);
			rot = SmoothStep(60 + (i * 60), 0, diff);
			Colour col = Colour(1.0f, 1.0f, 1.0f, SmoothStep(0.0f, 1.0f, diff));
			DrawImage(MiniGames[(MiniGameType)result->MiniGame]->Pic, xoff + x, yoff + y, rot, 1.0f, col);
		} else {
			x = result->Pos.X;
			y = result->Pos.Y;
			rot = 0;
			DrawImage(MiniGames[(MiniGameType)result->MiniGame]->Pic, xoff + x, yoff + y, rot, 1.0f, (*White));
			Colour col = Colour(1.0f, 1.0f, 1.0f, SmoothStep(0.0f, 1.0f, result->FadeUpAnim));
			DrawImage(sfcResults90[result->Rank], xoff + x, yoff + y, 0.0f, 1.0f, col);
		}
	}
}

void BPGame::RenderHistory(float xoff, float yoff) {
	DrawImage(sfcHistoryBackground, xoff, yoff);
	
	DrawString(sfcHighestBrainWeight, xoff, 246.0f + yoff);
	
	if (Secret1) {
		DrawImage(sfcSecretHi, 2 + xoff, 349 + yoff);
	} else {
		DrawImage(sfcSecretLo, 2 + xoff, 349 + yoff);
	}
	
	if (Secret2) {
		DrawImage(sfcSecretHi, 83 + xoff, 349 + yoff);
	} else {
		DrawImage(sfcSecretLo, 83 + xoff, 349 + yoff);
	}
	
	if (Secret3) {
		DrawImage(sfcSecretHi, 164 + xoff, 349 + yoff);
	} else {
		DrawImage(sfcSecretLo, 164 + xoff, 349 + yoff);
	}
	
	if (Secret4) {
		DrawImage(sfcSecretHi, 246 + xoff, 349 + yoff);
	} else {
		DrawImage(sfcSecretLo, 246 + xoff, 349 + yoff);
	}
	
	int graph_height = 148;
	int last_score = -1;
	
	if (ShowScores.Count == 1) {
		// just one score - draw a dot!
		int this_score = round(ShowScores[0] / 2500.0f * graph_height);
		DrawLine(52 + xoff, 201 - this_score + yoff, 56 + xoff, 201 - this_score + yoff, Red, 4.0f);
	} else {
		for (int i = 0; i < ShowScores.Count; ++i) {
			int this_score = round(ShowScores[i] / 2500.0f * graph_height);
		
			if (last_score != -1) {
				// connect the last dot to this dot
				DrawLine(54 + ((i - 1) * 30) + xoff, 201 - last_score + yoff, 54 + (i * 30) + xoff, 201 - this_score + yoff, Red, 3.0f);
			}
		
			last_score = this_score;
		}
	}
}

void BPGame::RenderBrainBoost() {
	DrawImage(sfcBrainBoost, 0, 0);
}

void BPGame::RenderMarathon() {
	DrawImage(sfcMarathonMode, 0, 0);	
}

void BPGame::RenderHelp(float xoff, float yoff) {
	DrawImage(sfcHelp, xoff, yoff);
}

void BPGame::RenderCredits(float xoff, float yoff) {
	DrawImage(sfcCredits, xoff, yoff);
}

void BPGame::CalculateTestResults() {
	// add up all our scores and see how well we did...
	TestBrainWeight = 0;
	
	for (int i = 0; i < TestScores.Count; ++i) {
		TestBrainWeight += TestScores[i];
	}
	
	if (TestBrainWeight < 500) {
		TestBrainJob = TestBrainJobs[0];
	} else if (TestBrainWeight < 600) {
		TestBrainJob = TestBrainJobs[1];
	} else if (TestBrainWeight < 650) {
		TestBrainJob = TestBrainJobs[2];
	} else if (TestBrainWeight < 700) {
		TestBrainJob = TestBrainJobs[3];
	} else if (TestBrainWeight < 750) {
		TestBrainJob = TestBrainJobs[4];
	} else if (TestBrainWeight < 800) {
		TestBrainJob = TestBrainJobs[5];
	} else if (TestBrainWeight < 850) {
		TestBrainJob = TestBrainJobs[6];
	} else if (TestBrainWeight < 900) {
		TestBrainJob = TestBrainJobs[7];
	} else if (TestBrainWeight < 950) {
		TestBrainJob = TestBrainJobs[8];
	} else if (TestBrainWeight < 1000) {
		TestBrainJob = TestBrainJobs[9];
	} else if (TestBrainWeight < 1050) {
		TestBrainJob = TestBrainJobs[10];
	} else if (TestBrainWeight < 1100) {
		TestBrainJob = TestBrainJobs[11];
	} else if (TestBrainWeight < 1150) {
		TestBrainJob = TestBrainJobs[12];
	} else if (TestBrainWeight < 1175) {
		TestBrainJob = TestBrainJobs[13];
	} else if (TestBrainWeight < 1200) {
		TestBrainJob = TestBrainJobs[14];
	} else if (TestBrainWeight < 1225) {
		TestBrainJob = TestBrainJobs[15];
	} else if (TestBrainWeight < 1250) {
		TestBrainJob = TestBrainJobs[16];
	} else if (TestBrainWeight < 1275) {
		TestBrainJob = TestBrainJobs[17];
	} else if (TestBrainWeight < 1300) {
		TestBrainJob = TestBrainJobs[18];
	} else if (TestBrainWeight < 1325) {
		TestBrainJob = TestBrainJobs[19];
	} else if (TestBrainWeight < 1350) {
		TestBrainJob = TestBrainJobs[20];
	} else if (TestBrainWeight < 1375) {
		TestBrainJob = TestBrainJobs[21];
	} else if (TestBrainWeight < 1400) {
		TestBrainJob = TestBrainJobs[22];
	} else if (TestBrainWeight < 1425) {
		TestBrainJob = TestBrainJobs[23];
	} else if (TestBrainWeight < 1450) {
		TestBrainJob = TestBrainJobs[24];
	} else if (TestBrainWeight < 1475) {
		TestBrainJob = TestBrainJobs[25];
	} else if (TestBrainWeight < 1500) {
		TestBrainJob = TestBrainJobs[26];
	} else if (TestBrainWeight < 1525) {
		TestBrainJob = TestBrainJobs[27];
	} else if (TestBrainWeight < 1550) {
		TestBrainJob = TestBrainJobs[28];
	} else if (TestBrainWeight < 1575) {
		TestBrainJob = TestBrainJobs[29];
	} else if (TestBrainWeight < 1600) {
		TestBrainJob = TestBrainJobs[30];
	} else if (TestBrainWeight < 1625) {
		TestBrainJob = TestBrainJobs[31];
	} else if (TestBrainWeight < 1650) {
		TestBrainJob = TestBrainJobs[32];
	} else if (TestBrainWeight < 1675) {
		TestBrainJob = TestBrainJobs[33];
	} else if (TestBrainWeight < 1700) {
		TestBrainJob = TestBrainJobs[34];
	} else if (TestBrainWeight < 1725) {
		TestBrainJob = TestBrainJobs[35];
	} else if (TestBrainWeight < 1750) {
		TestBrainJob = TestBrainJobs[36];
	} else if (TestBrainWeight < 1775) {
		TestBrainJob = TestBrainJobs[37];
	} else if (TestBrainWeight < 1800) {
		TestBrainJob = TestBrainJobs[38];
	} else if (TestBrainWeight < 1825) {
		TestBrainJob = TestBrainJobs[39];
	} else if (TestBrainWeight < 1850) {
		TestBrainJob = TestBrainJobs[40];
	} else if (TestBrainWeight < 1875) {
		TestBrainJob = TestBrainJobs[41];
	} else if (TestBrainWeight < 1900) {
		TestBrainJob = TestBrainJobs[42];
	} else if (TestBrainWeight < 1925) {
		TestBrainJob = TestBrainJobs[43];
	} else if (TestBrainWeight < 1950) {
		TestBrainJob = TestBrainJobs[44];
	} else if (TestBrainWeight < 1975) {
		TestBrainJob = TestBrainJobs[45];
	} else if (TestBrainWeight < 2000) {
		TestBrainJob = TestBrainJobs[46];
	} else if (TestBrainWeight < 2025) {
		TestBrainJob = TestBrainJobs[47];
	} else if (TestBrainWeight < 2050) {
		TestBrainJob = TestBrainJobs[48];
	} else if (TestBrainWeight < 2075) {
		TestBrainJob = TestBrainJobs[49];
	} else if (TestBrainWeight < 2100) {
		TestBrainJob = TestBrainJobs[50];
	} else if (TestBrainWeight < 2125) {
		TestBrainJob = TestBrainJobs[51];
	} else if (TestBrainWeight < 2150) {
		TestBrainJob = TestBrainJobs[52];
	} else if (TestBrainWeight < 2175) {
		TestBrainJob = TestBrainJobs[53];
	} else if (TestBrainWeight < 2200) {
		TestBrainJob = TestBrainJobs[54];
	} else {
		TestBrainJob = TestBrainJobs[55];
	}
	
	AllocString(&sfcTestJob, TestBrainJob, NORMAL, 320.0f, 80.0f, CENTRED, false);
	
	// add the new score to the array
	AllTestScores.Add(TestBrainWeight);
	CurrentBrainWeight = TestBrainWeight;
	
	// unlock the games we just played
	for (int i = 0; i < TestMiniGames.Count; ++i) {
		MiniGames[TestMiniGames[i]]->Unlocked = true;
	}
	
	// now prepare the results screen
	TestResultContainers.Clear();
	
	for (int i = 0; i < TestMiniGames.Count; ++i) {
		TestResultContainer* result = new TestResultContainer();
		
		ostringstream brainweight;
		brainweight << TestScores[i] << "g";
		AllocString(&result->sfcBrainWeight, brainweight.str().c_str(), NORMAL, 90.0f, 30.0f, CENTRED, true);
		
		result->MiniGame = TestMiniGames[i];
		result->Rank = BPMiniGame::GetRank(TestScores[i]);
		result->GoingUp = false;
		
		if (i < 2) {
			// first row
			result->Pos = BPPoint(110 + (i * 100), 325);
		} else {
			// second row
			result->Pos = BPPoint(60 + ((i - 2) * 100), 425);
		}
		
		result->FadeUpAnim = Lerp(-1.0f, 0.0f, i / (float)(TestMiniGames.Count - 1));
		TestResultContainers.Add(result);
	}
	
	SaveSettings();
}

void BPGame::CreateMiniGame(MiniGameType game, bool marathon, bool practise) {	
	if (ActiveMiniGame != NULL) {		
		SAFE_DELETE(ActiveMiniGame);
	}
		
	switch (game) {
		case BALLOONBLASTER:
			ActiveMiniGame = new BPMiniGame_BalloonBlaster(this);
			break;
			
		case BOMBHUNT:
			ActiveMiniGame = new BPMiniGame_BombHunt(this);
			break;
			
		case BPSAYS:
			ActiveMiniGame = new BPMiniGame_BPSays(this);
			break;
			
		case BUBBLETROUBLE:
			ActiveMiniGame = new BPMiniGame_BubbleTrouble(this);
			break;
			
		case CARDMATCH:
			ActiveMiniGame = new BPMiniGame_CardMatch(this);
			break;
			
		case CONNEX:
			ActiveMiniGame = new BPMiniGame_Connex(this);
			break;

		case CUPSNBALLS:
			ActiveMiniGame = new BPMiniGame_CupsNBalls(this);
			break;
			
		case DICEOFF:
			ActiveMiniGame = new BPMiniGame_DiceOff(this);
			break;
			
		case FLASHCOUNTING:
			ActiveMiniGame = new BPMiniGame_FlashCounting(this);
			break;
			
		case FLASHLIGHT:
			ActiveMiniGame = new BPMiniGame_Flashlight(this);
			break;
			
		case IQTEST:
			ActiveMiniGame = new BPMiniGame_IQTest(this);
			break;
			
		case JEWELFLIP:
			ActiveMiniGame = new BPMiniGame_JewelFlip(this);
			break;
			
		case JEWELJAM:
			ActiveMiniGame = new BPMiniGame_JewelJam(this);
			break;
			
		case MARBLEDROP:
			ActiveMiniGame = new BPMiniGame_MarbleDrop(this);
			break;
			
		case MEMORYBLOX:
			ActiveMiniGame = new BPMiniGame_MemoryBlox(this);
			break;
			
		case MEMORYBOX:
			ActiveMiniGame = new BPMiniGame_MemoryBox(this);
			break;
			
		case MEMORYMATHS:
			ActiveMiniGame = new BPMiniGame_MemoryMaths(this);
			break;
			
		case MINESWEEP:
			ActiveMiniGame = new BPMiniGame_MineSweep(this);
			break;
			
		case MOONJUMP:
			ActiveMiniGame = new BPMiniGame_MoonJump(this);
			break;

		case NEXTINLINE:
			ActiveMiniGame = new BPMiniGame_NextInLine(this);
			break;
			
		case NUMBERSNAKE:
			ActiveMiniGame = new BPMiniGame_NumberSnake(this);
			break;
			
		case ODDONEOUT:
			ActiveMiniGame = new BPMiniGame_OddOneOut(this);
			break;

		case PATCHMATCH:
			ActiveMiniGame = new BPMiniGame_PatchMatch(this);
			break;
			
		case PERFECTPATHS:
			ActiveMiniGame = new BPMiniGame_PerfectPaths(this);
			break;
			
		case ROUTEFINDER:
			ActiveMiniGame = new BPMiniGame_RouteFinder(this);
			break;
			
		case RPS:
			ActiveMiniGame = new BPMiniGame_RPS(this);
			break;
			
		case SCRAMBLED:
			ActiveMiniGame = new BPMiniGame_Scrambled(this);
			break;
			
		case SETFINDER:
			ActiveMiniGame = new BPMiniGame_SetFinder(this);
			break;
			
		case SHARPSHOOTER:
			ActiveMiniGame = new BPMiniGame_Sharpshooter(this);
			break;

		case SHORTCIRCUITSUDOKU:
			ActiveMiniGame = new BPMiniGame_ShortCircuitSudoku(this);
			break;
			
		case SHUFFLEPUZZLER:
			ActiveMiniGame = new BPMiniGame_ShufflePuzzler(this);
			break;
			
		case STRANGERDANGER:
			ActiveMiniGame = new BPMiniGame_StrangerDanger(this);
			break;
			
		case SYMBOLICLOGIC:
			ActiveMiniGame = new BPMiniGame_SymbolicLogic(this);
			break;
			
		case UNDERTHEHAT:
			ActiveMiniGame = new BPMiniGame_UnderTheHat(this);
			break;

		case UNTANGLER:
			ActiveMiniGame = new BPMiniGame_Untangler(this);
			break;
			
		case WORDSMASH:
			ActiveMiniGame = new BPMiniGame_WordSmash(this);
			break;
	}
	
	ActiveMiniGame->Init();
	
	if (marathon) ActiveMiniGame->SetMarathon();
	
	if (GameState == HISTORY) {
		ActiveMiniGame->ReturnType = RT_SECRET;
	} else if (GameState == BRAINBOOST) {
		ActiveMiniGame->ReturnType = RT_BRAINBOOST;
	}
	
	if (practise) {
		SetGameState(PRACTISE_PROFESSOR);
	} else {
		SetGameState(PROFESSOR);		
	}
}

void BPGame::SetGameState(GameStates state) {
	GameState = state;
	AnimCounter = 0.0f;
	GameStateJustChanged = true;
	
	if (state != TEST_RESULTS_PLAY) {
		// don't reset LastStateChange when going from test results back to the play menu, as that resets the
		// animation on the results screen
		LastStateChange = TickCount;
	}
	
	switch (state) {
		case PLAY_PRACTISE:
			PractisePageNumber = 0;
			PractisePageMoveLeft = true;
			
			for (map<MiniGameType, BPMiniGame_Container*>::const_iterator it = MiniGames.begin(); it != MiniGames.end(); ++it) {
				it->second->IsShowing = false;
			}
			
			PositionPractiseGames();
			
			break;
			
		case HISTORY:
			ShowScores.Clear();
			
			if (AllTestScores.Count > 8) {
				for (int i = AllTestScores.Count - 8; i < AllTestScores.Count; ++i) {
					ShowScores.Add(AllTestScores[i]);
				}				
			} else {
				for (int i = 0; i < AllTestScores.Count; ++i) {
					ShowScores.Add(AllTestScores[i]);
				}
			}
			
			break;
			
		case PROFESSOR:
			PlayMusic("professor");
			break;
			
		case TEST_STATUS:
			PlaySound("happy_ring");
			break;
	}
}

void BPGame::PositionPractiseGames() {
	PractiseAnimCounter = 0.0f;
	
	// get all minigames off the screen
	for (map<MiniGameType, BPMiniGame_Container*>::const_iterator it = MiniGames.begin(); it != MiniGames.end(); ++it) {
		if (it->second->IsShowing) {
			// these are visible - make them scroll offscreen neatly
			it->second->StartX = it->second->X;
			it->second->StartY = it->second->Y;
			
			if (PractisePageMoveLeft) {
				it->second->DestX = -100;
			} else {
				it->second->DestX = 320;
			}
			
			it->second->DestY = 175;
		} else {
			it->second->Y = it->second->StartY = it->second->DestY = 175;			

			if (PractisePageMoveLeft) {
				it->second->X = it->second->StartX = it->second->DestX = -100;
			} else {
				it->second->X = it->second->StartX = it->second->DestX = 320;				
			}
		}
		
		it->second->IsShowing = false;
	}
	
	BPPList<BPPoint*> PractisePositions;
	
	for (int row = 0; row < 3; ++row) {
		for (int col = 0; col < 3; ++col) {
			PractisePositions.Add(new BPPoint(15 + (col * 100), 75 + (row * 100)));
		}
	}
		
	int i = 0;
	int j = 0;
	
	for (map<MiniGameType, BPMiniGame_Container*>::const_iterator it = MiniGames.begin(); it != MiniGames.end(); ++it) {
		if (it->second->IsSecret) continue;
		if (!it->second->Unlocked) continue;
		
		if (i < PractisePageNumber) {
			++i;
			it->second->IsShowing = false;
			continue;
		}
		
		it->second->DestX = PractisePositions[j]->X;
		it->second->DestY = PractisePositions[j]->Y;
		
		it->second->StartY = PractisePositions[j]->Y;
		
		if (PractisePageMoveLeft) {
			it->second->StartX = 320;
		} else {
			it->second->StartX = -100;
		}
		
		it->second->IsShowing = true;
		
		++i;
		++j;
		
		if (i >= PractisePageNumber + 9) break;
	}
	
	PractisePositions.Clear();
}

void BPGame::RunTest() {
	StopMusic();
	
	TestMiniGames.Clear();
	TestScores.Clear();
	TestPosition = 0;
	
	BPList<MiniGameType> SeenMiniGames;
	BPList<MiniGameType> UnseenMiniGames;
	
	// add them all to an array, then shuffle the array
	for (map<MiniGameType, BPMiniGame_Container*>::const_iterator it = MiniGames.begin(); it != MiniGames.end(); ++it) {
		if (it->second->IsSecret) continue; // don't allow hidden minigames into the test
		
		if (it->second->Unlocked) {
			SeenMiniGames.Add(it->first);
		} else {
			UnseenMiniGames.Add(it->first);			
		}
	}
	
	SeenMiniGames.Shuffle();
	UnseenMiniGames.Shuffle();
	
	int to_add;
	
	if (SeenMiniGames.Count == 0) {
		// we've seen no games yet; add five unseen games
		to_add = 5;
	} else {
		to_add = min(UnseenMiniGames.Count, 4);
	}
	
	for (int i = 0; i < to_add; ++i) {
		TestMiniGames.Add(UnseenMiniGames[i]);
	}
	
	// figure out how many seen games we need to add, and add them
	to_add = 5 - to_add;
	for (int i = 0; i < to_add; ++i) {
		TestMiniGames.Add(SeenMiniGames[i]);
	}
	
	// shuffle the list so that the seen/unseen games come in any order
	TestMiniGames.Shuffle();
	
	InTestMode = true;
	SetGameState(TEST_STATUS);
}

void BPGame::NextTest() {
	if (TestPosition == TestMiniGames.Count) {
		// use this for quick testing
//	if (true) {
//		TestScores.Add(150);
//		TestScores.Add(250);
//		TestScores.Add(350);
//		TestScores.Add(450);
//		TestScores.Add(500);
		CalculateTestResults();
		SetGameState(TEST_RESULTS);
		
		InTestMode = false;
		
		PlayMusic("results");

		return;
	}
	
	CreateMiniGame(TestMiniGames[TestPosition], false, false);
	++TestPosition;
}

void BPGame::PlaySound(const char* sound, bool preload_only, bool force_play) {

	if (!EnableSound && !force_play) return;
	
	if (Sounds.find(sound) != Sounds.end()) {
		if (preload_only) {
			// do nothing - this is only for iPhone
		} else {
			Mix_PlayChannel(-1, Sounds[sound], 0);
		}
	} else {
		printf("Error: sound %s doesn't exist.\n", sound);
	}
}

void BPGame::PlayMusic(const char* name) {
	if (!EnableMusic) return;
	
	if (Music != NULL) {
		Mix_HaltMusic();
		Mix_FreeMusic(Music);
		Music = NULL;
	}
	
	string* file = new string(name);
	file->insert(0, "Content/");
	file->append(".ogg");
	
	Music = Mix_LoadMUS(file->c_str());
	Mix_PlayMusic(Music, -1);

	delete(file);
}

void BPGame::StopMusic() {
	if (Music != NULL) {
		Mix_HaltMusic();
		Mix_FreeMusic(Music);
		Music = NULL;
	}
}

bool BPGame::FileExists(const char * filename) {
    if (FILE * file = fopen(filename, "r")) {
        fclose(file);
        return true;
    }
    return false;
}

Mix_Chunk* BPGame::LoadSound(const char* filename, const char* extension) {
	string* file = new string(filename);
	file->insert(0, "Content/");
	file->append(".wav");
	
	Mix_Chunk* retval = Mix_LoadWAV(file->c_str());
	if (retval == NULL) printf("Warning: couldn't load file %s.\n", filename);
	delete file;
	
	return retval;
}

void BPGame::CancelTest() {
	GameState = PLAY_MENU;
	TestMiniGames.Clear();
	TestScores.Clear();
	TestPosition = 0;
	
	InTestMode = false;
	
	PlayMusic("theme");
}

void BPGame::Accelerate(float x, float y, float z) {
	Acceleration[0] = x;
	Acceleration[1] = y;
	Acceleration[2] = z;
}

void BPGame::PromptResetScores() {
	ShowingClearScores = true;
	MessageBox::Show("Are you sure you want to wipe all your test scores so far? Press Y on your keyboard now to wipe your scores. Click the mouse or press any other key cancel.", "Clear scores?");
}

void BPGame::ExecuteResetScores() {
	AllTestScores.Clear();
	SaveSettings();
	MessageBox::Show("Your test scores have been wiped and you have a clean slate - good luck!", "Scores wiped!");
}

Colour* BPGame::ColorLerp(Colour* from, Colour* to, float amount) {
	Colour* col = new Colour(Lerp(from->R, to->R, amount), Lerp(from->G, to->G, amount), Lerp(from->B, to->B, amount), Lerp(from->A, to->A, amount));
	return col;
}