Geometric Tilings with Guile-SDL2

Equilateral triangle tiling using Guile SDL2

I am trying to get back to the fun of being able to draw things on the screen using lisp. The Guile SDL2 module proved a handy tool. The code below was run using guile and guile-sdl from Guix commit 62942992831249d6d1c047c0a11c41d2ecccc4fc.

;; Copyright 2021 Christopher Howard

;; 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.

(use-modules (sdl2)
             (sdl2 video)
             (sdl2 rect)
             (sdl2 surface)
             (sdl2 render)
             (sdl2 input keyboard)
             (sdl2 events))

(define (ex x) (inexact->exact (round x)))

(define (draw-tri-block r orig-x orig-y base-l)
  (let* ((base-hl (* 0.5 base-l))
         (base-h (* (* base-l (sqrt 3)) 0.5)))
    (render-draw-lines
     r
     (list (list (ex (+ orig-x base-l)) (ex orig-y))
           (list (ex (+ orig-x base-hl))
                 (ex (+ orig-y base-h)))
           (list (ex (+ orig-x base-hl base-l))
                 (ex (+ orig-y base-h)))
           (list (ex (+ orig-x base-l)) (ex orig-y))))))

(define (draw-tri-pattern r)
  (let* ((base-l 20)
         (base-h (* (* base-l (sqrt 3)) 0.5))
         (base-hl (* 0.5 base-l)))
    (do ((j -4 (1+ j)))
        ((> j 40))
        (do ((i -4 (1+ i)))
            ((> i 40))
          (draw-tri-block
           r
           (+ (* i base-l) (if (zero? (floor-remainder j 2)) 0 base-hl))
           (* j base-h) base-l)))))

(define (main)
  (sdl-init)
  (display (sdl-version))
  (display "\r\n")
  (display (sdl-ticks))
  (display "\r\n")
  (let* ((mywindow (make-window
                  #:title "Super Awesome Window"
                  #:opengl? #t))
         (glc (make-gl-context mywindow))
         (myrect (make-rect 100 100 200 100))
         (mysurface (make-rgb-surface 300 300 32))
         (myrenderer (make-renderer mywindow)))
    (set-gl-swap-interval! 'vsync)
    (set-render-draw-color myrenderer 0 0 0 255)
    (clear-renderer myrenderer)
    (set-render-draw-color myrenderer 0 255 255 255)
    (draw-tri-pattern myrenderer)
    (present-renderer myrenderer)
    (while (not (key-pressed? 'q)) (usleep 100) (poll-event))
    (close-window! mywindow)
  (sdl-quit)))
Advertisement

1 thought on “Geometric Tilings with Guile-SDL2”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s