First commit!

This commit is contained in:
David Thompson 2024-05-17 13:08:43 -04:00
commit 2c824b40a9
31 changed files with 2048 additions and 0 deletions

72
modules/dom/canvas.scm Normal file
View file

@ -0,0 +1,72 @@
;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Commentary:
;;;
;;; HTMLCanvasElement and CanvasRenderingContext2D bindings.
;;;
;;; Code:
(define-module (dom canvas)
#:pure
#:use-module (scheme base)
#:use-module (hoot ffi)
#:export (get-context
set-fill-color!
set-font!
set-text-align!
clear-rect
fill-rect
fill-text
draw-image
set-scale!
set-transform!
set-image-smoothing-enabled!))
;; HTMLCanvasElement
(define-foreign get-context
"canvas" "getContext"
(ref extern) (ref string) -> (ref extern))
;; CanvasRenderingContext2D
(define-foreign set-fill-color!
"canvas" "setFillColor"
(ref extern) (ref string) -> none)
(define-foreign set-font!
"canvas" "setFont"
(ref extern) (ref string) -> none)
(define-foreign set-text-align!
"canvas" "setTextAlign"
(ref extern) (ref string) -> none)
(define-foreign clear-rect
"canvas" "clearRect"
(ref extern) f64 f64 f64 f64 -> none)
(define-foreign fill-rect
"canvas" "fillRect"
(ref extern) f64 f64 f64 f64 -> none)
(define-foreign fill-text
"canvas" "fillText"
(ref extern) (ref string) f64 f64 -> none)
(define-foreign draw-image
"canvas" "drawImage"
(ref extern) (ref extern) f64 f64 f64 f64 f64 f64 f64 f64 -> none)
(define-foreign set-scale!
"canvas" "setScale"
(ref extern) f64 f64 -> none)
(define-foreign set-transform!
"canvas" "setTransform"
(ref extern) f64 f64 f64 f64 f64 f64 -> none)
(define-foreign set-image-smoothing-enabled!
"canvas" "setImageSmoothingEnabled"
(ref extern) i32 -> none)

45
modules/dom/document.scm Normal file
View file

@ -0,0 +1,45 @@
;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Commentary:
;;;
;;; Document bindings.
;;;
;;; Code:
(define-module (dom document)
#:pure
#:use-module (scheme base)
#:use-module (hoot ffi)
#:export (current-document
document-body
get-element-by-id
make-text-node
make-element))
(define-foreign current-document
"document" "get"
-> (ref extern))
(define-foreign document-body
"document" "body"
-> (ref null extern))
(define-foreign get-element-by-id
"document" "getElementById"
(ref string) -> (ref null extern))
(define-foreign make-text-node
"document" "createTextNode"
(ref string) -> (ref extern))
(define-foreign make-element
"document" "createElement"
(ref string) -> (ref extern))

71
modules/dom/element.scm Normal file
View file

@ -0,0 +1,71 @@
;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Commentary:
;;;
;;; Element bindings.
;;;
;;; Code:
(define-module (dom element)
#:pure
#:use-module (scheme base)
#:use-module (hoot ffi)
#:export (element-value
set-element-value!
element-width set-element-width!
element-height set-element-height!
append-child!
remove!
replace-with!
set-attribute!
remove-attribute!
clone-element))
(define-foreign element-value
"element" "value"
(ref extern) -> (ref string))
(define-foreign set-element-value!
"element" "setValue"
(ref extern) (ref string) -> none)
(define-foreign element-width
"element" "width"
(ref extern) -> i32)
(define-foreign element-height
"element" "height"
(ref extern) -> i32)
(define-foreign set-element-width!
"element" "setWidth"
(ref extern) i32 -> none)
(define-foreign set-element-height!
"element" "setHeight"
(ref extern) i32 -> none)
(define-foreign append-child!
"element" "appendChild"
(ref extern) (ref extern) -> (ref extern))
(define-foreign remove!
"element" "remove"
(ref extern) -> none)
(define-foreign replace-with!
"element" "replaceWith"
(ref extern) (ref extern) -> none)
(define-foreign set-attribute!
"element" "setAttribute"
(ref extern) (ref string) (ref string) -> none)
(define-foreign remove-attribute!
"element" "removeAttribute"
(ref extern) (ref string) -> none)
(define-foreign clone-element
"element" "clone"
(ref extern) -> (ref extern))

46
modules/dom/event.scm Normal file
View file

