flan/examples/core-window-should-close.flan
Joseph Ferano 1df6f7a642 Five more core examples, and the 3D half of the bindings they wanted
core-2d-camera, core-scissor-test, core-window-flags, core-world-screen
and core-window-should-close, ported from raylib 5.5's examples/core.

What each one asked of vendor/raylib:

  2d-camera        nothing. A whole Camera2D by value, per frame, into the
                   call that actually draws with it — the layout the
                   acceptance table pins through arithmetic, now going
                   through the path it was bound for.
  scissor-test     begin-scissor-mode / end-scissor-mode, hand-written.
  window-flags     twelve more ConfigFlags constants; raylib.flan carried
                   the four sand.flan sets and this reads eleven.
  world-screen     the 3D surface did not exist: no Vector3, no Camera3D,
                   and the importer refused every 3D function in raylib.h
                   by name for want of them. Two defstructs, two defenums,
                   begin/end-mode-3d, update-camera, get-world-to-screen,
                   draw-cube, draw-cube-wires, draw-grid — and 24 more 3D
                   lines the importer can generate now that the types are
                   described.
  window-should-close  set-exit-key, and Key/null to pass it.

The hand-written/generated line, written down in vendor/raylib/bindings:
a drawing pair inside a frame is hand-written, and so is anything whose
Flan face is not the C signature — set-exit-key takes a Key, update-camera
takes a (Ptr Camera3D) and a CameraMode. The window-state family and
get-mouse-x/y stay generated: plain scalars in and bool out, with nothing
for a hand-written line to add.

Camera3D's projection field stays i32, because the header says int and the
layout check holds this file to that; rl/camera-projection is the
conversion, and still takes a keyword.
2026-09-13 12:51:19 +07:00

69 lines
2.7 KiB
Plaintext

;;;; raylib [core] example - window should close
;;;;
;;;; examples/core/core_window_should_close.c. Needed set-exit-key, and one
;;;; enum member to go with it: raylib.flan's `Key` now has `null 0`, read off
;;;; KEY_NULL in raylib.h. The binding is hand-written rather than taken from
;;;; the generated half because the Flan face differs — raylib declares
;;;; `void SetExitKey(int key)` and the generated line therefore takes an i32,
;;;; where this one takes a Key, so `(rl/set-exit-key :null)` is checked
;;;; against the enum and `:nul` is a compile error instead of a 0.
;;;;
;;;; The whole example is about what window-should-close? means. It is not a
;;;; flag raylib latches: it is "the close button was clicked, or the exit key
;;;; is down", recomputed each frame, and it goes back to false on its own.
;;;; That is what lets this program ask for confirmation and then carry on —
;;;; and it is also why the loop is driven by a `defvar` of its own rather
;;;; than by the predicate, which is the one structural difference from every
;;;; other example in this directory.
;;;;
;;;; `(set-exit-key :null)` takes ESC away from raylib so the program can read
;;;; it itself. The window's X button still works and is still what
;;;; window-should-close? reports.
(import rl "vendor:raylib")
(defconst screen-width 800)
(defconst screen-height 450)
(defvar exit-requested bool)
(defvar exiting bool)
(defn main [] ()
(rl/init-window screen-width screen-height
"raylib [core] example - window should close")
(defer (rl/close-window))
;; No key closes the window any more. ESC is an ordinary key from here on.
(rl/set-exit-key :null)
(rl/set-target-fps 60)
(until exiting
;; Update. Either way of asking to leave raises the question; only Y and N
;; answer it. window-should-close? is false again on the next frame, so
;; the request has to be remembered in a variable.
(when (or (rl/window-should-close?) (rl/key-pressed? :escape))
(set exit-requested true))
;; The C's `if (Y) ... else if (N) ...`, which is an `if` with a `when`
;; in its else and not a `cond`: a `cond` needs an `:else` arm and there
;; is nothing to do when neither key was pressed.
(when exit-requested
(if (rl/key-pressed? :y)
(set exiting true)
(when (rl/key-pressed? :n) (set exit-requested false))))
;; Draw
(rl/begin-drawing)
(rl/clear-background rl/raywhite)
(if exit-requested
(do
(rl/draw-rectangle 0 100 screen-width 200 rl/black)
(rl/draw-text "Are you sure you want to exit program? [Y/N]" 40 180 30
rl/white))
(rl/draw-text "Try to close the window to get confirmation message!"
120 200 20 rl/lightgray))
(rl/end-drawing)))