Menu

[r760]: / trunk / lispbuilder-sdl / sdl / point.lisp  Maximize  Restore  History

Download this file

83 lines (68 with data), 2.8 kB

 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
;; SDL (Simple Media Layer) library using CFFI for foreign function interfacing...
;; (C)2006 Justin Heyes-Jones <justinhj@gmail.com> and Luke Crook <luke@balooga.com>
;; Thanks to Frank Buss and Surendra Singh
;; see COPYING for license
;; This file contains some useful functions for using SDL from Common lisp
;; using sdl.lisp (the CFFI wrapper)
(in-package #:lispbuilder-sdl)
(defmacro with-point ((var &optional point)
&body body)
"A convenience macro that binds `\*DEFAULT-POINT\*` to `VAR` within the scope of `WITH-POINT`. `VAR` must be of type `POINT`.
If `POINT` is not `NIL`, then `VAR is set to `POINT`."
`(let* ((,@(if point
`(,var ,point)
`(,var ,var)))
(*default-point* ,var))
(symbol-macrolet ((,(intern (string-upcase (format nil "~A.x" var))) (x ,var))
(,(intern (string-upcase (format nil "~A.y" var))) (y ,var)))
,@body)))
(deftype point ()
`vector)
(defun point (&key (x 0) (y 0))
"Creates a new `POINT` set to the specified horizontal `X` and vertical `Y` coordinate."
(multiple-value-bind (int-x int-y)
(cast-all-to-int x y)
(vector int-x int-y)))
(defun copy-point (point)
"Returns a copy of the point `POINT`."
(copy-seq point))
(defmethod x ((point vector))
"Returns the `X` coordindate of the point `POINT` as an `INTEGER`."
(elt point 0))
(defmethod (setf x) (x-val (point vector))
"Sets the `X` coordindate of the point `POINT`."
(setf (elt point 0) x-val))
(defmethod y ((point vector))
"Returns the `Y` coordindate of the point `POINT` as an `INTEGER`."
(elt point 1))
(defmethod (setf y) (y-val (point vector))
"Sets the `X` coordindate of the point `POINT`."
(setf (elt point 1) y-val))
(defmethod point-* ((point vector))
"Returns the `X` and `Y` coordinates of the point `POINT` as a spread."
(values (x point) (y point)))
(defmethod get-point ((point vector))
"Returns the point `POINT`."
point)
(defmethod set-point ((dst vector) (src vector))
"Sets the `X` and `Y` coordinates of the destination point `DST` to the coordinates in
the source point `SRC`. Returns the destination point `DST`."
(set-point-* dst :x (x src) :y (y src))
dst)
(defmethod set-point-* ((point vector) &key x y)
"Sets the `X` and `Y` coordinates of the point `POINT` to `X` and `Y`."
(when x (setf (x point) x))
(when y (setf (y point) y))
point)
(defmethod position-* ((point vector))
"See [POINT-*](#point-*)."
(values (x point) (y point)))
(defmethod set-position ((dst vector) (src vector))
"See [SET-POINT](#set-point)."
(set-point-* dst :x (x src) :y (y src))
dst)
(defmethod set-position-* ((point vector) &key x y)
"See [SET-POINT-*](#set-point-*)."
(when x (setf (x point) x))
(when y (setf (y point) y))
point)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.