prosperon/source/scripts/engine.scm

98 lines
2.7 KiB
Scheme
Raw Normal View History

(define-macro (time expr)
`(let ((start (*s7* 'cpu-time)))
(let ((res (list ,expr)))
(list (car res)
(- (*s7* 'cpu-time) start)))))
(define-macro (funcinfo func info)
`(*function* (funclet ,func) ,info))
(define-macro (funcloc func)
`(format #f "~A:~A" (funcinfo ,func 'file) (funcinfo ,func 'line)))
(define-macro (funcsrc func)
`(funcinfo ,func 'source))
2022-12-20 08:16:26 -06:00
(define (objects->string . objs)
2022-12-20 19:34:22 -06:00
(apply string-append (map (lambda (obj) (object->string obj #f)) objs)))
(define-bacro (glog data lvl)
(let ((file (port-filename))
(line (port-line-number)))
`(log ,lvl ,data
(if (equal? ,file "*stdin*") (*function* (outlet (curlet)) 'file) ,file)
(if (equal? ,file "*stdin*") (*function* (outlet (curlet)) 'line) ,line))))
(define-bacro (loginfo . data)
`(glog (objects->string ,@data) 0))
(define-bacro (logwarn . data)
`(glog (objects->string ,@data) 1))
(define-bacro (logerr . data)
`(glog (objects->string ,@data) 2))
(define-bacro (logcrit . data)
`(glog (objects->string ,@data) 3))
(define (set_fps fps) (settings_cmd 0 (/ 1 fps)))
(define (set_update fps) (settings_cmd 1 (/ 1 fps)))
(define (set_phys fps) (settings_cmd 2 (/ 1 fps)))
(define (win_fulltoggle w) (win_cmd w 0))
(define (win_fullscreen w) (win_cmd w 1))
(define (win_unfullscreen w) (win_cmd w 2))
(define (win_title s) (win_cmd 0 3 s))
(define (load_level s) (gen_cmd 0 s))
(define (load_prefab s) (gen_cmd 1 s))
(define (quit) (sys_cmd 0))
(define (exit) (quit))
(define (sound_play sound) (sound_cmd sound 0))
(define (sound_pause sound) (sound_cmd sound 1))
(define (sound_stop sound) (sound_cmd sound 2))
(define (sound_restart sound) (sound_cmd sound 3))
2022-12-19 09:12:34 -06:00
2022-12-20 19:34:22 -06:00
(define-macro (registertype type . expr)
2022-12-19 09:12:34 -06:00
(let ((f (gensym)))
`(begin
(define (,f) (begin . ,expr))
2022-12-20 19:34:22 -06:00
(register ,(case type
((update) 0)
((gui) 1)) ,f))))
(define-macro (update . expr)
`(registertype update ,@expr))
2022-12-19 18:15:38 -06:00
2022-12-20 19:34:22 -06:00
(define-macro (gui . expr)
`(registertype gui ,@expr))
2022-12-19 09:12:34 -06:00
(define-macro (while condition . body)
(let ((loop (gensym)))
`(let ,loop ()
(cond (,condition
(begin . ,body)
(,loop))))))
2022-12-19 18:15:38 -06:00
(define (clamp val min max)
(cond ((< val min) min)
((> val max) max)
(else val)))
(define (lerp s f dt)
(+ s (* (clamp dt 0 1) (- f s))))
(define (deg2rad deg)
(* deg 0.01745329252))
(define (body_angle! body angle) (set_body body 0 angle))
2022-12-20 08:16:26 -06:00
(define (body_pos! body x y) (set_body_pos body 0 x y))
(define (body_move! body x y) (set_body_pos body 1 x y))
(define-macro (collide . expr)
(let ((f (gensym)))
`(begin
(define (,f) (begin . ,expr))
(phys_cmd 0 ,f))))