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
;;;; -*- 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.




(defpackage :tcod
  (:use :cl :cffi)
  (:export
   #:*root*
   #:+null+
   ;; == Colours ==
   #:start-colours
   #:start-colors
   #:colour
   #:compose-colour
   #:compose-color
   #:decompose-colour
   #:decompose-color
   #:invert-colour
   #:invert-color
   #:colour->grayscale
   #:color->grayscale
   ;; == Console ==
   #:console-wait-for-keypress
   #:console-check-for-keypress
   #:console-set-colour-control
   #:console-set-color-control
   #:console-flush
   #:console-get-fading-colour
   #:console-get-fading-color
   #:console-get-fade
   #:console-set-fade
   #:console-get-char
   #:console-get-fore
   #:console-get-back
   #:console-get-foreground-colour
   #:console-get-background-colour
   #:console-get-foreground-color
   #:console-get-background-color
   #:console-print-left
   #:console-print-right
   #:console-print-centre
   #:console-print-left-rect
   #:console-print-right-rect
   #:console-print-centre-rect
   #:console-print-frame
   #:console-print-double-frame
   #:legal-console-coordinates?
   #:console-put-char
   #:console-put-char-ex
   #:console-set-char
   #:console-set-fore
   #:console-set-back
   #:console-clear
   #:console-set-foreground-colour
   #:console-set-background-colour
   #:console-set-foreground-color
   #:console-set-background-color
   #:console-init-root
   #:console-is-fullscreen?
   #:console-set-fullscreen
   #:console-is-window-closed?
   #:console-credits
   #:console-credits-render
   #:console-set-custom-font
   #:console-set-window-title
   #:console-rect
   #:colour-set-hsv
   #:colour-get-hsv
   #:colour-equals?
   #:colour-add
   #:colour-multiply
   #:colour-multiply-scalar
   #:colour-lerp
   #:make-colour
   #:color-set-hsv
   #:color-get-hsv
   #:color-equals?
   #:color-add
   #:color-multiply
   #:color-multiply-scalar
   #:color-lerp
   #:make-color
   #:keycode
   #:drawing-character
   #:colctrl
   #:colctrl->char
   #:background-flag
   #:key
   #: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
   #:console
   #:is-key-pressed?
   #:console-new
   #:console-delete
   #:console-get-width
   #:console-get-height
   #:console-blit
   ;; == Unicode ==
   ;; todo not yet implemented
   ;; == 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-down
   #:mouse-wheel-up
   #:mouse-flags
   #:mouse-move
   #:mouse-get-status
   ;; == Image ==
   #:image-load
   #:image-save
   #:image-from-console
   #:image-clear
   #:image-put-pixel
   #:image-blit
   #:image-set-key-color
   #:image-set-key-colour
   ;; == Random ==
   #:random-new
   #:random-get-int
   #:random-get-float
   ;; (@> "Noise") ============================================================
   #:noise-new
   #:noise-perlin
   #:noise-simplex
   #:noise-fbm-perlin
   #:noise-turbulence-perlin
   #:noise-delete
   ;; (@> "Heightmap") ========================================================
   #:heightmap
   #:heightmap-new
   #:heightmap-get-value
   #:heightmap-get-interpolated-value
   #:heightmap-get-slope
   #:heightmap-set-value
   #:heightmap-add
   #:heightmap-add-fbm
   #:heightmap-scale
   #:heightmap-lerp
   #:heightmap-add-hm
   #:heightmap-multiply-hm
   #:heightmap-clear
   #:heightmap-delete
   #:heightmap-clamp
   #:heightmap-count-cells
   #:heightmap-has-land-on-border?
   #:heightmap-get-min
   #:heightmap-get-max
   #:heightmap-normalize
   #:heightmap-normalise
   #:heightmap-copy
   #:heightmap-dig-bezier
   #:heightmap-dig-line
   #:heightmap-rain-erosion
   ;; == System layer ==
   #:sys-save-screenshot
   #:sys-sleep-milli
   #:sys-set-fps
   #:sys-get-fps
   #:sys-get-current-resolution
   #:sys-flush
   ))

(in-package :tcod)

;;; 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)

;;; uncomment this form if using pre 1.4.3b1 version of libtcod
;;;(eval-when (:compile-toplevel :load-toplevel :execute)
;;;  (pushnew :libtcod-old *features*))
 
 
(define-foreign-library libtcod
	(:unix "libtcod.so")
	(:windows "libtcod-mingw.dll")
	;; (:macintosh "name-of-libtcod-file-in-macos")
	(t (:default "libtcod")))

(defvar *libtcod-loaded* nil)

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

