Menu

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

Download this file

64 lines (56 with data), 2.3 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
;;;; Demonstration/Test of using the SDL_gfx library
;;;; using CFFI for foreign function interfacing...
;;;; (C)2006 Luke Crook
;;;; see COPYING for license
(in-package #:sdl-gfx-examples)
(defvar *circles-per-frame* 1)
(let* ((frame-values 600)
(frame-times (make-array frame-values :initial-element 0 :element-type 'fixnum))
(frame-time-last 0)
(frame-count 0))
(declare (type fixnum frame-values frame-time-last frame-count))
(defun fps-init ()
(setf frame-count 0
frame-time-last (sdl:sdl-get-ticks))
(dotimes (i frame-values)
(setf (aref frame-times i) 0)))
(defun display-fps (surface)
(declare (optimize (safety 0) (speed 3) (space 1)))
(let ((get-ticks (sdl:sdl-get-ticks))
(frames-per-second 0.0))
(declare (type fixnum get-ticks)
(type float frames-per-second))
(setf (aref frame-times frame-count) (- get-ticks frame-time-last))
(setf frame-time-last get-ticks)
(incf frame-count)
(when (>= frame-count frame-values)
(setf frame-count 0)
(dotimes (i frame-values)
(incf frames-per-second (aref frame-times i)))
(setf frames-per-second (the float (/ 1000 (/ frames-per-second frame-values))))
(sdl:fill-surface (sdl:color) :surface surface :update-p t)
(sdl-gfx:draw-string-* (format nil "fps : ~d" (coerce frames-per-second 'float))
0 0
:surface surface
:color sdl:*white*))
surface)))
(defun random-circles ()
(let ((width 640) (height 480))
(sdl:with-init ()
(sdl:window width height)
(sdl-gfx:gfx-Primitives-Set-Font sdl-gfx:*font-data* 8 8)
(setf (sdl:frame-rate) 0)
(fps-init)
(sdl:with-surface (fps (sdl:create-surface 150 16 :surface sdl:*default-display* :rle-accel t))
(sdl:with-events ()
(:quit-event () t)
(:key-down-event (:key key)
(if (sdl:key= key :SDL-KEY-ESCAPE)
(sdl:push-quit-event)))
(:idle ()
(dotimes (i *circles-per-frame*)
(sdl-gfx:draw-filled-Circle-* (random width) (random height) (random 100)
:color (sdl:color :r (random 255) :g (random 255) :b (random 255) :a (random 255))
:surface sdl:*default-display*))
(sdl:blit-surface (display-fps fps) sdl:*default-display*)
(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.