-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.lisp
1766 lines (1470 loc) · 59.8 KB
/
console.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
;;; console.lisp --- core operations for XE2
;; Copyright (C) 2006, 2007, 2008, 2009, 2010 David O'Toole
;; Author: David O'Toole <[email protected]> <[email protected]>
;; Keywords: multimedia, games
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; The "console" is the library which provides all XE2 system
;; services. Primitive operations such as setting the resolution,
;; displaying bitmaps, drawing lines, playing sounds, file access, and
;; keyboard/mouse input are handled here.
;; Currently it uses the cross-platform SDL library (via
;; LISPBUILDER-SDL) as its device driver, and wraps the library for
;; use by the rest of XE2.
;; http://lispbuilder.sourceforge.net/
(in-package :xe2)
;;; Platform detection
(defvar *windows* nil)
(defvar *linux* nil)
(defvar *osx* nil)
;;; Counting timesteps
(defparameter *timesteps* 0)
;;; Keyboard state
(defun keyboard-id (key)
"Look up the SDL symbol corresponding to the XE2 symbol KEY. See keys.lisp."
(let* ((entry (find key *key-identifiers* :key #'first))
(entry2 (find (second entry) *sdl-key-identifiers* :key #'second)))
(first entry2)))
(defun keyboard-mod (mod)
"Look up the SDL symbol corresponding to the XE2 symbol MOD. See keys.lisp."
(let* ((entry (find mod *key-modifiers* :key #'first))
(entry2 (find (second entry) *sdl-key-modifiers* :key #'second)))
(first entry2)))
(defun keyboard-held-p (key)
"Returns the duration in seconds that the key has been depressed over a number of game loops."
(sdl:key-held-p (keyboard-id key)))
(defun keyboard-pressed-p (key)
"Returns t if the key has just been depressed in the current game loop."
(sdl:key-pressed-p (keyboard-id key)))
(defun keyboard-released-p (key)
"Returns t if the key has just been released in the current game loop."
(sdl:key-released-p (keyboard-id key)))
(defun keyboard-time-in-current-state (key)
"Returns the duration in seconds that key is either pressed or depressed."
(sdl:key-time-in-current-state (keyboard-id key)))
(defun keyboard-time-in-previous-state (key)
"Returns the duration in seconds that key was in its previous state either pressed or depressed."
(sdl:key-time-in-previous-state (keyboard-id key)))
(defun keyboard-down-p (key)
"Returns t if the key is depressed."
(sdl:key-down-p (keyboard-id key)))
(defun keyboard-modifier-down-p (mod)
"Returns t if the modifier key is depressed."
(sdl:mod-down-p (keyboard-mod mod)))
;; see also keys.lisp
(defun keyboard-keys-down ()
"Returns a list of the keys that are depressed."
(labels ((translate (key)
(let ((entry (find key *sdl-key-identifiers* :key #'first)))
(let ((entry2 (find (second entry) *key-identifiers* :key #'second)))
(first entry2)))))
(mapcar #'translate (sdl:keys-down-p))))
(defun keyboard-modifiers ()
"Returns a list of the modifier keys that are depressed."
(labels ((translate (mod)
(let ((entry (find mod *sdl-key-modifiers* :key #'first)))
(let ((entry2 (find (second entry) *key-modifiers* :key #'second)))
(first entry2)))))
(mapcar #'translate (sdl:mods-down-p))))
;; Processing once per timestep
(defvar *keys* nil "List of keywords of currently pressed keys.")
(defvar *mods* nil "List of keywords of currently pressed modifiers.")
(defvar *keyboard-timestep-number* 0)
(defun get-keys ()
(if (/= *keyboard-timestep-number* *timesteps*)
(setf *keys* (keyboard-keys-down)
*mods* (keyboard-modifiers)
*keyboard-timestep-number* *timesteps*)
(setf *keys* nil *mods* nil))
(values *keys* *mods*))
;;; Message logging
(defparameter *message-logging* nil)
(defun message (format-string &rest args)
"Print a log message to the standard output. The FORMAT-STRING and
remaining arguments are passed to `format'.
When the variable `*message-logging*' is nil, this output is
disabled."
(when *message-logging*
(apply #'format t format-string args)
(fresh-line)))
;;; Sequence numbers
(defvar *sequence-number* 0)
(defun genseq (&optional (x 0))
"Generate an all-purpose sequence number."
(+ x (incf *sequence-number*)))
;;; Hooks
(defun add-hook (hook func)
"Hooks are special variables whose names are of the form
`*foo-hook*' and whose values are lists of functions taking no
arguments. The functions of a given hook are all invoked (in list
order) whenever the hook is run with `run-hook'.
This function arranges for FUNC to be invoked whenever HOOK is triggered with
`run-hook'. The function should have no arguments."
(pushnew func (symbol-value hook)))
(defun remove-hook (hook func)
"Stop calling FUNC whenever HOOK is triggered."
(setf (symbol-value hook)
(delete func (symbol-value hook))))
(defun run-hook (hook)
"Call all the functions in HOOK, in list order."
(dolist (func (symbol-value hook))
(funcall func)))
;;; Vector utility macro
(defmacro do-cells ((var expr) &body body)
"Execute the forms in BODY with VAR bound successively to the
elements of the vector produced by evaluating EXPR."
(let ((counter (gensym))
(vector (gensym)))
`(progn
(let* ((,var nil)
(,vector (progn ,expr)))
(when (vectorp ,vector)
(let ((,counter (fill-pointer ,vector)))
(decf ,counter)
(loop while (>= ,counter 0)
do (setf ,var (aref ,vector ,counter))
(progn (decf ,counter)
(when ,var ,@body)))))))))
;;; The active widgets list
(defvar *active-widgets* nil "List of active widget objects.
These widgets receive input events and are rendered to the screen by
the console. See also `send-event-to-widgets'.
Do not set this variable directly from a module; instead, call
`install-widgets'.")
(defun show-widgets ()
"Draw the active widgets to the screen."
(dolist (widget *active-widgets*)
(with-field-values (image x y visible) widget
(when (and image visible)
[render widget]
(sdl:draw-surface-at-* image x y)))))
(defvar *module-widgets* nil "List of widget objects in the current module.")
(defun install-widgets (&rest widgets)
"User-level function for setting the active widget set. Note that
XE2 may override the current widget set at any time for system menus
and the like."
(setf *module-widgets* widgets)
(setf *active-widgets* widgets))
;; TODO why does this crash:
;; (show-widgets))
(defun install-widget (widget)
(unless (find widget *module-widgets*)
(setf *module-widgets* (append *module-widgets* (list widget))))
(unless (find widget *active-widgets*)
(setf *active-widgets* (append *active-widgets* (list widget))))
(show-widgets))
(defun uninstall-widget (widget)
(setf *module-widgets* (delete widget *module-widgets* :test #'eq))
(setf *active-widgets* (delete widget *active-widgets* :test #'eq))
(show-widgets))
;;; "Classic" key repeat
(defun enable-classic-key-repeat (delay interval)
;; (let ((delay-milliseconds (truncate (* delay (/ 1000.0 *frame-rate*))))
;; (interval-milliseconds (truncate (* interval (/ 1000.0 *frame-rate*)))))
(sdl:enable-key-repeat delay interval))
(defun disable-classic-key-repeat ()
(sdl:disable-key-repeat))
;;; "Held Keys" key repeat emulation
(defvar *key-table* (make-hash-table :test 'equal))
(defvar *held-keys* nil)
(defun enable-held-keys (&rest args)
"Enable key repeat on every frame when held. Arguments are ignored
for backward-compatibility."
(when args
(message "Warning. DELAY and INTERVAL arguments to XE2:ENABLE-HELD-KEYS are deprecated and ignored."))
(setf *key-table* (make-hash-table :test 'equal))
(setf *held-keys* t))
(defun disable-held-keys ()
"Disable key repeat."
(setf *held-keys* nil))
;; (sdl:disable-key-repeat))
(defun hold-event (event)
(when (null (gethash event *key-table*))
(setf (gethash event *key-table*) 0)))
(defun release-held-event (event)
(setf (gethash event *key-table*) -1))
(defun send-held-events ()
(unless (null *key-table*)
(maphash #'(lambda (event counter)
(dispatch-event event)
;; A counter value of -1 means that the key release
;; happened before the event had a chance to be sent.
;; These must be removed immediately after sending once.
(if (minusp counter)
(remhash event *key-table*)
;; Otherwise, keep counting how many frames the
;; key is held for.
(when (numberp (gethash event *key-table*))
(incf (gethash event *key-table*)))))
*key-table*)))
(defun break-events (event)
(labels ((break-it (event2 ignore)
(when (intersection event event2 :test 'equal)
(message "Breaking ~S due to match with ~S." event2 event)
(remhash event2 *key-table*))))
(maphash #'break-it *key-table*)))
;;; Physics timestep callback
(defvar *dt* 10)
(defvar *physics-function* nil)
(defun do-physics (&rest args)
(incf *timesteps*)
(when (functionp *physics-function*)
(apply *physics-function* args)))
;;; Event handling and widgets
(defun send-event-to-widgets (event)
"Keyboard, mouse, joystick, and timer events are represented as
event lists of the form:
: (STRING . MODIFIERS)
Where MODIFIERS is a list of symbols like :shift, :control, :alt,
:timer, :system, :mouse, and so on.
The default event handler attempts to deliver a keypress to one of
the widgets in `*active-widgets*'. See widgets.lisp and the docstrings
below for more information.
This function attempts to deliver EVENT to each of the *active-widgets*
one at a time (in list order) until one of them is found to have a
matching keybinding, in which case the keybinding's corresponding
function is triggered. If none of the widgets have a matching
keybinding, nothing happens, and this function returns nil."
(some #'(lambda (widget)
[handle-key widget event])
*active-widgets*))
(defvar *event-handler-function* #'send-event-to-widgets
"Function to be called with keypress events. This function should
accept an event list of the form
(STRING . MODIFIERS)
where STRING is a string representing the key, and MODIFIERS is a list
of key modifier symbols like :shift, :control, :alt, and so on.
The modifier list is sorted; thus, events can be compared for
equality with `equal' and used as hashtable keys.
The default event handler is `send-event-to-widgets', which see. An
XE2 game can use the widget framework to do its drawing and event
handling, or override `*event-handler-function*' and do something
else.")
(defun normalize-event (event)
"Convert EVENT to a normal form suitable for `equal' comparisons."
(setf (rest event)
(sort (remove-duplicates (delete nil (rest event)))
#'string< :key #'symbol-name))
event)
(defun dispatch-event (event)
"Send EVENT to the handler function."
(if *event-handler-function*
(progn (message "TRANSLATED EVENT: ~A" event)
(funcall *event-handler-function* event))
(error "No event handler registered.")))
(defun hit-widgets (x y &optional (widgets *active-widgets*))
"Hit test the WIDGETS to find the clicked widget."
(some #'(lambda (widget)
[hit widget x y])
(reverse widgets)))
;;; Translating SDL key events into XE2 event lists
(defparameter *other-modifier-symbols* '(:button-down :button-up :axis))
(defun make-key-modifier-symbol (sdl-mod)
"Translate from the SDL key modifier symbol SDL-MOD to our own
key event symbols."
(if (or (member sdl-mod *joystick-button-symbols*)
(member sdl-mod *other-modifier-symbols*))
sdl-mod
(case sdl-mod
(:SDL-KEY-MOD-NONE nil)
(:SDL-KEY-MOD-LSHIFT :shift)
(:SDL-KEY-MOD-RSHIFT :shift)
(:SDL-KEY-MOD-LCTRL :control)
(:SDL-KEY-MOD-RCTRL :control)
(:SDL-KEY-MOD-LALT :alt)
(:SDL-KEY-MOD-RALT :alt)
(:SDL-KEY-MOD-LMETA :meta)
(:SDL-KEY-MOD-RMETA :meta)
;; for compatibility:
(:SDL-KEY-NONE nil)
(:SDL-KEY-LSHIFT :shift)
(:SDL-KEY-RSHIFT :shift)
(:SDL-KEY-LCTRL :control)
(:SDL-KEY-RCTRL :control)
(:SDL-KEY-LALT :alt)
(:SDL-KEY-RALT :alt)
(:SDL-KEY-LMETA :meta)
(:SDL-KEY-RMETA :meta)
;; fix for windows
(:SDL-KEY-MOD-NUM nil)
(:SDL-KEY-CAPS :caps-lock)
(:SDL-KEY-MOD-CAPS :caps-lock) ;; macintosh
(:SDL-KEY-MODE nil)
(:SDL-KEY-MOD-MODE :mode)
(:SDL-KEY-RESERVED nil)
)))
(defun make-key-string (sdl-key)
"Translate from :SDL-KEY-X to the string \"X\"."
(let ((prefix "SDL-KEY-"))
(subseq (symbol-name sdl-key)
(length prefix))))
(defun make-event (sdl-key sdl-mods)
"Create a normalized event out of the SDL data SDL-KEY and SDL-MODS.
The purpose of putting events in a normal form is to enable their use
as hash keys."
(message "SDL KEY AND MODS: ~A" (list sdl-key sdl-mods))
(normalize-event
(cons (if (eq sdl-key :joystick)
"JOYSTICK"
(if (eq sdl-key :axis)
"AXIS"
(make-key-string sdl-key)))
(mapcar #'make-key-modifier-symbol
(cond ((keywordp sdl-mods)
(list sdl-mods))
((listp sdl-mods)
sdl-mods)
;; catch apparent lispbuilder-sdl bug?
((eql 0 sdl-mods)
nil))))))
;;; Joystick support (gamepad probably required)
(defparameter *joystick-button-symbols*
'(:button-0 :button-1 :button-2 :button-3 :button-4 :button-5 :button-6 :button-7 :button-8 :button-9
:button-10 :button-11 :button-12 :button-13 :button-14 :button-15 :button-16 :button-17 :button-18 :button-19
:left :right :up :down :select :start))
(defparameter *generic-joystick-mapping*
'((0 . :button-0)
(1 . :button-1)
(2 . :button-2)
(3 . :button-3)
(4 . :button-4)
(5 . :button-5)
(6 . :button-6)
(7 . :button-7)
(8 . :button-8)
(9 . :button-9)
(10 . :button-10)
(11 . :button-11)
(12 . :button-12)
(13 . :button-13)
(14 . :button-14)
(15 . :button-15)
(16 . :button-16)
(17 . :button-17)
(18 . :button-18)
(19 . :button-19)
(20 . :button-20)))
(defvar *joystick-dead-zone* 2000)
(defvar *joystick-axis-mapping* '((0 :left :right)
(1 :up :down)))
(defun axis-value-to-direction (axis value)
(let ((entry (assoc axis *joystick-axis-mapping*)))
(if entry
(if (plusp value)
(second (cdr entry))
(when (minusp value)
(first (cdr entry)))))))
(defvar *joystick-axis-values* (make-array 100 :initial-element 0))
(defun do-joystick-axis-event (axis value state)
(dispatch-event (make-event :axis
(list (axis-value-to-direction axis value)
state))))
(defun update-joystick-axis (axis value)
(let ((state (if (< (abs value) *joystick-dead-zone*)
:button-up :button-down)))
(setf (aref *joystick-axis-values* axis) value)))
(defun poll-joystick-axis (axis)
(aref *joystick-axis-values* axis))
(defvar *joystick-mapping* *generic-joystick-mapping*)
(defun translate-joystick-button (button)
(cdr (assoc button *joystick-mapping*)))
(defun symbol-to-button (sym)
(let ((entry (some #'(lambda (entry)
(when (eq sym (cdr entry))
entry))
*joystick-mapping*)))
(when entry
(car entry))))
(defvar *joystick-device* nil)
(defvar *joystick-buttons* nil
"The nth element is non-nil when the nth button is pressed.")
(defvar *joystick-position* nil "Current position of the joystick, as a direction keyword.")
(defun reset-joystick ()
"Re-open the joystick device and re-initialize the state."
(setf *joystick-device* (sdl-cffi::sdl-joystick-open 0))
(setf *joystick-buttons* (make-array 100 :initial-element nil))
(setf *joystick-position* :here))
(defun update-joystick (button state)
"Update the table in `*joystick-buttons*' to reflect the STATE of
the BUTTON. STATE should be either 1 (on) or 0 (off)."
(setf (aref *joystick-buttons* button) (ecase state
(1 t)
(0 nil)))
(let ((sym (translate-joystick-button button)))
(labels ((pressed (sym)
(let ((b (symbol-to-button sym)))
(when (integerp b)
(aref *joystick-buttons* b)))))
(setf *joystick-position*
(or (cond ((and (pressed :up) (pressed :right))
:northeast)
((and (pressed :up) (pressed :left))
:northwest)
((and (pressed :down) (pressed :right))
:southeast)
((and (pressed :down) (pressed :left))
:southwest)
((pressed :up)
:north)
((pressed :down)
:south)
((pressed :right)
:east)
((pressed :left)
:west))
:here)))
(message "UPDATE-JOYSTICK: BUTTON(~S) STATE(~S)" button state)))
(defun poll-joystick-button (button)
"Return 1 if the button numbered BUTTON is pressed, otherwise 0."
(sdl-cffi::sdl-joystick-get-button *joystick-device* button))
(defun poll-all-buttons ()
(dolist (entry *joystick-mapping*)
(destructuring-bind (button . symbol) entry
(update-joystick button (poll-joystick-button button)))))
(defun generate-button-events ()
(let ((button 0) state sym)
(loop while (< button (length *joystick-buttons*))
do (setf state (aref *joystick-buttons* button))
(setf sym (translate-joystick-button button))
(when (and state sym)
(dispatch-event (make-event :joystick sym)))
(incf button))))
;;; The active world
(defvar *world* nil
"The current world object. Only one may be active at a time. See also
worlds.lisp. Cells are free to send messages to `*world*' at
any time, because it is always bound to the world containing the cell
at the time the cell method is run.")
(defun world ()
"Return the current world."
*world*)
;;; Auto-zooming images
(defvar *zoom-factor* 1
"When set to some integer greater than 1, all image resources are
scaled by that factor unless marked with the property :nozoom t.")
(defun is-zoomed-resource (resource)
"Return non-nil if the RESOURCE should be zoomed by `*zoom-factor*'."
(not (getf (resource-properties resource)
:nozoom)))
(defun zoom-image (image &optional (factor *zoom-factor*))
"Return a zoomed version of IMAGE, zoomed by FACTOR.
Allocates a new image."
(assert (integerp factor))
(lispbuilder-sdl-gfx:zoom-surface factor factor
:surface image
:smooth nil))
;;; Timing
(defvar *frame-rate* 30
"The intended frame rate of the game. Recommended value is 30.")
(defun set-frame-rate (&optional (rate *frame-rate*))
"Set the SDL frame rate for the game."
(message "Setting frame rate to ~S" rate)
(setf (sdl:frame-rate) rate))
(defvar *clock* 0 "Number of frames until next timer event.")
(defvar *timer-p* nil "Non-nil if timer events are actually being sent.")
(defun enable-timer ()
"Enable timer events. The next scheduled event will be the first sent."
(setf *timer-p* t))
(defun disable-timer ()
"Disable timer events."
(setf *timer-p* nil))
(defvar *timer-event* (list nil :timer)
"Since all timer events are identical, this is the only one we need.")
(defvar *timer-interval* 15
"Number of frames to wait before sending each timer event.
Set this to 0 to get a timer event every frame.
Don't set this yourself; use `set-timer-interval'.")
(defun set-timer-interval (interval)
"Set the number of frames to wait before sending each timer event.
Set it to 0 to get a timer event every frame."
(setf *timer-interval* interval))
;;; Screen dimensions
(defvar *resizable* nil)
(defparameter *resize-hook* nil)
(defvar *screen-width* 640 "The width (in pixels) of the game
window. Set this in the game startup file.")
(defun set-screen-width (width)
(setf *screen-width* width))
(defvar *screen-height* 480 "The height (in pixels) of the game
window. Set this in the game startup file.")
(defun set-screen-height (height)
(setf *screen-height* height))
;;; The main loop of XE2
(defvar *next-module* "standard")
(defvar *quitting* nil)
(defvar *fullscreen* nil "When non-nil, attempt to use fullscreen mode.")
(defvar *window-title* "XE2")
(defvar *window-position* :center
"Controls the position of the game window. Either a list of coordinates or the symbol :center.")
(defun run-main-loop ()
"Initialize the console, open a window, and play.
We want to process all inputs, update the game state, then update the
display."
(let ((fps (make-instance 'sdl:fps-mixed :dt *dt*)))
(cond (*fullscreen*
(sdl:window *screen-width* *screen-height*
:fps fps
:title-caption *window-title*
:flags sdl:SDL-FULLSCREEN
:position *window-position*))
(*resizable*
(sdl:window *screen-width* *screen-height*
:fps fps
:title-caption *window-title*
:flags sdl:SDL-RESIZABLE
:position *window-position*))
(t (sdl:window *screen-width* *screen-height*
:fps fps
:title-caption *window-title*
:position *window-position*)))
(set-frame-rate *frame-rate*)
(reset-joystick)
(sdl:clear-display sdl:*black*)
(show-widgets)
(sdl:update-display)
(run-hook '*initialization-hook*)
(let ((events-this-frame 0))
(sdl:with-events ()
(:quit-event () (prog1 t))
(:video-resize-event (:w w :h h)
(setf *screen-width* w
*screen-height* h)
(run-hook '*resize-hook*)
(sdl:window w h :fps fps :title-caption *window-title*
:flags sdl:SDL-RESIZABLE
:position *window-position*))
(:mouse-motion-event (:state state :x x :y y :x-rel x-rel :y-rel y-rel)
nil)
(:mouse-button-down-event (:button button :state state :x x :y y)
(let ((object (hit-widgets x y *active-widgets*)))
(cond ((null object)
(message ""))
((eq t object)
nil)
(t
;; deliver messages in a queued environment
(sdl:clear-display sdl:*black*)
(when *world*
(when (field-value :message-queue *world*)
(with-message-queue (field-value :message-queue *world*)
(case button
(1 (when (has-method :select object)
[select object]))
(3 (when (has-method :activate object)
[activate object]))))
[process-messages *world*]))))))
(:mouse-button-up-event (:button button :state state :x x :y y)
nil)
(:joy-button-down-event (:which which :button button :state state)
(when (assoc button *joystick-mapping*)
(update-joystick button state)
(dispatch-event (make-event :joystick
(list (translate-joystick-button button)
:button-down)))))
(:joy-button-up-event (:which which :button button :state state)
(when (assoc button *joystick-mapping*)
(update-joystick button state)
(dispatch-event (make-event :joystick
(list (translate-joystick-button button)
:button-up)))))
(:joy-axis-motion-event (:which which :axis axis :value value)
(update-joystick-axis axis value))
(:video-expose-event () (sdl:update-display))
(:key-down-event (:key key :mod-key mod)
(let ((event (make-event key mod)))
(if *held-keys*
(hold-event event)
(dispatch-event event))))
(:key-up-event (:key key :mod-key mod)
(when *held-keys*
(let* ((event (make-event key mod))
(entry (gethash event *key-table*)))
(if (numberp entry)
(if (plusp entry)
(progn (message "Removing entry ~A:~A" event entry)
(remhash event *key-table*))
(when (zerop entry)
;; This event hasn't yet been sent,
;; but the key release happened
;; now. Mark this entry as pending
;; deletion.
(release-held-event event)))
(break-events event)))))
(:idle ()
(if *timer-p*
(if (zerop *clock*)
(progn
(sdl:clear-display sdl:*black*)
;; send held events
(when *held-keys*
(send-held-events))
;; send timer event
(dispatch-event *timer-event*)
;; send any joystick button events
;; (poll-all-buttons)
;; (generate-button-events)
;; update display
(show-widgets)
(sdl:update-display)
(setf *clock* *timer-interval*))
(decf *clock*))
;; clean this up. these two cases aren't that different.
(progn
(sdl:with-timestep ()
(do-physics))
(sdl:clear-display sdl:*black*)
(when *held-keys* (send-held-events)) ;; TODO move this to do-physics?
(show-widgets)
(sdl:update-display))))))))
;;; The .xe2rc user init file
(defparameter *user-init-file-name* ".xe2rc")
(defvar *initialization-hook* nil
"This hook is run after the XE2 console is initialized.
Set timer parameters and other settings here.")
(defun load-user-init-file ()
(let ((file (merge-pathnames (make-pathname :name *user-init-file-name*)
(user-homedir-pathname))))
(when (probe-file file)
(load (merge-pathnames (make-pathname :name *user-init-file-name*)
(user-homedir-pathname))))))
(defparameter *user-keyboard-layout* :qwerty)
(defparameter *use-sound* t "Non-nil (the default) is to use sound. Nil disables sound.")
;;; PAK resource interchange files
(defparameter *pak-file-extension* ".pak"
"PAK is a simple Lisp data interchange file format readable and
writable by both Emacs Lisp and Common Lisp. A PAK file can contain
one or more data resources. A 'resource' is an image, sound, text,
font, lisp program, or other data whose interpretation is up to the
client.
A PAK resource can be either self-contained, or point to an
external file for its data.
A 'resource record' defines a resource. A resource record is a
structure with the following elements:
:NAME A string; the name of the resource.
The colon character : is reserved and used to specify
resource transformations; see below.
:TYPE A keyword symbol identifying the data type.
Corresponding handlers are the responsibility of the client.
See also `*resource-handlers*' and `load-resource'.
The special type :pak is used to load the pak file
specified in :FILE, from (optionally) another module
whose name is given in :DATA.
The special type :alias is used to provide multiple names
for a resource. The :DATA field contains the name of the
target resource. This name can specify resource
transformations, see below.
:PROPERTIES Property list with extra data; for example :copyright,
:license, :author.
The special property :AUTOLOAD, when non-nil causes
the resource to be loaded automatically upon startup
(the default is to load resources on demand.)
:FILE Name of file to load data from, if any.
Relative to directory of PAK file.
:DATA Lisp data encoding the resource itself, if any.
In memory, these will be represented by resource structs (see below).
On disk, it's Lisp data printed as text. This text will compress very
well.
The string '()' is a valid .PAK file; it contains no resources.")
(defstruct resource
name type properties file data object modified-p)
;; The extra `object' field is not saved in .PAK files; it is used to
;; store driver-dependent loaded resources (i.e. SDL image surface
;; objects and so on). This is used in the resource table.
;; The modified-p field is likewise not stored.
(defun resource-to-plist (res)
"Convert the resource record RES into a property list.
This prepares it for printing as part of a PAK file."
(list :name (resource-name res)
:type (resource-type res)
:properties (resource-properties res)
:file (resource-file res)
:data (resource-data res)
:object nil))
;; First we need routines to read and write raw s-expressions to and
;; from text files.
(defconstant *keyword-package* (find-package :keyword))
(defun write-sexp-to-file (filename sexp)
(message "Writing data to file ~S" filename)
(with-open-file (file filename :direction :output
:if-exists :overwrite
:if-does-not-exist :create)
(let ((*package* *keyword-package*))
(format file "~S" sexp)))
(message "Writing data to file ~S... Done." filename))
(defun read-sexp-from-file (filename)
(message "Reading data from ~A..." filename)
(with-open-file (file filename :direction :input)
(prog1 (read file)
(message "Reading data from ~A... Done." filename))))
;; Now tie it all together with routines that read and write
;; collections of records into PAK files.
(defun write-pak (filename resources)
"Write the RESOURCES to the PAK file FILENAME."
(write-sexp-to-file filename (mapcar #'resource-to-plist resources)))
(defun read-pak (filename)
"Return a list of resources from the PAK file FILENAME."
(mapcar #'(lambda (plist)
(apply #'make-resource plist))
(read-sexp-from-file filename)))
;;; Resources and modules
(defvar *resource-table* nil
"A hash table mapping resource names to resource records. All loaded
resources go in this one hash table.
The `resource table' maps resource names to their corresponding
records. `Indexing' a resource means that its resource record is
added to the resource table. `Loading' a resource means that any
associated driver-dependent object (SDL image surface, audio buffer
object, etc) is created. This value is stored into the OBJECT field
of the resource record upon loading; see `load-resource'.
The loading operation may be driver-dependent, so each resource
type (i.e. :image, :text, :sound) is handled by its own plugin
function (see `*resource-handlers*').
`Finding' a resource means looking up its record in the resource
table, and loading the resource if it hasn't been loaded already.
A lookup failure results in an error. See `find-resource'.
A `module' is a directory full of resource files. The name of the
module is the name of the directory. Each module must contain a
file called {module-name}.pak, which should contain an index of
all the module's resources. Multiple modules may be loaded at one
time. In addition the special resource .startup will be loaded;
if this is type :lisp, the startup code for your game can go in
that external lisp file.")
(defun initialize-resource-table ()
"Create a new empty resource table."
(setf *resource-table* (make-hash-table :test 'equal)))
(defun index-resource (resource)
"Add the RESOURCE's record to the resource table.
If a record with that name already exists, it is replaced. However,
if the resource is an :alias, just the string name of the target
resource is stored; see also `find-resource'."
(let ((val (if (eq :alias (resource-type resource))
(resource-data resource)
resource)))
(setf (gethash (resource-name resource)
*resource-table*)
val)))
(defvar *executable* nil)
(defvar *module-directories*
(list (if *executable*
(make-pathname :directory (pathname-directory (car #+sbcl sb-ext:*posix-argv*
#+clozure ccl:*command-line-argument-list*)))
(make-pathname :directory
(pathname-directory
(make-pathname
:host (pathname-host #.(or *compile-file-truename*
*load-truename*))
:device (pathname-device #.(or *compile-file-truename*
*load-truename*))
:directory (pathname-directory #.(or *compile-file-truename*
*load-truename*)))))))
"List of directories where XE2 will search for modules.
Directories are searched in list order.")
;; (load-time-value
;; (or #.*compile-file-truename* *load-truename*))))
(defun find-module-path (module-name)
"Search the `*module-directories*' path for a directory with the
name MODULE-NAME. Returns the pathname if found, otherwise nil."
(let ((dirs *module-directories*))
(message "Probing directories ~S..." dirs)
(or
(loop
for dir in dirs for path
= (probe-file (make-pathname :directory
(append (pathname-directory
dir) (list module-name))
:defaults dir))
when path return path)
(error "Cannot find module ~s in paths ~S.
You must set the variable XE2:*MODULE-DIRECTORIES* in the configuration file ~~/.xe2rc
Please see the included file BINARY-README for instructions."
module-name dirs))))
(defun find-module-file (module-name file)
"Make a pathname for FILE within the module MODULE-NAME."
(merge-pathnames file (find-module-path module-name)))
(defun directory-is-module-p (dir)
"Test whether a PAK index file exists in a directory."
(let ((index-filename (concatenate 'string
(file-namestring dir)
*pak-file-extension*)))
(probe-file (make-pathname :name index-filename
:directory (if (stringp dir)
dir
(namestring dir))))))
(defun find-modules-in-directory (dir)
"Search DIR for modules and return a list of their names."
(let ((dirnames (mapcar #'(lambda (s)
(subseq s 0 (1- (length s))))
(mapcar #'namestring
(directory (concatenate 'string (namestring dir) "/*/"))))))
(remove-if-not #'directory-is-module-p dirnames)))
(defun find-all-modules ()
(mapcar #'file-namestring
(mapcan #'find-modules-in-directory *module-directories*)))
(defvar *pending-autoload-resources* '())