@ -0,0 +1,46 @@
;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Commentary:
;;;
;;; EventTarget and Event bindings.
;;;
;;; Code:
(define-module (dom event)
#:pure
#:use-module (scheme base)
#:use-module (hoot ffi)
#:export (add-event-listener!
remove-event-listener!
prevent-default!
keyboard-event-code))
;; EventTarget
(define-foreign add-event-listener!
"event" "addEventListener"
(ref extern) (ref string) (ref extern) -> none)
(define-foreign remove-event-listener!
"event" "removeEventListener"
(ref extern) (ref string) (ref extern) -> none)
;; Event
(define-foreign prevent-default!
"event" "preventDefault"
(ref extern) -> none)
;; KeyboardEvent
(define-foreign keyboard-event-code
"event" "keyboardCode"
(ref extern) -> (ref string))

29
modules/dom/image.scm Normal file
View file

@ -0,0 +1,29 @@
;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Commentary:
;;;
;;; HTMLImageElement bindings.
;;;
;;; Code:
(define-module (dom image)
#:pure
#:use-module (scheme base)
#:use-module (hoot ffi)
#:export (make-image))
(define-foreign make-image
"image" "new"
(ref string) -> (ref extern))

54
modules/dom/media.scm Normal file
View file

@ -0,0 +1,54 @@
;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Commentary:
;;;
;;; HTMLMediaElement bindings.
;;;
;;; Code:
(library (dom media)
(export make-audio
media-play
media-pause
media-volume
set-media-volume!
set-media-loop!
media-seek)
(import (scheme base)
(hoot ffi)
(hoot match)
(only (hoot syntax) define*))
(define-foreign make-audio
"media" "newAudio"
(ref string) -> (ref extern))
(define-foreign media-play
"media" "play"
(ref extern) -> none)
(define-foreign media-pause
"media" "pause"
(ref extern) -> none)
(define-foreign media-volume
"media" "volume"
(ref extern) -> f64)
(define-foreign set-media-volume!
"media" "setVolume"
(ref extern) f64 -> none)
(define-foreign set-media-loop!
"media" "setLoop"
(ref extern) i32 -> none)
(define-foreign media-seek
"media" "seek"
(ref extern) f64 -> none))

45
modules/dom/window.scm Normal file
View file

@ -0,0 +1,45 @@
;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Commentary:
;;;
;;; Window bindings.
;;;
;;; Code:
(define-module (dom window)
#:pure
#:use-module (scheme base)
#:use-module (hoot ffi)
#:export (current-window
window-inner-width
window-inner-height
request-animation-frame
timeout))
(define-foreign current-window
"window" "get"
-> (ref extern))
(define-foreign window-inner-width
"window" "innerWidth"
(ref extern) -> i32)
(define-foreign window-inner-height
"window" "innerHeight"
(ref extern) -> i32)
(define-foreign request-animation-frame
"window" "requestAnimationFrame"
(ref extern) -> none)
(define-foreign timeout
"window" "setTimeout"
(ref extern) f64 -> i32)

49
modules/math.scm Normal file
View file

