Working on KOTH.
[jars.git] / visualizer.scm
1 ;; Visualization module
2
3 (module visualizer
4     (make-vis)
5
6   (import scheme
7           (chicken base)
8           (chicken io)
9           (chicken process)
10           (chicken port)
11           (chicken string)
12           srfi-1 matchable)
13
14   (define (make-vis target-width target-height core-size palette)
15     ;; (let-values (((in out id) (process (conc "wish -geometry " w "x" h)))))
16     (let-values (((in out id) (process "wish")))
17       (define (wish% . args)
18         (with-output-to-port out
19           (lambda ()
20             (apply print args))))
21       (wish% "wm title . \"MARS Visualizer\"")
22       (wish% "frame .fb -borderwidth 10")
23       (wish% "pack .fb -side top")
24       (let loop ((remaining-palette palette))
25         (unless (null? remaining-palette)
26           (let ((name (caar remaining-palette))
27                 (col (cdar remaining-palette)))
28             (wish% "label .fb.l" name " -text " name " -fg " col)
29             (wish% "pack .fb.l" name " -side left"))
30           (loop (cdr remaining-palette))))
31       (wish% "frame .fc -relief sunken -borderwidth 2")
32       (wish% "pack .fc -side bottom")
33       (let* ((aspect-ratio (/ target-width target-height))
34              (core-width (inexact->exact (round (sqrt (* aspect-ratio core-size)))))
35              (core-height (inexact->exact (ceiling (/ core-size core-width))))
36              (cell-width (inexact->exact (round (/ target-width core-width))))
37              (cell-height (inexact->exact (round (/ target-height core-height))))
38              (w (* cell-width core-width))
39              (h (* cell-height core-height)))
40         (wish% "canvas .fc.c -width " w " -height " h " -bg black")
41         (wish% "pack .fc.c")
42         (wish% "image create photo core -width " w " -height " h " -palette 256/256/256")
43         (wish% ".fc.c create image 0 0 -anchor nw -image core")
44         (let loop ((xi (- (* core-height core-width) 1)))
45           (unless (< xi (modulo core-size core-width))
46             (wish% "core put grey -to "
47                    (* cell-width xi) " " (* cell-height (- core-height 1))
48                    " " (* cell-width (+ xi 1)) (* cell-height core-height))
49             (loop (- xi 1))))
50         (lambda args
51           (match args
52             (('wish args ...) (apply wish% args))
53             (('destroy) (wish% "destroy ."))
54             (('update-owner addr name)
55              (let ((xi (modulo addr core-width))
56                    (yi (quotient addr core-width)))
57                ;; (print "core width: " core-width)
58                ;; (print "core height: " core-height)
59                ;; (print "cell width: " cell-width)
60                ;; (print "cell height: " cell-height)
61                ;; (print "xi: " xi " yi: " yi)
62                ;; (print "col: " (cdr (assoc name palette)))
63                (wish% "core put " (cdr (assoc name palette))
64                       " -to " (* cell-width xi) " " (* cell-height yi)
65                       " " (* cell-width (+ xi 1)) " " (* cell-height (+ yi 1)))))))))))
66                       
67
68 ;; Test code
69
70 ;; (import visualizer)
71 ;; (define v (make-vis 640 480 8000 '((imp . "blue") (dwarf . "red"))))
72