eb92688beebc

Tiny proof of concept demo
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sat, 11 Feb 2017 14:37:44 +0000
parents f61929db839b
children 7d9a9d2a4af2
branches/tags (none)
files docs/03-reference.markdown examples/terrain.lisp src/high-level/bearlibterminal.lisp

Changes

diff -r f61929db839b -r eb92688beebc docs/03-reference.markdown
--- a/docs/03-reference.markdown	Sat Feb 11 00:52:24 2017 +0000
+++ b/docs/03-reference.markdown	Sat Feb 11 14:37:44 2017 +0000
@@ -1,6 +1,6 @@
 # API Reference
 
-The following is a list of all user-facing parts of `cl-pcg`.
+The following is a list of all user-facing parts of `cl-blt`.
 
 If there are backwards-incompatible changes to anything listed here, they will
 be noted in the changelog and the author will feel bad.
@@ -10,5 +10,561 @@
 
 [TOC]
 
-## Package `PCG`
+## Package `BEARLIBTERMINAL/HIGH-LEVEL`
+
+This package contains a high-level, lispy interface to bearlibterminal.  It has the nickname `blt` for easy prefixing.
+
+## Package `BEARLIBTERMINAL/LOW-LEVEL`
+
+This package contains the low-level, SWIG-generated wrapper functions for bearlibterminal.
+
+### `+--SIZEOF-WCHAR-T--+` (variable)
+
+### `+TERMINAL-VSPRINTF-MAXIMUM-BUFFER-SIZE+` (variable)
+
+### `+TERMINAL-WCHAR-SUFFIX+` (variable)
+
+### `+TK-0+` (variable)
+
+### `+TK-1+` (variable)
+
+### `+TK-2+` (variable)
+
+### `+TK-3+` (variable)
+
+### `+TK-4+` (variable)
+
+### `+TK-5+` (variable)
+
+### `+TK-6+` (variable)
+
+### `+TK-7+` (variable)
+
+### `+TK-8+` (variable)
+
+### `+TK-9+` (variable)
+
+### `+TK-A+` (variable)
+
+### `+TK-ALIGN-BOTTOM+` (variable)
+
+### `+TK-ALIGN-CENTER+` (variable)
+
+### `+TK-ALIGN-DEFAULT+` (variable)
+
+### `+TK-ALIGN-LEFT+` (variable)
+
+### `+TK-ALIGN-MIDDLE+` (variable)
+
+### `+TK-ALIGN-RIGHT+` (variable)
+
+### `+TK-ALIGN-TOP+` (variable)
+
+### `+TK-ALT+` (variable)
+
+### `+TK-APOSTROPHE+` (variable)
+
+### `+TK-B+` (variable)
+
+### `+TK-BACKSLASH+` (variable)
+
+### `+TK-BACKSPACE+` (variable)
+
+### `+TK-BKCOLOR+` (variable)
+
+### `+TK-C+` (variable)
+
+### `+TK-CELL-HEIGHT+` (variable)
+
+### `+TK-CELL-WIDTH+` (variable)
+
+### `+TK-CHAR+` (variable)
+
+### `+TK-CLOSE+` (variable)
+
+### `+TK-COLOR+` (variable)
+
+### `+TK-COMMA+` (variable)
+
+### `+TK-COMPOSITION+` (variable)
+
+### `+TK-CONTROL+` (variable)
+
+### `+TK-D+` (variable)
+
+### `+TK-DELETE+` (variable)
+
+### `+TK-DOWN+` (variable)
+
+### `+TK-E+` (variable)
+
+### `+TK-END+` (variable)
+
+### `+TK-ENTER+` (variable)
+
+### `+TK-EQUALS+` (variable)
+
+### `+TK-ESCAPE+` (variable)
+
+### `+TK-EVENT+` (variable)
+
+### `+TK-F+` (variable)
+
+### `+TK-F-1+` (variable)
+
+### `+TK-F-10+` (variable)
+
+### `+TK-F-11+` (variable)
+
+### `+TK-F-12+` (variable)
+
+### `+TK-F-2+` (variable)
+
+### `+TK-F-3+` (variable)
+
+### `+TK-F-4+` (variable)
+
+### `+TK-F-5+` (variable)
+
+### `+TK-F-6+` (variable)
+
+### `+TK-F-7+` (variable)
+
+### `+TK-F-8+` (variable)
+
+### `+TK-F-9+` (variable)
+
+### `+TK-FULLSCREEN+` (variable)
+
+### `+TK-G+` (variable)
+
+### `+TK-GRAVE+` (variable)
+
+### `+TK-H+` (variable)
+
+### `+TK-HEIGHT+` (variable)
+
+### `+TK-HOME+` (variable)
+
+### `+TK-I+` (variable)
+
+### `+TK-INPUT-CANCELLED+` (variable)
+
+### `+TK-INPUT-NONE+` (variable)
+
+### `+TK-INSERT+` (variable)
+
+### `+TK-J+` (variable)
+
+### `+TK-K+` (variable)
+
+### `+TK-KEY-RELEASED+` (variable)
+
+### `+TK-KP-0+` (variable)
+
+### `+TK-KP-1+` (variable)
+
+### `+TK-KP-2+` (variable)
+
+### `+TK-KP-3+` (variable)
+
+### `+TK-KP-4+` (variable)
+
+### `+TK-KP-5+` (variable)
+
+### `+TK-KP-6+` (variable)
+
+### `+TK-KP-7+` (variable)
+
+### `+TK-KP-8+` (variable)
+
+### `+TK-KP-9+` (variable)
+
+### `+TK-KP-DIVIDE+` (variable)
+
+### `+TK-KP-ENTER+` (variable)
+
+### `+TK-KP-MINUS+` (variable)
+
+### `+TK-KP-MULTIPLY+` (variable)
+
+### `+TK-KP-PERIOD+` (variable)
+
+### `+TK-KP-PLUS+` (variable)
+
+### `+TK-L+` (variable)
+
+### `+TK-LAYER+` (variable)
+
+### `+TK-LBRACKET+` (variable)
+
+### `+TK-LEFT+` (variable)
+
+### `+TK-M+` (variable)
+
+### `+TK-MINUS+` (variable)
+
+### `+TK-MOUSE-CLICKS+` (variable)
+
+### `+TK-MOUSE-LEFT+` (variable)
+
+### `+TK-MOUSE-MIDDLE+` (variable)
+
+### `+TK-MOUSE-MOVE+` (variable)
+
+### `+TK-MOUSE-PIXEL-X+` (variable)
+
+### `+TK-MOUSE-PIXEL-Y+` (variable)
+
+### `+TK-MOUSE-RIGHT+` (variable)
+
+### `+TK-MOUSE-SCROLL+` (variable)
+
+### `+TK-MOUSE-WHEEL+` (variable)
+
+### `+TK-MOUSE-X+` (variable)
+
+### `+TK-MOUSE-X-1+` (variable)
+
+### `+TK-MOUSE-X-2+` (variable)
+
+### `+TK-MOUSE-Y+` (variable)
+
+### `+TK-N+` (variable)
+
+### `+TK-O+` (variable)
+
+### `+TK-OFF+` (variable)
+
+### `+TK-ON+` (variable)
+
+### `+TK-P+` (variable)
+
+### `+TK-PAGEDOWN+` (variable)
+
+### `+TK-PAGEUP+` (variable)
+
+### `+TK-PAUSE+` (variable)
+
+### `+TK-PERIOD+` (variable)
+
+### `+TK-Q+` (variable)
+
+### `+TK-R+` (variable)
+
+### `+TK-RBRACKET+` (variable)
+
+### `+TK-RESIZED+` (variable)
+
+### `+TK-RETURN+` (variable)
+
+### `+TK-RIGHT+` (variable)
+
+### `+TK-S+` (variable)
+
+### `+TK-SEMICOLON+` (variable)
+
+### `+TK-SHIFT+` (variable)
+
+### `+TK-SLASH+` (variable)
+
+### `+TK-SPACE+` (variable)
+
+### `+TK-T+` (variable)
+
+### `+TK-TAB+` (variable)
+
+### `+TK-U+` (variable)
+
+### `+TK-UP+` (variable)
+
+### `+TK-V+` (variable)
+
+### `+TK-W+` (variable)
+
+### `+TK-WCHAR+` (variable)
+
+### `+TK-WIDTH+` (variable)
+
+### `+TK-X+` (variable)
 