@ -0,0 +1,49 @@
;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Commentary:
;;;
;;; Helpful math things.
;;;
;;; Code:
;; (library (math)
;; (export random
;; clamp)
;; (import (scheme base)
;; (hoot ffi))
;; (define-foreign random
;; "math" "random"
;; -> f64)
;; (define (clamp x min max)
;; (cond ((< x min) min)
;; ((> x max) max)
;; (else x))))
(define-module (math)
#:pure
#:use-module (scheme base)
#:use-module (hoot ffi)
#:export (random clamp))
(define-foreign random
"math" "random"
-> f64)
(define (clamp x min max)
(cond ((< x min) min)
((> x max) max)
(else x)))

96
modules/math/rect.scm Normal file
View file

@ -0,0 +1,96 @@
;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Commentary:
;;;
;;; Rectangle data type.
;;;
;;; Code:
(define-module (math rect)
#:pure
#:use-module (scheme base)
#:use-module ((hoot bytevectors)
#:select
(bytevector-ieee-double-native-ref
bytevector-ieee-double-native-set!))
#:export (make-rect
rect?
rect-x
rect-y
rect-width
rect-height
set-rect-x!
set-rect-y!
set-rect-width!
set-rect-height!
rect-intersects?
rect-clip))
;; For speed, a rect is a wrapper around a bytevector so that we can
;; use unboxed floats.
(define-record-type <rect>
(%make-rect bv)
rect?
(bv rect-bv))
(define f64-ref bytevector-ieee-double-native-ref)
(define f64-set! bytevector-ieee-double-native-set!)
(define (make-rect x y w h)
(let ((bv (make-bytevector (* 8 4))))
(f64-set! bv 0 x)
(f64-set! bv 8 y)
(f64-set! bv 16 w)
(f64-set! bv 24 h)
(%make-rect bv)))
(define (rect-x r)
(f64-ref (rect-bv r) 0))
(define (rect-y r)
(f64-ref (rect-bv r) 8))
(define (rect-width r)
(f64-ref (rect-bv r) 16))
(define (rect-height r)
(f64-ref (rect-bv r) 24))
(define (set-rect-x! r x)
(f64-set! (rect-bv r) 0 x))
(define (set-rect-y! r y)
(f64-set! (rect-bv r) 8 y))
(define (set-rect-width! r width)
(f64-set! (rect-bv r) 16 width))
(define (set-rect-height! r height)
(f64-set! (rect-bv r) 24 height))
(define (rect-intersects? a b)
(and (< (rect-x a) (+ (rect-x b) (rect-width b)))
(< (rect-y a) (+ (rect-y b) (rect-height b)))
(> (+ (rect-x a) (rect-width a)) (rect-x b))
(> (+ (rect-y a) (rect-height a)) (rect-y b))))
(define (rect-clip a b)
(let* ((x1 (max (rect-x a) (rect-x b)))
(x2 (min (+ (rect-x a) (rect-width a))
(+ (rect-x b) (rect-width b))))
(y1 (max (rect-y a) (rect-y b)))
(y2 (min (+ (rect-y a) (rect-height a))
(+ (rect-y b) (rect-height b)))))
(make-rect x1 y1 (- x2 x1) (- y2 y1))))

94
modules/math/vector.scm Normal file
View file

@ -0,0 +1,94 @@
;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;; Commentary:
;;;
;;; Vectors, in the linear algebra sense.
;;;
;;; Code:
(define-module (math vector)
#:pure
#:use-module (scheme base)
#:use-module (scheme inexact)
#:use-module ((hoot bytevectors)
#:select
(bytevector-ieee-double-native-ref
bytevector-ieee-double-native-set!))
#:use-module (math)
#:export (vec2
vec2?
vec2-x
vec2-y
set-vec2-x!
set-vec2-y!
vec2-add!
vec2-sub!
vec2-mul-scalar!
vec2-magnitude
vec2-normalize!
vec2-clamp!))
;; For speed, a vec2 is a wrapper around a bytevector so that we can
;; use unboxed floats.
(define-record-type <vec2>
(make-vec2 bv)
vec2?
(bv vec2-bv))
(define f64-ref bytevector-ieee-double-native-ref)
(define f64-set! bytevector-ieee-double-native-set!)
(define (vec2 x y)
(let ((v (make-vec2 (make-bytevector 16))))
(set-vec2-x! v x)
(set-vec2-y! v y)
v))
(define (vec2-x v)
(f64-ref (vec2-bv v) 0))
(define (vec2-y v)
(f64-ref (vec2-bv v) 8))
(define (set-vec2-x! v x)
(f64-set! (vec2-bv v) 0 x))
(define (set-vec2-y! v y)
(f64-set! (vec2-bv v) 8 y))
(define (vec2-add! v w)
(set-vec2-x! v (+ (vec2-x v) (vec2-x w)))
(set-vec2-y! v (+ (vec2-y v) (vec2-y w))))
(define (vec2-sub! v w)
(set-vec2-x! v (- (vec2-x v) (vec2-x w)))
(set-vec2-y! v (- (vec2-y v) (vec2-y w))))
(define (vec2-mul-scalar! v x)
(set-vec2-x! v (* (vec2-x v) x))
(set-vec2-y! v (* (vec2-y v) x)))
(define (vec2-magnitude v)
(sqrt (+ (* (vec2-x v) (vec2-x v)) (* (vec2-y v) (vec2-y v)))))
(define (vec2-normalize! v)
(unless (and (= (vec2-x v) 0.0) (= (vec2-y v) 0.0))
(let ((m (vec2-magnitude v)))
(set-vec2-x! v (/ (vec2-x v) m))
(set-vec2-y! v (/ (vec2-y v) m)))))
(define (vec2-clamp! v xmin ymin xmax ymax)
(set-vec2-x! v (clamp (vec2-x v) xmin xmax))
(set-vec2-y! v (clamp (vec2-y v) ymin ymax)))