cl-tcod / tcod.lisp

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
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*- ;;;;;;;;;;;;;;;;;80

(in-package :cl-user)


;;;
;;; Windows users: change the string "libtcod-mingw.dll" to reflect the name
;;; of your libtcod library (DLL file).
;;;
;;; Colours are passed to C as integers. There is also a system mapping
;;; - make a colour from R, G, B values using COMPOSE-COLOUR.
;;; - break down a colour into R, G and B values using DECOMPOSE-COLOUR.
;;; - to start the colour system call START-COLOURS.
;;; - to make a new colour and associate it with a name, use MAKE-COLOUR.

(declaim (optimize (speed 0) (safety 2) (debug 3)))

#+sbcl (declaim (sb-ext:muffle-conditions sb-ext:compiler-note))


(defpackage :tcod
  (:nicknames :cl-tcod)
  (:use :cl :cffi :defstar)
  (:export
   #:*root*
   #:+null+
   ;; [[Colours]] ==========================================================
   #:start-colours
   #:start-colors
   #:colour
   #:color
   #:compose-colour
   #:compose-color
   #:decompose-colour
   #:decompose-color
   #:colour-rgb
   #:color-rgb
   #:colour-hsv
   #:color-hsv
   #:invert-colour
   #:invert-color
   #:colour->grayscale
   #:color->grayscale
   #:colour-set-hsv
   #:colour-get-hsv
   #:colour-get-hue
   #:colour-get-saturation
   #:colour-get-value
   #:colour-set-hue
   #:colour-set-saturation
   #:colour-set-value
   #:colour-shift-hue
   #:colour-equals?
   #:colour-add
   #:colour-subtract
   #:colour-multiply
   #:colour-multiply-scalar
   #:colour-lerp
   #:make-colour
   #:color-set-hsv
   #:color-get-hsv
   #:color-get-hue
   #:color-get-saturation
   #:color-get-value
   #:color-set-hue
   #:color-set-saturation
   #:color-set-value
   #:color-shift-hue
   #:color-equals?
   #:color-add
   #:color-subtract
   #:color-multiply
   #:color-multiply-scalar
   #:color-lerp
   #:make-color
   #:background-alpha
   #:background-add-alpha
   ;; [[Console]] ==========================================================
   #:console-init-root
   #:console-set-window-title
   #:console-is-fullscreen?
   #:console-set-fullscreen
   #:console-is-window-closed?
   #:console-set-custom-font
   #:console-map-ascii-code-to-font
   #:console-map-ascii-codes-to-font
   #:console-map-string-to-font
   #:console-set-dirty
   #:console-set-default-foreground
   #:console-set-default-background
   #:console-set-char-foreground
   #:console-set-char-background
   #:console-set-char
   #:console-put-char
   #:console-put-char-ex
   #:console-set-background-flag
   #:console-get-background-flag
   #:console-set-alignment
   #:console-get-alignment
   #:console-print
   #:console-print-ex
   #:console-print-rect
   #:console-print-rect-ex
   #:console-get-height-rect
   #:console-rect
   #:console-hline
   #:console-vline
   #:console-print-frame
   #:console-print-double-frame
   #:console-get-default-foreground
   #:console-get-default-background
   #:console-get-char-foreground
   #:console-get-char-background
   #:console-get-char
   #:console-set-fade
   #:console-get-fade
   #:console-get-fading-colour
   #:console-flush
   #:console-set-colour-control
   #:console-new
   #:console-get-height
   #:console-get-width
   #:console-set-color-control
   #:console-get-fading-color
   #:console-clear
   #:console-blit
   #:console-delete
   #:console-credits
   #:console-credits-reset
   #:console-credits-renderer
   #:legal-console-coordinates?
   #:console-fill-char
   #:console-set-key-colour
   #:console-set-key-color
   #:drawing-character
   #:colctrl
   #:colctrl->char
   #:background-flag
   #:console
   ;; [[Unicode]] =============================================================
   #:console-map-string-to-font-utf
   #:console-print-utf
   #:console-print-ex-utf
   #:console-print-rect-utf
   #:console-print-rect-ex-utf
   #:console-get-rect-height-utf
   ;; [[Keyboard input]] ======================================================
   #:key
   #:console-check-for-keypress
   #:console-wait-for-keypress
   #:keycode
   #:key-p
   #:key-c
   #:key-vk
   #:key-lalt
   #:key-ralt
   #:key-lctrl
   #:key-rctrl
   #:key-shift
   #:make-key
   #:make-simple-key
   #:same-keys?
   #:key-state
   #:key-pressed
   #:is-key-pressed?
   #:console-set-keyboard-repeat
   #:console-disable-keyboard-repeat
   ;; [[Mouse]] ===============================================================
   #:mouse
   #:make-mouse
   #:mouse-x
   #:mouse-y
   #:mouse-cx
   #:mouse-cy
   #:mouse-dx
   #:mouse-dy
   #:mouse-lbutton
   #:mouse-mbutton
   #:mouse-rbutton
   #:mouse-lbutton-pressed
   #:mouse-mbutton-pressed
   #:mouse-rbutton-pressed
   #:mouse-wheel-up
   #:mouse-wheel-down
   #:mouse-move
   #:mouse-get-status
   #:mouse-is-cursor-visible?
   #:mouse-show-cursor
   #:mouse-get-x
   #:mouse-get-y
   #:mouse-get-cx
   #:mouse-get-cy
   #:mouse-get-dx
   #:mouse-get-dy
   #:mouse-get-dcx
   #:mouse-get-dcy
   #:mouse-get-lbutton
   #:mouse-get-mbutton
   #:mouse-get-rbutton
   #:mouse-get-lbutton-pressed
   #:mouse-get-mbutton-pressed
   #:mouse-get-rbutton-pressed
   ;; [[Image]] ===============================================================
   #:image-new
   #:image-from-console
   #:image-refresh-console
   #:image-load
   #:image-clear
   #:image-invert
   #:image-hflip
   #:image-vflip
   #:image-rotate90
   #:image-scale
   #:image-save
   #:image-get-width                    ; these replace image-get-size
   #:image-get-height
   #:image-get-pixel
   #:image-get-alpha
   #:image-get-mipmap-pixel
   #:image-put-pixel
   #:image-blit
   #:image-blit-rect
   #:image-blit-2x
   #:image-delete
   #:image-set-key-colour
   #:image-set-key-color
   #:image-is-pixel-transparent?
   ;; [[Random]] ==============================================================
   #:random-new
   #:random-get-instance
   #:random-save
   #:random-restore
   #:random-new-from-seed
   #:random-set-distribution
   #:random-delete
   #:random-get-int
   #:random-get-float
   #:random-get-double
   #:random-get-int-mean
   #:random-get-float-mean
   #:random-get-double-mean
   #:random-dice-new ;; not yet mentioned in libtcod docs
   #:random-dice-roll ;;
   #:random-dice-roll-s ;;
   ;; [[Noise]] ===============================================================
   #:noise-new
   #:noise-delete
   #:noise-set-type
   #:noise-get-ex
   #:noise-get-fbm-ex
   #:noise-get-turbulence-ex
   #:noise-get
   #:noise-get-fbm
   #:noise-get-turbulence
   ;; [[Heightmap]] ===========================================================
   #:heightmap
   #:heightmap-new
   #:heightmap-delete
   #:heightmap-set-value
   #:heightmap-add
   #:heightmap-scale
   #:heightmap-clear
   #:heightmap-clamp
   #:heightmap-copy
   #:heightmap-normalize
   #:heightmap-normalise
   #:heightmap-lerp-hm
   #:heightmap-add-hm
   #:heightmap-multiply-hm
   #:heightmap-add-hill
   #:heightmap-dig-hill
   #:heightmap-rain-erosion
   #:heightmap-kernel-transform
   #:heightmap-add-voronoi
   #:heightmap-add-fbm
   #:heightmap-scale-fbm
   #:heightmap-dig-bezier
   #:heightmap-get-value
   #:heightmap-get-interpolated-value
   #:heightmap-get-slope
   #:heightmap-get-normal
   #:heightmap-count-cells
   #:heightmap-has-land-on-border?
   #:heightmap-get-min
   #:heightmap-get-max
   #:heightmap-islandify
   #:heightmap-dig-line    ; defined in cl-tcod
   ;; [[Field of view]] =======================================================
   #:fov-algorithm
   #:mapptr
   #:map-new
   #:map-set-properties
   #:map-clear
   #:map-copy
   #:map-delete
   #:map-compute-fov
   #:map-is-in-fov?
   #:map-set-in-fov
   #:map-is-transparent?
   #:map-is-walkable?
   #:map-get-width
   #:map-get-height
   #:map-get-nb-cells
   ;; [[A* pathfinding]] ======================================================
   #:a*-path
   #:path-new-using-map
   #:path-new-using-function
   #:path-delete
   #:path-compute
   #:path-reverse
   #:path-get
   #:path-get-origin
   #:path-get-destination
   #:path-size
   #:path-walk
   #:path-is-empty?
   ;; [[Dijkstra pathfinding]] ================================================
   #:dijkstra-path
   #:dijkstra-new
   #:dijkstra-new-using-function
   #:dijkstra-delete
   #:dijkstra-compute
   #:dijkstra-reverse
   #:dijkstra-path-set
   #:dijkstra-size
   #:dijkstra-get
   #:dijkstra-is-empty?
   #:dijkstra-path-walk
   #:dijkstra-get-distance
   ;; [[Bresenham line drawing]] ==============================================
   #:line-init
   #:line-step
   #:line-line
   ;; [[BSP trees]] ===========================================================
   #:bsp-new-with-size
   #:bsp-remove-sons
   #:bsp-split-once
   #:bsp-split-recursive
   #:bsp-delete
   #:bsp-resize
   #:bsp-left
   #:bsp-right
   #:bsp-father
   #:bsp-is-leaf?
   #:bsp-contains?
   #:bsp-find-node
   #:bsp-traverse-pre-order
   #:bsp-traverse-in-order
   #:bsp-traverse-post-order
   #:bsp-traverse-level-order
   #:bsp-traverse-inverted-level-order
   ;; [[Name generation]] =====================================================
   #:namegen-parse
   #:namegen-destroy
   #:namegen-generate
   #:namegen-generate-custom
   ;;#:namegen-get-sets -- not yet implemented as returns a TCOD_list_t type
   ;; [[Compression toolkit]] =================================================
   #:zipptr
   #:zip-new
   #:zip-delete
   #:zip-put
   #:zip-put-char
   #:zip-put-int
   #:zip-put-float
   #:zip-put-string
   #:zip-put-colour
   #:zip-put-color
   #:zip-put-image
   #:zip-put-console
   #:zip-put-data
   #:zip-get-char
   #:zip-get-int
   #:zip-get-float
   #:zip-get-string
   #:zip-get-image
   #:zip-get-colour
   #:zip-get-color
   #:zip-get-console
   #:zip-get-data
   #:zip-get-current-bytes
   #:zip-get-remaining-bytes
   #:zip-skip-bytes
   #:zip-save-to-file
   #:zip-load-from-file
   ;; [[System]] ==============================================================
   #:sys-set-fps
   #:sys-get-fps
   #:sys-get-last-frame-length
   #:sys-sleep-milli
   #:sys-elapsed-milli
   #:sys-elapsed-seconds
   #:sys-save-screenshot
   #:sys-create-directory ;;
   #:sys-delete-directory ;;
   #:sys-get-current-resolution
   #:sys-force-fullscreen-resolution ;;
   #:sys-get-fullscreen-offsets ;;
   #:sys-get-renderer
   #:sys-set-renderer
   #:sys-register-sdl-renderer ;;
   #:sys-get-char-size
   #:sys-update-char ;;
   #:sys-clipboard-set ;;
   #:sys-clipboard-get ;;
   #:sys-flush
   ;; [[Testing]] =============================================================
   )
  (:documentation
   "* Introduction

Welcome to CL-TCOD, an interface between Common Lisp and the Doryen Library,
AKA `libtcod', a portable truecolour console library intended for use with
roguelike games.

CL-TCOD consists of the following files:
1. =tcod.lisp=, a lisp file which creates lisp bindings for C functions in the
   compiled libtcod library, using the =CFFI= lisp foreign function interface.
2. =tcod.asd=, which allows TCOD to be easily loaded and used as a library by
   other common lisp programs, via the =ASDF= library-loading facility.
3. =tcod-colours.lisp=, a lisp file containing definitions for all the colours
   named in /etc/X11/rgb.txt; autogenerated using 'parse-rgb' (see below)
4. =parse-rgb.lisp=, a lisp file containing code for parsing =/etc/X11/rgb.txt=
   and generating tcod-colours.lisp
5. =parse-rgb.asd=, ASDF system definition file for =parse-rgb.lisp=

CL-TCOD has been tested with SBCL 1.0.36 on Linux and Windows, Clozure 1.5
on Linux and Windows, and CLISP on Windows.

**Note** that it has not been used on a Mac; if you do this you may need to
tell CFFI the name of the compiled libtcod library under MacOS. To do this,
open =tcod.lisp= in an editor, find the ='(define-foreign-library...'= clause,
uncomment the ='(:macintosh...)'= line and change the string on that line to
the name of the libtcod library file.

* License

The CL-TCOD package is placed in the Public Domain by its author.

* Dependencies

=CL-TCOD= depends on the following libraries:
1. ASDF: [[http://common-lisp.net/project/asdf/]]
2. DEFSTAR: [[http://bitbucket.org/eeeickythump/defstar/]]
3. CFFI: [[http://common-lisp.net/project/cffi/]]

* Hints on installation

You need to know your way around your chosen common lisp and how to install and
load lisp libraries before proceeding. You also need to have a version of
libtcod newer than 1.4.1rc2, which is the first version that includes the
='wrappers.c'= and ='wrappers.h'= source files that allow CL-TCOD to interface
with libtcod.

1. Ensure you have a working common lisp installation.
2. Ensure either [[http://www.quicklisp.org/][Quicklisp]] or the ASDF lisp
   library is installed.
3. If CFFI or DEFSTAR are not installed, download and install them somewhere
   ASDF can find them. CFFI requires several third-party lisp libraries -- see
   the CFFI documentation for more details.  Note that if you have
   Quicklisp installed, you can install CFFI and its dependencies
   easily using the command =(ql:quickload \"cffi\")= at the Lisp prompt.
4. Put the CL-TCOD files in a directory where ASDF can find them.
5. Make sure libtcod is installed and compiled. Make sure the libtcod
   dynamically linked library (=.DLL= or =.SO= file) is somewhere your lisp system
   can find it. It probably is, but if CFFI complains about being unable to
   find the library, you can either copy it to an appropriate directory or add
   its directory to the list variable =cffi:*foreign-library-directories*=
   e.g. by typing the following in the lisp interpreter:

;;;   (push #P\"/my/libtcod/directory/\" cffi:*foreign-library-directories*)

   *On windows*, DLL files should be put in one of the directories listed in the
   =PATH= environment variable. You will need to put =SDL.dll= in the same place
   if you don't already have SDL installed.

   *On Linux*, you can usually put .SO files in =/usr/local/lib/=.
   Use your package installer to install =libSDL=.
   Try running the libtcod demo programs to check everything works.

6. Start lisp, then load CL-TCOD. Using Quicklisp:

;;;   (ql:quickload :tcod)

   Using ASDF:

;;;   (load \"/path/to/asdf/asdf.lisp\")
;;;   (asdf:oos 'asdf:load-op :tcod)

7. Type something like the following commands at the lisp prompt to start using TCOD
   from within Lisp. Alternatively you can type =(tcod:hello-world)=, which
   is a function containing the code below.

;;;   (tcod:console-set-custom-font \"terminal.png\" '(:font-layout-ascii-in-row) 16 16)
;;;   (tcod:console-init-root 80 25 \"Test\" nil)
;;;   (tcod:console-clear tcod:*root*)
;;;   (tcod:console-print tcod:*root* 1 1 \"Hello, world!~%\")
;;;   (tcod:console-wait-for-keypress t)


* Differences between CL-TCOD and libtcod

** Naming conventions

The C function =TCOD_foobar= corresponds to the lisp function =foobar=, which
is in the =tcod= package (and so requires a prefix of =tcod:= to access in most
situations). Underscores become hyphens. So:

:  TCOD_foobar_function(a, b)     <===>    (tcod:foobar-function a b)

`Predicate functions' are functions whose main job is to return a boolean
value, true (non =NIL=) or false (=NIL=), that answers a question. These have a
terminal '?' added to their name:

:  TCOD_console_is_fullscreen()   <===>    (tcod:console-is-fullscreen?)

C enums have generally more succinct names. As they are lisp keywords, their
names all begin with =':'=. THey are named according to the following pattern:

:  TCODK_BACKSPACE (etc)         <===>  :backspace
:  TCOD_CHAR_HLINE  (etc)        <===>  :char-hline
:  TCOD_COLCTRL_1  (etc)         <===>  :colctrl-1
:  TCOD_BKGND_SET (etc)          <===>  :set
:  TCOD_FONT_LAYOUT_ASCII_INCOL  <===>  :font-layout-ascii-in-col
:  FOV_SHADOW                    <===>  :fov-shadow
:  TCOD_KEY_PRESSED              <===>  :key-pressed
:  CENTER                        <===>  :center

In general, most functions exist in both U.S. and non-U.S. spellings, This is
mainly relevant to those functions with colour/color or centre/center in their
names.

** Colournums

In libtcod, colours are represented as structures containing three integer
values: *red*, *green* and *blue* (each 0-255). The name of the structure type is
=TCOD_color_t=.

In CL-TCOD, these colour structs are converted into 3-byte integers using the C
functions =int_to_color(int)= and =color_to_int(TCOD_color_t)=, both defined in
=wrappers.c=. The 3 bytes are red, green and blue in order (blue is 1's). ie:

:    /* C */                              ;; lisp ;;
:   struct TCOD_color_t {r, g, b}  <==>   #x00RRGGBB

So, for example, one way to use the function =TCOD_color_multiply_scalar= from
lisp is:

;;;  (tcod:color-multiply-scalar (tcod:compose-colour 218 165 32) 0.5)

All C functions that take or return =TCOD_color_t= structs, are wrapped by lisp
functions that take or return integers as described above.

** Colours by keyword

A lisp keyword is any symbol beginning with ':'. In lisp, keywords (like all
symbols) are first-class values and can be passed around just like any other
value. CL-TCOD uses keywords to refer to particular colours, for example the
keyword =:cyan= refers to the colour #x0056A3CD (or 5678029 in decimal notation).

You can use keywords instead of colournums as arguments to lisp functions, by
using the function =colour= to return the colournum associated with a keyword:

;;;  (tcod:colour :cyan)    ; returns 5678029


You can also define your own colour names, like so:

;;;  (tcod:make-colour :my-goldenrod 218 165 32)
;;;  (tcod:color-multiply-scalar (tcod:colour :my-goldenrod) 0.5)

CL-TCOD knows all the colour names defined in the 'rgb.txt' file under
Xwindows, eg =:navajo-white, :honeydew, :mint-cream=, and so on. There is
nothing special about the fact that rgb.txt comes from Xwindows -- the colours
are just named R,G,B values and can be used anywhere that CL-TCOD can be
used. Look in the source file ='tcod-colours.lisp'= to see the available colour
names. If you are using [[http://www.gnu.org/software/emacs/][GNU Emacs]], the
king of lisp IDEs, do =M-x list-colors-display= to see a list of all colours.

** Lisp =format= versus C =printf=

The TCOD functions that accept =printf=-like string-formatting arguments,
have been modified to instead accept arguments to Common Lisp's =format=
function.'  For example:

#+BEGIN_SRC c
  TCOD_console_print (con, x, y, \"Printing at %d, %d\n\", x, y);
#+END_SRC

becomes:

;;;    (tcod:console-print con x y \"Printing at ~D, ~D~%\" x y)

** Miscellaneous extra functions

- [[console-print-double-frame]] is like [[console-print-frame]], but
  but draws using `double-line' characters:

;;;  (tcod:console-print-double-frame CONSOLE X Y W H EMPTY? STRING...)

** Coverage

Does not provide wrappers for:
- File parser. Using this from lisp would be a very cumbersome way to read
  values from a file, as the resulting values are not lisp objects. You would
  be better to either consider using the lisp
  `read' function, or looking into lisp libraries for parser generation.
- =namegen-get-sets= -- I haven't yet implemented this as it will have to
  involve converting from libtcod's bespoke 'linked list' to a lisp list.
  You may be better to write your random name generator in lisp (fairly trivial).
- =sys-get-directory-content=, =sys-file-exists=, =sys-is-directory=,
  =sys-delete-file=: Common Lisp already has functions that do the same thing.

* Resources

** Specific to CL-TCOD and libtcod

The latest version of CL-TCOD is available at:

    [[http://bitbucket.org/eeeickythump/cl-tcod/]]

Forum for discussion of CL-TCOD and use of lisp in roguelike games:

    [[http://doryen.eptalys.net/forum/index.php?board=33.0][Roguecentral Lisp forum]]

The latest version of libtcod is available at:

    [[http://doryen.eptalys.net/libtcod/]]

This Common Lisp package depends on CFFI, the Common Foreign Function Interface:

    [[http://common-lisp.net/project/cffi/]]

** Learning Common Lisp

Recently written book, 'Practical Common Lisp'. buy hard copy or download free.
Recommended, especially if coming from non-lisp languages.

- [[http://www.gigamonkeys.com/book/]]

[[http://www.quicklisp.org/][Quicklisp]] allows you to very easily install
libraries -- it automatically downloads and installs a library and its
dependencies, from within Lisp.  If you don't decide to go with Lisp in a
Box (below), then Quicklisp should be the first thing you install once you have
your lisp running.

*\"Lisp in a Box\"* -- aims to make it easy to start using Common Lisp by
providing a single download with everything set up in advance (Lisp, Emacs,
SLIME, and Quicklisp).

- [[http://common-lisp.net/project/lispbox/]]

Lisp editors and IDEs:
- [[http://www.gnu.org/software/emacs/][GNU Emacs]] (the best; see below)
  - [[http://common-lisp.net/project/slime/][SLIME]] is the Emacs interface to
    Common Lisp.
- [[http://bitfauna.com/projects/cusp/][Cusp]], a common lisp plugin for Eclipse.
- The [[http://www.franz.com/products/allegrocl/][Allegro]] and
  [[http://www.lispworks.com/][LispWorks]] lisp implementations each have a
  builtin IDE.
- If you are on a Mac, the free, high-quality [[http://ccl.clozure.com][Clozure CL]]
  has a builtin graphical IDE.
- Some editors with good lisp syntax highlighting include jEdit and Notepad++.

** A note on editors and IDEs

Emacs is a very powerful program. It is mainly used as a programmers' text and
source code editor, but it can do -- and plugins exist to make it do -- just
about anything you can imagine. It is mostly written in a dialect of lisp, and
this is also its extension language. When combined with SLIME, a plugin
that allows it to communicate directly with a running common lisp
compiler/interpreter, Emacs is not only the best IDE for common lisp, but
one of the best and most advanced IDEs available for any programming language.

The downside: because Emacs + SLIME is so good, common lisp programmers have
put very little effort into getting other popular programming editors/IDEs to
support common lisp, at least beyond simple syntax highlighting. Emacs is an
idiosyncratic program (though development is active, it is about 34 years old)
and despite good efforts to modernise/regularise its interface it still has a
steeper learning curve than many other IDEs, especially when you are also
struggling to set up SLIME and get it to communicate through a socket with
your lisp process...

My advice is that while all roads lead to Emacs, you don't have to hurry to get
there. Initially you should concentrate on getting common lisp set up and
starting to learn the language. Think about using the trial version of one of
the big commercial implementations (Allegro or LispWorks), as they have
built-in IDEs. Once you are ready to move on from them, install Emacs and
SLIME.

** Commercial Common Lisp implementations

These are both high quality, but painfully expensive. Luckily they have
'trial' versions that can be downloaded for free, and which I recommend you
use when beginning to learn Common Lisp as they come with integrated
graphical editors/development environments (although if you have a Mac
you may wish to investigate Clozure CL's IDE -- see below).

- [[http://www.franz.com/products/allegrocl/][Allegro]] -- starts at $599 USD
- [[http://www.lispworks.com/][LispWorks]] -- starts at $900 USD for a
  noncommercial license. The trial version quits automatically after 5 hours.

** Full-featured, free Common Lisp implementations

Move on to one of these if and when you outgrow Allegro or LispWorks.

For the title of the best, most robust free multiplatform Common Lisp compiler,
it is currently a very close call between these two:
- [[http://www.sbcl.org][Steel Bank Common Lisp (SBCL)]] Compiles to
  machine code, great on Linux/Mac,
  still nominally 'experimental' on Windows but actually seems very stable
  on that platform.
- [[http://ccl.clozure.com][Clozure CL]] Compiles to machine code; native to
  Mac but recently ported to Linux and Windows. Formerly known as OpenMCL.
  The Mac version has a graphical IDE.
  Not to be confused with [[http://clojure.org][Clojure]], which is a different
  dialect of lisp from Common Lisp.

Other worthwhile free implementations:
- [[http://clisp.cons.org][GNU CLISP]] Bytecode compiler, but runs pretty much
  everywhere, easy to install on Windows.
- [[http://ecls.sourceforge.net/][Embeddable Common Lisp]] Promising, compiles
  to C and then passes code to your C compiler. Does this 'on the fly' when
  running as an interpreter. Also designed to be easily embeddable in non-Lisp
  applications as a scripting language.
- [[http://common-lisp.net/project/armedbear/][Armed Bear Common Lisp]]
  Common Lisp compiler running inside the Java virtual machine, so your
  code will run on any platform and can use all the Java libraries. I'm not
  sure you'll be able to use libtcod with this though.

Help & advice with lisp:

    [[http://www.lispforum.com]]
"))

(in-package :tcod)

(eval-when (:compile-toplevel :load-toplevel :execute)
 (pushnew :tcod *features*))

;;; Comment this out if you want cl-tcod to be 'fast' rather than 'safe and
;;; maximally debuggable'
(eval-when (:compile-toplevel :load-toplevel :execute)
 (pushnew :tcod-debug *features*))


#+tcod-debug (declaim (optimize (speed 0) (safety 3) (debug 3)))
#-tcod-debug (declaim (optimize (speed 3) (safety 1) (debug 0)))


;;; CFFI 0.10.0 started using Babel to "encode" strings. This breaks extended
;;; ASCII characters when the default encoding scheme of :UTF-8 is used, ie C
;;; will receive different characters from those which are sent to it by the
;;; Lisp program. To actually pass the literal string to C, we need to change
;;; the encoding scheme to ISO-8859-1.
;;;

(setf cffi:*default-foreign-encoding* :iso-8859-1)

;;; If debug mode is on, force compiler to explicitly enforce type declarations
;;; for function arguments, raising an error when a type mismatch occurs.
(eval-when (:compile-toplevel :load-toplevel :execute)
  (setf defstar:*check-argument-types-explicitly?*
        #+tcod-debug t
        #-tcod-debug nil))


;;;; <<Library>> ==============================================================



(define-foreign-library libtcod
	(:unix #+libtcod-debug "libtcod-debug.so"
               #-libtcod-debug "libtcod.so")
	(:windows #-libtcod-debug "libtcod-mingw.dll"
                  #+libtcod-debug "libtcod-mingw-debug.dll")
	;; (:macintosh "NAME-OF-LIBTCOD-LIBRARY-IN-MACOS")
	(t (:default "libtcod")))

(defvar *libtcod-loaded* nil
  "Global variable, set to non-nil once libtcod is loaded. This is to
avoid crashes which occur in some CL implementations when you load
an already-loaded foreign library.")

(eval-when (:load-toplevel :execute)
	(unless *libtcod-loaded*
		(use-foreign-library libtcod)
		(setf *libtcod-loaded* t)))


;;;; <<Macros>> ===============================================================


;;; The following are some wrapper macros to ease the creation
;;; of `type-safe' CFFI wrappers to C functions.


(defmacro define-c-enum (name &rest vals)
  "Defines both the CFFI =enum= type, and a lisp type of the same
name which is satisified by any of the values allowed by the enum type."
  `(eval-when (:compile-toplevel :load-toplevel :execute)
     (progn
       (defcenum ,name ,@vals)
       (deftype ,name ()
         '(member ,@(mapcar #'(lambda (val)
                                (if (listp val) (car val) val))
                            vals))))))


(defmacro define-c-bitfield (name &rest clauses)
  "Defines both the CFFI bitfield, and a lisp type of the same name, which
is satisfied by a list containing only bitfield keywords as members."
  (flet ((make-predicate (sym)
           (intern (concatenate 'string (string sym) "-PREDICATE"))))
    `(eval-when (:compile-toplevel :load-toplevel :execute)
       (progn
         (defbitfield ,name ,@clauses)
         (defun ,(make-predicate name) (ls)
           (and (listp ls)
                (null (set-difference
                       ls ',(mapcar #'car clauses)))))
         (deftype ,name ()
           '(satisfies ,(make-predicate name)))))))


(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun prepend-percent (sym)
    (intern (concatenate 'string "%%" (string sym)))))


(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun c-type->lisp-type (c-type)
    "Given a CFFI foreign type, return an equivalent lisp type."
    (case c-type
      (:boolean 'boolean)
      ((:int :unsigned-int) 'uint)
      (:char 'signed-char)
      (:unsigned-char 'uchar)
      (:uint8 'uint8)
      (:uint32 'uint32)
      (:float 'single-float)
      (:double 'double-float)
      (:pointer (type-of (null-pointer)))
      (:string 'string)
      (:void t)
      (otherwise
       (if (simple-type? c-type)
           c-type
           (error "In C-TYPE->LISP-TYPE: unrecognised C type `~S'." c-type))))))


(defmacro define-c-function ((foreign-fn-name fn-name) return-type args
                             &body body)
  "Format is similar to =CFFI:DEFCFUN=, except that:
1. The function arguments are wrapped in a set of outer parentheses.
2. Everything after this `arguments' term is considered to be the body
   of the wrapper function. Within this body, the macro =(call-it)=
   will call the actual C function. If =call-it= is called with no arguments,
   it will pass all the arguments given to the wrapper function, to the
   C function. Otherwise it will pass whatever arguments it is given, to
   the C function (similar to =call-next-method=).
3. If there is nothing after the function arguments, then the wrapper function
   body will automatically consist of a single call to the underlying
   C function."
  (let ((args-no-rest (remove '&rest args)))
    `(progn
       (cond
         ((null (cffi:foreign-symbol-pointer ,foreign-fn-name))
          (warn "Foreign function not found: ~S" ,foreign-fn-name))
         (t
          (defcfun (,foreign-fn-name ,(prepend-percent fn-name)) ,return-type
            ,@args)
          (declaim (inline ,fn-name))
          (defun* (,fn-name -> ,(c-type->lisp-type return-type))
              ,(mapcar #'(lambda (clause)
                           `(,(first clause) ,(c-type->lisp-type (second clause))
                              ,@(cddr clause)))
                       args-no-rest)
            ,@(if (stringp (car body)) (list (pop body)) nil)
            ,(if body
                 `(macrolet ((call-it (&rest callargs)
                               (cons ',(prepend-percent fn-name)
                                     (or callargs '(,@(mapcar #'car args-no-rest))))))
                    ,@body)
                 `(,(prepend-percent fn-name) ,@(mapcar #'car args-no-rest)))))))))



(defmacro define-c-type (name foreign-type)
  "Define both a CFFI foreign type, and a corresponding lisp type of the same
name."
  `(progn
     (defctype ,name ,foreign-type)
     (deftype ,name () ',(c-type->lisp-type foreign-type))))


(defmacro clamp (low hi expr)
  "Return the numeric value of EXPR, constrained to the range [LOW ... HI]."
  `(min ,hi (max ,low ,expr)))


;;;; <<Types>> ================================================================


(deftype uint32 () `(unsigned-byte 32))
(deftype uint24 () `(unsigned-byte 24))
(deftype uint16 () `(unsigned-byte 16))
(deftype uint8 () `(unsigned-byte 8))
(deftype uint () `(unsigned-byte ,(* 8 (foreign-type-size :int))))
(deftype uchar () `(unsigned-byte ,(* 8 (foreign-type-size :unsigned-char))))
(deftype signed-char () `(signed-byte ,(* 8 (foreign-type-size :char))))

(deftype sint16 () `(signed-byte 16))

(deftype ucoord () `(integer 0 1000))


(define-c-type colournum :unsigned-int)


;; TCOD_color_t
;; This is seldom used -- colournums are used instead (see above).
(defcstruct colour
  (r :uint8)
  (g :uint8)
  (b :uint8))


;; TCOD_renderer_t (enum)
(define-c-enum renderer
  :RENDERER-GLSL
  :RENDERER-OPENGL
  :RENDERER-SDL)


;; TCOD_keycode_t (enum)
(define-c-enum keycode
  :NONE
  :ESCAPE
  :BACKSPACE
  :TAB
  :ENTER
  :SHIFT
  :CONTROL
  :ALT
  :PAUSE
  :CAPSLOCK
  :PAGEUP
  :PAGEDOWN
  :END
  :HOME
  :UP
  :LEFT
  :RIGHT
  :DOWN
  :PRINTSCREEN
  :INSERT
  :DELETE
  :LWIN
  :RWIN
  :APPS
  :key-0
  :key-1
  :key-2
  :key-3
  :key-4
  :key-5
  :key-6
  :key-7
  :key-8
  :key-9
  :KP0
  :KP1
  :KP2
  :KP3
  :KP4
  :KP5
  :KP6
  :KP7
  :KP8
  :KP9
  :KPADD
  :KPSUB
  :KPDIV
  :KPMUL
  :KPDEC
  :KPENTER
  :F1
  :F2
  :F3
  :F4
  :F5
  :F6
  :F7
  :F8
  :F9
  :F10
  :F11
  :F12
  :NUMLOCK
  :SCROLLLOCK
  :SPACE
  :CHAR)


;; TCOD_key_t
;; This is no longer used -- key structs are converted to a bitfield by
;; wrapper functions in libtcod.
(defcstruct key-press
  (vk keycode)                        ; character if vk == TCODK_CHAR else 0
  (c :unsigned-char)
  (flags :uint8))                  ; does this correspond to a key press or key
                                   ; release event ?


(defstruct key
  "The structure used by CL-TCOD to represent key-press events. Corresponds
to the structure used by libtcod."
  (vk :none :type keyword)
  (c #\null :type character)
  (pressed nil :type boolean)
  (lalt nil :type boolean)
  (lctrl nil :type boolean)
  (ralt nil :type boolean)
  (rctrl nil :type boolean)
  (shift nil :type boolean))




(define-c-enum drawing-character
  (:CHAR-HLINE 196)
  (:CHAR-VLINE 179)
  (:CHAR-NE 191)
  (:CHAR-NW 218)
  (:CHAR-SE 217)
  (:CHAR-SW 192)
  (:CHAR-TEEW 180)
  (:CHAR-TEEE 195)
  (:CHAR-TEEN 193)
  (:CHAR-TEES 194)
  (:CHAR-CROSS 197)
  ;; Double walls
  (:CHAR-DHLINE 205)
  (:CHAR-DVLINE 186)
  (:CHAR-DNE 187)
  (:CHAR-DNW 201)
  (:CHAR-DSE 188)
  (:CHAR-DSW 200)
  (:CHAR-DTEEW 181)
  (:CHAR-DTEEE 198)
  (:CHAR-DTEEN 208)
  (:CHAR-DTEES 210)
  (:CHAR-DCROSS 206)
  ;; Blocks
  (:CHAR-BLOCK1 178)
  (:CHAR-BLOCK2 177)
  (:CHAR-BLOCK3 176)
  ;; Arrows
  (:CHAR-ARROW-N 24)
  (:CHAR-ARROW-S 25)
  (:CHAR-ARROW-E 26)
  (:CHAR-ARROW_W 27)
  ;; Arrows without tail
  (:CHAR-ARROW2-N 30)
  (:CHAR-ARROW2-S 31)
  (:CHAR-ARROW2-E 16)
  (:CHAR-ARROW2-W 17)
  ;; Double arrows
  (:CHAR-DARROW2-H 29)
  (:CHAR-DARROW2-V 18)
  ;; GUI stuff
  (:CHAR-CHECKBOX-UNSET 224)
  (:CHAR-CHECKBOX-SET 225)
  (:CHAR-RADIO-UNSET 9)
  (:CHAR-RADIO-SET 10)
  ;; Subpixel resolution kit
  (:CHAR-SUBP-NW 226)
  (:CHAR-SUBP-NE 227)
  (:CHAR-SUBP-N 228)
  (:CHAR-SUBP-SE 229)
  (:CHAR-SUBP-DIAG 230)
  (:CHAR-SUBP-E 231)
  (:CHAR-SUBP-SW 232))


;; TCOD_colctrl_t (enum)
(define-c-enum colctrl
  (:COLCTRL-1 1)
  :COLCTRL-2
  :COLCTRL-3
  :COLCTRL-4
  :COLCTRL-5
  (:COLCTRL-NUMBER 5)
  :COLCTRL-FORE-RGB
  :COLCTRL-BACK-RGB
  :COLCTRL-STOP )

;; TCOD_bkgnd_flag_t (enum)
(define-c-enum background-flag
  :NONE
  :SET
  :MULTIPLY
  :LIGHTEN
  :DARKEN
  :SCREEN
  :COLOR-DODGE
  :COLOR-BURN
  :ADD
  :ADDA
  :BURN
  :OVERLAY
  :ALPH)


(define-c-enum alignment
  :LEFT
  :CENTER
  :RIGHT)


(define-c-bitfield key-state
  (:KEY-PRESSED 1)
  (:KEY-RELEASED 2))


(define-c-bitfield custom-font-flags
  (:FONT-LAYOUT-ASCII-IN-COL 1)
  (:FONT-LAYOUT-ASCII-IN-ROW 2)
  (:FONT-TYPE-GREYSCALE 4)
  (:FONT-LAYOUT-TCOD 8))


(define-c-enum noise-type
  (:NOISE-DEFAULT 0)
  (:NOISE-PERLIN 1)
  (:NOISE-SIMPLEX 2)
  (:NOISE-WAVELET 4))


(define-c-enum rng-algorithm            ; TCOD_random_algo_t
  :RNG-MT
  :RNG-CMWC)

(define-c-enum rng-distribution         ; TCOD_distribution_t
  :DISTRIBUTION-LINEAR
  :DISTRIBUTION-GAUSSIAN
  :DISTRIBUTION-GAUSSIAN-RANGE
  :DISTRIBUTION-GAUSSIAN-INVERSE
  :DISTRIBUTION-GAUSSIAN-RANGE-INVERSE)


(define-c-enum fov-algorithm
    :FOV-BASIC
    :FOV-DIAMOND
    :FOV-SHADOW
    :FOV-PERMISSIVE-0
    :FOV-PERMISSIVE-1
    :FOV-PERMISSIVE-2
    :FOV-PERMISSIVE-3
    :FOV-PERMISSIVE-4
    :FOV-PERMISSIVE-5
    :FOV-PERMISSIVE-6
    :FOV-PERMISSIVE-7
    :FOV-PERMISSIVE-8
    :FOV-RESTRICTIVE)


;; TCOD_console_t
(define-c-type console :pointer)


;; TCOD_bsp_t = a struct
;; but all variables pass a pointer to this struct
(define-c-type bsp-ptr :pointer)


;; TCOD_random_t = pointer to void
(define-c-type randomptr :pointer)


;; TCOD_parser_t = pointer to void
(define-c-type parser :pointer)


;; TCOD_zip_t = pointer to void
(define-c-type zipptr :pointer)


;; TCOD_mouse_t
(defcstruct mouse-state
  (x :int)
  (y :int)
  (dx :int)
  (dy :int)
  (cx :int)
  (cy :int)
  (dcx :int)
  (dcy :int)
  (lbutton :boolean)
  (rbutton :boolean)
  (mbutton :boolean)
  (lbutton-pressed :boolean)
  (rbutton-pressed :boolean)
  (mbutton-pressed :boolean)
  (wheel-up :boolean)
  (wheel-down :boolean))

(defstruct mouse
  "Structure used by CL-TCOD to represent mouse status."
  (x 0 :type uint16) ;; absolute position
  (y 0 :type uint16)
  (dx 0 :type sint16) ;; movement since last update in pixels
  (dy 0 :type sint16)
  (cx 0 :type uint16) ;; cell coordinates in the root console
  (cy 0 :type uint16)
  (dcx 0 :type sint16)	;; movement since last update in console cells
  (dcy 0 :type sint16)
  (lbutton nil :type boolean)                ;; left button status
  (rbutton nil :type boolean)                ;; right button status
  (mbutton nil :type boolean)                ;; middle button status
  (lbutton-pressed nil :type boolean)        ;; left button pressed event
  (rbutton-pressed nil :type boolean)        ;; right button pressed event
  (mbutton-pressed nil :type boolean)        ;; middle button pressed event
  (wheel-up nil :type boolean)
  (wheel-down nil :type boolean))

;; TCOD_image_t = pointer to void
(define-c-type image :pointer)

;; TCOD_noise_t = pointer to void
(define-c-type noise :pointer)

;; TCOD_heightmap_t = a struct
;; but all functions pass/take pointers to heightmaps
(define-c-type heightmap-ptr :pointer)

;; TCOD_map_t = pointer to void
(define-c-type mapptr :pointer)

;; TCOD_path_t = pointer to void
(define-c-type a*-path :pointer)

;; TCOD_dijkstra_t = pointer to void
(define-c-type dijkstra-path :pointer)


;;;; <<Utilities>> ============================================================


(defun* (get-bit -> boolean) ((n integer) (pos uint8))
  "Return the bit at position POS within the integer N (represented as
a bitfield). POS = 1 refers to the 1's (rightmost) bit."
  (/= 0 (logand n (expt 2 (1- pos)))))



(defvar *root* (null-pointer) "The root console.")
(defparameter +NULL+ (null-pointer) "The null pointer.")
(defconstant +NOISE-DEFAULT-HURST+ 0.5
  "Default Hurst exponent for noise functions.")
(defconstant +NOISE-DEFAULT-LACUNARITY+ 2.0
  "Default lacunarity for noise functions.")


(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun simple-type? (sym)
    "* Arguments
- SYM :: A symbol.
* Return Value
Boolean.
* Description
Returns =T= if =SYM= names a non-class type, such as can be
defined by [[deftype]]."
    (handler-case (typep t sym)
      (error () (return-from simple-type? nil)))
    t))



(defun* (make-simple-key -> key) ((ch character))
  (make-key :vk :char :c ch))


(defun* (same-keys? -> boolean) ((key1 key) (key2 key))
  (and (key-p key1) (key-p key2)
       (eql (key-vk key1) (key-vk key2))
       (eql (key-c key1) (key-c key2))
       (eql (key-shift key1) (key-shift key2))
       (eql (or (key-lalt key1) (key-ralt key1))
            (or (key-lalt key2) (key-ralt key2)))
       (eql (or (key-lctrl key1) (key-rctrl key1))
            (or (key-lctrl key2) (key-rctrl key2)))))



;;;; <<Colours>> ==============================================================


(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun* (compose-colour -> uint32) ((r uint8) (g uint8) (b uint8))
    "Given three integer values R, G and B, representing the red, green and
blue components of a colour, return a 3 byte integer whose value is #xBBGGRR."
    (+ (ash b 16) (ash g 8) r))
  (declaim (inline compose-color))
  (defun compose-color (r g b) (compose-colour r g b)))


(defun* (colour-rgb -> uint32) ((r uint8) (g uint8) (b uint8))
  (compose-colour r g b))
(defun color-rgb (r g b) (colour-rgb r g b))


(defun* (colour-hsv -> uint32) ((hue single-float) (sat single-float)
                                (val single-float))
  "Return a new colour with the given HSV (hue, saturation and value)
components."
  (cond
   ((zerop sat)
    ;; Achromatic grey
    (compose-colour (truncate (+ 0.5 (* 255.0 val)))
                    (truncate (+ 0.5 (* 255.0 val)))
                    (truncate (+ 0.5 (* 255.0 val)))))
   (t
    (loop while (minusp hue) do (incf hue 360.0))
    (loop while (>= hue 360.0) do (decf hue 360.0))
    (setf hue (/ hue 60.0))
    (let* ((i (truncate hue))
           (f (- hue i))
           (p (* val (- 1 sat)))
           (q (* val (- 1 (* sat f))))
           (z (* val (- 1 (* sat (- 1 f)))))) ; variable t renamed to z
      (compose-colour
       (truncate
        (+ 0.5 (* 255.0 (case i
                          (1 q)
                          ((2 3) p)
                          (4 z)
                          (t val)))))
       (truncate
        (+ 0.5 (* 255.0 (case i
                          ((1 2) val)
                          (3 q)
                          (t p)))))
       (truncate
        (+ 0.5 (* 255.0 (case i
                          ((0 1) p) (2 z) ((3 4) val) (t q))))))))))
(defun color-hsv (hue sat val) (colour-hsv hue sat val))


(defun* (decompose-colour -> (values uint8 uint8 uint8)) ((num colournum))
  "Given a colournum #xBBGGRR, return R, G and B integer values as 3 separate
return values."
  (values
         (logand num #x0000ff)
         (ash (logand num #x00ff00) -8)
         (ash (logand num #xff0000) -16)
         ))
(declaim (inline decompose-color))
(defun decompose-color (num) (decompose-colour num))


(defun* (invert-colour -> colournum) ((num colournum))
  (multiple-value-bind (r g b) (decompose-colour num)
    (compose-colour (- 255 r) (- 255 g) (- 255 b))))
(declaim (inline invert-color))
(defun invert-color (num) (invert-colour num))


(defvar *colour-table* nil)
(defvar *initial-colours*
  `((:true-black        #x00 #x00 #x00)
    (:true-pink                 #xFF #x00 #xFF)
    (:true-white        #xFF #xFF #xFF)
    (:true-red          #xFF #x00 #x00)
    (:true-green        #x00 #xFF #x00)
    (:true-blue                 #x00 #x00 #xFF)
    (:black             #x00 #x00 #x00)
    (:dark-grey         96 96 96)
    (:grey              196 196 196)
    (:white             255 255 255)
    (:blue              13 103 196)
    (:dark-blue         40 40 128)
    (:light-blue        120 120 255)
    (:dark-red                  128 0 0)
    (:light-red         255 100 50)
    (:dark-brown        32 16 0)
    (:light-yellow      255 255 150)
    (:yellow            255 255 0)
    (:dark-yellow       164 164 0)
    (:green             0 220 0)
    (:cyan              86 163 205)
    (:orange            255 150 0)
    (:red               255 0 0)
    (:silver            203 203 203)
    (:gold              255 255 102)
    (:purple            204 51 153)
    (:dark-purple       51 0 51)
    ;; Some colours not defined in TCOD.
    (:slate-grey        #x80 #x80 #x80)
    (:umber             #x80 #x40 0)
    (:pink              #xFF #x00 #xFF)
    (:chocolate         210 105 30)))


;;#define TCOD_BKGND_ALPHA(alpha)
;;    ((TCOD_bkgnd_flag_t)(TCOD_BKGND_ALPH|(((uint8)(alpha*255))<<8)))
(defun background-alpha (alpha)
  (foreign-enum-keyword 'background-flag
                        (logior (foreign-enum-value 'background-flag :alph)
                                (ash (mod (* alpha 255) 256) 8))))

;;
;;#define TCOD_BKGND_ADDALPHA(alpha)
;;    ((TCOD_bkgnd_flag_t)(TCOD_BKGND_ADDA|(((uint8)(alpha*255))<<8)))
(defun background-add-alpha (alpha)
  (foreign-enum-keyword 'background-flag
                        (logior (foreign-enum-value 'background-flag :adda)
                                (ash (mod (* alpha 255) 256) 8))))


(defun start-colours ()
  (setf *colour-table* (make-hash-table :test #'eql))
  (dolist (term *initial-colours*)
    (destructuring-bind (name r g b) term
      (make-colour name r g b)))
  (make-rgb.txt-colours))
(defun start-colors () (start-colours))


(defun make-colour (kwd r g b)
  (unless (hash-table-p *colour-table*)
    (start-colours))
  (setf (gethash kwd *colour-table*)
        (compose-colour r g b)))
(defun make-color (kwd r g b) (make-colour kwd r g b))


(defun colour->grayscale (col)
  (multiple-value-bind (r g b) (decompose-colour col)
    (let ((brightness (round (+ r g b) 3)))
      (compose-colour brightness brightness brightness))))
(defun color->grayscale (col) (colour->grayscale col))


(defun* (colour -> colournum) ((keywd (or colournum symbol)))
  "Given a colour keyword such as :GREY, return its corresponding RGB
value (#xRRGGBB)."
  (cond
    ((integerp keywd)
     keywd)
    (t
     (unless *colour-table*
       (start-colours))
     (gethash keywd *colour-table*))))
(declaim (inline color))
(defun color (keywd) (colour keywd))


(defun colctrl->char (ctrl)
  (code-char (foreign-enum-value 'colctrl ctrl)))



;; TCODLIB_API bool TCOD_color_equals (TCOD_color_t c1, TCOD_color_t c2);
(define-c-function ("TCOD_color_equals_wrapper" colour-equals?) :boolean
        ((c1 colournum) (c2 colournum)))
(declaim (inline color-equals?))
(defun color-equals? (c1 c2)
  (colour-equals? c1 c2))


;;TCODLIB_API TCOD_color_t TCOD_color_add (TCOD_color_t c1, TCOD_color_t c2);
(define-c-function ("TCOD_color_add_wrapper" colour-add) colournum
        ((c1 colournum) (c2 colournum)))
(declaim (inline color-add))
(defun color-add (c1 c2)
  (colour-add c1 c2))


(define-c-function ("TCOD_color_subtract_wrapper" colour-subtract) colournum
        ((c1 colournum) (c2 colournum)))
(declaim (inline color-subtract))
(defun color-subtract (c1 c2)
  (colour-subtract c1 c2))


;;TCODLIB_API TCOD_color_t TCOD_color_multiply (TCOD_color_t c1,
;; TCOD_color_t c2);
(define-c-function ("TCOD_color_multiply_wrapper" colour-multiply) colournum
        ((c1 colournum) (c2 colournum)))
(declaim (inline color-multiply))
(defun color-multiply (c1 c2)
  (colour-multiply c1 c2))


;;TCODLIB_API TCOD_color_t TCOD_color_multiply_scalar (TCOD_color_t c1,
;; float value);
(define-c-function ("TCOD_color_multiply_scalar_wrapper" colour-multiply-scalar)
    colournum
    ((c1 colournum) (value :float)))
(declaim (inline color-multiply-scalar))
(defun color-multiply-scalar (c1 value)
  (colour-multiply-scalar c1 value))


;; TCODLIB_API TCOD_color_t TCOD_color_lerp(TCOD_color_t c1, TCOD_color_t c2,
;; float coef);
(define-c-function ("TCOD_color_lerp_wrapper" colour-lerp) colournum
        ((c1 colournum) (c2 colournum) (coef :float)))
(declaim (inline color-lerp))
(defun color-lerp (c1 c2 coef)
  (colour-lerp c1 c2 coef))


;; TCODLIB_API void TCOD_color_set_HSV(TCOD_color_t *c,float h, float s,
;; float v);
(define-c-function ("TCOD_color_set_HSV" colour-set-hsv) :void
        ((con :pointer) (hue :float) (sat :float) (v :float)))
(declaim (inline color-set-hsv))
(defun color-set-hsv (con hue sat v)
  (colour-set-hsv con hue sat v))


(define-c-function ("TCOD_color_get_hue_" colour-get-hue) :float
    ((c colournum)))

(define-c-function ("TCOD_color_get_saturation_" colour-get-saturation) :float
    ((c colournum)))

(define-c-function ("TCOD_color_get_value_" colour-get-value) :float
    ((c colournum)))


(defun* (colour-get-hsv -> list) ((c colournum))
  (list (colour-get-hue c)
        (colour-get-saturation c)
        (colour-get-value c)))


(defun* (colour-set-hue -> colournum) ((colour colournum) (hue single-float))
  "Return COLOUR with its hue modified to HUE."
  (let ((sat (colour-get-saturation colour))
        (val (colour-get-value colour)))
    (colour-hsv hue sat val)))


(defun* (colour-set-saturation -> colournum) ((colour colournum) (sat single-float))
  "Return COLOUR with its saturation modified to SAT."
  (let ((hue (colour-get-hue colour))
        (val (colour-get-value colour)))
    (colour-hsv hue sat val)))


(defun* (colour-set-value -> colournum) ((colour colournum) (val single-float))
  "Return COLOUR with its HSV value modified to VAL."
  (let ((sat (colour-get-saturation colour))
        (hue (colour-get-hue colour)))
    (colour-hsv hue sat val)))


(defun* (colour-shift-hue -> colournum) ((colour colournum) (hshift single-float))
  (if (zerop hshift)
      colour
      (destructuring-bind (h s v) (colour-get-hsv colour)
        (colour-hsv (+ h hshift) s v))))


(defun* (colour-scale-hsv -> colournum) ((colour colournum) (scoef single-float)
                                         (vcoef single-float))
  (destructuring-bind (h s v) (colour-get-hsv colour)
    (colour-hsv h (clamp 0.0 1.0 (* s scoef)) (clamp 0.0 1.0 (* v vcoef)))))


(declaim (inline color-get-hsv color-get-hue color-get-saturation
                 color-get-value
                 color-set-hue color-set-saturation
                 color-set-value
                 color-shift-hue))

(defun color-get-hsv (colour)
  (colour-get-hsv colour))

(defun color-get-hue (colour)
  (colour-get-hue colour))

(defun color-get-saturation (colour)
  (colour-get-saturation colour))

(defun color-get-value (colour)
  (colour-get-value colour))

(defun color-set-hue (colour hue)
  (colour-set-hue colour hue))

(defun color-set-saturation (colour sat)
  (colour-set-saturation colour sat))

(defun color-set-value (colour val)
  (colour-set-value colour val))

(defun color-shift-hue (colour hshift)
  (colour-shift-hue colour hshift))



;;;; <<Console>> ==============================================================


(defvar *console-width-table* (make-hash-table))
(defvar *console-height-table* (make-hash-table))


;; (defun* (legal-console-coordinates? -> boolean) ((con console) (x fixnum) (y fixnum))
;;   (and (< x (console-get-width con))
;;        (< y (console-get-height con))))

(defmacro legal-console-coordinates? (con x y)
  "Are the relative coordinates X,Y within the bounds of console CON?"
  `(and (< ,x (console-get-width ,con))
        (< ,y (console-get-height ,con))))


(define-c-function ("TCOD_console_credits" console-credits) :void
    ())

(define-c-function ("TCOD_console_credits_reset" console-credits-reset) :void
    ())

(define-c-function ("TCOD_console_credits_render" console-credits-render) :boolean
  ((x :int) (y :int) (alpha :boolean)))


;;TCODLIB_API void TCOD_console_init_root(int w, int h, const char * title,
;;                                        bool fullscreen);
(define-c-function ("TCOD_console_init_root" console-init-root) :void
    ((width :int) (height :int) (title :string) (fullscreen? :boolean)
     (renderer renderer))
  (check-type width ucoord)
  (check-type height ucoord)
  (setf (gethash *root* *console-width-table*) width)
  (setf (gethash *root* *console-height-table*) height)
  (call-it))


;;TCODLIB_API void TCOD_console_set_custom_font(const char *fontFile,
;;                        int char_width, int char_height, int nb_char_horiz,
;;                        int nb_char_vertic, bool chars_by_row,
;;                        TCOD_color_t key_color);
(define-c-function ("TCOD_console_set_custom_font" console-set-custom-font) :void
    ((fontfile :string) (flags custom-font-flags)
     (chars-horizontal :int) (chars-vertical :int))
  (assert (probe-file fontfile))
  (check-type chars-horizontal (unsigned-byte 16))
  (check-type chars-vertical (unsigned-byte 16))
  (call-it))


;; TCODLIB_API void TCOD_console_map_ascii_code_to_font(int asciiCode,
;;      int fontCharX, int fontCharY);
(define-c-function ("TCOD_console_map_ascii_code_to_font"
                    console-map-ascii-code-to-font) :void
    ((asciicode :int) (fontchar-x :int) (fontchar-y :int)))

;; TCODLIB_API void TCOD_console_map_ascii_codes_to_font(int asciiCode,
;;      int nbCodes, int fontCharX, int fontCharY);
(define-c-function ("TCOD_console_map_ascii_codes_to_font"
                    console-map-ascii-codes-to-font) :void
    ((asciicode :int) (num-codes :int) (fontchar-x :int) (fontchar-y :int)))

;; TCODLIB_API void TCOD_console_map_string_to_font(const char *s,
;;      int fontCharX, int fontCharY);
(define-c-function ("TCOD_console_map_string_to_font"
                    console-map-string-to-font) :void
    ((str :string) (fontchar-x :int) (fontchar-y :int)))


;;TCODLIB_API void TCOD_console_set_window_title(const char *title);
(define-c-function ("TCOD_console_set_window_title" console-set-window-title) :void
  ((title :string)))

;;TCODLIB_API void TCOD_console_set_fullscreen(bool fullscreen);
(define-c-function ("TCOD_console_set_fullscreen" console-set-fullscreen) :void
  ((full? :boolean)))

;;TCODLIB_API bool TCOD_console_is_fullscreen();
(define-c-function ("TCOD_console_is_fullscreen" console-is-fullscreen?) :boolean
    ())

;;TCODLIB_API bool TCOD_console_is_window_closed();
(define-c-function ("TCOD_console_is_window_closed" console-is-window-closed?)
    :boolean ())


;;TCODLIB_API void TCOD_console_set_background_color(TCOD_console_t con,
;; TCOD_color_t col);
(define-c-function ("TCOD_console_set_default_background_wrapper"
          console-set-default-background) :void
        ((con console) (col colournum)))
(declaim (inline console-set-default-background))


;;TCODLIB_API void TCOD_console_set_foreground_color(TCOD_console_t con,
;;                                                   TCOD_color_t col);
(define-c-function ("TCOD_console_set_default_foreground_wrapper"
          console-set-default-foreground) :void
        ((con console) (col colournum)))
(declaim (inline console-set-default-foreground))


;;TCODLIB_API TCOD_color_t TCOD_console_get_background_color(TCOD_console_t
;;con);
(define-c-function ("TCOD_console_get_default_background_wrapper"
                    console-get-default-background) colournum
  ((con console)))
(declaim (inline console-get-default-background))


;;TCODLIB_API TCOD_color_t TCOD_console_get_foreground_color(TCOD_console_t con);
(define-c-function ("TCOD_console_get_default_foreground_wrapper"
                    console-get-default-foreground) colournum
  ((con console)))
(declaim (inline console-get-default-foreground))


;;TCODLIB_API void TCOD_console_clear(TCOD_console_t con);
(define-c-function ("TCOD_console_clear" console-clear) :void
        ((con console)))


;; New in 1.5.0rc1
;;TCODLIB_API void TCOD_console_set_dirty(int dx, int dy, int dw, int dh);
(define-c-function ("TCOD_console_set_dirty" console-set-dirty) :void
  ((rootx :int) (rooty :int) (width :int) (height :int))
  "Declares an area of the =*root*= console to be 'dirty'."
  (assert (legal-console-coordinates? *root* rootx rooty))
  (assert (legal-console-coordinates?
           *root* (+ rootx (1- width)) (+ rooty (1- height))))
  (call-it))


;;TCODLIB_API TCOD_color_t TCOD_console_get_back(TCOD_console_t con,int x, int y)
(define-c-function ("TCOD_console_get_char_background_wrapper"
                    console-get-char-background) colournum
  ((con console) (x :int) (y :int))
  (assert (legal-console-coordinates? con x y))
  (call-it))


;;TCODLIB_API TCOD_color_t TCOD_console_get_fore(TCOD_console_t con,
;;                                               int x, int y);
(define-c-function ("TCOD_console_get_char_foreground_wrapper"
                    console-get-char-foreground) colournum
  ((con console) (x :int) (y :int))
  (assert (legal-console-coordinates? con x y))
  (call-it))


;;TCODLIB_API void TCOD_console_set_back(TCOD_console_t con,int x, int y,
;;                                       TCOD_color_t col,
;;                                       TCOD_bkgnd_flag_t flag);
(define-c-function ("TCOD_console_set_char_background_wrapper"
                    console-set-char-background) :void
    ((con console) (x :int) (y :int) (col colournum) (flag background-flag))
  (assert (legal-console-coordinates? con x y))
  (call-it con x y col flag))


;;TCODLIB_API void TCOD_console_set_fore(TCOD_console_t con,int x, int y,
;;                                       TCOD_color_t col);
(define-c-function ("TCOD_console_set_char_foreground_wrapper"
                    console-set-char-foreground) :void
    ((con console) (x :int) (y :int) (col colournum))
  (assert (legal-console-coordinates? con x y))
  (call-it con x y col))


;;TCODLIB_API void TCOD_console_set_char(TCOD_console_t con,int x, int y,
;; int c);
(defcfun ("TCOD_console_set_char" %console-set-char) :void
    (con console) (x :int) (y :int) (ch :unsigned-char))


(defun* console-set-char ((con console) (x integer) (y integer) ch)
  (assert (legal-console-coordinates? con x y))
  (when (characterp ch)
    (setf ch (char-code ch)))
  (%console-set-char con x y ch))


(defun* (console-fill-char -> null)  ((con console) (ch (or character uchar))
                                      (fx ucoord) (fy ucoord)
                                      (fw ucoord) (fh ucoord))
  "Fill a rectangular area with the character CH."
  (if (characterp ch)
      (setf ch (char-code ch)))
  (loop for x from fx below (+ fx fw) do
       (loop for y from fy below (+ fy fh) do
            (when (legal-console-coordinates? con x y)
            (console-set-char con x y ch)))))


;;TCODLIB_API void TCOD_console_put_char(TCOD_console_t con,int x, int y,
;;                                       int c, TCOD_bkgnd_flag_t flag);
(define-c-function ("TCOD_console_put_char" console-put-char) :void
    ((con console) (x :int) (y :int) (ch :unsigned-char)
     (flag background-flag))
  (assert (legal-console-coordinates? con x y))
  (call-it))


(define-c-function ("TCOD_console_put_char_ex_wrapper" console-put-char-ex) :void
    ((con console) (x :int) (y :int) (ch :unsigned-char) (fg colournum)
     (bg colournum))
  (assert (legal-console-coordinates? con x y))
  (call-it))


;;TCODLIB_API void TCOD_console_print_left(TCOD_console_t con,int x, int y,
;;                                         TCOD_bkgnd_flag_t flag,
;;                                         const char *fmt, ...);

;; This has to have a separate lisp wrapper, as we need to be able
;; to pass 'args...' to lisp.
;; (defcfun ("TCOD_console_print_left" %console-print-left) :void
;;   (con console) (x :int) (y :int) (flag background-flag) (fmt :string)
;;   &rest)
;;
;; (defun* console-print-left ((con console) (x ucoord) (y ucoord)
;;                             (flag background-flag) (fmt string)
;;                             &rest args)
;;   (assert (legal-console-coordinates? con x y))
;;   (%console-print-left con x y flag (apply #'format nil fmt args)))


(define-c-function ("TCOD_console_set_background_flag"
                    console-set-background-flag) :void
    ((con console) (flag background-flag))
  (call-it))


(define-c-function ("TCOD_console_get_background_flag"
                    console-get-background-flag) background-flag
    ((con console))
  (call-it))


(define-c-function ("TCOD_console_set_alignment"
                    console-set-alignment) :void
    ((con console) (align alignment))
  (call-it))


(define-c-function ("TCOD_console_get_alignment"
                    console-get-alignment) alignment
    ((con console))
  (call-it))


(defcfun ("TCOD_console_print" %console-print) :void
  (con console) (x :int) (y :int) (fmt :string) &rest)


(defun* console-print ((con console) (x ucoord) (y ucoord)
                            (fmt string) &rest args)
  (assert (legal-console-coordinates? con x y))
  (%console-print con x y (apply #'format nil fmt args)))


(defcfun ("TCOD_console_print_ex" %console-print-ex) :void
  (con console) (x :int) (y :int) (flag background-flag)
  (align alignment) (fmt :string) &rest)


(defun* console-print-ex ((con console) (x ucoord) (y ucoord)
                          (flag background-flag) (align alignment)
                            (fmt string) &rest args)
  (assert (legal-console-coordinates? con x y))
  (%console-print-ex con x y flag align
                     (apply #'format nil fmt args)))


;; In wrapper.c
(define-c-function ("TCOD_console_print_return_string"
                    console-print-return-string) :string
    ((con console) (x :int) (y :int) (rw :int) (rh :int)
     (flag background-flag) (align alignment) (str :string)
     (can-split? :boolean) (count-only? :boolean))
  (assert (legal-console-coordinates? con x y))
  (call-it))


;;TCODLIB_API void TCOD_console_print_right(TCOD_console_t con,int x, int y,
;; TCOD_bkgnd_flag_t flag, const char *fmt, ...);
;; (defcfun ("TCOD_console_print_right" %console-print-right) :void
;;      (con console) (x :int) (y :int) (flag background-flag) (fmt :string)
;;      &rest)
;;
;; (defun* console-print-right ((con console) (x ucoord) (y ucoord)
;;                              (flag background-flag) (fmt string) &rest args)
;;   (assert (legal-console-coordinates? con x y))
;;   (%console-print-right con x y flag (apply #'format nil fmt args)))

;;TCODLIB_API void TCOD_console_print_center(TCOD_console_t con,int x, int y,
;; TCOD_bkgnd_flag_t flag, const char *fmt, ...);
;; (defcfun ("TCOD_console_print_center" %console-print-centre) :void
;;      (con console) (x :int) (y :int) (flag background-flag) (fmt :string)
;;      &rest)
;;
;; (defun* console-print-centre ((con console) (x ucoord) (y ucoord)
;;                               (flag background-flag) (fmt string)
;;                               &rest args)
;;   (assert (legal-console-coordinates? con x y))
;;   (%console-print-centre con x y flag (apply #'format nil fmt args)))
;;
;; (declaim (inline console-print-center))
;; (defun console-print-center (con x y flag fmt &rest args)
;;   (apply #'console-print-centre con x y flag fmt args))


;;TCODLIB_API int TCOD_console_print_left_rect(TCOD_console_t con,int x, int y,
;; int w, int h, TCOD_bkgnd_flag_t flag, const char *fmt, ...);
;; (defcfun ("TCOD_console_print_left_rect" %console-print-left-rect) :int
;;      (con console) (x :int) (y :int) (w :int) (h :int)
;;      (flag background-flag) (fmt :string)
;;      &rest)
;;
;; (defun* console-print-left-rect ((con console) (x ucoord) (y ucoord)
;;                                  (w ucoord) (h ucoord)
;;                                  (flag background-flag) (fmt string) &rest args)
;;   (assert (legal-console-coordinates? con x y))
;;   (%console-print-left-rect con x y w h flag
;;                             (apply #'format nil fmt args)))

;;TCODLIB_API int TCOD_console_print_right_rect(TCOD_console_t con,int x,
;; int y, int w, int h, TCOD_bkgnd_flag_t flag, const char *fmt, ...);
;; (defcfun ("TCOD_console_print_right_rect" %console-print-right-rect) :int
;;      (con console) (x :int) (y :int) (w :int) (h :int)
;;      (flag background-flag) (fmt :string)
;;      &rest)
;;
;; (defun* console-print-right-rect ((con console) (x ucoord) (y ucoord)
;;                                   (w ucoord) (h ucoord)
;;                                   (flag background-flag) (fmt string) &rest args)
;;   (assert (legal-console-coordinates? con x y))
;;   (%console-print-right-rect con x y w h flag
;;                              (apply #'format nil fmt args)))


(defcfun ("TCOD_console_print_rect" %console-print-rect) :int
        (con console) (x :int) (y :int) (w :int) (h :int)
        (fmt :string) &rest)

(defun* console-print-rect ((con console) (x ucoord) (y ucoord)
                                  (w ucoord) (h ucoord)
                                  (fmt string) &rest args)
  (assert (legal-console-coordinates? con x y))
  (%console-print-rect con x y w h (apply #'format nil fmt args)))


(defcfun ("TCOD_console_print_rect_ex" %console-print-rect-ex) :int
        (con console) (x :int) (y :int) (w :int) (h :int)
        (flag background-flag) (align alignment) (fmt :string) &rest)

(defun* console-print-rect-ex ((con console) (x ucoord) (y ucoord)
                               (w ucoord) (h ucoord)
                               (flag background-flag) (align alignment)
                               (fmt string) &rest args)
  (assert (legal-console-coordinates? con x y))
  (%console-print-rect-ex con x y w h flag align
                          (apply #'format nil fmt args)))



;;TCODLIB_API void TCOD_console_rect(TCOD_console_t con,int x, int y, int w,
;; int h, bool clear, TCOD_bkgnd_flag_t flag);
(define-c-function ("TCOD_console_rect" console-rect) :void
  ((con console) (x :int) (y :int) (width :int) (height :int) (clear? :boolean)
   (flag background-flag))
  (assert (legal-console-coordinates? con x y))
  (check-type width ucoord)
  (check-type height ucoord)
  (call-it))



(defcfun ("TCOD_console_get_height_rect" %console-get-height-rect) :int
        (con console) (x :int) (y :int) (w :int) (h :int) (fmt :string)
        &rest)

(defun* console-get-height-rect ((con console) (x ucoord) (y ucoord)
                                 (w ucoord) (h ucoord) (fmt string) &rest args)
  (assert (legal-console-coordinates? con x y))
  (%console-get-height-rect con x y w h (apply #'format nil fmt args)))


(define-c-function ("TCOD_console_hline" console-hline) :void
    ((con console) (x :int) (y :int) (len :int) (flag background-flag))
  (assert (legal-console-coordinates? con x y))
  (call-it))


(define-c-function ("TCOD_console_vline" console-vline) :void
    ((con console) (x :int) (y :int) (len :int) (flag background-flag))
  (assert (legal-console-coordinates? con x y))
  (call-it))

(defcfun ("TCOD_console_print_frame" %console-print-frame) :void
  (con console) (x :int) (y :int) (width :int) (height :int)
  (empty? :boolean) (flag background-flag)
  (fmt :string) &rest)

(defun* console-print-frame ((con console) (x ucoord) (y ucoord)
                             (width ucoord) (height ucoord)
                             (empty? boolean) (flag background-flag)
                             (fmt string) &rest args)
  (assert (legal-console-coordinates? con x y))
  (check-type width ucoord)
  (check-type height ucoord)
  (%console-print-frame con x y width height empty? flag
                        (apply #'format nil fmt args)))


;; Added in wrappers.c
(defcfun ("TCOD_console_print_double_frame" %console-print-double-frame) :void
  (con console) (x :int) (y :int) (width :int) (height :int)
  (empty? :boolean) (flag background-flag)
  (fmt :string) &rest)

(defun* console-print-double-frame ((con console) (x ucoord) (y ucoord)
                                    (width ucoord) (height ucoord)
                                    (empty? boolean) (flag background-flag)
                                    (fmt string) &rest args)
  (assert (legal-console-coordinates? con x y))
  (check-type width ucoord)
  (check-type height ucoord)
  (%console-print-double-frame con x y width height empty? flag
                               (apply #'format nil fmt args)))



;;TCODLIB_API int TCOD_console_get_char(TCOD_console_t con,int x, int y);
(define-c-function ("TCOD_console_get_char" console-get-char) :int
  ((con console) (x :int) (y :int))
  (assert (legal-console-coordinates? con x y))
  (call-it))


;;TCODLIB_API void TCOD_console_set_fade(uint8 val, TCOD_color_t fade);
(define-c-function ("TCOD_console_set_fade_wrapper" console-set-fade) :void
  ((val :uint8) (fade colournum)))

;;TCODLIB_API uint8 TCOD_console_get_fade();
(define-c-function ("TCOD_console_get_fade" console-get-fade) :uint8
    ())


;;TCODLIB_API TCOD_color_t TCOD_console_get_fading_color();
(define-c-function ("TCOD_console_get_fading_color_wrapper"
          console-get-fading-color) colournum
    ())
(declaim (inline console-get-fading-colour))
(defun console-get-fading-colour ()
  (console-get-fading-color))


;;TCODLIB_API void TCOD_console_flush();
(define-c-function ("TCOD_console_flush" console-flush) :void
    ())


;;TCODLIB_API void TCOD_console_set_color_control(TCOD_colctrl_t con,
;;     TCOD_color_t fore, TCOD_color_t back);
;; This is to do with "colour control" strings
(define-c-function ("TCOD_console_set_color_control_wrapper"
          console-set-colour-control) :void
  ((control-num colctrl) (fore colournum) (back colournum)))

(declaim (inline console-set-color-control))
(defun console-set-color-control (control-num fore back)
  (console-set-colour-control control-num fore back))



;;TCODLIB_API TCOD_console_t TCOD_console_new(int w, int h);
(define-c-function ("TCOD_console_new" console-new) console
    ((width :int) (height :int))
  (check-type width ucoord)
  (check-type height ucoord)
  (let ((newcon (call-it width height)))
    (setf (gethash newcon *console-width-table*) width)
    (setf (gethash newcon *console-height-table*) height)
    newcon))


;;TCODLIB_API int TCOD_console_get_width(TCOD_console_t con);
(define-c-function ("TCOD_console_get_width" console-get-width) :int
  ((con console))
  (or (gethash con *console-width-table*)
      (call-it)))


;; (defun* (console-get-width -> ucoord) ((con console))
;;   (or (gethash con *console-width-table*)
;;       (%console-get-width con)))


;;TCODLIB_API int TCOD_console_get_height(TCOD_console_t con);
(define-c-function ("TCOD_console_get_height" console-get-height) :int
  ((con console))
  (or (gethash con *console-height-table*)
      (call-it)))


;; (defun* (console-get-height -> ucoord) ((con console))
;;   (or (gethash con *console-height-table*)
;;       (%console-get-height con)))


;;TCODLIB_API void TCOD_console_blit(TCOD_console_t src,int xSrc, int ySrc,
;; int wSrc, int hSrc, TCOD_console_t dst, int xDst, int yDst, int fade);
(define-c-function ("TCOD_console_blit" console-blit) :void
    ((src console)
     (xsrc :int) (ysrc :int)
     (wsrc :int) (hsrc :int)
     (dest console)
     (xdest :int) (ydest :int)
     (foreground-alpha :float) (background-alpha :float))
  (check-type xsrc ucoord)
  (check-type ysrc ucoord)
  (check-type wsrc ucoord)
  (check-type hsrc ucoord)
  (check-type xdest ucoord)
  (check-type ydest ucoord)
  (check-type foreground-alpha (real 0 1.0))
  (check-type background-alpha (real 0 1.0))
  ;; Blitting a console to a position that lies completely outside the
  ;; destination console's bounds will do nothing, rather than causing
  ;; an error.
  (when (legal-console-coordinates? dest xdest ydest)
    ;; TCOD_console_blit unceremoniously crashes libtcod if this assertion is
    ;; not true when it is called. We therefore check the assertion here first,
    ;; so we have access to lisp debugging facilities if the conditions are not
    ;; met.
    (assert (and (plusp wsrc) (plusp hsrc)
                 (>= (+ xdest wsrc) 0) (>= (+ ydest hsrc) 0)))
    (call-it src xsrc ysrc wsrc hsrc dest xdest ydest
             foreground-alpha background-alpha)))



;;TCODLIB_API void TCOD_console_delete(TCOD_console_t console);
(define-c-function ("TCOD_console_delete" console-delete) :void
    ((con console)))


#+nil
(define-c-function ("TCOD_console_set_key_color_wrapper" console-set-key-colour) :void
    ((con console) (colour colournum)))


;;;; <<Unicode>> ==============================================================



(define-c-function ("TCOD_console_map_string_to_font_utf"
                    %console-map-string-to-font-utf) :void
    ((str :string) (fontchar-x :int) (fontchar-y :int)))


(defun* console-map-string-to-font-utf ((str string) (fontchar-x uint)
                                        (fontchar-y uint))
  (with-foreign-pointer-as-string (strbuf (length str))
    (%console-map-string-to-font-utf
     (lisp-string-to-foreign str strbuf (length str)
                             :encoding :utf-16)
     fontchar-x fontchar-y)))


(defcfun ("TCOD_console_print_utf" %console-print-utf) :void
  (con console) (x :int) (y :int) (fmt :string) &rest)

(defun* console-print-utf ((con console) (x ucoord) (y ucoord)
                           (fmt string) &rest args)
  (let ((str (apply #'format nil fmt args)))
    (with-foreign-pointer-as-string (strbuf (length str))
      (%console-print-utf con x y
                          (lisp-string-to-foreign str strbuf (length str)
                                                  :encoding :utf-16)))))


(defcfun ("TCOD_console_print_ex_utf" %console-print-ex-utf) :void
  (con console) (x :int) (y :int) (flag background-flag) (align alignment)
  (fmt :string) &rest)


(defun* console-print-ex-utf ((con console) (x ucoord) (y ucoord)
                              (flag background-flag) (align alignment)
                              (fmt string) &rest args)
  (let ((str (apply #'format nil fmt args)))
    (with-foreign-pointer-as-string (strbuf (length str))
      (%console-print-ex-utf con x y flag align
                             (lisp-string-to-foreign str strbuf (length str)
                                                     :encoding :utf-16)))))


(defcfun ("TCOD_console_print_rect_utf" %console-print-rect-utf) :void
  (con console) (x :int) (y :int) (w :int) (h :int)
  (fmt :string) &rest)


(defun* console-print-rect-utf ((con console) (x ucoord) (y ucoord)
                                (w ucoord) (h ucoord)
                                (fmt string) &rest args)
  (let ((str (apply #'format nil fmt args)))
    (with-foreign-pointer-as-string (strbuf (length str))
      (%console-print-rect-utf con x y w h
                               (lisp-string-to-foreign str strbuf (length str)
                                                       :encoding :utf-16)))))


(defcfun ("TCOD_console_print_rect_ex_utf" %console-print-rect-ex-utf) :void
  (con console) (x :int) (y :int) (w :int) (h :int)
  (flag background-flag) (align alignment) (fmt :string) &rest)


(defun* console-print-rect-ex-utf ((con console) (x ucoord) (y ucoord)
                                   (w ucoord) (h ucoord)
                                   (flag background-flag) (align alignment)
                                   (fmt string) &rest args)
  (let ((str (apply #'format nil fmt args)))
    (with-foreign-pointer-as-string (strbuf (length str))
      (%console-print-rect-ex-utf con x y w h flag align
                                  (lisp-string-to-foreign str strbuf (length str)
                                                          :encoding :utf-16)))))


(defcfun ("TCOD_console_get_height_rect_utf" %console-get-height-rect-utf) :void
  (con console) (x :int) (y :int) (w :int) (h :int)
  (fmt :string) &rest)


(defun* console-get-height-rect-utf ((con console) (x ucoord) (y ucoord)
                                     (w ucoord) (h ucoord)
                                     (fmt string) &rest args)
  (let ((str (apply #'format nil fmt args)))
    (with-foreign-pointer-as-string (strbuf (length str))
      (%console-get-height-rect-utf con x y w h
                                    (lisp-string-to-foreign str strbuf (length str)
                                                            :encoding :utf-16)))))



;;;; <<Keyboard input>> ========================================================


;; (defun key->keypress (keyptr)
;;   (let ((flags (foreign-slot-value keyptr 'key-press 'flags)))
;;     (make-key :vk (foreign-slot-value keyptr 'key-press 'vk)
;;            :c (code-char (foreign-slot-value keyptr 'key-press 'c))
;;            :pressed (get-bit flags 1)
;;            :lalt (get-bit flags 2)
;;            :lctrl (get-bit flags 3)
;;            :ralt (get-bit flags 4)
;;            :rctrl (get-bit flags 5)
;;            :shift (get-bit flags 6))))

(defmacro and& (a b)
  "Shorthand for (BOOLE 'BOOLE-AND A B)."
  `(boole boole-and ,a ,b))


(defun key-bitfield->vk (key-bf)
  (foreign-enum-keyword 'keycode
                        (and& (ash key-bf -16) #x00FF)))


(defun* key->keypress ((key-bf (unsigned-byte 32)))
  (let ((flags (ash key-bf -24)))
    (make-key :vk (key-bitfield->vk key-bf) ;;(ldb (byte 8 16) key-bf)
              :c (code-char (and& key-bf #x0000FFFF))  ;;(ldb (byte 16 0) key-bf)
              :pressed (get-bit flags 1)
              :lalt (get-bit flags 2)
              :lctrl (get-bit flags 3)
              :ralt (get-bit flags 4)
              :rctrl (get-bit flags 5)
              :shift (get-bit flags 6))))


;;TCODLIB_API TCOD_key_t TCOD_console_check_for_keypress(int flags);
(defcfun ("TCOD_console_check_for_keypress_bitfield"
          %console-check-for-keypress) :int
    (flags key-state))


(defun* (console-check-for-keypress -> (or null key)) ((flags key-state))
  (let ((key-bf (%console-check-for-keypress flags)))
    (if (eql (key-bitfield->vk key-bf) :none)
        nil
        (key->keypress key-bf))))


;; (defun* console-check-for-keypress ((flags key-state))
;;   ;; (unless *key*
;;   ;;   (setf *key* (foreign-alloc 'key-press)))
;;   (with-foreign-object (key 'key-press)
;;     (%console-check-for-keypress key flags)
;;     (unless (eql :none (foreign-slot-value key 'key-press 'vk))
;;       (key->keypress key))))


;;TCODLIB_API TCOD_key_t TCOD_console_wait_for_keypress(bool flush);
(defcfun ("TCOD_console_wait_for_keypress_bitfield"
          %console-wait-for-keypress) :int
  (flush? :boolean))


(defun* console-wait-for-keypress ((flush? boolean))
  (key->keypress (%console-wait-for-keypress flush?)))



;; (defun* console-wait-for-keypress ((flush? boolean))
;;   ;; (unless *key*
;;   ;;   (setf *key* (foreign-alloc 'key-press)))
;;   (with-foreign-object (key 'key-press)
;;     (%console-wait-for-keypress key flush?)
;;     (key->keypress key)))


;;TCODLIB_API void TCOD_console_set_keyboard_repeat(int initial_delay,
;; int interval);
(define-c-function ("TCOD_console_set_keyboard_repeat"
                    console-set-keyboard-repeat) :void
    ((initial-delay :int) (interval :int)))


;;TCODLIB_API void TCOD_console_disable_keyboard_repeat();
(define-c-function ("TCOD_console_disable_keyboard_repeat"
                    console-disable-keyboard-repeat) :void
    ())


;;TCODLIB_API bool TCOD_console_is_key_pressed(TCOD_keycode_t key);
(define-c-function ("TCOD_console_is_key_pressed" is-key-pressed?) :boolean
  ((code keycode)))


;;;; <<Name generation>> ======================================================


(define-c-function ("TCOD_namegen_parse" namegen-parse) :void
    ((filename :string) (rng randomptr)))

(define-c-function ("TCOD_namegen_generate" namegen-generate) :string
    ((name :string) (allocate? :boolean)))

(define-c-function ("TCOD_namegen_generate_custom" namegen-generate-custom) :string
    ((name :string) (rule :string) (allocate? :boolean)))

(define-c-function ("TCOD_namegen_destroy" namegen-destroy) :void
    ())


;;;; <<Compression toolkit>> ==================================================


(define-c-function ("TCOD_zip_new" zip-new) zipptr
    ())

(define-c-function ("TCOD_zip_delete" zip-delete) :void
    ((zip zipptr)))

(define-c-function ("TCOD_zip_put_char" zip-put-char) :void
    ((zip zipptr) (ch :char)))

(define-c-function ("TCOD_zip_put_int" zip-put-int) :void
    ((zip zipptr) (val :int)))

(define-c-function ("TCOD_zip_put_float" zip-put-float) :void
    ((zip zipptr) (val :float)))

(define-c-function ("TCOD_zip_put_string" zip-put-string) :void
    ((zip zipptr) (val :string)))

(define-c-function ("TCOD_zip_put_data" zip-put-data) :void
    ((zip zipptr) (nbytes :int) (data :pointer)))

(define-c-function ("TCOD_zip_put_image" zip-put-image) :void
    ((zip zipptr) (image image)))

(define-c-function ("TCOD_zip_put_console" zip-put-console) :void
    ((zip zipptr) (con console)))

(defun zip-put (zip val)
  (typecase val
    (string (zip-put-string zip val))
    (character (zip-put-char zip (char-code val)))
    (integer (zip-put-int zip val))
    (float (zip-put-float zip val))
    (otherwise (error "ZIP-PUT: don't know how to translate value ~S" val))))

(defun* zip-put-colour (zip (colour colournum))
  (multiple-value-bind (r g b) (decompose-colour colour)
    (zip-put-char zip r)
    (zip-put-char zip g)
    (zip-put-char zip b)))

(declaim (inline zip-put-color))
(defun zip-put-color (zip color)
  (zip-put-colour zip color))

(define-c-function ("TCOD_zip_save_to_file" zip-save-to-file) :int
    ((zip zipptr) (filename :string)))

(define-c-function ("TCOD_zip_load_from_file" zip-load-from-file) :int
    ((zip zipptr) (filename :string)))

(define-c-function ("TCOD_zip_get_char" zip-get-char) :char
    ((zip zipptr)))

(define-c-function ("TCOD_zip_get_int" zip-get-int) :int
    ((zip zipptr)))

(define-c-function ("TCOD_zip_get_float" zip-get-float) :float
    ((zip zipptr)))

(define-c-function ("TCOD_zip_get_string" zip-get-string) :string
    ((zip zipptr)))

(define-c-function ("TCOD_zip_get_data" zip-get-data) :int
    ((zip zipptr) (nbytes :int) (data :pointer)))

(define-c-function ("TCOD_zip_get_image" zip-get-image) image
    ((zip zipptr)))

(define-c-function ("TCOD_zip_get_console" zip-get-console) console
    ((zip zipptr)))

(define-c-function ("TCOD_zip_get_current_bytes" zip-get-current-bytes) :uint32
    ((zip zipptr)))

(define-c-function ("TCOD_zip_get_remaining_bytes"
                    zip-get-remaining-bytes) :uint32
    ((zip zipptr)))

(define-c-function ("TCOD_zip_skip_bytes" zip-skip-bytes) :void
    ((zip zipptr) (nbytes :uint32)))


(defun* zip-get-colour ((zip zipptr))
  (let ((r (zip-get-char zip))
        (g (zip-get-char zip))
        (b (zip-get-char zip)))
    (compose-colour r g b)))

(declaim (inline zip-get-color))
(defun zip-get-color (zip)
  (zip-get-colour zip))


;;;; <<System>> ===============================================================


(define-c-function ("TCOD_sys_create_directory" sys-create-directory) :boolean
    ((path :string)))

(define-c-function ("TCOD_sys_delete_directory" sys-delete-directory) :boolean
    ((path :string)))

(define-c-function ("TCOD_sys_elapsed_milli" sys-elapsed-milli) :uint32
    ())

(define-c-function ("TCOD_sys_elapsed_seconds" sys-elapsed-seconds) :float
    ())

(define-c-function ("TCOD_sys_get_last_frame_length"
                    sys-get-last-frame-length) :float
    ())

(define-c-function ("TCOD_sys_sleep_milli" sys-sleep-milli) :void
    ((val :unsigned-int)))

(defcfun ("TCOD_sys_save_screenshot" %sys-save-screenshot) :void
  (filename :string))

(defun sys-save-screenshot (&optional (filename (null-pointer)))
  (%sys-save-screenshot filename))

(define-c-function ("TCOD_sys_set_fps" sys-set-fps) :void
    ((val :int)))

(define-c-function ("TCOD_sys_get_fps" sys-get-fps) :int
    ())

(define-c-function ("TCOD_sys_register_SDL_renderer"
                    sys-register-SDL-renderer) :void
    ((callback :pointer)))

;; Lisp wrapper needed because actual function returns nothing, whereas we
;; want to return resolution.
(defcfun ("TCOD_sys_get_current_resolution_x" sys-get-current-resolution-x) :int)
(defcfun ("TCOD_sys_get_current_resolution_y" sys-get-current-resolution-y) :int)

(defun sys-get-current-resolution ()
  (values (sys-get-current-resolution-x)
          (sys-get-current-resolution-y)))

(define-c-function ("TCOD_sys_force_fullscreen_resolution"
                    sys-force-fullscreen-resolution) :void
    ((width :int) (height :int)))

(define-c-function ("TCOD_sys_get_renderer" sys-get-renderer) renderer
    ()
  "Return the currently active renderer.")


(define-c-function ("TCOD_sys_set_renderer" sys-set-renderer) :void
    ((renderer renderer))
  "Change the currently active renderer.")


(defcfun ("TCOD_sys_get_char_size" %sys-get-char-size) :void
  (widthptr :pointer) (heightptr :pointer))

(defun* (sys-get-char-size -> (values fixnum fixnum)) ()
  "Return the dimensions of each character in the current font bitmap."
  (with-foreign-object (width :int)
    (with-foreign-object (height :int)
      (%sys-get-char-size width height)
      (values (mem-aref width :int) (mem-aref height :int)))))

(defcfun ("TCOD_sys_get_fullscreen_offsets"
          %sys-get-fullscreen-offsets) :void
  (offx-ptr :pointer) (offy-ptr :pointer))

(defun* (sys-get-fullscreen-offsets -> (values fixnum fixnum)) ()
  (with-foreign-object (offx :int)
    (with-foreign-object (offy :int)
      (%sys-get-fullscreen-offsets offx offy)
      (values (mem-aref offx :int) (mem-aref offy :int)))))

(define-c-function ("TCOD_sys_clipboard_set" sys-clipboard-set) :void
    ((text :string)))

(define-c-function ("TCOD_sys_clipboard_get" sys-clipboard-get) :string
    ())

(define-c-function ("TCOD_sys_update_char" sys-update-char) :void
    ((ascii :int) (fontx :int) (fonty :int) (image image) (x :int) (y :int)))


;;;; <<Random>> ===============================================================


;;; mersenne.h

(define-c-function ("TCOD_random_new" random-new) randomptr
  ((algorithm rng-algorithm)))

(define-c-function ("TCOD_random_new_from_seed" random-new-from-seed) randomptr
  ((algorithm rng-algorithm) (seed :uint32)))

(define-c-function ("TCOD_random_get_instance" random-get-instance) randomptr
    ())

(define-c-function ("TCOD_random_delete" random-delete) :void
  ((rng randomptr)))

(define-c-function ("TCOD_random_save" random-save) randomptr
  ((rng randomptr)))

(define-c-function ("TCOD_random_restore" random-restore) :void
  ((rng randomptr) (backup randomptr)))

(define-c-function ("TCOD_random_set_distribution" random-set-distribution) :void
  ((rng randomptr) (dist rng-distribution)))

(define-c-function ("TCOD_random_get_int" random-get-int) :int
  ((rng randomptr) (min :int) (max :int)))

(define-c-function ("TCOD_random_get_float" random-get-float) :float
  ((rng randomptr) (min :float) (max :float)))

(define-c-function ("TCOD_random_get_double" random-get-double) :double
  ((rng randomptr) (min :double) (max :double)))

(define-c-function ("TCOD_random_get_int_mean" random-get-int-mean) :int
  ((rng randomptr) (min :int) (max :int) (mean :int)))

(define-c-function ("TCOD_random_get_float_mean" random-get-float-mean) :float
  ((rng randomptr) (min :float) (max :float) (mean :float)))

(define-c-function ("TCOD_random_get_double_mean" random-get-double-mean) :double
  ((rng randomptr) (min :double) (max :double) (mean :double)))



;;;; <<Mouse>> ================================================================


;; This may not work, because each time any one of these functions is called,
;; a mouse state is fetched. Events such as release of a mouse button might
;; only appear in a single mouse state.
(defcfun ("TCOD_mouse_get_x" mouse-get-x) :int)
(defcfun ("TCOD_mouse_get_y" mouse-get-y) :int)
(defcfun ("TCOD_mouse_get_cx" mouse-get-cx) :int)
(defcfun ("TCOD_mouse_get_cy" mouse-get-cy) :int)
(defcfun ("TCOD_mouse_get_dx" mouse-get-dx) :int)
(defcfun ("TCOD_mouse_get_dy" mouse-get-dy) :int)
(defcfun ("TCOD_mouse_get_dcx" mouse-get-dcx) :int)
(defcfun ("TCOD_mouse_get_dcy" mouse-get-dcy) :int)
(defcfun ("TCOD_mouse_get_lbutton" mouse-get-lbutton) :unsigned-int)
(defcfun ("TCOD_mouse_get_mbutton" mouse-get-mbutton) :unsigned-int)
(defcfun ("TCOD_mouse_get_rbutton" mouse-get-rbutton) :unsigned-int)
(defcfun ("TCOD_mouse_get_lbutton_pressed" mouse-get-lbutton-pressed)
    :unsigned-int)
(defcfun ("TCOD_mouse_get_mbutton_pressed" mouse-get-mbutton-pressed)
    :unsigned-int)
(defcfun ("TCOD_mouse_get_rbutton_pressed" mouse-get-rbutton-pressed)
    :unsigned-int)


#+nil
(defun* (mouse-state->mouse -> mouse) (ms)
  ;;(let ((flags (foreign-slot-value ms 'mouse-state 'flags)))
  (break)
  (make-mouse :x (foreign-slot-value ms 'mouse-state 'x)
              :y (foreign-slot-value ms 'mouse-state 'y)
              :dx (foreign-slot-value ms 'mouse-state 'dx)
              :dy (foreign-slot-value ms 'mouse-state 'dy)
              :cx (foreign-slot-value ms 'mouse-state 'cx)
              :cy (foreign-slot-value ms 'mouse-state 'cy)
              :dcx (foreign-slot-value ms 'mouse-state 'dcx)
              :dcy (foreign-slot-value ms 'mouse-state 'dcy)
              ;; :lbutton (get-bit flags 1)
              ;; :rbutton (get-bit flags 2)
              ;; :mbutton (get-bit flags 3)
              ;; :lbutton-pressed (get-bit flags 4)
              ;; :rbutton-pressed (get-bit flags 5)
              ;; :mbutton-pressed (get-bit flags 6))))
              :lbutton (foreign-slot-value ms 'mouse-state 'lbutton)
              :rbutton (foreign-slot-value ms 'mouse-state 'rbutton)
              :mbutton (foreign-slot-value ms 'mouse-state 'mbutton)
              :lbutton-pressed (foreign-slot-value ms 'mouse-state 'lbutton-pressed)
              :rbutton-pressed (foreign-slot-value ms 'mouse-state 'rbutton-pressed)
              :mbutton-pressed (foreign-slot-value ms 'mouse-state 'mbutton-pressed)))



;;TCODLIB_API TCOD_mouse_t TCOD_mouse_get_status();
(defcfun ("TCOD_mouse_get_status_wrapper" %mouse-get-status) :void
  (mouseptr :pointer))

;; Old version - creates a foreign struct.
#+nil
(defun* (mouse-get-status -> mouse) ()
  (with-foreign-object (rodent 'mouse-state)
    (%mouse-get-status rodent)
    (mouse-state->mouse rodent)))


;; New version - gets all data from foreign functions.
(defun mouse-get-status ()
  (%mouse-get-status (null-pointer))
  (make-mouse :x (mouse-get-x)
              :y (mouse-get-y)
              :dx (mouse-get-dx)
              :dy (mouse-get-dy)
              :cx (mouse-get-cx)
              :cy (mouse-get-cy)
              :dcx (mouse-get-dcx)
              :dcy (mouse-get-dcy)
              :lbutton (plusp (mouse-get-lbutton))
              :rbutton (plusp (mouse-get-rbutton))
              :mbutton (plusp (mouse-get-mbutton))
              :lbutton-pressed (plusp (mouse-get-lbutton-pressed))
              :rbutton-pressed (plusp (mouse-get-rbutton-pressed))
              :mbutton-pressed (plusp (mouse-get-mbutton-pressed))))



(define-c-function ("TCOD_mouse_show_cursor" mouse-show-cursor) :void
  ((visible? :boolean)))

(define-c-function ("TCOD_mouse_is_cursor_visible"
                    mouse-is-cursor-visible?) :boolean
  ())

(define-c-function ("TCOD_mouse_move" mouse-move) :void
  ((pixel-x :int) (pixel-y :int)))



;;;; <<Image>> ================================================================


;;; image.h


;;TCODLIB_API TCOD_image_t TCOD_image_new(int width, int height);
(define-c-function ("TCOD_image_new" image-new) image
  ((width :int) (height :int))
  "Return a new image, filled with black.")

(define-c-function ("TCOD_image_refresh_console" image-refresh-console) :void
  ((image image) (con console)))

(define-c-function ("TCOD_image_delete" image-delete) :void
  ((image image)))

;;TCODLIB_API TCOD_image_t TCOD_image_from_console(TCOD_console_t console);
(define-c-function ("TCOD_image_from_console" image-from-console) image
  ((con console))
  "Return a new image whose contents are a 'screenshot' of the
console =CON=.")

;;TCODLIB_API TCOD_image_t TCOD_image_load(const char *filename);
(define-c-function ("TCOD_image_load" image-load) image
  ((filename :string))
  "Read an image from a file and return it.")

(define-c-function ("TCOD_image_invert" image-invert) :void
  ((image image)))

(define-c-function ("TCOD_image_hflip" image-hflip) :void
  ((image image)))

(define-c-function ("TCOD_image_vflip" image-vflip) :void
  ((image image)))

(define-c-function ("TCOD_image_rotate90" image-rotate90) :void
  ((image image) (num-rotations :int)))

(define-c-function ("TCOD_image_scale" image-scale) :void
  ((image image) (new-width :int) (new-height :int)))

(define-c-function ("TCOD_image_clear_wrapper" image-clear) :void
  ((image image) (colour colournum))
  "Fill the image =IMAGE= with the colour =COLOUR=.")

;;TCODLIB_API void TCOD_image_save(TCOD_image_t image, const char *filename);
(define-c-function ("TCOD_image_save" image-save) :void
  ((image image) (filename :string))
  "Write the image =IMAGE= to a file. The filename must end in =.BMP=
or =.PNG=.")


;;TCODLIB_API void TCOD_image_get_size(TCOD_image_t image, int *w,int *h);
;;TCODLIB_API TCOD_color_t TCOD_image_get_pixel(TCOD_image_t image,int x, int y);
(define-c-function ("TCOD_image_get_pixel_wrapper" image-get-pixel) colournum
  ((image image) (pixel-x :int) (pixel-y :int))
  "Return the colour of the pixel at =(PIXEL-X, PIXEL-Y)= in =IMAGE=.")


;;TCODLIB_API TCOD_color_t TCOD_image_get_mipmap_pixel(TCOD_image_t image,
;; float x0,float y0, float x1, float y1);
(define-c-function ("TCOD_image_get_mipmap_pixel_wrapper"
          image-get-mipmap-pixel) colournum
  ((image image) (x0 :float) (y0 :float) (x1 :float) (y1 :float))
  "Calculate the interpolated colour of the pixel at =(PIXEL-X, PIXEL-Y)=
in =IMAGE=.")


;;TCODLIB_API void TCOD_image_put_pixel(TCOD_image_t image,int x, int y,
;; TCOD_color_t col);
(define-c-function ("TCOD_image_put_pixel_wrapper" image-put-pixel) :void
  ((image image) (pixel-x :int) (pixel-y :int) (colour colournum))
  "Set the colour of the pixel at =(PIXEL-X, PIXEL-Y)= in =IMAGE= to =COLOUR=.")

;;TCODLIB_API void TCOD_image_blit(TCOD_image_t image, TCOD_console_t console,
;; float x, float y,
;;      TCOD_bkgnd_flag_t bkgnd_flag, float scalex, float scaley, float angle);
(define-c-function ("TCOD_image_blit" image-blit) :void
    ((image image) (con console) (x :int) (y :int) (flag background-flag)
     (scalex :float) (scaley :float) (angle :float))
  (assert (legal-console-coordinates? con x y))
  (call-it))


;;TCODLIB_API void TCOD_image_blit_rect(TCOD_image_t image,
;; TCOD_console_t console, int x, int y, int w, int h,
;;      TCOD_bkgnd_flag_t bkgnd_flag);
(define-c-function ("TCOD_image_blit_rect" image-blit-rect) :void
    ((image image) (con console) (x :int) (y :int) (width :int) (height :int)
     (flag background-flag))
  (assert (legal-console-coordinates? con x y))
  (call-it))


(define-c-function ("TCOD_image_blit_2x" image-blit-2x) :void
    ((image image) (dest console) (dx :int) (dy :int) (sx :int) (sy :int)
     (width :int) (height :int)))


;;TCODLIB_API void TCOD_image_delete(TCOD_image_t image);
;;TCODLIB_API void TCOD_image_set_key_color(TCOD_image_t image,
;; TCOD_color_t key_color);
(define-c-function ("TCOD_image_set_key_color" image-set-key-color) :void
  ((image image) (key-color colournum)))
(declaim (inline image-set-key-colour))
(defun image-set-key-colour (image key-colour)
  (image-set-key-color image key-colour))


(defcfun ("TCOD_image_get_size" %image-get-size) :void
  (image image) (widthptr :pointer) (heightptr :pointer))


(defun* (image-get-width -> uint32) ((image image))
  (with-foreign-object (width :int)
    (with-foreign-object (height :int)
      (%image-get-size image width height)
      (mem-aref width :int))))


(defun* (image-get-height -> uint32) ((image image))
  (with-foreign-object (width :int)
    (with-foreign-object (height :int)
      (%image-get-size image width height)
      (mem-aref height :int))))


(define-c-function ("TCOD_image_get_alpha" image-get-alpha) :int
  ((image image) (x :int) (y :int)))

(define-c-function ("TCOD_image_is_pixel_transparent"
                    image-is-pixel-transparent?) :boolean
  ((image image) (x :int) (y :int)))


;;;; <<Noise>> ================================================================


;; TCODLIB_API TCOD_noise_t TCOD_noise_new(int dimensions, float hurst,
;; float lacunarity, TCOD_random_t random);
;; For randomptr, use +NULL+ to use the default RNG
(defcfun ("TCOD_noise_new" %noise-new) noise
  (dimensions :int) (hurst :float) (lacunarity :float) (randomptr :pointer))

(defun* (noise-new -> noise) ((dimensions uint8)
                              &key ((hurst float) +NOISE-DEFAULT-HURST+)
                              ((lacunarity float) +NOISE-DEFAULT-LACUNARITY+)
                              ((rng randomptr) +NULL+))
  "Return a new noise object with the given characteristics."
  (%noise-new dimensions hurst lacunarity rng))


;; TCODLIB_API void TCOD_noise_delete(TCOD_noise_t noise);
(define-c-function ("TCOD_noise_delete" noise-delete) :void
  ((noise noise))
  "Destroy a noise object.")


(define-c-function ("TCOD_noise_set_type" noise-set-type) :void
  ((noise noise) (noise-type noise-type))
  "Set the type of noise produced by a noise object.")

;;; noise-get =================================================================

(defcfun ("TCOD_noise_get" %noise-get) :float
  (noise noise) (f :pointer))

(defun* (noise-get -> float) ((noise noise) &rest nums)
  "Returns the flat noise function at the given coordinates."
  (with-foreign-object (f :float (length nums))
    (dotimes (i (length nums))
      (setf (mem-aref f :float i) (coerce (nth i nums) 'single-float)))
    (%noise-get noise f)))

(defcfun ("TCOD_noise_get_ex" %noise-get-ex) :float
  (noise noise) (f :pointer) (noise-type noise-type))

(defun* (noise-get-ex -> float) ((noise noise) (noise-type noise-type)
                                 &rest nums)
  "Returns the flat noise function at the given coordinates,
using noise type NOISE-TYPE."
  (with-foreign-object (f :float (length nums))
    (dotimes (i (length nums))
      (setf (mem-aref f :float i) (coerce (nth i nums) 'single-float)))
    (%noise-get-ex noise f noise-type)))

;;; noise-get-FBM =============================================================

(defcfun ("TCOD_noise_get_fbm" %noise-get-fbm) :float
  (noise noise) (f :pointer) (octaves :float))

(defun* (noise-get-fbm -> float) ((noise noise) (octaves float) &rest nums)
  "Returns the fractional Brownian motion function at the given coordinates."
  (with-foreign-object (f :float (length nums))
    (dotimes (i (length nums))
      (setf (mem-aref f :float i) (coerce (nth i nums) 'single-float)))
    (%noise-get-fbm noise f octaves)))

(defcfun ("TCOD_noise_get_fbm_ex" %noise-get-fbm-ex) :float
  (noise noise) (f :pointer) (octaves :float) (noise-type noise-type))

(defun* (noise-get-fbm-ex -> float) ((noise noise) (noise-type noise-type)
                                     (octaves float) &rest nums)
  "Returns the fractional Brownian motion function at the given coordinates,
using noise type NOISE-TYPE."
  (with-foreign-object (f :float (length nums))
    (dotimes (i (length nums))
      (setf (mem-aref f :float i) (coerce (nth i nums) 'single-float)))
    (%noise-get-fbm-ex noise f octaves noise-type)))


;;; noise-get-turbulence ======================================================


(defcfun ("TCOD_noise_get_turbulence" %noise-get-turbulence) :float
  (noise noise) (f :pointer) (octaves :float))

(defun* (noise-get-turbulence -> float) ((noise noise) (octaves float)
                                         &rest nums)
  "Returns the turbulence function at the given coordinates."
  (with-foreign-object (f :float (length nums))
    (dotimes (i (length nums))
      (setf (mem-aref f :float i) (coerce (nth i nums) 'single-float)))
    (%noise-get-turbulence noise f octaves)))

(defcfun ("TCOD_noise_get_turbulence_ex" %noise-get-turbulence-ex) :float
  (noise noise) (f :pointer) (octaves :float) (noise-type noise-type))

(defun* (noise-get-turbulence-ex -> float) ((noise noise)
                                            (noise-type noise-type)
                                            (octaves float) &rest nums)
  "Returns the turbulence function at the given coordinates,
using noise type NOISE-TYPE."
  (with-foreign-object (f :float (length nums))
    (dotimes (i (length nums))
      (setf (mem-aref f :float i) (coerce (nth i nums) 'single-float)))
    (%noise-get-turbulence-ex noise f octaves noise-type)))



;;;; <<Heightmap>> ============================================================



(define-c-function ("TCOD_heightmap_new" heightmap-new) heightmap-ptr
  ((width :int) (height :int))
  "Return a new heightmap with the given dimensions.")


(define-c-function ("TCOD_heightmap_get_value" heightmap-get-value) :float
  ;; 0 <= x < WIDTH
  ((heightmap heightmap-ptr) (x :int) (y :int))
  "Return the height at position =(X, Y)= in the heightmap.")


(define-c-function ("TCOD_heightmap_get_interpolated_value"
          heightmap-get-interpolated-value) :float
  ;; 0 <= x < WIDTH
  ((heightmap heightmap-ptr) (x :float) (y :float))
  "Calculate the height at position =(X, Y)= in the heightmap, where the
coordinates might not be integers.")


(define-c-function ("TCOD_heightmap_get_slope" heightmap-get-slope) :float
  ((heightmap heightmap-ptr) (x :int) (y :int))
  "Return the slope at position =(X, Y)= in the heightmap. The value returned
will be between 0 and pi/2.")


(define-c-function ("TCOD_heightmap_set_value" heightmap-set-value) :void
  ;; 0 <= x < WIDTH
  ((heightmap heightmap-ptr) (x :int) (y :int) (value :float))
  "Set the height at position =(X, Y)= in the heightmap to =VALUE=.")


(define-c-function ("TCOD_heightmap_add" heightmap-add) :void
  ((heightmap heightmap-ptr) (value :float))
  "Add =VALUE= to all heights in the heightmap.")


(define-c-function ("TCOD_heightmap_add_fbm" heightmap-add-fbm) :void
    ((heightmap heightmap-ptr) (noise noise) (mulx :float) (muly :float)
     (addx :float) (addy :float) (octaves :float) (delta :float) (scale :float))
  "Add values from the random noise object =NOISE= to all heights in
equivalent positions in =HEIGHTMAP=.")


(define-c-function ("TCOD_heightmap_scale" heightmap-scale) :void
  ((heightmap heightmap-ptr) (factor :float))
  "Multiply all the heights in the heightmap by =SCALE=.")


(define-c-function ("TCOD_heightmap_lerp_hm" heightmap-lerp-hm) :void
  ((hm1 heightmap-ptr) (hm2 heightmap-ptr) (result heightmap-ptr) (coef :float))
  "Fill the heightmap =RESULT= with the results of a lerp operation between
the two heightmaps =HM1= and =HM2=.")


(define-c-function ("TCOD_heightmap_add_hm" heightmap-add-hm) :void
  ((hm1 heightmap-ptr) (hm2 heightmap-ptr) (result heightmap-ptr))
  "Add the heights in =HM1= to heights in equivalent positions in
=HM2=, and store the results in the heightmap =RESULT=.")


(define-c-function ("TCOD_heightmap_multiply_hm" heightmap-multiply-hm) :void
  ((hm1 heightmap-ptr) (hm2 heightmap-ptr) (result heightmap-ptr))
  "Multiply the heights in =HM1= by the heights in equivalent positions in
=HM2=, and store the results in the heightmap =RESULT=.")


(define-c-function ("TCOD_heightmap_clear" heightmap-clear) :void
  ((heightmap heightmap-ptr))
  "Set all the heights in the heightmap to zero.")


(define-c-function ("TCOD_heightmap_delete" heightmap-delete) :void
  ((heightmap heightmap-ptr))
  "Destroy the heightmap object =HEIGHTMAP=.")


(define-c-function ("TCOD_heightmap_clamp" heightmap-clamp) :void
  ((heightmap heightmap-ptr) (min :float) (max :float))
  "If any height in =HEIGHTMAP= is below =MIN= or above =MAX=, set it
equal to =MIN= or =MAX= respectively.")


(define-c-function ("TCOD_heightmap_count_cells" heightmap-count-cells) :int
  ((heightmap heightmap-ptr) (min :float) (max :float))
  "Return the number of cells in =HEIGHTMAP= which contain heights between
=MIN= and =MAX=.")


(define-c-function ("TCOD_heightmap_has_land_on_border"
                    heightmap-has-land-on-border?) :boolean
  ((heightmap heightmap-ptr) (waterlevel :float))
  "Return true if any of the border cells of =HEIGHTMAP= have heights greater
than =WATERLEVEL=.")


(defcfun ("TCOD_heightmap_get_minmax" %heightmap-get-minmax) :void
  (heightmap heightmap-ptr) (minfloat :pointer) (maxfloat :pointer))


(defun* (heightmap-get-min -> float) ((heightmap heightmap-ptr))
  "Return the lowest height in =HEIGHTMAP=."
  (with-foreign-object (minf :float)
    (with-foreign-object (maxf :float)
      (%heightmap-get-minmax heightmap minf maxf)
      (mem-aref minf :float))))


(defun* (heightmap-get-max -> float) ((heightmap heightmap-ptr))
  "Return the highest height in =HEIGHTMAP=."
  (with-foreign-object (minf :float)
    (with-foreign-object (maxf :float)
      (%heightmap-get-minmax heightmap minf maxf)
      (mem-aref maxf :float))))



(define-c-function ("TCOD_heightmap_normalize" heightmap-normalize) :void
  ((heightmap heightmap-ptr) (min :float) (max :float))
  "Scale all the heights in =HEIGHTMAP= so that the lowest is equal to
=MIN= and the highest is equal to =MAX=.")


(declaim (inline heightmap-normalise))
(defun heightmap-normalise (heightmap min max)
  (heightmap-normalize heightmap min max))


(define-c-function ("TCOD_heightmap_copy" heightmap-copy) :void
  ((source heightmap-ptr) (dest heightmap-ptr))
  "Copy the heightmap =SOURCE= into the heightmap object =DEST=.")


(defcfun ("TCOD_heightmap_rain_erosion" %heightmap-rain-erosion) :void
  (heightmap heightmap-ptr) (num-drops :int) (erosion-coef :float)
  (sediment-coef :float) (randomptr :pointer))


(defun* heightmap-rain-erosion ((heightmap heightmap-ptr) (num-drops fixnum)
                                (erosion-coef float)
                                (sedimentation-coef float)
                               &optional ((rng randomptr) +NULL+))
  "'Erode' the heightmap =HEIGHTMAP= by dropping =NUM-DROPS= 'raindrops' in
random locations."
  (%heightmap-rain-erosion heightmap num-drops erosion-coef
                           sedimentation-coef rng))


(defcfun ("TCOD_heightmap_dig_bezier" %heightmap-dig-bezier) :void
  (heightmap heightmap-ptr) (px :pointer) (py :pointer) (start-radius :float)
  (start-depth :float) (end-radius :float) (end-depth :float))


(defun* heightmap-dig-bezier ((heightmap heightmap-ptr) (coords list)
                              (start-radius float) (start-depth float)
                              (end-radius float) (end-depth float))
  "Carve a path through =HEIGHTMAP= using a cubic Bezier curve."
  (with-foreign-object (px :int 4)
    (with-foreign-object (py :int 4)
      (dotimes (i 4)
        (assert (realp (car (nth i coords))))
        (assert (realp (cdr (nth i coords))))
        (setf (mem-aref px :int i) (coerce (car (nth i coords)) 'integer))
        (setf (mem-aref py :int i) (coerce (cdr (nth i coords)) 'integer)))
      (%heightmap-dig-bezier heightmap px py ;(mem-ref px :int) (mem-ref py :int)
                             start-radius start-depth
                             end-radius end-depth))))


(defun* heightmap-dig-line ((heightmap heightmap-ptr)
                            (x1 fixnum) (y1 fixnum)
                            (x2 fixnum) (y2 fixnum) (radius float)
                            (depth float))
  (heightmap-dig-bezier heightmap `((,x1 . ,y1) (,x1 . ,y1) (,x2 . ,y2) (,x2 . ,y2))
                        radius depth radius depth))


(define-c-function ("TCOD_heightmap_add_hill" heightmap-add-hill) :void
  ((heightmap heightmap-ptr) (hx :float) (hy :float)
   (hradius :float) (hheight :float)))

(define-c-function ("TCOD_heightmap_dig_hill" heightmap-dig-hill) :void
  ((heightmap heightmap-ptr) (hx :float) (hy :float)
   (hradius :float) (hheight :float)))


(define-c-function ("TCOD_heightmap_kernel_transform"
                    %heightmap-kernel-transform) :void
  ((heightmap heightmap-ptr) (kernel-size :int)
   (dx-ptr :pointer) (dy-ptr :pointer)
   (weight-ptr :pointer) (min-level :float) (max-level :float)))


(defun* heightmap-kernel-transform ((heightmap heightmap-ptr)
                                    (coords list) (weights list)
                                    (min-level single-float)
                                    (max-level single-float))
  "* Arguments:
- HEIGHTMAP :: pointer to a heightmap object.
- COORDS :: a list of (X . Y) cons cells specifying coordinates relative to the
cell being processed. For example (-1 . 0) is the cell to the west, (0 . 1) is the
cell to the south, etc.
- WEIGHTS :: a list of factors by which to scale the values in processed cells.
The list must be the same length as COORDS.
- MIN-LEVEL, MAX-LEVEL :: Cells are only processed if their values lies within
these limits."
  (assert (= (length coords) (length weights)))
  (let ((ksize (length coords)))
    (with-foreign-objects ((dx-array :int ksize)
                           (dy-array :int ksize)
                           (weight-array :float ksize))
      (dotimes (i ksize)
        (setf (mem-aref dx-array :int i) (car (nth i coords))
              (mem-aref dx-array :int i) (cdr (nth i coords))
              (mem-aref weight-array :float i) (nth i weights)))
      (%heightmap-kernel-transform heightmap ksize dx-array dy-array
                                   weight-array min-level max-level))))



(define-c-function ("TCOD_heightmap_add_voronoi" %heightmap-add-voronoi) :void
    ((heightmap heightmap-ptr) (num-points :int) (num-coefs :int) (coef-ptr :pointer)
     (rng randomptr)))

(defun* heightmap-add-voronoi ((heightmap heightmap-ptr) (num-points uint32)
                               (coefs list) (rng randomptr))
  "* Arguments:
- HEIGHTMAP :: pointer to a heightmap object.
- NUM-POINTS :: number of Voronoi sites to create.
- COEFS :: list of floats to use to scale the distance to each site.
- RNG :: pointer to a random number generator object."
  (with-foreign-object (coef-array :float (length coefs))
    (dotimes (i (length coefs))
      (setf (mem-aref coef-array :float i) (nth i coefs)))
    (%heightmap-add-voronoi heightmap num-points (length coefs)
                            coef-array rng)))


(define-c-function ("TCOD_heightmap_scale_fbm" heightmap-scale-fbm) :void
    ((heightmap heightmap-ptr) (noise noise) (mulx :float) (muly :float)
     (addx :float) (addy :float) (octaves :float) (delta :float) (scale :float)))

(define-c-function ("TCOD_heightmap_get_normal" %heightmap-get-normal) :void
    ;; n is a pointer to an array of 3 floats.
  ((heightmap heightmap-ptr) (x :float) (y :float) (n :pointer)
   (water-level :float)))

(defun* (heightmap-get-normal -> list) ((heightmap heightmap-ptr)
                                        (x single-float) (y single-float)
                                        (water-level single-float))
  "* Returns: a list of 3 floats, representing the normalised normal
vector of the point at X, Y."
  (with-foreign-object (n-array :float 3)
    (%heightmap-get-normal heightmap x y n-array water-level)
    (loop for i from 0 below 3 collecting (mem-aref n-array :float i))))


(define-c-function ("TCOD_heightmap_islandify" heightmap-islandify) :void
    ((heightmap heightmap-ptr) (sea-level :float) (rng randomptr)))


;;;; <<Field of view>> =========================================================


;; Create a map
(define-c-function ("TCOD_map_new" map-new) mapptr
    ((width :int) (height :int))
  "Return a new map object of the given dimensions.")

(define-c-function ("TCOD_map_set_properties" map-set-properties) :void
    ((map mapptr) (x :int) (y :int) (transparent? :boolean) (walkable? :boolean))
  "Set the properties of the map cell at =(X, Y)=. It is walkable if
=walkable?= is true, and transparent if =transparent?= is true.")

(define-c-function ("TCOD_map_compute_fov" map-compute-fov) :void
    ((map mapptr) (player-x :int) (player-y :int) (max-radius :int)
     (light-walls? :boolean) (algorithm fov-algorithm))
  "Compute field of view information for =MAP=, assuming the player is at
=(PLAYER-X, PLAYER-Y)=, and using the field of view algorithm =ALGORITHM=.")

(define-c-function ("TCOD_map_is_in_fov" map-is-in-fov?) :boolean
    ((map mapptr) (x :int) (y :int))
  "Return true if position =(X, Y)= on the map is visible.")

(define-c-function ("TCOD_map_set_in_fov" map-set-in-fov) :void
    ((map mapptr) (x :int) (y :int) (fov? :boolean))
  "Set whether the cell at X, Y in =MAP= is in field of view.")

(define-c-function ("TCOD_map_is_transparent" map-is-transparent?) :boolean
    ((map mapptr) (x :int) (y :int))
  "Return true if position =(X, Y)= on the map is set to be transparent.")

(define-c-function ("TCOD_map_is_walkable" map-is-walkable?) :boolean
    ((map mapptr) (x :int) (y :int))
  "Return true if position =(X, Y)= on the map is set to be walkable.")

(define-c-function ("TCOD_map_clear" map-clear) :void
    ((map mapptr))
  "Set all cells in =MAP= to be neither walkable nor transparent.")

(define-c-function ("TCOD_map_get_width" map-get-width) :int
    ((map mapptr))
  "Return the width of the map object =MAP=.")

(define-c-function ("TCOD_map_get_height" map-get-height) :int
    ((map mapptr))
  "Return the height of the map object =MAP=.")

(define-c-function ("TCOD_map_get_nb_cells" map-get-nb-cells) :int
    ((map mapptr))
  "Return the number of cells in the map object =MAP=.")

(define-c-function ("TCOD_map_delete" map-delete) :void
    ((map mapptr))
  "Destroy the map object =MAP=.")

(define-c-function ("TCOD_map_copy" map-copy) :void
    ((map-src mapptr) (map-dest mapptr))
  "Copy the map object =SRC= into the new map object =DEST=.")



;;;; <<A* pathfinding>> =======================================================


;; Example of how to define a lisp function which can be called from C,
;; for use with PATH-NEW-USING-FUNCTION.
(defcallback my-a*-callback :float
    ((from-x :int) (from-y :int) (to-x :int) (to-y :int) (userdata :pointer))
  ;; In the function body: all args will have been converted to lisp.
  ;; The function's return value will be converted to C.
  (declare (ignore from-x from-y to-x to-y userdata))
  )


(define-c-function ("TCOD_path_new_using_map" path-new-using-map) a*-path
    ((map mapptr) (diagonal-cost :float))
  "Return a new A* path object, using the map =MAP=.")

;; Call like this:
;;   (tcod:path-new-using-function x y (callback my-a*-callback) ptr)
;; Where 'my-a*-callback' is a lisp function defined using defcallback
;; (see above).
(define-c-function ("TCOD_path_new_using_function" path-new-using-function) a*-path
    ((xdim :int) (ydim :int) (callback :pointer) (user-data :pointer)
     (diagonal-cost :float))
  "Return a new A* path object, which will call the function =CALLBACK= to
calculate movement costs.")

(define-c-function ("TCOD_path_delete" path-delete) :void
    ((a*-path a*-path))
  "Delete an A* path object.")

(define-c-function ("TCOD_path_reverse" path-reverse) :void
    ((a*-path a*-path))
  "Swap origin and destination for an A* path object.")

(define-c-function ("TCOD_path_compute" path-compute) :boolean
    ((a*-path a*-path) (ox :int) (oy :int) (dx :int) (dy :int))
  "Compute the path between the two points =(OX,OY)= and =(DX,DY)=, using the
A* algorithm.")

(defcfun ("TCOD_path_get_origin" %path-get-origin) :void
  (a*-path a*-path) (xptr :pointer) (yptr :pointer))

(defun* (path-get-origin -> (cons fixnum fixnum)) ((a*-path a*-path))
  "Return the coordinates of the current origin of the A* path =PATH=."
  (with-foreign-object (x :int)
    (with-foreign-object (y :int)
      (%path-get-origin a*-path x y)
      (cons (mem-aref x :int) (mem-aref y :int)))))

(defcfun ("TCOD_path_get_destination" %path-get-destination) :void
  (a*-path a*-path) (xptr :pointer) (yptr :pointer))

(defun* (path-get-destination -> (cons fixnum fixnum)) ((a*-path a*-path))
  "Return the coordinates of the current destination of the A* path =PATH=."
  (with-foreign-object (x :int)
    (with-foreign-object (y :int)
      (%path-get-destination a*-path x y)
      (cons (mem-aref x :int) (mem-aref y :int)))))

(define-c-function ("TCOD_path_size" path-size) :int
    ((a*-path a*-path))
  "Return the number of steps in the path.")

(defcfun ("TCOD_path_get" %path-get) :void
  (a*-path a*-path) (index :int) (xptr :pointer) (yptr :pointer))

(defun* (path-get -> (cons fixnum fixnum)) ((a*-path a*-path) (index fixnum))
  "Return the INDEXth step in the path from its current origin to its current
destination."
  (with-foreign-object (x :int)
    (with-foreign-object (y :int)
      (%path-get a*-path index x y)
      (cons (mem-aref x :int) (mem-aref y :int)))))

(defcfun ("TCOD_path_walk" %path-walk) :boolean
  (a*-path a*-path) (xptr :pointer) (yptr :pointer) (recalc-when-needed? :boolean))

(defun* (path-walk -> (or null (cons fixnum fixnum))) ((a*-path a*-path)
                                                       (recalc-when-needed? boolean))
  "Move one step along =PATH=. The path becomes one step shorter. Returns
the coordinates of the new location."
  (with-foreign-object (x :int)
    (with-foreign-object (y :int)
      (if (%path-walk a*-path x y recalc-when-needed?)
          (cons (mem-aref x :int) (mem-aref y :int))
          nil))))

(define-c-function ("TCOD_path_is_empty" path-is-empty?) :boolean
  ((a*-path a*-path))
  "Return true if the path object is empty (has zero steps).")


;; <<Dijkstra pathfinding>> ===================================================


(define-c-function ("TCOD_dijkstra_new" dijkstra-new) dijkstra-path
    ((map mapptr) (diagonal-cost :float))
  "Return a new Dijkstra path object which uses =MAP=.")

(define-c-function ("TCOD_dijkstra_new_using_function" dijkstra-new-using-function)
    dijkstra-path
    ((xdim :int) (ydim :int) (callback :pointer) (user-data :pointer)
     (diagonal-cost :float))
  "Return a new Dijkstra path object which calls the function =CALLBACK= to
calculate movement costs.")

(define-c-function ("TCOD_dijkstra_delete" dijkstra-delete) :void
    ((dijkstra-path dijkstra-path))
  "Delete a Dijkstra path object.")

(define-c-function ("TCOD_dijkstra_reverse" dijkstra-reverse) :void
    ((dijkstra-path dijkstra-path))
  "Swap origin and destination for a Dijkstra path object.")

(define-c-function ("TCOD_dijkstra_compute" dijkstra-compute) :void
    ((dijkstra-path dijkstra-path) (rootx :int) (rooty :int))
  "Compute paths leading to the point at =(ROOTX, ROOTY)=, using the
Dijkstra algorithm.")

(define-c-function ("TCOD_dijkstra_path_set" dijkstra-path-set) :boolean
    ((dijkstra-path dijkstra-path) (to-x :int) (to-y :int))
  "Return true if a path can be found leading from the root node to the
point at =(TO-X, TO-Y)=.")

(define-c-function ("TCOD_dijkstra_size" dijkstra-size) :int
    ((dijkstra-path dijkstra-path))
  "Return the number of steps in the path.")

(define-c-function ("TCOD_dijkstra_get_distance" dijkstra-get-distance) :float
    ((dijkstra-path dijkstra-path) (to-x :int) (to-y :int))
  "Return the number of steps on the path leading from the root node to
the point at =(TO-X, TO-Y)=.")

(defcfun ("TCOD_dijkstra_get" %dijkstra-get) :void
  (dijkstra-path dijkstra-path) (index :int) (xptr :pointer) (yptr :pointer))

(defun* (dijkstra-get -> (cons fixnum fixnum)) ((dijkstra-path dijkstra-path)
                                                  (index fixnum))
  "Return the INDEXth step in the path from its current origin to its current
destination."
  (with-foreign-object (x :int)
    (with-foreign-object (y :int)
      (%dijkstra-get dijkstra-path index x y)
      (cons (mem-aref x :int) (mem-aref y :int)))))

(define-c-function ("TCOD_dijkstra_is_empty" dijkstra-is-empty?) :boolean
    ((dijkstra-path dijkstra-path))
  "Return true if the path object is empty (has zero steps).")

(defcfun ("TCOD_dijkstra_path_walk" %dijkstra-path-walk) :boolean
  (dijkstra-path dijkstra-path) (xptr :pointer) (yptr :pointer))

(defun* (dijkstra-path-walk -> (or null (cons fixnum fixnum)))
    ((dijkstra-path dijkstra-path))
  "Move one step along =PATH=. The path becomes one step shorter. Returns
the coordinates of the new location."
  (with-foreign-object (x :int)
    (with-foreign-object (y :int)
      (if (%dijkstra-path-walk dijkstra-path x y)
          (cons (mem-aref x :int) (mem-aref y :int))
          nil))))


;;;; <<Bresenham line drawing>> ===============================================


(define-c-function ("TCOD_line_init" line-init) :void
    ((xfrom :int) (yfrom :int) (xto :int) (yto :int)))

(define-c-function ("TCOD_line_step" line-step) :boolean
    ((xcur :pointer) (ycur :pointer)))

(define-c-function ("TCOD_line" line-line) :boolean
    ((xfrom :int) (yfrom :int) (xto :int) (yto :int) (callback :pointer)))

;; Example of how to define a lisp function which can be called from C
;; for use as the callback function for `line-line'.
(defcallback my-bresenham-callback :boolean
    ((x :int) (y :int))
  ;; In the function body: all args will have been converted to lisp.
  ;; The function's return value will be converted to C.
  (declare (ignore x y))
  )


;;;; <<BSP trees>> ============================================================


(define-c-function ("TCOD_bsp_new_with_size" bsp-new-with-size) bsp-ptr
    ((x :int) (y :int) (w :int) (h :int)))

(define-c-function ("TCOD_bsp_remove_sons" bsp-remove-sons) :void
    ((node bsp-ptr)))

(define-c-function ("TCOD_bsp_delete" bsp-delete) :void
    ((node bsp-ptr)))

(define-c-function ("TCOD_bsp_split_once" bsp-split-once) :void
    ((node bsp-ptr) (horizontal? :boolean) (pos :int)))

(define-c-function ("TCOD_bsp_split_recursive" bsp-split-recursive) :void
    ((node bsp-ptr) (randomiser randomptr) (nb :int) (min-h-size :int)
     (min-v-size :int) (max-h-ratio :float) (max-v-ratio :float)))

(define-c-function ("TCOD_bsp_resize" bsp-resize) :void
    ((node bsp-ptr) (x :int) (y :int) (w :int) (h :int)))

(define-c-function ("TCOD_bsp_left" bsp-left) bsp-ptr
    ((node bsp-ptr)))

(define-c-function ("TCOD_bsp_right" bsp-right) bsp-ptr
    ((node bsp-ptr)))

(define-c-function ("TCOD_bsp_father" bsp-father) bsp-ptr
    ((node bsp-ptr)))

(define-c-function ("TCOD_bsp_is_leaf" bsp-is-leaf?) :boolean
    ((node bsp-ptr)))

(define-c-function ("TCOD_bsp_contains" bsp-contains?) :boolean
    ((node bsp-ptr) (cx :int) (cy :int)))

(define-c-function ("TCOD_bsp_find_node" bsp-find-node) bsp-ptr
    ((node bsp-ptr) (cx :int) (cy :int)))

(define-c-function ("TCOD_bsp_traverse_pre_order"
                    bsp-traverse-pre-order) :boolean
    ((node bsp-ptr) (callback :pointer) (userdata :pointer)))

(define-c-function ("TCOD_bsp_traverse_in_order"
                    bsp-traverse-in-order) :boolean
    ((node bsp-ptr) (callback :pointer) (userdata :pointer)))

(define-c-function ("TCOD_bsp_traverse_post_order"
                    bsp-traverse-post-order) :boolean
    ((node bsp-ptr) (callback :pointer) (userdata :pointer)))

(define-c-function ("TCOD_bsp_traverse_level_order"
                    bsp-traverse-level-order) :boolean
    ((node bsp-ptr) (callback :pointer) (userdata :pointer)))

(define-c-function ("TCOD_bsp_traverse_inverted_level_order"
                    bsp-traverse-inverted-level-order) :boolean
    ((node bsp-ptr) (callback :pointer) (userdata :pointer)))

;; Example of how to define a lisp function which can be called from C
;; for use as the callback function for `bsp-traverse-X-order'.
(defcallback my-bsp-callback :boolean
    ((node bsp-ptr) (userdata :pointer))
  ;; In the function body: all args will have been converted to lisp.
  ;; The function's return value will be converted to C.
  (declare (ignore node userdata))
  )


;;;; <<Testing>> ==============================================================


(defun hello-world ()
  (tcod:console-init-root 80 50 "Libtcod Hello World" nil :renderer-sdl)
  (tcod:console-set-alignment *root* :center)
  (tcod:console-print tcod:*root* 40 25 "Hello World!")
  (tcod:console-flush)
  (tcod:console-wait-for-keypress t))



;;;; tcod.lisp ends here ======================================================
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.