+### `+TK-Y+` (variable)
+
+### `+TK-Z+` (variable)
+
+### `COLOR-FROM-ARGB` (function)
+
+    (COLOR-FROM-ARGB)
+
+### `COLOR-FROM-NAME` (function)
+
+    (COLOR-FROM-NAME)
+
+### `COLOR-FROM-NAME-16` (function)
+
+    (COLOR-FROM-NAME-16)
+
+### `COLOR-FROM-NAME-32` (function)
+
+    (COLOR-FROM-NAME-32)
+
+### `COLOR-FROM-NAME-8` (function)
+
+    (COLOR-FROM-NAME-8)
+
+### `COLOR-FROM-WNAME` (function)
+
+    (COLOR-FROM-WNAME)
+
+### `DIMENSIONS-T`
+
+`#<STANDARD-CLASS DOCPARSER:CFFI-STRUCT>`
+
+### `TERMINAL-BKCOLOR` (function)
+
+    (TERMINAL-BKCOLOR)
+
+### `TERMINAL-CHECK` (function)
+
+    (TERMINAL-CHECK)
+
+### `TERMINAL-CLEAR` (function)
+
+    (TERMINAL-CLEAR)
+
+### `TERMINAL-CLEAR-AREA` (function)
+
+    (TERMINAL-CLEAR-AREA)
+
+### `TERMINAL-CLOSE` (function)
+
+    (TERMINAL-CLOSE)
+
+### `TERMINAL-COLOR` (function)
+
+    (TERMINAL-COLOR)
+
+### `TERMINAL-COMPOSITION` (function)
+
+    (TERMINAL-COMPOSITION)
+
+### `TERMINAL-CROP` (function)
+
+    (TERMINAL-CROP)
+
+### `TERMINAL-DELAY` (function)
+
+    (TERMINAL-DELAY)
+
+### `TERMINAL-GET` (function)
+
+    (TERMINAL-GET)
+
+### `TERMINAL-GET-16` (function)
+
+    (TERMINAL-GET-16)
+
+### `TERMINAL-GET-32` (function)
+
+    (TERMINAL-GET-32)
+
+### `TERMINAL-GET-8` (function)
+
+    (TERMINAL-GET-8)
+
+### `TERMINAL-HAS-INPUT` (function)
+
+    (TERMINAL-HAS-INPUT)
+
+### `TERMINAL-LAYER` (function)
+
+    (TERMINAL-LAYER)
+
+### `TERMINAL-MEASURE` (function)
+
+    (TERMINAL-MEASURE)
+
+### `TERMINAL-MEASURE-EXT` (function)
+
+    (TERMINAL-MEASURE-EXT)
+
+### `TERMINAL-MEASURE-EXT-16` (function)
+
+    (TERMINAL-MEASURE-EXT-16)
+
+### `TERMINAL-MEASURE-EXT-32` (function)
+
+    (TERMINAL-MEASURE-EXT-32)
+
+### `TERMINAL-MEASURE-EXT-8` (function)
+
+    (TERMINAL-MEASURE-EXT-8)
+
+### `TERMINAL-MEASUREF` (function)
+
+    (TERMINAL-MEASUREF)
+
+### `TERMINAL-MEASUREF-EXT` (function)
+
+    (TERMINAL-MEASUREF-EXT)
+
+### `TERMINAL-OPEN` (function)
+
+    (TERMINAL-OPEN)
+
+### `TERMINAL-PEEK` (function)
+
+    (TERMINAL-PEEK)
+
+### `TERMINAL-PICK` (function)
+
+    (TERMINAL-PICK)
+
+### `TERMINAL-PICK-BKCOLOR` (function)
+
+    (TERMINAL-PICK-BKCOLOR)
+
+### `TERMINAL-PICK-COLOR` (function)
+
+    (TERMINAL-PICK-COLOR)
+
+### `TERMINAL-PRINT` (function)
+
+    (TERMINAL-PRINT)
+
+### `TERMINAL-PRINT-EXT` (function)
+
+    (TERMINAL-PRINT-EXT)
+
+### `TERMINAL-PRINT-EXT-16` (function)
+
+    (TERMINAL-PRINT-EXT-16)
+
+### `TERMINAL-PRINT-EXT-32` (function)
+
+    (TERMINAL-PRINT-EXT-32)
+
+### `TERMINAL-PRINT-EXT-8` (function)
+
+    (TERMINAL-PRINT-EXT-8)
+
+### `TERMINAL-PRINTF` (function)
+
+    (TERMINAL-PRINTF)
+
+### `TERMINAL-PRINTF-EXT` (function)
+
+    (TERMINAL-PRINTF-EXT)
+
+### `TERMINAL-PUT` (function)
+
+    (TERMINAL-PUT)
+
+### `TERMINAL-PUT-EXT` (function)
+
+    (TERMINAL-PUT-EXT)
+
+### `TERMINAL-READ` (function)
+
+    (TERMINAL-READ)
+
+### `TERMINAL-READ-STR` (function)
+
+    (TERMINAL-READ-STR)
+
+### `TERMINAL-READ-STR-16` (function)
+
+    (TERMINAL-READ-STR-16)
+
+### `TERMINAL-READ-STR-32` (function)
+
+    (TERMINAL-READ-STR-32)
+
+### `TERMINAL-READ-STR-8` (function)
+
+    (TERMINAL-READ-STR-8)
+
+### `TERMINAL-READ-WSTR` (function)
+
+    (TERMINAL-READ-WSTR)
+
+### `TERMINAL-REFRESH` (function)
+
+    (TERMINAL-REFRESH)
+
+### `TERMINAL-SET` (function)
+
+    (TERMINAL-SET)
+
+### `TERMINAL-SET-16` (function)
+
+    (TERMINAL-SET-16)
+
+### `TERMINAL-SET-32` (function)
+
+    (TERMINAL-SET-32)
+
+### `TERMINAL-SET-8` (function)
+
+    (TERMINAL-SET-8)
+
+### `TERMINAL-SETF` (function)
+
+    (TERMINAL-SETF)
+
+### `TERMINAL-STATE` (function)
+
+    (TERMINAL-STATE)
+
+### `TERMINAL-VSPRINTF` (function)
+
+    (TERMINAL-VSPRINTF)
+
+### `TERMINAL-VSWPRINTF` (function)
+
+    (TERMINAL-VSWPRINTF)
+
+### `TERMINAL-WGET` (function)
+
+    (TERMINAL-WGET)
+
+### `TERMINAL-WMEASURE` (function)
+
+    (TERMINAL-WMEASURE)
+
+### `TERMINAL-WMEASURE-EXT` (function)
+
+    (TERMINAL-WMEASURE-EXT)
+
+### `TERMINAL-WMEASUREF` (function)
+
+    (TERMINAL-WMEASUREF)
+
+### `TERMINAL-WMEASUREF-EXT` (function)
+
+    (TERMINAL-WMEASUREF-EXT)
+
+### `TERMINAL-WPRINT` (function)
+
+    (TERMINAL-WPRINT)
+
+### `TERMINAL-WPRINT-EXT` (function)
+
+    (TERMINAL-WPRINT-EXT)
+
+### `TERMINAL-WPRINTF` (function)
+
+    (TERMINAL-WPRINTF)
+
+### `TERMINAL-WPRINTF-EXT` (function)
+
+    (TERMINAL-WPRINTF-EXT)
+
+### `TERMINAL-WSET` (function)
+
+    (TERMINAL-WSET)
+
+### `TERMINAL-WSETF` (function)
+
+    (TERMINAL-WSETF)
+
diff -r f61929db839b -r eb92688beebc examples/terrain.lisp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/terrain.lisp	Sat Feb 11 14:37:44 2017 +0000
@@ -0,0 +1,128 @@
+(in-package :cl-user)
+
+(ql:quickload '(:cl-blt :losh :iterate))
+
+(use-package :losh)
+(use-package :iterate)
+
+;;;; Heightmap ----------------------------------------------------------------
+(defconstant +world-exponent+ 9)
+(defconstant +world-size+ (expt 2 +world-exponent+))
+
+
+(defun allocate-heightmap ()
+  (make-array (list +world-size+ +world-size+)
+    :element-type 'single-float
+    :initial-element 0.0
+    :adjustable nil))
+
+
+(defun average4 (a b c d)
+  (/ (+ a b c d) 4))
+
+(defun hm-ref (heightmap x y)
+  (flet ((ref (n)
+           (cond
+             ((< -1 n +world-size+) n)
+             ((= n +world-size+) 0)
+             (t (mod n +world-size+)))))
+    (aref heightmap (ref x) (ref y))))
+
+
+(defun heightmap-extrema (heightmap)
+  (iterate
+    (for v :across-flat-array heightmap :with-index i)
+    (maximize v :into max)
+    (minimize v :into min)
+    (finally (return (values min max)))))
+
+(defun normalize-heightmap (heightmap)
+  (multiple-value-bind (min max) (heightmap-extrema heightmap)
+    (iterate
+      (with span = (- max min))
+      (for v :across-flat-array heightmap :with-index i)
+      (setf (row-major-aref heightmap i)
+            (/ (- v min) span)))))
+
+
+(defun ds-init (heightmap)
+  (setf (aref heightmap 0 0) 0.5))
+
+
+(defun ds-square (heightmap x y radius spread)
+  (setf (aref heightmap x y)
+        (random-around (average4 (hm-ref heightmap (- x radius) (- y radius))
+                                 (hm-ref heightmap (- x radius) (+ y radius))
+                                 (hm-ref heightmap (+ x radius) (- y radius))
+                                 (hm-ref heightmap (+ x radius) (+ y radius)))
+                       spread)))
+
+(defun ds-diamond (heightmap x y radius spread)
+  (setf (aref heightmap x y)
+        (random-around (average4 (hm-ref heightmap (- x radius) y)
+                                 (hm-ref heightmap (+ x radius) y)
+                                 (hm-ref heightmap x (- y radius))
+                                 (hm-ref heightmap x (+ y radius)))
+                       spread)))
+
+(defun ds-squares (heightmap radius spread)
+  (iterate
+    (for-nested ((x :from radius :below +world-size+ :by (* 2 radius))
+                 (y :from radius :below +world-size+ :by (* 2 radius))))
+    (ds-square heightmap x y radius spread)))
+
+(defun ds-diamonds (heightmap radius spread)
+  (iterate
+    (for i :from 0)
+    (for y :from 0 :below +world-size+ :by radius)
+    (iterate
+      (with shift = (if (evenp i) radius 0))
+      (for x :from shift :below +world-size+ :by (* 2 radius))
+      (ds-diamond heightmap x y radius spread))))
+
+
+(defun diamond-square (heightmap)
+  (ds-init heightmap)
+  (let ((spread 0.8)
+        (spread-reduction 0.7))
+    (recursively ((radius (floor +world-size+ 2))
+                  (spread spread))
+      (when (>= radius 1)
+        (ds-squares heightmap radius spread)
+        (ds-diamonds heightmap radius spread)
+        (recur (/ radius 2)
+               (* spread spread-reduction)))))
+  (normalize-heightmap heightmap)
+  heightmap)
+
+
+;;;; GUI ----------------------------------------------------------------------
+(defun terrain-char (height)
+  (cond ((< height 0.4) #\~)
+        ((< height 0.7) #\.)
+        ((< height 0.9) #\#)
+        (t #\*)))
+
+(defparameter *heightmap* (allocate-heightmap))
+
+(defun draw ()
+  (iterate
+    (for-nested ((x :from 0 :below (blt::terminal-width))
+                 (y :from 0 :below (blt::terminal-height))))
+    (blt::terminal-put-char x y (terrain-char (aref *heightmap* x y))))
+  (blt::terminal-refresh))
+
+(defun config ()
+  (blt::terminal-set "window.resizeable = true")
+  (blt::terminal-set "window.cellsize = 10x10")
+  (blt::terminal-set "window.title = Terrain Gen Demo"))
+
+(defun main ()
+  (blt::with-terminal
+    (config)
+    (iterate
+      (draw)
+      (blt::key-case (blt::terminal-read)
+        (:space (diamond-square *heightmap*))
+        (:escape (return))
+        (:close (return))))))
diff -r f61929db839b -r eb92688beebc src/high-level/bearlibterminal.lisp
--- a/src/high-level/bearlibterminal.lisp	Sat Feb 11 00:52:24 2017 +0000
+++ b/src/high-level/bearlibterminal.lisp	Sat Feb 11 14:37:44 2017 +0000
@@ -126,6 +126,19 @@
   (blt/ll:terminal-delay (truncate (* seconds 1000))))
 
 
+(defun terminal-put-char (x y char)
+  (blt/ll:terminal-put x y (char-code char)))
+
+(defun terminal-put-code (x y code-point)
+  (blt/ll:terminal-put x y code-point))
+
+(defun terminal-width ()
+  (blt/ll:terminal-state blt/ll:+tk-width+))
+
+(defun terminal-height ()
+  (blt/ll:terminal-state blt/ll:+tk-height+))
+
+
 ;;;; Higher-Level API ---------------------------------------------------------
 (defmacro defuck-floats (&body body)
   #+sbcl