Menu

[r760]: / trunk / lispbuilder-sdl-gfx / examples / objects.lisp  Maximize  Restore  History

Download this file

84 lines (69 with data), 3.1 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
83
;;;;; Converted from the "Objects" Processing example at:
;;;;; "http://www.processing.org/learning/examples/objects.html"
;;;;; (C)2006 Luke J Crook
(in-package #:sdl-gfx-examples)
(defvar *width* 200)
(defvar *height* 200)
(defclass m-rect ()
((w :accessor w :initform 0 :initarg :w)
(xpos :accessor xpos :initform 0 :initarg :xpos)
(h :accessor h :initform 0 :initarg :h)
(ypos :accessor ypos :initform 0 :initarg :ypos)
(d :accessor d :initform 0 :initarg :d)
(tt :accessor tt :initform 0 :initarg :tt)))
(defvar *r1* (make-instance 'm-rect :w 1 :xpos 134.0 :h 0.532 :ypos (* 0.083 *height*) :d 10.0 :tt 60))
(defvar *r2* (make-instance 'm-rect :w 2 :xpos 44.0 :h 0.166 :ypos (* 0.332 *height*) :d 5.0 :tt 50))
(defvar *r3* (make-instance 'm-rect :w 2 :xpos 58.0 :h 0.332 :ypos (* 0.4482 *height*) :d 10.0 :tt 35))
(defvar *r4* (make-instance 'm-rect :w 1 :xpos 120.0 :h 0.0498 :ypos (* 0.913 *height*) :d 15.0 :tt 60))
(defmethod move-to-y ((rect m-rect) posy damping)
(let ((dif (- (ypos rect) posy)))
(if (> (abs dif) 1)
(decf (ypos rect) (/ dif damping)))))
(defmethod move-to-x ((rect m-rect) posx damping)
(let ((dif (- (xpos rect) posx)))
(if (> (abs dif) 1)
(decf (xpos rect) (/ dif damping)))))
(defmethod display ((rect m-rect) &key (surface sdl:*default-display*))
(dotimes (i (tt rect))
(sdl-gfx:draw-box-* (sdl-base:to-int (sdl-base::clamp-to-sshort (+ (xpos rect)
(* i (+ (d rect)
(w rect))))))
(sdl-base:to-int (sdl-base::clamp-to-sshort (ypos rect)))
(sdl-base:to-int (sdl-base::clamp-to-sshort (w rect)))
(sdl-base:to-int (sdl-base::clamp-to-sshort (* (h rect)
(sdl:height surface))))
:surface surface
:color sdl:*white*)))
(defun objects ()
(let ((mouse-x 0) (mouse-y 0)
(100-frames-p (every-n-frames 100)))
(sdl:with-init ()
(sdl:window *width* *height* :title-caption "Objects, from Processing.org")
(setf (sdl:frame-rate) 60)
(sdl:clear-display (sdl:color))
(sdl-gfx:initialise-default-font)
(draw-fps "Calculating FPS....." 10 150 sdl:*default-font* sdl:*default-display* t)
(sdl:with-events ()
(:quit-event () t)
(:video-expose-event () (sdl:update-display))
(:mouse-motion-event (:x x :y y)
(setf mouse-x x
mouse-y y))
(:idle ()
(sdl:clear-display (sdl:color))
(display *r1*)
(display *r2*)
(display *r3*)
(display *r4*)
(move-to-x *r1* (- mouse-x (/ *width* 2)) 30)
(move-to-x *r2* (mod (+ mouse-x (* *width* 0.05)) *width*) 20)
(move-to-x *r3* (/ mouse-x 4) 40)
(move-to-x *r4* (- mouse-x (/ *width* 2)) 50)
(move-to-y *r1* (+ mouse-y (* *height* 0.1)) 30)
(move-to-y *r2* (+ mouse-y (* *height* 0.025)) 20)
(move-to-y *r3* (- mouse-y (* *height* 0.025)) 40)
(move-to-y *r4* (- *height* mouse-y) 50)
(draw-fps (format nil "FPS : ~2$" (sdl:average-fps))
10 150 sdl:*default-font* sdl:*default-display*
(funcall 100-frames-p))
(sdl:update-display))))))
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.