(defvar *root* (null-pointer) "The root console.")
(defparameter +NULL+ (null-pointer))
(defconstant +NOISE-DEFAULT-HURST+ 0.5)
(defconstant +NOISE-DEFAULT-LACUNARITY+ 2.0)


;;; Foreign types.

(defctype colournum :unsigned-int)

(defcstruct colour  ; TCOD_color_t
	(r :uint8)
	(g :uint8)
	(b :uint8))


(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun compose-colour (r g b)
    "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 #xRRGGBB."
    (+ (ash r 16) (ash g 8) b))
  (defun compose-color (r g b) (compose-colour r g b)))


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


(defun invert-colour (num)
  (multiple-value-bind (r g b) (decompose-colour num)
    (compose-colour (- 255 r) (- 255 g) (- 255 b))))
(defun invert-color (num) (invert-colour num))


;; (defvar *black* (compose-colour 0 0 0))
;; (defvar *dark-grey* (compose-colour 96 96 96))
;; (defvar *grey* (compose-colour 196 196 196))
;; (defvar *white* (compose-colour 255 255 255))
;; (defvar *dark-blue* (compose-colour 40 40 128))
;; (defvar *light-blue* (compose-colour 120 120 255))
;; (defvar *dark-red* (compose-colour 128 0 0))
;; (defvar *light-red* (compose-colour 255 100 50))
;; (defvar *dark-brown* (compose-colour 32 16 0))
;; (defvar *light-yellow* (compose-colour 255 255 150))
;; (defvar *yellow* (compose-colour 255 255 0))
;; (defvar *dark-yellow* (compose-colour 164 164 0))
;; (defvar *green* (compose-colour 0 220 0))
;; (defvar *orange* (compose-colour 255 150 0))
;; (defvar *red* (compose-colour 255 0 0))
;; (defvar *silver* (compose-colour 203 203 203))
;; (defvar *gold* (compose-colour 255 255 102))
;; (defvar *purple* (compose-colour 204 51 153))
;; (defvar *dark-purple* (compose-colour 51 0 51))
;; ;;; Colours not defined in TCOD.
;; (defvar *slate-grey* (compose-colour #x80 #x80 #x80))
;; (defvar *umber* (compose-colour #x80 #x40 0))
;; (defvar *pink* (compose-colour 255 0 255))
;; (defvar *chocolate* (compose-colour 210 105 30))
;; ;;; ...etc...


(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)
    ;; Colours not defined in TCOD.
    (:slate-grey 	#x80 #x80 #x80)
    (:umber 		#x80 #x40 0)
    (:pink 		#xFF #x00 #xFF)
    (:chocolate 	210 105 30)))

;; TCOD_keycode_t
(defcenum 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
(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
  (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))


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


(defun same-keys? (key1 key2)
  (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)))))
       


(defcenum 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
(defcenum 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
(defcenum background-flag
	:NONE
	:SET
	:MULTIPLY
	:LIGHTEN
	:DARKEN
	:SCREEN
	:COLOR-DODGE
	:COLOR-BURN
	:ADD
	:ADDA
	:BURN
	:OVERLAY
	:ALPH)


(defcenum alignment
	:LEFT
	:CENTER
	:RIGHT)


(defcenum 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
  :NB-FOV-ALGORITHMS)


(defbitfield key-state
	(:KEY-PRESSED 1)
	(:KEY-RELEASED 2))

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


;; TCOD_console_t
(defctype console :pointer)

;;#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))))
(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 (keywd)
  "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*))))
(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); 
(defcfun ("TCOD_color_equals_wrapper" color-equals?) :boolean
	(c1 colournum) (c2 colournum))
(defun colour-equals? (c1 c2) (color-equals? c1 c2))


;;TCODLIB_API TCOD_color_t TCOD_color_add (TCOD_color_t c1, TCOD_color_t c2);
(defcfun ("TCOD_color_add_wrapper" color-add) colournum
	(c1 colournum) (c2 colournum))
(defun colour-add (c1 c2) (color-add c1 c2))


;;TCODLIB_API TCOD_color_t TCOD_color_multiply (TCOD_color_t c1,
;; TCOD_color_t c2); 
(defcfun ("TCOD_color_multiply_wrapper" color-multiply) colournum
	(c1 colournum) (c2 colournum))
(defun colour-multiply (c1 c2) (color-multiply c1 c2))


;;TCODLIB_API TCOD_color_t TCOD_color_multiply_scalar (TCOD_color_t c1,
;; float value);
(defcfun ("TCOD_color_multiply_scalar_wrapper" color-multiply-scalar) colournum
	(c1 colournum) (value :float))
(defun colour-multiply-scalar (c1 value) (color-multiply-scalar c1 value))


;; TCODLIB_API TCOD_color_t TCOD_color_lerp(TCOD_color_t c1, TCOD_color_t c2,
;; float coef);
(defcfun ("TCOD_color_lerp_wrapper" color-lerp) colournum
	(c1 colournum) (c2 colournum) (coef :float))
(defun colour-lerp (c1 c2 coef) (color-lerp c1 c2 coef))


;; TCODLIB_API void TCOD_color_set_HSV(TCOD_color_t *c,float h, float s,
;; float v);
(defcfun ("TCOD_color_set_HSV" color-set-hsv) :void
	(c :pointer) (h :float) (s :float) (v :float))
(defun colour-set-hsv (c h s v) (color-set-hsv c h s v))


;; TCODLIB_API void TCOD_color_get_HSV(TCOD_color_t c,float * h, float * s,
;; float * v);

(defvar *internal-color-hue-ptr* (foreign-alloc :int))
(defvar *internal-color-saturation-ptr* (foreign-alloc :int))
(defvar *internal-color-value-ptr* (foreign-alloc :int))

(defcfun ("TCOD_color_get_HSV_wrapper" %color-get-hsv) :void
	(c colournum) (h :pointer) (s :pointer) (v :pointer))

(defun color-get-hsv (color)
    (%color-get-hsv color
		    *internal-color-hue-ptr*
		    *internal-color-saturation-ptr*
		    *internal-color-value-ptr*)
    (list (mem-ref *internal-color-hue-ptr* :int)
	  (mem-ref *internal-color-saturation-ptr* :int)
	  (mem-ref *internal-color-value-ptr* :int)))

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

(defcfun ("TCOD_console_credits" console-credits) :void)

(defcfun ("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);
(defcfun ("TCOD_console_init_root" console-init-root) :void
	(w :int) (h :int) (title :string) (fullscreen :boolean))

;;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);
(defcfun ("TCOD_console_set_custom_font" %console-set-custom-font) :void
	(fontfile :string) (flags custom-font-flags)
	(chars-horizontal :int) (chars-vertical :int)
	)
;; (nb-char-vertic :int)
;; 	(chars-by-row? :boolean) (key-colour colournum))


(defun console-set-custom-font (fontfile flags chars-horizontal chars-vertical)
  "FLAGS accepts a quoted list containing one or more of the symbols
:FONT-LAYOUT-ASCII-IN-ROW, :FONT-LAYOUT-ASCII-IN-COL, :FONT-TYPE-GREYSCALE,
or :FONT-LAYOUT-TCOD."
  (assert (probe-file fontfile))
  (%console-set-custom-font fontfile flags chars-horizontal chars-vertical))


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

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

;;TCODLIB_API bool TCOD_console_is_fullscreen();
(defcfun ("TCOD_console_is_fullscreen" console-is-fullscreen?) :boolean)

;;TCODLIB_API bool TCOD_console_is_window_closed();
(defcfun ("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);
(defcfun ("TCOD_console_set_background_color_wrapper"
	  console-set-background-color) :void
	(con console) (col colournum))
(defun console-set-background-colour (con col)
  (console-set-background-color con col))


;;TCODLIB_API void TCOD_console_set_foreground_color(TCOD_console_t con,
;;                                                   TCOD_color_t col);
(defcfun ("TCOD_console_set_foreground_color_wrapper"
	  console-set-foreground-color) :void
	(con console) (col colournum))
(defun console-set-foreground-colour (con col)
  (console-set-foreground-color con col))


;;TCODLIB_API void TCOD_console_clear(TCOD_console_t con);
(defcfun ("TCOD_console_clear" console-clear) :void
	(con console))


;;TCODLIB_API void TCOD_console_set_back(TCOD_console_t con,int x, int y,
;;                                       TCOD_color_t col,
;;                                       TCOD_bkgnd_flag_t flag);
(defcfun ("TCOD_console_set_back_wrapper" %console-set-back) :void
  (con console) (x :int) (y :int) (col colournum) (flag background-flag))


(defun console-set-back (con x y col flag)
  ;; Assertion in libtcod
  (assert (legal-console-coordinates? con x y))
  (%console-set-back con x y col flag))


;;TCODLIB_API void TCOD_console_set_fore(TCOD_console_t con,int x, int y,
;;                                       TCOD_color_t col);
(defcfun ("TCOD_console_set_fore_wrapper" %console-set-fore) :void
  (con console) (x :int) (y :int) (col colournum))


(defun console-set-fore (con x y col)
  ;; Assertion in libtcod
  (assert (legal-console-coordinates? con x y))
  (%console-set-fore 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) (c :unsigned-char))


(defun console-set-char (con x y ch)
  ;; Assertion in libtcod
  (assert (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);
(defcfun ("TCOD_console_put_char" console-put-char) :void
	(con console) (x :int) (y :int) (c :unsigned-char)
	(flag background-flag))

;;TCODLIB_API void TCOD_console_put_char_ex(TCOD_console_t con,int x, int y,
;;                                          int c, TCOD_bkgnd_flag_t flag);
#-libtcod-old
(defcfun ("TCOD_console_put_char_ex_wrapper" %console-put-char-ex) :void
	(con console) (x :int) (y :int) (c :unsigned-char)
	(fg colournum) (bg colournum))

;; wrapper to TCOD_console_put_char_ex is currently only in SVN
#+libtcod-old
(defun %console-put-char-ex (con x y c fg bg)
    (console-set-fore con x y fg)
    (console-set-back con x y bg :set)
    (console-set-char con x y c))


(defun console-put-char-ex (con x y c fg bg)
  (assert (and (not (null-pointer-p con))
               (< x (console-get-width con))
               (< y (console-get-height con))))
  (%console-put-char-ex con x y c fg bg))


;;TCODLIB_API void TCOD_console_print_left(TCOD_console_t con,int x, int y,
;;                                         TCOD_bkgnd_flag_t flag,
;;                                         const char *fmt, ...); 
(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 x y flag fmt &rest args)
  (%console-print-left con x y flag (apply #'format nil fmt args)))

(defcfun ("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) (msg :string)
	(can-split? :boolean) (count-only? :boolean))

(defun console-print-return-string (con x y rw rh flag align msg can-split?
				    count-only?)
  (%console-print-return-string con x y rw rh flag align msg
				can-split? count-only?))

;;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 x y flag fmt &rest args)
  (%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)

;;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)

;;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)


;;TCODLIB_API int TCOD_console_print_center_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_center_rect" console-print-centre-rect) :int
	(con console) (x :int) (y :int) (w :int) (h :int)
	(flag background-flag) (fmt :string)
	&rest)

;;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);
(defcfun ("TCOD_console_rect" console-rect) :void
  (con console) (x :int) (y :int) (w :int) (h :int) (clear? :boolean)
  (flag background-flag))

;;TCODLIB_API void TCOD_console_hline(TCOD_console_t con,int x,int y, int l,
;; TCOD_bkgnd_flag_t flag);
;;TCODLIB_API void TCOD_console_vline(TCOD_console_t con,int x,int y, int l,
;; TCOD_bkgnd_flag_t flag);

;;TCODLIB_API void TCOD_console_print_frame(TCOD_console_t con,int x,int y,
;; int w,int h, bool empty, const char *fmt, ...);
;;#-libtcod-old
(defcfun ("TCOD_console_print_frame" console-print-frame) :void
  (con console) (x :int) (y :int) (w :int) (h :int)
  (empty? :boolean) (flag background-flag) 
  (fmt :string) &rest)

;; #+libtcod-old
;; (defcfun ("TCOD_console_print_frame" %console-print-frame) :void
;;   (con console) (x :int) (y :int) (w :int) (h :int)
;;   (empty? :boolean) (fmt :string) &rest)

  
;; #+libtcod-old
;; (defun console-print-frame (con x y w h empty? flag fmt &rest args)
;;   (declare (ignore flag))
;;   (%console-print-frame con x y w h empty? 
;;     (if (stringp fmt) (apply #'format nil fmt args) +NULL+)))
  

;; Added in wrappers.c
#-libtcod-old
(defcfun ("TCOD_console_print_double_frame" console-print-double-frame) :void
  (con console) (x :int) (y :int) (w :int) (h :int)
  (empty? :boolean) (flag background-flag) 
  (fmt :string) &rest)

#+libtcod-old
(defcfun ("TCOD_console_print_double_frame" %console-print-double-frame) :void
  (con console) (x :int) (y :int) (w :int) (h :int)
  (empty? :boolean) (fmt :string) &rest)

  
#+libtcod-old
(defun console-print-double-frame (con x y w h empty? flag fmt &rest args)
  (declare (ignore flag))
  (%console-print-double-frame con x y w h empty? 
    (if (stringp fmt) (apply #'format nil fmt args) +NULL+)))

  
;;TCODLIB_API TCOD_color_t TCOD_console_get_background_color(TCOD_console_t con);
(defcfun ("TCOD_console_get_background_color"
	  console-get-background-color) colournum
  (con console))
(defun console-get-background-colour (con)
  (console-get-background-color con))


;;TCODLIB_API TCOD_color_t TCOD_console_get_foreground_color(TCOD_console_t con);
(defcfun ("TCOD_console_get_foreground_color"
	  console-get-foreground-color) colournum
  (con console))
(defun console-get-foreground-colour (con)
  (console-get-foreground-color con))


;;TCODLIB_API TCOD_color_t TCOD_console_get_back(TCOD_console_t con,int x, int y)
(defcfun ("TCOD_console_get_back_wrapper" %console-get-back) colournum
  (con console) (x :int) (y :int))


(defun legal-console-coordinates? (con x y)
  (and (not (null-pointer-p con))
       (< x (console-get-width con))
       (< y (console-get-height con))))


(defun console-get-back (con x y)
  ;; Assertion in libtcod
  (assert (legal-console-coordinates? con x y))
  (%console-get-back con x y))


;;TCODLIB_API TCOD_color_t TCOD_console_get_fore(TCOD_console_t con,
;;                                               int x, int y);
(defcfun ("TCOD_console_get_fore_wrapper" %console-get-fore) colournum
  (con console) (x :int) (y :int))


(defun console-get-fore (con x y)
  ;; Assertion in libtcod
  (assert (legal-console-coordinates? con x y))
  (%console-get-fore con x y))

  
;;TCODLIB_API int TCOD_console_get_char(TCOD_console_t con,int x, int y);
(defcfun ("TCOD_console_get_char" %console-get-char) :unsigned-char
  (con console) (x :int) (y :int))


(defun console-get-char (con x y)
  ;; Assertion in libtcod
  (assert (legal-console-coordinates? con x y))
  (%console-get-char con x y))


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

;;TCODLIB_API uint8 TCOD_console_get_fade();
(defcfun ("TCOD_console_get_fade" console-get-fade) :uint8)


;;TCODLIB_API TCOD_color_t TCOD_console_get_fading_color();
(defcfun ("TCOD_console_get_fading_color_wrapper"
	  console-get-fading-color) colournum)
(defun console-get-fading-colour ()
  (console-get-fading-color))


;;TCODLIB_API void TCOD_console_flush();
(defcfun ("TCOD_console_flush" console-flush) :void)

;; (sys-flush t) forces an 'update' of the system timer, FPS, etc.
;; If render is true, also forces an update of the root console.
#-libtcod-old
(defcfun ("TCOD_sys_flush" sys-flush) :void
	(render :boolean))

;;TCODLIB_API void TCOD_console_set_color_control(TCOD_colctrl_t con,
;;     TCOD_color_t fore, TCOD_color_t back);
(defcfun ("TCOD_console_set_color_control_wrapper"
	  console-set-color-control) :void
  (con colctrl) (fore colournum) (back colournum))
(defun console-set-colour-control (con fore back)
  (console-set-color-control con fore back))


;;TCODLIB_API TCOD_key_t TCOD_console_check_for_keypress(int flags);
(defcfun ("TCOD_console_check_for_keypress_wrapper"
	  %console-check-for-keypress) :boolean
	(keyptr key-press) (flags key-state))


(defun get-bit (n pos)
  "POS = 1 refers to the 1's bit"
  (/= 0 (logand n (expt 2 (1- pos)))))


(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))))


(defvar *key* nil)

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

(defun console-wait-for-keypress (flush)
  (unless *key*
    (setf *key* (foreign-alloc 'key-press)))
  (%console-wait-for-keypress *key* flush)
  (key->keypress *key*))


;;TCODLIB_API TCOD_key_t TCOD_console_wait_for_keypress(bool flush);
(defcfun ("TCOD_console_wait_for_keypress_wrapper"
	  %console-wait-for-keypress) :void
	(keyptr key-press) (flush :boolean))

;;TCODLIB_API void TCOD_console_set_keyboard_repeat(int initial_delay,
;; int interval);
;;TCODLIB_API void TCOD_console_disable_keyboard_repeat();
;;TCODLIB_API bool TCOD_console_is_key_pressed(TCOD_keycode_t key);
(defcfun ("TCOD_console_is_key_pressed" is-key-pressed?) :boolean
  (code keycode))

;;TCODLIB_API TCOD_console_t TCOD_console_new(int w, int h);
(defcfun ("TCOD_console_new" console-new) console
  (w :int) (h :int))

;;TCODLIB_API int TCOD_console_get_width(TCOD_console_t con);
(defcfun ("TCOD_console_get_width" console-get-width) :int
  (con console))

;;TCODLIB_API int TCOD_console_get_height(TCOD_console_t con);
(defcfun ("TCOD_console_get_height" console-get-height) :int
  (con console))

;;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);
(defcfun ("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))


(defun console-blit (src xsrc ysrc wsrc hsrc dest xdest ydest
                     foreground-alpha background-alpha)
  (check-type xsrc (integer 0))
  (check-type ysrc (integer 0))
  (check-type wsrc (integer 0))
  (check-type hsrc (integer 0))
  (check-type xdest (integer 0))
  (check-type ydest (integer 0))
  (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.
  (unless (or (>= xdest (console-get-width dest))
              (>= ydest (console-get-height dest)))
    ;; 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 debugging facilities if the conditions
    ;; are not met.
    (assert (and (plusp wsrc) (plusp hsrc)
                 (>= (+ xdest wsrc) 0) (>= (+ ydest hsrc) 0)))
    (%console-blit src xsrc ysrc wsrc hsrc dest xdest ydest
                   foreground-alpha background-alpha)))


;;TCODLIB_API void TCOD_console_delete(TCOD_console_t console);
(defcfun ("TCOD_console_delete" console-delete) :void
  (con console))

;;; sys.h

;;TCODLIB_API uint32 TCOD_sys_elapsed_milli();
;;TCODLIB_API float TCOD_sys_elapsed_seconds();
;;TCODLIB_API void TCOD_sys_sleep_milli(uint32 val);
(defcfun ("TCOD_sys_sleep_milli" sys-sleep-milli) :void
  (val :unsigned-int))

;;TCODLIB_API void TCOD_sys_save_screenshot(const char *filename);
(defcfun ("TCOD_sys_save_screenshot" %sys-save-screenshot) :void
  (filename :string))

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

;;TCODLIB_API void TCOD_sys_force_fullscreen_resolution(int width, int height);
;;TCODLIB_API void TCOD_sys_set_fps(int val);
(defcfun ("TCOD_sys_set_fps" sys-set-fps) :void
  (val :int))

;;TCODLIB_API int TCOD_sys_get_fps();
(defcfun ("TCOD_sys_get_fps" sys-get-fps) :int)

;;TCODLIB_API float TCOD_sys_get_last_frame_length();
;;TCODLIB_API void TCOD_sys_get_current_resolution(int *w, int *h);
(defvar *internal-width-ptr* (foreign-alloc :int))
(defvar *internal-height-ptr* (foreign-alloc :int))

(defcfun ("TCOD_sys_get_current_resolution" %sys-get-current-resolution) :void
  (w-ptr :pointer) (h-ptr :pointer))

(defun sys-get-current-resolution ()
  (%sys-get-current-resolution *internal-width-ptr* *internal-height-ptr*)
  (values (mem-ref *internal-width-ptr* :int)
	  (mem-ref *internal-height-ptr* :int)))

;;; mersenne.h

;; TCOD_random_t
(defctype randomptr :pointer)

;;TCODLIB_API TCOD_random_t TCOD_random_get_instance();
;;TCODLIB_API TCOD_random_t TCOD_random_new();
(defcfun ("TCOD_random_new" random-new) randomptr)

;;TCODLIB_API TCOD_random_t TCOD_random_new_from_seed(uint32 seed);
;;TCODLIB_API int TCOD_random_get_int(TCOD_random_t mersenne, int min, int max);
(defcfun ("TCOD_random_get_int" random-get-int) :int
  (rng randomptr) (min :int) (max :int))

;;TCODLIB_API float TCOD_random_get_float(TCOD_random_t mersenne, float min,
;;   float max);
(defcfun ("TCOD_random_get_float" random-get-float) :float
  (rng randomptr) (min :float) (max :float))

;;TCODLIB_API int TCOD_random_get_int_from_byte_array(int min, int max,
;;   const char *data,int len);
;;TCODLIB_API void TCOD_random_delete(TCOD_random_t mersenne);

;;; mouse.h

(defcstruct mouse-state
	(x :int)	
	(y :int)	
	(dx :int)	
	(dy :int)
	(cx :int)	
	(cy :int)
	(dcx :int)	
	(dcy :int)
	(flags :uint8))

(defstruct mouse
	(x 0 :type integer)	;; absolute position
	(y 0 :type integer)	
	(dx 0 :type integer)	;; movement since last update in pixels
	(dy 0 :type integer)
	(cx 0 :type integer)	;; cell coordinates in the root console 
	(cy 0 :type integer)
	(dcx 0 :type integer)	;; movement since last update in console cells
	(dcy 0 :type integer)
	(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 up event
	(wheel-down nil :type boolean)   ;; wheel down event 
	(flags 0 :type integer))	;; copied from mouse-state


(defun mouse-state->mouse (ms)
  (let ((flags (foreign-slot-value ms 'mouse-state 'flags)))
    (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)
		:wheel-up (get-bit flags 7)
		:wheel-down (get-bit flags 8)
		:flags flags)))

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

(let ((rodent nil))
  (defun mouse-get-status ()
    (unless rodent
      (setf rodent (foreign-alloc 'mouse-state)))
    (%mouse-get-status rodent)
    (mouse-state->mouse rodent)))


;;TCODLIB_API void TCOD_mouse_show_cursor(bool visible);
;;TCODLIB_API bool TCOD_mouse_is_cursor_visible();
;;TCODLIB_API void TCOD_mouse_move(int x, int y);
(defcfun ("TCOD_mouse_move" mouse-move) :void
  (x :int) (y :int))


;;; image.h

;; TCOD_image_t
(defctype image :pointer)

;;TCODLIB_API TCOD_image_t TCOD_image_new(int width, int height);
;;TCODLIB_API TCOD_image_t TCOD_image_from_console(TCOD_console_t console);
(defcfun ("TCOD_image_from_console" image-from-console) image
  (con console))

;;TCODLIB_API TCOD_image_t TCOD_image_load(const char *filename);
(defcfun ("TCOD_image_load" image-load) image
  (filename :string))


;;TCODLIB_API void TCOD_image_clear(TCOD_image_t image, TCOD_color_t color);
(defcfun ("TCOD_image_clear_wrapper" image-clear) :void
  (image image) (color colournum))

;;TCODLIB_API void TCOD_image_save(TCOD_image_t image, const char *filename);
(defcfun ("TCOD_image_save" image-save) :void
  (image image) (filename :string))


;;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);
(defcfun ("TCOD_image_get_pixel_wrapper" image-get-pixel) colournum
  (image image) (x :int) (y :int))

;;TCODLIB_API TCOD_color_t TCOD_image_get_mipmap_pixel(TCOD_image_t image,
;; float x0,float y0, float x1, float y1);
(defcfun ("TCOD_image_get_mipmap_pixel_wrapper"
	  image-get-mipmap-pixel) colournum
  (image image) (x0 :float) (y0 :float) (x1 :float) (y1 :float))

;;TCODLIB_API void TCOD_image_put_pixel(TCOD_image_t image,int x, int y,
;; TCOD_color_t col);
(defcfun ("TCOD_image_put_pixel_wrapper" image-put-pixel) :void
  (image image) (x :int) (y :int) (col colournum))

;;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);
(defcfun ("TCOD_image_blit" image-blit) :void
  (image image) (con console) (x :float) (y :float) (flag background-flag)
  (scalex :float) (scaley :float) (angle :float))


;;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);
(defcfun ("TCOD_image_blit_rect" image-blit-rect) :void
  (image image) (con console) (x :int) (y :int) (w :int) (h :int)
  (flag background-flag))


;;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);
(defcfun ("TCOD_image_set_key_color" image-set-key-color) :void
  (image image) (key-color colournum))
(defun image-set-key-colour (image key-colour)
  (image-set-key-color image key-colour))


;;TCODLIB_API bool TCOD_image_is_pixel_transparent(TCOD_image_t image, int x,
;;  int y);



;;;;  (@> "Noise") ============================================================


(defctype noise :pointer)

;; 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 (dimensions &key (hurst +NOISE-DEFAULT-HURST+)
		  (lacunarity +NOISE-DEFAULT-LACUNARITY+)
		  (rng +NULL+))
  (%noise-new dimensions hurst lacunarity rng))


;; // basic perlin noise
;; TCODLIB_API float TCOD_noise_get( TCOD_noise_t noise, float *f );
(defcfun ("TCOD_noise_perlin" %noise-perlin) :float
  (noise noise) (f :pointer))

(defun noise-perlin (noise &rest nums)
  (with-foreign-object (f :float (length nums))
    (dotimes (i (length nums))
      (setf (mem-aref f :float i) (coerce (nth i nums) 'single-float)))
    (%noise-perlin noise f)))


;; simplex noise
(defcfun ("TCOD_noise_simplex" %noise-simplex) :float
  (noise noise) (f :pointer))

(defun noise-simplex (noise &rest nums)
  (with-foreign-object (f :float (length nums))
    (dotimes (i (length nums))
      (setf (mem-aref f :float i) (coerce (nth i nums) 'single-float)))
    (%noise-simplex noise f)))


;; // fractional brownian motion
;; TCODLIB_API float TCOD_noise_fbm( TCOD_noise_t noise, float *f,
;; float octaves );
(defcfun ("TCOD_noise_fbm_perlin" %noise-fbm-perlin) :float
  (noise noise) (f :pointer) (octaves :float))

(defun noise-fbm-perlin (noise octaves &rest nums)
  (with-foreign-object (f :float (length nums))
    (dotimes (i (length nums))
      (setf (mem-aref f :float i) (coerce (nth i nums) 'single-float)))
    (%noise-fbm-perlin noise f octaves)))

;; // turbulence
;; TCODLIB_API float TCOD_noise_turbulence( TCOD_noise_t noise,
;;   float *f, float octaves );
(defcfun ("TCOD_noise_turbulence_perlin" %noise-turbulence-perlin) :float
  (noise noise) (f :pointer) (octaves :float))

(defun noise-turbulence-perlin (noise octaves &rest nums)
  (with-foreign-object (f :float (length nums))
    (dotimes (i (length nums))
      (setf (mem-aref f :float i) (coerce (nth i nums) 'single-float)))
    (%noise-turbulence-perlin noise f octaves)))


;; TCODLIB_API void TCOD_noise_delete(TCOD_noise_t noise);
(defcfun ("TCOD_noise_delete" noise-delete) :void
  (noise noise))


;;;; (@> "Heightmap") =========================================================



(defctype heightmap :pointer)


(defcfun ("TCOD_heightmap_new" heightmap-new) heightmap
  (width :int) (height :int))


(defcfun ("TCOD_heightmap_get_value" heightmap-get-value) :float 
  ;; 0 <= x < WIDTH
  (heightmap heightmap) (x :int) (y :int))


(defcfun ("TCOD_heightmap_get_interpolated_value"
          heightmap-get-interpolated-value) :float 
  ;; 0 <= x < WIDTH
  (heightmap heightmap) (x :float) (y :float))


(defcfun ("TCOD_heightmap_get_slope" heightmap-get-slope) :float 
  (heightmap heightmap) (x :int) (y :int))


(defcfun ("TCOD_heightmap_set_value" heightmap-set-value) :void
  ;; 0 <= x < WIDTH
  (heightmap heightmap) (x :int) (y :int) (value :float))


(defcfun ("TCOD_heightmap_add" heightmap-add) :void
  (heightmap heightmap) (value :float))


(defcfun ("TCOD_heightmap_add_fbm" heightmap-add-fbm) :void
  (heightmap heightmap) (noise noise) (mulx :float) (muly :float)
  (addx :float) (addy :float) (octaves :float) (delta :float) (scale :float))


(defcfun ("TCOD_heightmap_scale" heightmap-scale) :void
  (heightmap heightmap) (factor :float))


(defcfun ("TCOD_heightmap_lerp_hm" heightmap-lerp) :void
  (hm1 heightmap) (hm2 heightmap) (result heightmap) (coef :float))


(defcfun ("TCOD_heightmap_add_hm" heightmap-add-hm) :void
  (hm1 heightmap) (hm2 heightmap) (result heightmap))


(defcfun ("TCOD_heightmap_multiply_hm" heightmap-multiply-hm) :void
  (hm1 heightmap) (hm2 heightmap) (result heightmap))


(defcfun ("TCOD_heightmap_clear" heightmap-clear) :void
  (heightmap heightmap))


(defcfun ("TCOD_heightmap_delete" heightmap-delete) :void
  (heightmap heightmap))


(defcfun ("TCOD_heightmap_clamp" heightmap-clamp) :void
  (heightmap heightmap) (min :float) (max :float))


(defcfun ("TCOD_heightmap_count_cells" heightmap-count-cells) :int
  (heightmap heightmap) (min :float) (max :float))


(defcfun ("TCOD_heightmap_has_land_on_border"
          heightmap-has-land-on-border?) :boolean
  (heightmap heightmap) (waterlevel :float))


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


(defun heightmap-get-min (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 (heightmap)
  (with-foreign-object (minf :float)
    (with-foreign-object (maxf :float)
      (%heightmap-get-minmax heightmap minf maxf)
      (mem-aref maxf :float))))



(defcfun ("TCOD_heightmap_normalize" heightmap-normalize) :void
  (heightmap heightmap) (min :float) (max :float))


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


(defcfun ("TCOD_heightmap_copy" heightmap-copy) :void
  (source heightmap) (dest heightmap))


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


(defun heightmap-rain-erosion (heightmap num-drops erosion sedimentation
                               &optional (rng +NULL+))
  (%heightmap-rain-erosion heightmap num-drops erosion sedimentation rng))


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


(defun heightmap-dig-bezier (heightmap coords start-radius start-depth
                             end-radius end-depth)
  (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 x1 y1 x2 y2 radius depth)
  (heightmap-dig-bezier heightmap `((,x1 . ,y1) (,x1 . ,y1) (,x2 . ,y2) (,x2 . ,y2))
                        radius depth radius depth))
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.