-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcod.lisp
3764 lines (2962 loc) · 128 KB
/
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
;;;; -*- Mode: lisp; coding: utf-8-unix -*- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;80
(in-package :cl-user)
;;;; The documentation for this package is generated using the CLOD library.
;;;;
;;;; The following command is used: (tcod and clod systems must be
;;;; loaded into the running lisp image first)
;;;;
#+nil
(clod:document-package :tcod "tcod.org"
:title "CL-TCOD"
:internal-symbols? nil
:brief-methods t
:author "Paul Sexton"
:email "[email protected]")
;;;
;;; 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->keyword
#:color->keyword
#: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-initialised?
#: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-lmeta
#:key-rmeta
#:key-shift
#:make-key
#:make-simple-key
#:same-keys?
#:key-state
#:key-press
#:key-pressed
#:is-key-pressed?
#:console-set-keyboard-repeat
#:console-disable-keyboard-repeat
;; [[Mouse]] ===============================================================
#:mouse
#:mouse-state
#: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-check-for-event
#:sys-wait-for-event
#:sys-get-events
#:sys-wait-events
#:sys-clipboard-set ;;
#:sys-clipboard-get ;;
#:sys-flush
;; [[SDL]] =================================================================
#:sdl-get-mouse-state
;; [[Testing]] =============================================================
#:hello-world
)
(: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]] (recommended) 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 and libSDL
dynamically linked libraries (=.DLL= or =.SO= files) are somewhere your lisp
system can find them. They probably are, but if CFFI complains about being unable
to find the libraries, you can either copy them to an appropriate directory or
add their 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 (recommended):
;;; (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 :title \"Test\")
;;; (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, so programs won't run
as fast as in the compiled lisps discussed above. However it runs pretty much
everywhere, and is 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 doubt
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))
;;;; <<Libraries>> ============================================================
#+darwin
(dolist (path '(#p"/opt/local/lib/" #p"/usr/local/lib/"))
(pushnew path cffi:*foreign-library-directories*))
(define-foreign-library libtcod
(:darwin "libtcod.dylib")
(:windows #-libtcod-debug "libtcod-mingw.dll"
#+libtcod-debug "libtcod-mingw-debug.dll")
(:unix #+libtcod-debug "libtcod-debug.so"
#-libtcod-debug "libtcod.so")
(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.")
(defvar *root-console-initialised?* nil
"Set to T once `console-init-root' has been called.")
(eval-when (:load-toplevel :execute)
(unless *libtcod-loaded*
(use-foreign-library libtcod)
(setf *libtcod-loaded* t)))
;;; We need direct access to SDL because libtcod 1.5.1rc1 does not report
;;; mouse buttons correctly (or at least, reading them via CFFI gives
;;; strange, random results.)
(define-foreign-library libsdl2
(:darwin "libSDL2.dylib")
(:unix "libSDL2.so")
(:windows "SDL2.dll")
(t (:default "libsdl2")))
(defvar *libsdl2-loaded* nil)
(eval-when (:load-toplevel :execute)
(unless *libsdl2-loaded*
(use-foreign-library libsdl2)
(setf *libsdl2-loaded* t)))
;; Returns an 8-bit integer.
;; bit 1: lbutton
;; bit 2: mbutton
;; bit 3: rbutton
;; The arguments xptr and yptr can be null pointers.
(defcfun ("SDL_GetMouseState" sdl-get-mouse-state) :int
(xptr :pointer) (yptr :pointer))
;;;; <<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 ,(if (listp name) (car name) 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 '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)
(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 int () `(signed-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-struct
(r :uint8)
(g :uint8)
(b :uint8))
;; TCOD_renderer_t (enum)
(define-c-enum renderer
:RENDERER-GLSL
:RENDERER-OPENGL
:RENDERER-SDL)
;; TCOD_keycode_t (enum)