summaryrefslogtreecommitdiffstats
path: root/apps/plugins/spacerocks.c
blob: 946d97d075edc2bb1eb08405e4a0bfd90ed4f102 (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 by Mat Holton
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "plugin.h"
#include "lib/display_text.h"
#include "lib/helper.h"
#include "lib/highscore.h"
#include "lib/playback_control.h"



/* variable button definitions */
#if CONFIG_KEYPAD == RECORDER_PAD
#define AST_PAUSE BUTTON_ON
#define AST_QUIT BUTTON_OFF
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_PLAY

#elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
#define AST_PAUSE BUTTON_ON
#define AST_QUIT BUTTON_OFF
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_SELECT

#elif CONFIG_KEYPAD == ONDIO_PAD
#define AST_PAUSE (BUTTON_MENU | BUTTON_OFF)
#define AST_QUIT BUTTON_OFF
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_MENU

#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
      (CONFIG_KEYPAD == IRIVER_H300_PAD)
#define AST_PAUSE BUTTON_REC
#define AST_QUIT BUTTON_OFF
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_SELECT

#define AST_RC_QUIT BUTTON_RC_STOP

#elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
#define AST_PAUSE BUTTON_PLAY
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_SELECT

#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
      (CONFIG_KEYPAD == IPOD_1G2G_PAD)
#define AST_PAUSE (BUTTON_SELECT | BUTTON_PLAY)
#define AST_QUIT (BUTTON_SELECT | BUTTON_MENU)
#define AST_THRUST BUTTON_MENU
#define AST_HYPERSPACE BUTTON_PLAY
#define AST_LEFT BUTTON_SCROLL_BACK
#define AST_RIGHT BUTTON_SCROLL_FWD
#define AST_FIRE BUTTON_SELECT

#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
#define AST_PAUSE BUTTON_A
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_SELECT

#elif (CONFIG_KEYPAD == SANSA_E200_PAD)
#define AST_PAUSE BUTTON_REC
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_SCROLL_BACK
#define AST_RIGHT BUTTON_SCROLL_FWD
#define AST_FIRE BUTTON_SELECT

#elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
#define AST_PAUSE (BUTTON_SELECT | BUTTON_UP)
#define AST_QUIT (BUTTON_HOME|BUTTON_REPEAT)
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_SCROLL_BACK
#define AST_RIGHT BUTTON_SCROLL_FWD
#define AST_FIRE BUTTON_SELECT

#elif (CONFIG_KEYPAD == SANSA_C200_PAD)
#define AST_PAUSE BUTTON_REC
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_SELECT

#elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
#define AST_PAUSE BUTTON_HOME
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_SELECT

#elif (CONFIG_KEYPAD == SANSA_M200_PAD)
#define AST_PAUSE (BUTTON_SELECT | BUTTON_UP)
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE (BUTTON_SELECT | BUTTON_REL)

#elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
#define AST_PAUSE BUTTON_PLAY
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_SCROLL_UP
#define AST_HYPERSPACE BUTTON_SCROLL_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_REW

#elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
#define AST_PAUSE BUTTON_PLAY
#define AST_QUIT BUTTON_BACK
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_SELECT

#elif (CONFIG_KEYPAD == MROBE100_PAD)
#define AST_PAUSE BUTTON_DISPLAY
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_SELECT

#elif CONFIG_KEYPAD == IAUDIO_M3_PAD
#define AST_PAUSE BUTTON_RC_PLAY
#define AST_QUIT BUTTON_RC_REC
#define AST_THRUST BUTTON_RC_VOL_UP
#define AST_HYPERSPACE BUTTON_RC_VOL_DOWN
#define AST_LEFT BUTTON_RC_REW
#define AST_RIGHT BUTTON_RC_FF
#define AST_FIRE BUTTON_RC_MODE

#elif (CONFIG_KEYPAD == COWON_D2_PAD)
#define AST_QUIT BUTTON_POWER

#elif CONFIG_KEYPAD == CREATIVEZVM_PAD
#define AST_PAUSE BUTTON_PLAY
#define AST_QUIT BUTTON_BACK
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_SELECT

#elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
#define AST_PAUSE BUTTON_VIEW
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_PLAYLIST

#elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
#define AST_PAUSE BUTTON_PLAY
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_VOL_DOWN

#elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
#define AST_PAUSE BUTTON_RIGHT
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_PREV
#define AST_RIGHT BUTTON_NEXT
#define AST_FIRE BUTTON_LEFT

#elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
      (CONFIG_KEYPAD == ONDAVX777_PAD) || \
      (CONFIG_KEYPAD == MROBE500_PAD)
#define AST_QUIT BUTTON_POWER

#elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
#define AST_PAUSE      BUTTON_FFWD
#define AST_QUIT       BUTTON_REC
#define AST_THRUST     BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT       BUTTON_LEFT
#define AST_RIGHT      BUTTON_RIGHT
#define AST_FIRE       BUTTON_PLAY

#elif (CONFIG_KEYPAD == PBELL_VIBE500_PAD)
#define AST_PAUSE      BUTTON_PLAY
#define AST_QUIT       BUTTON_REC
#define AST_THRUST     BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT       BUTTON_PREV
#define AST_RIGHT      BUTTON_NEXT
#define AST_FIRE       BUTTON_OK

#elif (CONFIG_KEYPAD == MPIO_HD200_PAD)

#define AST_PAUSE (BUTTON_PLAY|BUTTON_FUNC)
#define AST_QUIT (BUTTON_REC|BUTTON_PLAY)
#define AST_THRUST BUTTON_REC
#define AST_HYPERSPACE BUTTON_PLAY
#define AST_LEFT BUTTON_REW
#define AST_RIGHT BUTTON_FF
#define AST_FIRE BUTTON_FUNC

#elif (CONFIG_KEYPAD == MPIO_HD300_PAD)

#define AST_PAUSE BUTTON_PLAY
#define AST_QUIT (BUTTON_REC|BUTTON_REPEAT)
#define AST_THRUST BUTTON_MENU
#define AST_HYPERSPACE (BUTTON_PLAY|BUTTON_REPEAT)
#define AST_LEFT BUTTON_REW
#define AST_RIGHT BUTTON_FF
#define AST_FIRE BUTTON_ENTER

#elif (CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD)

#define AST_PAUSE BUTTON_PLAYPAUSE
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_SELECT

#elif (CONFIG_KEYPAD == SANSA_CONNECT_PAD)

#define ALT_PAUSE BUTTON_VOL_DOWN
#define AST_QUIT BUTTON_POWER
#define AST_THRUST BUTTON_UP
#define AST_HYPERSPACE BUTTON_DOWN
#define AST_LEFT BUTTON_LEFT
#define AST_RIGHT BUTTON_RIGHT
#define AST_FIRE BUTTON_SELECT

#elif (CONFIG_KEYPAD == SAMSUNG_YPR0_PAD)
#define AST_PAUSE       BUTTON_MENU
#define AST_QUIT        BUTTON_BACK
#define AST_THRUST      BUTTON_UP
#define AST_HYPERSPACE  BUTTON_DOWN
#define AST_LEFT        BUTTON_LEFT
#define AST_RIGHT       BUTTON_RIGHT
#define AST_FIRE        BUTTON_SELECT

#else
#error No keymap defined!
#endif

#ifdef HAVE_TOUCHSCREEN
#ifndef AST_PAUSE
#define AST_PAUSE       BUTTON_CENTER
#endif
#ifndef AST_QUIT
#define AST_QUIT        BUTTON_TOPLEFT
#endif
#ifndef AST_THRUST
#define AST_THRUST      BUTTON_TOPMIDDLE
#endif
#ifndef AST_HYPERSPACE
#define AST_HYPERSPACE  BUTTON_TOPRIGHT
#endif
#ifndef AST_LEFT
#define AST_LEFT        BUTTON_MIDLEFT
#endif
#ifndef AST_RIGHT
#define AST_RIGHT       BUTTON_MIDRIGHT
#endif
#ifndef AST_FIRE
#define AST_FIRE        BUTTON_BOTTOMMIDDLE
#endif
#endif

#define RES                     MAX(LCD_WIDTH, LCD_HEIGHT)
#define LARGE_LCD               (RES >= 200)

#define CYCLETIME               30

#define SHOW_COL                0
#define SCALE                   5000
#define WRAP_GAP                (LARGE*SCALE*3)
#define POINT_SIZE              2
#define START_LEVEL             1
#define SHOW_LEVEL_TIME         50
#define EXPLOSION_LENGTH        20

#define MAX_NUM_ASTEROIDS       25
#define MAX_NUM_MISSILES        6
#define NUM_STARS               50
#define NUM_TRAIL_POINTS        70
#define MAX_LEVEL               MAX_NUM_ASTEROIDS

#define NUM_ASTEROID_VERTICES   10
#define NUM_SHIP_VERTICES       4
#define NUM_ENEMY_VERTICES      8

#define INVULNERABLE_TIME       30
#define BLINK_TIME              10
#define EXTRA_LIFE              250
#define START_LIVES             3
#define MISSILE_LIFE_LENGTH     40

#define ASTEROID_SPEED          (RES/20)
#define SPACE_CHECK_SIZE        30*SCALE

#if (LARGE_LCD)
#define SIZE_SHIP_COLLISION     8*SCALE
#else
#define SIZE_SHIP_COLLISION     6*SCALE
#endif

#define LITTLE_SHIP             1
#define BIG_SHIP                2
#define ENEMY_BIG_PROBABILITY_START     10
#define ENEMY_APPEAR_PROBABILITY_START  35
#define ENEMY_APPEAR_TIMING_START       600
#define ENEMY_SPEED             4
#define ENEMY_MISSILE_LIFE_LENGTH   (RES/2)
#if (LARGE_LCD)
#define SIZE_ENEMY_COLLISION    7*SCALE
#else
#define SIZE_ENEMY_COLLISION    5*SCALE
#endif

#define SIN_COS_SCALE           10000

#define FAST_ROT_CW_SIN         873
#define FAST_ROT_CW_COS         9963
#define FAST_ROT_ACW_SIN        -873
#define FAST_ROT_ACW_COS        9963

#define MEDIUM_ROT_CW_SIN       350
#define MEDIUM_ROT_CW_COS       9994
#define MEDIUM_ROT_ACW_SIN      -350
#define MEDIUM_ROT_ACW_COS      9994

#define SLOW_ROT_CW_SIN         350
#define SLOW_ROT_CW_COS         9994
#define SLOW_ROT_ACW_SIN        -350
#define SLOW_ROT_ACW_COS        9994

#ifdef HAVE_LCD_COLOR
#define SHIP_ROT_CW_SIN         2419
#define SHIP_ROT_CW_COS         9702
#define SHIP_ROT_ACW_SIN        -2419
#define SHIP_ROT_ACW_COS        9702
#else
#define SHIP_ROT_CW_SIN         3827
#define SHIP_ROT_CW_COS         9239
#define SHIP_ROT_ACW_SIN        -3827
#define SHIP_ROT_ACW_COS        9239
#endif


#define SCALED_WIDTH            (LCD_WIDTH*SCALE)
#define SCALED_HEIGHT           (LCD_HEIGHT*SCALE)
#define CENTER_LCD_X            (LCD_WIDTH/2)
#define CENTER_LCD_Y            (LCD_HEIGHT/2)

#ifdef HAVE_LCD_COLOR
#define ASTEROID_R  230
#define ASTEROID_G  200
#define ASTEROID_B  100
#define SHIP_R      255
#define SHIP_G      255
#define SHIP_B      255
#define ENEMY_R     50
#define ENEMY_G     220
#define ENEMY_B     50
#define THRUST_R    200
#define THRUST_G    200
#define THRUST_B    0

#define COL_MISSILE  LCD_RGBPACK(200,0,0)
#define COL_PLAYER   LCD_RGBPACK(200,200,200)
#define COL_INVULN   LCD_RGBPACK(100,100,200)
#define COL_STARS    LCD_WHITE
#define COL_ASTEROID LCD_RGBPACK(ASTEROID_R,ASTEROID_G,ASTEROID_B)
#define COL_TEXT     LCD_RGBPACK(200,200,255)
#define COL_ENEMY    LCD_RGBPACK(ENEMY_R,ENEMY_G,ENEMY_B)
#define SET_FG       rb->lcd_set_foreground
#define SET_BG       rb->lcd_set_background
#else
#define SET_FG(x)
#define SET_BG(x)
#endif

#define SCORE_FILE PLUGIN_GAMES_DATA_DIR "/spacerocks.score"
#define NUM_SCORES 5

static struct highscore highscores[NUM_SCORES];

/* The array of points that make up an asteroid */
static const short asteroid_one[NUM_ASTEROID_VERTICES*2] =
{
    -2, -12,
     4,  -8,
     8, -14,
    16,  -5,
    14,   0,
    20,   2,
    12,  14,
    -4,  14,
   -10,   6,
   -10,  -8,
};

/* The array of points that make up an asteroid */
static const short asteroid_two[NUM_ASTEROID_VERTICES*2] =
{
    -2, -12,
     4, -16,
     6, -14,
    16,  -8,
    14,   0,
    20,   2,
    12,  14,
    -4,  14,
   -10,   6,
   -10,  -8,
};

/* The array of points that make up an asteroid */
static const short asteroid_three[NUM_ASTEROID_VERTICES*2] =
{
    -2, -12,
     4, -16,
     6, -14,
     2,  -8,
    14,   0,
    20,   2,
    12,  14,
    -4,  14,
   -16,   6,
   -10,  -8,
};

/* The array of points the make up the ship */
static const short ship_vertices[NUM_SHIP_VERTICES*2] =
{
#if (LARGE_LCD)
    0, -6,
    4,  6,
    0,  2,
   -4,  6,
#else
    0, -4,
    3,  4,
    0,  1,
   -3,  4,
#endif
};

/* The array of points the make up the bad spaceship */
static const short enemy_vertices[NUM_ENEMY_VERTICES*2] =
{
#if (LARGE_LCD)
   -8,  0,
   -4,  4,
    4,  4,
    8,  0,
   -8,  0,
    8,  0,
    4, -4,
   -4, -4,
#else
   -5,  0,
   -2,  2,
    2,  2,
    5,  0,
   -5,  0,
    5,  0,
    2, -2,
   -2, -2,
#endif
};

enum asteroid_type
{
#if (LARGE_LCD)
    SMALL  = 2,
    MEDIUM = 4,
    LARGE  = 6,
#else
    SMALL  = 1,
    MEDIUM = 2,
    LARGE  = 3,
#endif
};

enum explosion_type
{
    EXPLOSION_SHIP,
    EXPLOSION_ASTEROID,
    EXPLOSION_ENEMY,
    EXPLOSION_THRUST,
};

enum game_state
{
    GAME_OVER,
    SHOW_LEVEL,
    PLAY_MODE,
    PAUSE_MODE,
};

struct Point
{
    int x;
    int y;
    int dx;
    int dy;
};

struct TrailPoint
{
    struct Point position;
    int alive;
#ifdef HAVE_LCD_COLOR
    short r;
    short g;
    short b;
    short dec;
#endif
};

/* Asteroid structure, contains an array of points */
struct Asteroid
{
    struct Point position;
    struct Point rotation;
    struct Point vertices[NUM_ASTEROID_VERTICES];
    bool exists;
    int explode_countdown;
    enum asteroid_type type;
    int radius;
    long speed_cos;
    long speed_sin;
};

struct Ship
{
    struct Point position;
    struct Point rotation;
    struct Point vertices[NUM_SHIP_VERTICES];
    bool exists;
    int explode_countdown;
    int invulnerable_time;
};

struct Enemy
{
    struct Point position;
    struct Point vertices[NUM_ENEMY_VERTICES];
    bool exists;
    int explode_countdown;
    int appear_countdown;
    short size_probability;
    short appear_probability;
    short appear_timing;
};

struct Missile
{
    struct Point position;
    struct Point oldpoint;
    int alive;
};

static enum game_state game_state;
static int asteroid_count;
static int next_missile_count;
static int next_thrust_count;
static int num_lives;
static int extra_life;
static int show_level_timeout;
static int current_level;
static int current_score;

static struct Ship ship;
static struct Point stars[NUM_STARS];
static struct Asteroid asteroids_array[MAX_NUM_ASTEROIDS];
static struct Missile missiles_array[MAX_NUM_MISSILES];
static struct Missile enemy_missile;
static struct Enemy enemy;
static struct Point lives_points[NUM_SHIP_VERTICES];
static struct TrailPoint trail_points[NUM_TRAIL_POINTS];

/*************************************************
** Handle polygon and point
*************************************************/

/* Check if point is in a polygon */
static bool is_point_in_polygon(struct Point* vertices, int num_vertices,
                                int x, int y)
{
    struct Point* pi;
    struct Point* pj;
    int n;
    bool c = false;

    if (x < -SCALED_WIDTH/2) x += SCALED_WIDTH;
    else if (x > SCALED_WIDTH/2) x -= SCALED_WIDTH;
    if (y < -SCALED_HEIGHT/2) y += SCALED_HEIGHT;
    else if (y > SCALED_HEIGHT/2) y -= SCALED_HEIGHT;

    pi = vertices;
    pj = vertices + num_vertices-1;

    n = num_vertices;
    while (n--)
    {
        if ((((pi->y <= y) && (y < pj->y)) || ((pj->y <= y) && (y < pi->y))) &&
            (x < (pj->x - pi->x) * (y - pi->y) / (pj->y - pi->y) + pi->x))
            c = !c;

        pj = pi;
        pi++;
    }

    return c;
}

/* Check if point is within a rectangle */
static bool is_point_within_rectangle(struct Point* rect, struct Point* p,
                                      int size)
{
    int dx = p->x - rect->x;
    int dy = p->y - rect->y;
#if SHOW_COL
    rb->lcd_drawrect((rect->x - size)/SCALE, (rect->y - size)/SCALE,
                     (size*2+1)/SCALE, (size*2+1)/SCALE);
#endif
    if (dx < -SCALED_WIDTH/2) dx += SCALED_WIDTH;
    else if (dx > SCALED_WIDTH/2) dx -= SCALED_WIDTH;
    if (dy < -SCALED_HEIGHT/2) dy += SCALED_HEIGHT;
    else if (dy > SCALED_HEIGHT/2) dy -= SCALED_HEIGHT;
    return (dx > -size && dx < size && dy > -size && dy < size);
}

/* Rotate polygon */
static void rotate_polygon(struct Point* vertices, int num_vertices,
                           struct Point* rotation, int cos, int sin)
{
    struct Point* point;
    int n;
    long temp_x, temp_y;

    temp_x = rotation->x;
    temp_y = rotation->y;
    rotation->x = (temp_x*cos - temp_y*sin)/SIN_COS_SCALE;
    rotation->y = (temp_y*cos + temp_x*sin)/SIN_COS_SCALE;
#define MIN_SCALE   (SIN_COS_SCALE-10)
#define MAX_SCALE   (SIN_COS_SCALE+10)
    /* normalize vector. this is not accurate but would be enough. */
    temp_x = rotation->x*rotation->x + rotation->y*rotation->y;
    if (temp_x <= MIN_SCALE*MIN_SCALE)
    {
        rotation->x = rotation->x*SIN_COS_SCALE/MIN_SCALE;
        rotation->y = rotation->y*SIN_COS_SCALE/MIN_SCALE;
    }
    else if (temp_x >= MAX_SCALE*MAX_SCALE)
    {
        rotation->x = rotation->x*SIN_COS_SCALE/MAX_SCALE;
        rotation->y = rotation->y*SIN_COS_SCALE/MAX_SCALE;
    }
#undef  MIN_SCALE
#undef  MAX_SCALE

    point = vertices;
    n = num_vertices;
    while (n--)
    {
        point->x = (point->dx*rotation->x - point->dy*rotation->y)/SIN_COS_SCALE;
        point->y = (point->dy*rotation->x + point->dx*rotation->y)/SIN_COS_SCALE;
        point++;
    }
}

/* Draw polygon */
static void draw_polygon(struct Point* vertices, int num_vertices,
                         int px, int py)
{
    int n, new_x, new_y, old_x, old_y;
    struct Point *p;
    bool draw_wrap;

    if (px > SCALED_WIDTH - WRAP_GAP)
        px -= SCALED_WIDTH;
    if (py > SCALED_HEIGHT - WRAP_GAP)
        py -= SCALED_HEIGHT;

    draw_wrap = (px < WRAP_GAP || py < WRAP_GAP);

    p = vertices + num_vertices - 1;
    old_x = (p->x + px)/SCALE;
    old_y = (p->y + py)/SCALE;
    p = vertices;
    n = num_vertices;
    while (n--)
    {
        new_x = (p->x + px)/SCALE;
        new_y = (p->y + py)/SCALE;

        rb->lcd_drawline(old_x, old_y, new_x, new_y);
        if (draw_wrap)
        {
            rb->lcd_drawline(old_x + LCD_WIDTH, old_y, new_x + LCD_WIDTH, new_y);
            rb->lcd_drawline(old_x, old_y + LCD_HEIGHT, new_x, new_y + LCD_HEIGHT);
            rb->lcd_drawline(old_x + LCD_WIDTH, old_y + LCD_HEIGHT,
                             new_x + LCD_WIDTH, new_y + LCD_HEIGHT);
        }
        old_x = new_x;
        old_y = new_y;
        p++;
    }
}

static void move_point(struct Point* point)
{
    point->x += point->dx;
    point->y += point->dy;

    /* Check bounds on the x-axis: */
    point->x %= SCALED_WIDTH;
    if (point->x < 0)
        point->x += SCALED_WIDTH;

    /* Check bounds on the y-axis: */
    point->y %= SCALED_HEIGHT;
    if (point->y < 0)
        point->y += SCALED_HEIGHT;
}

/*************************************************
** Handle trail blaiz.
*************************************************/

static void create_ship_trail(struct TrailPoint* tpoint)
{
    tpoint->position.x += ship.vertices[2].x;
    tpoint->position.y += ship.vertices[2].y;
    tpoint->position.dx = -( ship.vertices[0].x - ship.vertices[2].x )/10;
    tpoint->position.dy = -( ship.vertices[0].y - ship.vertices[2].y )/10;
}

static void create_explosion_trail(struct TrailPoint* tpoint)
{
    tpoint->position.dx = (rb->rand()%5001)-2500;
    tpoint->position.dy = (rb->rand()%5001)-2500;
}

static void create_trail_blaze(int colour, struct Point* position)
{
    int numtoadd;
    struct TrailPoint* tpoint;
    int n;

    if (colour != EXPLOSION_SHIP)
    {
        numtoadd = NUM_TRAIL_POINTS/5;
    }
    else
    {
        numtoadd = NUM_TRAIL_POINTS/8;
    }

    /* give the point a random countdown timer, so they dissapears at different
       times */
    tpoint = trail_points;
    n = NUM_TRAIL_POINTS;
    while (n--)
    {
        /* find a space in the array of trail_points that is NULL or DEAD or
           whatever and place this one here. */
        if (tpoint->alive <= 0)
        {
            /* take a random point near the position. */
            tpoint->position.x = (rb->rand()%18000)-9000 + position->x;
            tpoint->position.y = (rb->rand()%18000)-9000 + position->y;

            switch(colour)
            {
                case EXPLOSION_SHIP:
                    create_explosion_trail(tpoint);
                    tpoint->alive = 51;
#ifdef HAVE_LCD_COLOR
                    tpoint->r = SHIP_R;
                    tpoint->g = SHIP_G;
                    tpoint->b = SHIP_B;
                    tpoint->dec = 2;
#endif
                break;
                case EXPLOSION_ASTEROID:
                    create_explosion_trail(tpoint);
                    tpoint->alive = 51;
#ifdef HAVE_LCD_COLOR
                    tpoint->r = ASTEROID_R;
                    tpoint->g = ASTEROID_G;
                    tpoint->b = ASTEROID_B;
                    tpoint->dec = 2;
#endif
                break;
                case EXPLOSION_ENEMY:
                    create_explosion_trail(tpoint);
                    tpoint->alive = 51;
#ifdef HAVE_LCD_COLOR
                    tpoint->r = ENEMY_R;
                    tpoint->g = ENEMY_G;
                    tpoint->b = ENEMY_B;
                    tpoint->dec = 2;
#endif
                break;
                case EXPLOSION_THRUST:
                    create_ship_trail(tpoint);
                    tpoint->alive = 17;
#ifdef HAVE_LCD_COLOR
                    tpoint->r = THRUST_R;
                    tpoint->g = THRUST_G;
                    tpoint->b = THRUST_B;
                    tpoint->dec = 4;
#endif
                break;
            }

            /* give the points a speed based on direction of travel
               - i.e. opposite */
            tpoint->position.dx += position->dx;
            tpoint->position.dy += position->dy;

            numtoadd--;
            if (numtoadd <= 0)
                break;
        }
        tpoint++;
    }
}

static void draw_and_move_trail_blaze(void)
{
    struct TrailPoint* tpoint;
    int n;

    /* loop through, if alive then move and draw.
       when drawn, countdown it's timer.
       if zero kill it! */

    tpoint = trail_points;
    n = NUM_TRAIL_POINTS;
    while (n--)
    {
        if (tpoint->alive > 0)
        {
            if (game_state != PAUSE_MODE)
            {
                tpoint->alive--;
                move_point(&(tpoint->position));
#ifdef HAVE_LCD_COLOR
                /* intensity = tpoint->alive/2; */
                if (tpoint->r >= tpoint->dec) tpoint->r -= tpoint->dec;
                if (tpoint->g >= tpoint->dec) tpoint->g -= tpoint->dec;
                if (tpoint->b >= tpoint->dec) tpoint->b -= tpoint->dec;
#endif
            }
            SET_FG(LCD_RGBPACK(tpoint->r, tpoint->g, tpoint->b));
            rb->lcd_drawpixel(tpoint->position.x/SCALE, tpoint->position.y/SCALE);
        }
        tpoint++;
    }
}

/*************************************************
** Handle asteroid.
*************************************************/

static void rotate_asteroid(struct Asteroid* asteroid)
{
    rotate_polygon(asteroid->vertices, NUM_ASTEROID_VERTICES,
                   &asteroid->rotation,
                   asteroid->speed_cos, asteroid->speed_sin);
}

/* Initialise the passed Asteroid.
 * if position is NULL, place it at the random loacation
 * where ship doesn't exist
 */
static void initialise_asteroid(struct Asteroid* asteroid,
                                enum asteroid_type type, struct Point *position)
{
    const short *asteroid_vertices;
    struct Point* point;
    int n;

    asteroid->exists = true;
    asteroid->explode_countdown = 0;
    asteroid->type = type;

    /* Set the radius of the asteroid: */
    asteroid->radius = (int)type*SCALE*3;

    /* shall we move Clockwise and Fast */
    n = rb->rand()%100;
    if (n < 25)
    {
        asteroid->speed_cos = FAST_ROT_CW_COS;
        asteroid->speed_sin = FAST_ROT_CW_SIN;
    }
    else if (n < 50)
    {
        asteroid->speed_cos = FAST_ROT_ACW_COS;
        asteroid->speed_sin = FAST_ROT_ACW_SIN;
    }
    else if (n < 75)
    {
        asteroid->speed_cos = SLOW_ROT_ACW_COS;
        asteroid->speed_sin = SLOW_ROT_ACW_SIN;
    }
    else
    {
        asteroid->speed_cos = SLOW_ROT_CW_COS;
        asteroid->speed_sin = SLOW_ROT_CW_SIN;
    }

    n = rb->rand()%99;
    if (n < 33)
        asteroid_vertices = asteroid_one;
    else if (n < 66)
        asteroid_vertices = asteroid_two;
    else
        asteroid_vertices = asteroid_three;

    point = asteroid->vertices;
    for(n = 0; n < NUM_ASTEROID_VERTICES*2; n += 2)
    {
        point->x = asteroid_vertices[n];
        point->y = asteroid_vertices[n+1];
        point->x *= asteroid->radius/20;
        point->y *= asteroid->radius/20;
        /* dx and dy are used when rotate polygon */
        point->dx = point->x;
        point->dy = point->y;
        point++;
    }

    if (!position)
    {
        do {
            /* Set the position randomly: */
            asteroid->position.x = (rb->rand()%SCALED_WIDTH);
            asteroid->position.y = (rb->rand()%SCALED_HEIGHT);
        } while (is_point_within_rectangle(&ship.position, &asteroid->position,
                                           SPACE_CHECK_SIZE));
    }
    else
    {
        asteroid->position.x = position->x;
        asteroid->position.y = position->y;
    }

    do {
        asteroid->position.dx = (rb->rand()%ASTEROID_SPEED)-ASTEROID_SPEED/2;
    } while (asteroid->position.dx == 0);

    do {
        asteroid->position.dy = (rb->rand()%ASTEROID_SPEED)-ASTEROID_SPEED/2;
    } while (asteroid->position.dy == 0);

    asteroid->position.dx *= SCALE/10;
    asteroid->position.dy *= SCALE/10;

    asteroid->rotation.x = SIN_COS_SCALE;
    asteroid->rotation.y = 0;

    /* Now rotate the asteroid a bit, so they all look a bit different */
    for(n = (rb->rand()%30)+2; n--; )
        rotate_asteroid(asteroid);

    /* great, we've created an asteroid, don't forget to increment the total: */
    asteroid_count++;
}

/*
 * Creates a new asteroid of the given 4type (size) and at the given location.
 */
static void create_asteroid(enum asteroid_type type, struct Point *position)
{
    struct Asteroid* asteroid;
    int n;

    asteroid = asteroids_array;
    n = MAX_NUM_ASTEROIDS;
    while (n--)
    {
        if (!asteroid->exists && asteroid->explode_countdown <= 0)
        {
            initialise_asteroid(asteroid, type, position);
            break;
        }
        asteroid++;
    }
}

/* Draw and move all asteroids */
static void draw_and_move_asteroids(void)
{
    struct Asteroid* asteroid;
    int n;

    SET_FG(COL_ASTEROID);

    asteroid = asteroids_array;
    n = MAX_NUM_ASTEROIDS;
    while (n--)
    {
        if (asteroid->exists)
        {
            draw_polygon(asteroid->vertices, NUM_ASTEROID_VERTICES,
                         asteroid->position.x, asteroid->position.y);
        }
        if (game_state != PAUSE_MODE)
        {
            if (asteroid->exists)
            {
                move_point(&asteroid->position);
                rotate_asteroid(asteroid);
            }
            else if (asteroid->explode_countdown > 0)
            {
                asteroid->explode_countdown--;
            }
        }
        asteroid++;
    }
}

static void explode_asteroid(struct Asteroid* asteroid)
{
    struct Point p;
    p.dx = asteroid->position.dx;
    p.dy = asteroid->position.dy;
    p.x  = asteroid->position.x;
    p.y  = asteroid->position.y;

    asteroid_count--;
    asteroid->exists = false;

    switch(asteroid->type)
    {
        case SMALL:
            asteroid->explode_countdown = EXPLOSION_LENGTH;
            create_trail_blaze(EXPLOSION_ASTEROID, &p);
            break;

        case MEDIUM:
            create_asteroid(SMALL, &p);
            create_asteroid(SMALL, &p);
            break;

        case LARGE:
            create_asteroid(MEDIUM, &p);
            create_asteroid(MEDIUM, &p);
            break;
    }
}

/*************************************************
** Handle ship.
*************************************************/

/* Initialise the ship */
static void initialise_ship(void)
{
    struct Point* point;
    struct Point* lives_point;
    int n;

    ship.position.x = CENTER_LCD_X * SCALE;
    ship.position.y = CENTER_LCD_Y * SCALE;
    ship.position.dx = 0;
    ship.position.dy = 0;
    ship.rotation.x = SIN_COS_SCALE;
    ship.rotation.y = 0;
    ship.exists = true;
    ship.explode_countdown = 0;
    ship.invulnerable_time = INVULNERABLE_TIME;

    point = ship.vertices;
    lives_point = lives_points;
    for(n = 0; n < NUM_SHIP_VERTICES*2; n += 2)
    {
        point->x = ship_vertices[n];
        point->y = ship_vertices[n+1];
        point->x *= SCALE;
        point->y *= SCALE;
        /* dx and dy are used when rotate polygon */
        point->dx = point->x;
        point->dy = point->y;
        /* grab a copy of the ships points for the lives display: */
        lives_point->x = point->x;
        lives_point->y = point->y;

        point++;
        lives_point++;
    }
}

/*
 * Draws the ship, moves the ship and creates a new
 * one if it's finished exploding.
 */
static void draw_and_move_ship(void)
{
    if (ship.invulnerable_time > BLINK_TIME || ship.invulnerable_time % 2 != 0)
    {
        SET_FG(COL_INVULN);
    }
    else
    {
        SET_FG(COL_PLAYER);
    }

    if (ship.exists)
    {
        draw_polygon(ship.vertices, NUM_SHIP_VERTICES,
                     ship.position.x, ship.position.y);
    }

    if (game_state != PAUSE_MODE)
    {
        if (ship.exists)
        {
            if (ship.invulnerable_time > 0)
                ship.invulnerable_time--;
            move_point(&ship.position);
        }
        else if (ship.explode_countdown > 0)
        {
            ship.explode_countdown--;
            if (ship.explode_countdown <= 0)
            {
                num_lives--;
                if (num_lives <= 0)
                {
                    game_state = GAME_OVER;
                }
                else
                {
                    initialise_ship();
                }
            }
        }
    }
}

static void explode_ship(void)
{
    if (!ship.invulnerable_time)
    {
        /* if not invulnerable, blow up ship */
        ship.explode_countdown = EXPLOSION_LENGTH;
        ship.exists = false;
        create_trail_blaze(EXPLOSION_SHIP, &ship.position);
    }
}

/* Rotate the ship using the passed sin & cos values */
static void rotate_ship(int cos, int sin)
{
    if (ship.exists)
    {
        rotate_polygon(ship.vertices, NUM_SHIP_VERTICES,
                       &ship.rotation, cos, sin);
    }
}

static void thrust_ship(void)
{
    if (ship.exists)
    {
        ship.position.dx += ( ship.vertices[0].x - ship.vertices[2].x )/20;
        ship.position.dy += ( ship.vertices[0].y - ship.vertices[2].y )/20;

        /* if dx and dy are below a certain threshold, then set 'em to 0
           but to do this we need to ascertain if the spacehip as moved on
           screen for more than a certain amount. */

        create_trail_blaze(EXPLOSION_THRUST, &ship.position);
    }
}

/* stop movement of ship, 'cos that's what happens when you go into hyperspace. */
static void hyperspace(void)
{
    if (ship.exists)
    {
        ship.position.dx = ship.position.dy = 0;
        ship.position.x = (rb->rand()%SCALED_WIDTH);
        ship.position.y = (rb->rand()%SCALED_HEIGHT);
    }
}

static void draw_lives(void)
{
    int n;
#if (LARGE_LCD)
    int px = (LCD_WIDTH-1 - 4)*SCALE;
    int py = (LCD_HEIGHT-1 - 6)*SCALE;
#else
    int px = (LCD_WIDTH-1 - 3)*SCALE;
    int py = (LCD_HEIGHT-1 - 4)*SCALE;
#endif

    SET_FG(COL_PLAYER);

    n = num_lives-1;
    while (n--)
    {
        draw_polygon(lives_points, NUM_SHIP_VERTICES, px, py);
#if (LARGE_LCD)
        px -= 8*SCALE;
#else
        px -= 6*SCALE;
#endif
    }
}

/*
 * missile
 */

/* Initialise a missile */
static void initialise_missile(struct Missile* missile)
{
    missile->position.x = ship.position.x + ship.vertices[0].x;
    missile->position.y = ship.position.y + ship.vertices[0].y;
    missile->position.dx = (ship.vertices[0].x - ship.vertices[2].x)/2;
    missile->position.dy = (ship.vertices[0].y - ship.vertices[2].y)/2;
    missile->alive = MISSILE_LIFE_LENGTH;
    missile->oldpoint.x = missile->position.x;
    missile->oldpoint.y = missile->position.y;
}

/* Fire the next missile */
static void fire_missile(void)
{
    struct Missile* missile;
    int n;

    if (ship.exists)
    {
        missile = missiles_array;
        n = MAX_NUM_MISSILES;
        while (n--)
        {
            if (missile->alive <= 0)
            {
                initialise_missile(missile);
                break;
            }
            missile++;
        }
    }
}

/* Draw and Move all the missiles */
static void draw_and_move_missiles(void)
{
    struct Missile* missile;
    struct Point vertices[2];
    int n;

    SET_FG(COL_MISSILE);

    missile = missiles_array;
    n = MAX_NUM_MISSILES;
    while (n--)
    {
        if (missile->alive > 0)
        {
            vertices[0].x = 0;
            vertices[0].y = 0;
            vertices[1].x = -missile->position.dx;
            vertices[1].y = -missile->position.dy;
            draw_polygon(vertices, 2, missile->position.x, missile->position.y);

            if (game_state != PAUSE_MODE)
            {
                missile->oldpoint.x = missile->position.x;
                missile->oldpoint.y = missile->position.y;
                move_point(&missile->position);
                missile->alive--;
            }
        }
        missile++;
    }
}

/*************************************************
** Handle enemy.
*************************************************/

static void initialise_enemy(void)
{
    struct Point* point;
    int n;
    int size;

    if (rb->rand()%100 > enemy.size_probability)
    {
        size = BIG_SHIP;
        enemy.size_probability++;
        if (enemy.size_probability > 90)
        {
            enemy.size_probability = ENEMY_BIG_PROBABILITY_START;
        }
    }
    else
    {
        size = LITTLE_SHIP;
        enemy.size_probability = ENEMY_BIG_PROBABILITY_START;
    }

    enemy.exists = true;
    enemy.explode_countdown = 0;
    enemy.appear_countdown = enemy.appear_timing;

    point = enemy.vertices;
    for(n = 0; n < NUM_ENEMY_VERTICES*2; n += 2)
    {
        point->x  = enemy_vertices[n];
        point->y  = enemy_vertices[n+1];
        point->x *= size*SCALE/2;
        point->y *= size*SCALE/2;
        point++;
    }

    if (ship.position.x >= SCALED_WIDTH/2)
    {
        enemy.position.dx = ENEMY_SPEED;
        enemy.position.x  = 0;
    }
    else
    {
        enemy.position.dx = -ENEMY_SPEED;
        enemy.position.x  = SCALED_WIDTH;
    }

    if (ship.position.y >= SCALED_HEIGHT/2)
    {
        enemy.position.dy = ENEMY_SPEED;
        enemy.position.y  = 0;
    }
    else
    {
        enemy.position.dy = -ENEMY_SPEED;
        enemy.position.y  = SCALED_HEIGHT;
    }

    enemy.position.dx *= SCALE/10;
    enemy.position.dy *= SCALE/10;
}

static void draw_and_move_enemy(void)
{
    SET_FG(COL_ENEMY);

    if (enemy.exists)
    {
        draw_polygon(enemy.vertices, NUM_ENEMY_VERTICES,
                     enemy.position.x, enemy.position.y);
    }

    if (game_state != PAUSE_MODE)
    {
        if (enemy.exists)
        {
            enemy.position.x += enemy.position.dx;
            enemy.position.y += enemy.position.dy;

            if (enemy.position.x > SCALED_WIDTH || enemy.position.x < 0)
                enemy.exists = false;

            enemy.position.y %= SCALED_HEIGHT;
            if (enemy.position.y < 0)
                enemy.position.y += SCALED_HEIGHT;

            if ((rb->rand()%1000) < 10)
                enemy.position.dy = -enemy.position.dy;
        }
        else if (enemy.explode_countdown > 0)
        {
            enemy.explode_countdown--;
        }
        else
        {
            if (enemy.appear_countdown > 0)
                enemy.appear_countdown--;
            else if (rb->rand()%100 >= enemy.appear_probability)
                initialise_enemy();
        }
    }

    if (enemy_missile.alive <= 0)
    {
        /* if no missile and the enemy is here and not exploding..
           then shoot baby! */
        if (enemy.exists && ship.exists &&
            game_state == PLAY_MODE && (rb->rand()%10) >= 5 )
        {
            int dx = ship.position.x - enemy.position.x;
            int dy = ship.position.y - enemy.position.y;

            if (dx < -SCALED_WIDTH/2) dx += SCALED_WIDTH;
            else if (dx > SCALED_WIDTH/2) dx -= SCALED_WIDTH;
            if (dy < -SCALED_HEIGHT/2) dy += SCALED_HEIGHT;
            else if (dy > SCALED_HEIGHT/2) dy -= SCALED_HEIGHT;

            enemy_missile.position.x = enemy.position.x;
            enemy_missile.position.y = enemy.position.y;

            /* lame, needs to be sorted - it's trying to shoot at the ship */
            if (dx < -5*SCALE)
                enemy_missile.position.dx = -1;
            else if (dx > 5*SCALE)
                enemy_missile.position.dx = 1;
            else
                enemy_missile.position.dx = 0;

            if (dy < -5*SCALE)
                enemy_missile.position.dy = -1;
            else if (dy > 5*SCALE)
                enemy_missile.position.dy = 1;
            else
                enemy_missile.position.dy = 0;

            while (enemy_missile.position.dx == 0 &&
                   enemy_missile.position.dy == 0)
            {
                enemy_missile.position.dx = rb->rand()%2-1;
                enemy_missile.position.dy = rb->rand()%2-1;
            }

            enemy_missile.position.dx *= SCALE;
            enemy_missile.position.dy *= SCALE;
            enemy_missile.alive = ENEMY_MISSILE_LIFE_LENGTH;
        }
    }
    else
    {
        rb->lcd_fillrect( enemy_missile.position.x/SCALE,
                          enemy_missile.position.y/SCALE,
                          POINT_SIZE, POINT_SIZE );
        if (game_state != PAUSE_MODE)
        {
            move_point(&enemy_missile.position);
            enemy_missile.alive--;
        }
    }
}

/*************************************************
** Check collisions.
*************************************************/

/* Add score if missile hit asteroid or enemy */
static void add_score(int val)
{
    current_score += val;
    if (current_score >= extra_life)
    {
        num_lives++;
        extra_life += EXTRA_LIFE;
    }
}

static bool is_point_within_asteroid(struct Asteroid* asteroid,
                                     struct Point* point)
{
    if (is_point_within_rectangle(&asteroid->position, point, asteroid->radius)
     && is_point_in_polygon(asteroid->vertices, NUM_ASTEROID_VERTICES,
                            point->x - asteroid->position.x,
                            point->y - asteroid->position.y))
    {
        explode_asteroid(asteroid);
        return true;
    }
    else
        return false;
}

static bool is_point_within_ship(struct Point* point)
{
    if (is_point_within_rectangle(&ship.position, point, SIZE_SHIP_COLLISION)
     && is_point_in_polygon(ship.vertices, NUM_SHIP_VERTICES,
                            point->x - ship.position.x,
                            point->y - ship.position.y))
    {
        return true;
    }
    else
        return false;
}

static bool is_point_within_enemy(struct Point* point)
{
    if (is_point_within_rectangle(&enemy.position, point, SIZE_ENEMY_COLLISION))
    {
        add_score(5);
        enemy.explode_countdown = EXPLOSION_LENGTH;
        enemy.exists = false;
        create_trail_blaze(EXPLOSION_ENEMY, &enemy.position);
        return true;
    }
    else
        return false;
}

static bool is_ship_within_asteroid(struct Asteroid* asteroid)
{
    struct Point p;

    if (!is_point_within_rectangle(&asteroid->position, &ship.position,
                                   asteroid->radius+SIZE_SHIP_COLLISION))
        return false;

    p.x = ship.position.x + ship.vertices[0].x;
    p.y = ship.position.y + ship.vertices[0].y;
    if (is_point_within_asteroid(asteroid, &p))
        return true;

    p.x = ship.position.x + ship.vertices[1].x;
    p.y = ship.position.y + ship.vertices[1].y;
    if (is_point_within_asteroid(asteroid, &p))
        return true;

    p.x = ship.position.x + ship.vertices[3].x;
    p.y = ship.position.y + ship.vertices[3].y;
    if (is_point_within_asteroid(asteroid, &p))
        return true;

    return false;
}

/* Check for collsions between the missiles and the asteroids and the ship */
static void check_collisions(void)
{
    struct Missile* missile;
    struct Asteroid* asteroid;
    int m, n;
    bool asteroids_onscreen = false;

    asteroid = asteroids_array;
    m = MAX_NUM_ASTEROIDS;
    while (m--)
    {
        /* if the asteroids exists then test missile collision: */
        if (asteroid->exists)
        {
            missile = missiles_array;
            n = MAX_NUM_MISSILES;
            while (n--)
            {
                /* if the missiles exists: */
                if (missile->alive > 0)
                {
                    /* has the missile hit the asteroid? */
                    if (is_point_within_asteroid(asteroid, &missile->position) ||
                        is_point_within_asteroid(asteroid, &missile->oldpoint))
                    {
                        add_score(1);
                        missile->alive = 0;
                        break;
                    }
                }
                missile++;
            }

            /* now check collision with ship: */
            if (asteroid->exists && ship.exists)
            {
                if (is_ship_within_asteroid(asteroid))
                {
                    add_score(1);
                    explode_ship();
                }
            }

            /* has the enemy missile blown something up? */
            if (asteroid->exists && enemy_missile.alive > 0)
            {
                if (is_point_within_asteroid(asteroid, &enemy_missile.position))
                {
                    enemy_missile.alive = 0;
                }
            }
        }

        /* is an asteroid still exploding? */
        if (asteroid->explode_countdown > 0)
            asteroids_onscreen = true;

        asteroid++;
    }

    /* now check collision between ship and enemy */
    if (enemy.exists && ship.exists)
    {
        /* has the enemy collided with the ship? */
        if (is_point_within_enemy(&ship.position))
        {
            explode_ship();
            create_trail_blaze(EXPLOSION_ENEMY, &enemy.position);
        }

        if (enemy.exists)
        {
            /* Now see if the enemy has been shot at by the ships missiles: */
            missile = missiles_array;
            n = MAX_NUM_MISSILES;
            while (n--)
            {
                if (missile->alive > 0 &&
                    is_point_within_enemy(&missile->position))
                {
                    missile->alive = 0;
                    break;
                }
                missile++;
            }
        }
    }

    /* test collision with enemy missile and ship: */
    if (enemy_missile.alive > 0 && is_point_within_ship(&enemy_missile.position))
    {
        explode_ship();
        enemy_missile.alive = 0;
        enemy_missile.position.x = enemy_missile.position.y = 0;
    }

    /* if all asteroids cleared then start again: */
    if (asteroid_count == 0 && !asteroids_onscreen
        && !enemy.exists && enemy.explode_countdown <= 0)
    {
        current_level++;
        if (current_level > MAX_LEVEL)
            current_level = START_LEVEL;
        enemy.appear_probability += 5;
        if (enemy.appear_probability >= 100)
            enemy.appear_probability = ENEMY_APPEAR_PROBABILITY_START;
        enemy.appear_timing -= 30;
        if (enemy.appear_timing < 30)
            enemy.appear_timing = 30;
        game_state = SHOW_LEVEL;
        show_level_timeout = SHOW_LEVEL_TIME;
    }
}

/*
 * stars
 */

static void create_stars(void)
{
    struct Point* p;
    int n;

    p = stars;
    n = NUM_STARS;
    while (n--)
    {
        p->x = (rb->rand()%LCD_WIDTH);
        p->y = (rb->rand()%LCD_HEIGHT);
        p++;
    }
}

static void drawstars(void)
{
    struct Point* p;
    int n;

    SET_FG(COL_STARS);

    p = stars;
    n = NUM_STARS;
    while (n--)
    {
        rb->lcd_drawpixel(p->x , p->y);
        p++;
    }
}

/*************************************************
** Creates start_num number of new asteroids of
** full size.
**************************************************/
static void initialise_level(int start_num)
{
    struct Asteroid* asteroid;
    struct Missile* missile;
    struct TrailPoint* tpoint;
    int n;
    asteroid_count = next_missile_count = next_thrust_count = 0;

    /* no enemy */
    enemy.exists = 0;
    enemy.explode_countdown = 0;
    enemy_missile.alive = 0;

    /* clear asteroids */
    asteroid = asteroids_array;
    n = MAX_NUM_ASTEROIDS;
    while (n--)
    {
        asteroid->exists = false;
        asteroid++;
    }

    /* make some LARGE asteroids */
    for(n = 0; n < start_num; n++)
        initialise_asteroid(&asteroids_array[n], LARGE, NULL);

    /* ensure all missiles are out of action: */
    missile = missiles_array;
    n = MAX_NUM_MISSILES;
    while (n--)
    {
        missile->alive = 0;
        missile++;
    }

    tpoint = trail_points;
    n = NUM_TRAIL_POINTS;
    while (n--)
    {
        tpoint->alive = 0;
        tpoint++;
    }
}

static void initialise_game(void)
{
    enemy.appear_probability = ENEMY_APPEAR_PROBABILITY_START;
    enemy.appear_timing = ENEMY_APPEAR_TIMING_START;
    enemy.appear_countdown = enemy.appear_timing;
    enemy.size_probability = ENEMY_BIG_PROBABILITY_START;
    current_level = START_LEVEL;
    num_lives = START_LIVES;
    extra_life = EXTRA_LIFE;
    current_score = 0;
    initialise_ship();
    initialise_level(0);
    game_state = SHOW_LEVEL;
    show_level_timeout = SHOW_LEVEL_TIME;
}

/* menu stuff */
static bool spacerocks_help(void)
{
    static char *help_text[] = {
        "Spacerocks", "", "Aim", "",
        "The", "goal", "of", "the", "game", "is", "to", "blow", "up",
        "the", "asteroids", "and", "avoid", "being", "hit", "by", "them.",
        "Also", "you'd", "better", "watch", "out", "for", "the", "UFOs!"
    };
    static struct style_text formation[]={
        { 0, TEXT_CENTER|TEXT_UNDERLINE },
        { 2, C_RED },
        LAST_STYLE_ITEM
    };

    rb->lcd_setfont(FONT_UI);
    SET_BG(LCD_BLACK);
    SET_FG(LCD_WHITE);
    if (display_text(ARRAYLEN(help_text), help_text, formation, NULL, true))
        return true;
    rb->lcd_setfont(FONT_SYSFIXED);

    return false;
}

#define PLUGIN_OTHER 10
static bool ingame;
static int spacerocks_menu_cb(int action, const struct menu_item_ex *this_item)
{
    if (action == ACTION_REQUEST_MENUITEM
        && !ingame && ((intptr_t)this_item)==0)
        return ACTION_EXIT_MENUITEM;
    return action;
}

static int spacerocks_menu(void)
{
    int selection = 0;
    MENUITEM_STRINGLIST(main_menu, "Spacerocks Menu", spacerocks_menu_cb,
                        "Resume Game", "Start New Game",
                        "Help", "High Scores",
                        "Playback Control", "Quit");
    rb->button_clear_queue();

    while (1)
    {
        switch (rb->do_menu(&main_menu, &selection, NULL, false))
        {
            case 0:
                return PLUGIN_OTHER;
            case 1:
                initialise_game();
                return PLUGIN_OTHER;
            case 2:
                if (spacerocks_help())
                    return PLUGIN_USB_CONNECTED;
                break;
            case 3:
                highscore_show(-1, highscores, NUM_SCORES, true);
                break;
            case 4:
                playback_control(NULL);
                break;
            case 5:
                return PLUGIN_OK;
            case MENU_ATTACHED_USB:
                return PLUGIN_USB_CONNECTED;
            default:
                break;
        }
    }
}

static int spacerocks_game_loop(void)
{
    int button;
    int end;
    int position;
    int ret;

    if ((ret = spacerocks_menu()) != PLUGIN_OTHER)
        return ret;

    SET_BG(LCD_BLACK);

    ingame = true;
    while (true)
    {
        end = *rb->current_tick + (CYCLETIME * HZ) / 1000;
        rb->lcd_clear_display();
        SET_FG(COL_TEXT);
        switch(game_state)
        {
            case GAME_OVER:
                ingame = false;
                rb->splash (HZ * 2, "Game Over");
                rb->lcd_clear_display();
                position = highscore_update(current_score, current_level, "",
                                            highscores, NUM_SCORES);
                if (position != -1)
                {
                    if (position == 0)
                        rb->splash(HZ*2, "New High Score");
                    highscore_show(position, highscores, NUM_SCORES, true);
                }
                return PLUGIN_OTHER;
                break;

            case PAUSE_MODE:
                rb->lcd_putsxyf(1,LCD_HEIGHT-8, "score %d ", current_score);
                rb->lcd_putsxy(CENTER_LCD_X - 15,
                               CENTER_LCD_Y + CENTER_LCD_Y/2 - 4, "pause");
                draw_and_move_missiles();
                draw_lives();
                draw_and_move_ship();
                break;

            case PLAY_MODE:
                rb->lcd_putsxyf(1, LCD_HEIGHT-8, "score %d ", current_score);
                draw_and_move_missiles();
                draw_lives();
                check_collisions();
                draw_and_move_ship();
                break;

            case SHOW_LEVEL:
                rb->lcd_putsxyf(1, LCD_HEIGHT-8, "score %d ", current_score);
                rb->lcd_putsxyf(CENTER_LCD_X - 20,
                               CENTER_LCD_Y + CENTER_LCD_Y/2 - 4,
                               "stage %d ", current_level);
                draw_lives();
                draw_and_move_ship();
                show_level_timeout--;
                if (show_level_timeout <= 0)
                {
                    initialise_level(current_level);
                    game_state = PLAY_MODE;
                }
                break;
        }
        draw_and_move_trail_blaze();
        drawstars();
        draw_and_move_asteroids();
        draw_and_move_enemy();

        rb->lcd_update();

#ifdef HAS_BUTTON_HOLD
        if (rb->button_hold() && game_state == PLAY_MODE)
            game_state = PAUSE_MODE;
#endif
        button = rb->button_get(false);
        switch(button)
        {
            case(AST_QUIT):
                return PLUGIN_OTHER;
                break;
#ifdef AST_PAUSE
            case(AST_PAUSE):
                if (game_state == PAUSE_MODE)
                    game_state = PLAY_MODE;
                else if (game_state == PLAY_MODE)
                    game_state = PAUSE_MODE;
                break;
#endif
            case (AST_LEFT):
            case (AST_LEFT | BUTTON_REPEAT):
                if (game_state == PLAY_MODE || game_state == SHOW_LEVEL)
                    rotate_ship(SHIP_ROT_ACW_COS, SHIP_ROT_ACW_SIN);
                break;

            case (AST_RIGHT):
            case (AST_RIGHT | BUTTON_REPEAT):
                if (game_state == PLAY_MODE || game_state == SHOW_LEVEL)
                    rotate_ship(SHIP_ROT_CW_COS, SHIP_ROT_CW_SIN);
                break;

            case (AST_THRUST):
            case (AST_THRUST | BUTTON_REPEAT):
                if (game_state == PLAY_MODE || game_state == SHOW_LEVEL)
                {
                    if (next_thrust_count <= 0)
                    {
                        next_thrust_count = 5;
                        thrust_ship();
                    }
                }
                break;

            case (AST_HYPERSPACE):
                if (game_state == PLAY_MODE)
                    hyperspace();
                /* maybe shield if it gets too hard */
                break;

            case (AST_FIRE):
            case (AST_FIRE | BUTTON_REPEAT):
                if (game_state == PLAY_MODE)
                {
                    if (next_missile_count <= 0)
                    {
                        fire_missile();
                        next_missile_count = 10;
                    }
                }
                else if(game_state == PAUSE_MODE)
                    game_state = PLAY_MODE;
                break;

            default:
                if (rb->default_event_handler(button)==SYS_USB_CONNECTED)
                    return PLUGIN_USB_CONNECTED;
                break;
        }

        if (next_missile_count > 0)
            next_missile_count--;

        if (next_thrust_count > 0)
            next_thrust_count--;

        if (TIME_BEFORE(*rb->current_tick, end))
            rb->sleep(end-*rb->current_tick);
        else
            rb->yield();
    }
}

enum plugin_status plugin_start(const void* parameter)
{
    (void)parameter;
    int ret = PLUGIN_OTHER;

#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
#endif
    /* universal font */
    rb->lcd_setfont(FONT_SYSFIXED);
    /* Turn off backlight timeout */
    backlight_ignore_timeout();
    highscore_load(SCORE_FILE, highscores, NUM_SCORES);
    rb->srand(*rb->current_tick);

    /* create stars once, and once only: */
    create_stars();

    while (ret == PLUGIN_OTHER)
        ret = spacerocks_game_loop();

    rb->lcd_setfont(FONT_UI);
    highscore_save(SCORE_FILE, highscores, NUM_SCORES);
    /* Turn on backlight timeout (revert to settings) */
    backlight_use_settings();

    return ret;
}